Skip to content

Reject NaN and Infinity when writing OptionalDouble by default - #3193

Open
Inf1mum wants to merge 4 commits into
google:mainfrom
Inf1mum:optional-double-special-values
Open

Inf1mum wants to merge 4 commits into
google:mainfrom
Inf1mum:optional-double-special-values

Conversation

@Inf1mum

@Inf1mum Inf1mum commented Nov 29, 2026

Copy link
Copy Markdown

Fixes #3190

Purpose

With default settings, gson.toJson(OptionalDouble.of(Double.NaN)) produced the non-JSON token NaN, while the same write for a plain Double throws IllegalArgumentException unless serializeSpecialFloatingPointValues() is enabled. The OptionalDouble adapter called JsonWriter.value(double) directly and so skipped the validity check that the Double adapter applies.

Description

  • OptionalDoubleAdapter is now created with the Double adapter from the same Gson instance and writes the value through it. Default settings reject NaN and Infinity; serializeSpecialFloatingPointValues() still writes them ({"isPresent":true,"value":NaN}).
  • Reading is unchanged. OptionalInt and OptionalLong are untouched. Empty OptionalDouble still writes {"isPresent":false,"value":0.0}.

Verification

  • RED on master 2bc8b5e: mvn -pl gson test -Dtest=JdkOptionalTest — Tests run: 16, Failures: 1 (testOptionalDoubleSpecialValuesRejectedByDefault:181 expected java.lang.IllegalArgumentException to be thrown, but nothing was thrown).
  • GREEN after the fix: Tests run: 16, Failures: 0, Errors: 0.
  • Full gson module suite after the fix: Tests run: 4748, Failures: 0, Errors: 0, Skipped: 23 (pre-existing skips).

The OptionalDouble adapter wrote the value with JsonWriter.value(double)
directly, so toJson produced the non-JSON tokens NaN and Infinity even
with default settings, while plain double and Double values are rejected
with IllegalArgumentException unless serializeSpecialFloatingPointValues()
is enabled.

The adapter now writes the value through the Gson-configured Double
adapter, so default settings reject NaN and Infinity, and
serializeSpecialFloatingPointValues() keeps writing them. Reading is
unchanged.
@google-cla

google-cla Bot commented Nov 29, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

// serializeSpecialFloatingPointValues() is enabled.
// Note: in OpenJDK, OptionalDouble.EMPTY has value = Double.NaN; writing 0.0 when empty
// avoids that failure.
doubleAdapter.write(out, value.orElse(0));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Delegating write() to gson.getAdapter(Double.class) while read() still calls in.nextDouble() makes round-tripping asymmetric whenever a custom TypeAdapter<Double> is registered on GsonBuilder (writes go through the custom adapter, reads bypass it). Also, ReflectiveTypeAdapterFactory resolves the underlying double field with double.class rather than Double.class. If custom adapters are meant to apply here, read() should delegate to the same double.class adapter (guarding against null); otherwise, checking serializeSpecialFloatingPointValues directly avoids routing OptionalDouble through unrelated custom Double adapters.

Signed-off-by: Vadim Lazuk <vadimlazuk5@gmail.com>
Delegate OptionalDouble reading to the same Gson-configured
double adapter used for writing, so round-trips stay symmetric
when custom double adapters are registered. Resolve the adapter
for primitive double, mirroring what ReflectiveTypeAdapterFactory
uses for a double field and what double[] elements use.

Signed-off-by: Vadim Lazuk <vadimlazuk5@gmail.com>
@Inf1mum

Inf1mum commented Nov 29, 2026

Copy link
Copy Markdown
Author

Good catch — the asymmetry was real, and the double.class point too: that's what ReflectiveTypeAdapterFactory uses for a double field and what double[] elements resolve. Fixed in 133a028: read() now delegates to the same Gson-configured adapter as write(), null-guarded, and the lookup is double.class.

Nothing changes with default settings (both classes resolve the same built-in adapter). With custom adapters, OptionalDouble now behaves exactly like a plain primitive double field — a Double.class-only registration no longer leaks into writes, and a double.class registration applies to reads and writes symmetrically.

Covered by tests: a custom boxed adapter must not apply, a custom primitive adapter round-trips symmetrically, and NaN/±Infinity write → read back under serializeSpecialFloatingPointValues().

Comment on lines +234 to +239
return in.nextDouble() - 1;
}

@Override
public void write(JsonWriter out, Double value) throws IOException {
out.value(value + 1);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Just a suggestion: Could alternatively for read and write do something like throw new AssertionError("should not be called").

That might make the intention even clearer.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Applied in d444349 - the Double.class stubs now throw AssertionError("should not be called"). Kept the real +/- 1 transforms in the double.class test below, since there the registered adapter is expected to be invoked.

@Inf1mum

Inf1mum commented Dec 1, 2026

Copy link
Copy Markdown
Author

Just a small update: the Google CLA check is green now on the current head (d444349), and all CI checks are passing as well. Whenever you have a moment, I'd appreciate a look at this PR. Happy to address any feedback.

@Inf1mum

Inf1mum commented Dec 3, 2026

Copy link
Copy Markdown
Author

Following up on my note from December 1st — the PR is still green, including the CLA check, and nothing has changed on my side. Whenever review time allows, a look at this would be much appreciated.

@Inf1mum Inf1mum closed this Dec 4, 2026
@Inf1mum Inf1mum reopened this Dec 4, 2026
@Inf1mum Inf1mum closed this Dec 4, 2026
@Inf1mum Inf1mum reopened this Dec 4, 2026
Testing: JdkOptionalTest adds cases covering default rejection of NaN and
Infinity and continued acceptance when serializeSpecialFloatingPointValues()
is enabled.
@Inf1mum
Inf1mum force-pushed the optional-double-special-values branch from d444349 to 03b19ed Compare December 4, 2026 21:17

This branch has not been deployed

No deployments
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.

OptionalDouble adapter writes NaN and Infinity without serializeSpecialFloatingPointValues()

3 participants