Skip to content

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

Merged
zslayton merged 2 commits intomasterfrom
numeric-coercions
Jul 2, 2024
Merged

Restores float <-> decimal conversions in the binary reader#905
zslayton merged 2 commits intomasterfrom
numeric-coercions

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 popematt and tgregg 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.

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
@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