Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restores float <-> decimal conversions in the binary reader #905

Merged
merged 2 commits into from
Jul 2, 2024

Conversation

zslayton
Copy link
Contributor

@zslayton zslayton commented Jul 1, 2024

While the documentation for IonReader#doubleValue and decimalValue indicates that those methods will throw an exception when then the current value's IonType is not FLOAT or DECIMAL respectively, versions of ion-java up to and including v1.10.5 would quietly coerce numeric values to the requested type. The new binary reader implementation hewed to the documentation's stated contract, but this ended up being a breaking change for a small number of users who depended on the undocumented behavior.

This PR restores the previously supported numeric type coercions for doubleValue, decimalValue, and bigDecimalValue and adds unit tests to prevent future regressions. It also replaces uses of the deprecated $buildDir accessor in build.gradle.kts with the now-recommended ${layout.buildDirectory}.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

While the documentation for `IonReader#doubleValue` and `decimalValue`
indicates that those methods will throw an exception when then the
current value's `IonType` is not `FLOAT` or `DECIMAL` respectively,
versions of `ion-java` up to and including v1.10.5 would quietly
coerce numeric values to the requested type. The new binary reader
implementation hewed to the documentation's stated contract, but
this ended up being a breaking change for a small number of users
who depended on the previous behavior.

This PR restores the previously supported numeric type coercions
for `doubleValue`, `decimalValue`, and `bigDecimalValue` and adds
unit tests to prevent future regressions.
@zslayton zslayton requested review from tgregg and popematt July 1, 2024 16:09
Copy link
Contributor

@popematt popematt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple of formatting nitpicks, but otherwise good.

@@ -594,6 +593,12 @@ public BigDecimal bigDecimalValue() {
scalarConverter.cast(scalarConverter.get_conversion_fnid(_Private_ScalarConversions.AS_TYPE.decimal_value));
value = scalarConverter.getBigDecimal();
scalarConverter.clear();
} else if (valueTid.type == IonType.FLOAT) {
scalarConverter.addValue(doubleValue());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit—Indentation is off here.

static Stream<InputStream> testData() {
byte[] textData = "$ion_1_0 42 42e0 42.0".getBytes(StandardCharsets.UTF_8);
byte[] binaryData = {
// Version marker 1.0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit—I think we're trying to use a single level of indentation for cases like this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How odd--this is what IntelliJ's auto-formatter chose, but I can't imagine why. Fixed!

Copy link
Contributor

@tgregg tgregg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 to Matt's comments, but looks good.

@zslayton zslayton merged commit f8e1205 into master Jul 2, 2024
12 checks passed
@zslayton zslayton deleted the numeric-coercions branch July 2, 2024 10:34
linlin-s pushed a commit that referenced this pull request Jul 3, 2024
While the documentation for `IonReader#doubleValue` and `decimalValue`
indicates that those methods will throw an exception when then the
current value's `IonType` is not `FLOAT` or `DECIMAL` respectively,
versions of `ion-java` up to and including v1.10.5 would quietly
coerce numeric values to the requested type. The new binary reader
implementation hewed to the documentation's stated contract, but
this ended up being a breaking change for a small number of users
who depended on the previous behavior.

This PR restores the previously supported numeric type coercions
for `doubleValue`, `decimalValue`, and `bigDecimalValue` and adds
unit tests to prevent future regressions.
linlin-s pushed a commit that referenced this pull request Jul 3, 2024
While the documentation for `IonReader#doubleValue` and `decimalValue`
indicates that those methods will throw an exception when then the
current value's `IonType` is not `FLOAT` or `DECIMAL` respectively,
versions of `ion-java` up to and including v1.10.5 would quietly
coerce numeric values to the requested type. The new binary reader
implementation hewed to the documentation's stated contract, but
this ended up being a breaking change for a small number of users
who depended on the previous behavior.

This PR restores the previously supported numeric type coercions
for `doubleValue`, `decimalValue`, and `bigDecimalValue` and adds
unit tests to prevent future regressions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants