Repository navigation
Conversation
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.
|
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)); |
There was a problem hiding this comment.
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>
|
Good catch — the asymmetry was real, and the Nothing changes with default settings (both classes resolve the same built-in adapter). With custom adapters, Covered by tests: a custom boxed adapter must not apply, a custom primitive adapter round-trips symmetrically, and NaN/±Infinity write → read back under |
| return in.nextDouble() - 1; | ||
| } | ||
|
|
||
| @Override | ||
| public void write(JsonWriter out, Double value) throws IOException { | ||
| out.value(value + 1); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
|
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. |
|
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. |
Testing: JdkOptionalTest adds cases covering default rejection of NaN and Infinity and continued acceptance when serializeSpecialFloatingPointValues() is enabled.
d444349 to
03b19ed
Compare
Fixes #3190
Purpose
With default settings,
gson.toJson(OptionalDouble.of(Double.NaN))produced the non-JSON tokenNaN, while the same write for a plainDoublethrowsIllegalArgumentExceptionunlessserializeSpecialFloatingPointValues()is enabled. TheOptionalDoubleadapter calledJsonWriter.value(double)directly and so skipped the validity check that theDoubleadapter applies.Description
OptionalDoubleAdapteris now created with theDoubleadapter from the sameGsoninstance and writes the value through it. Default settings reject NaN and Infinity;serializeSpecialFloatingPointValues()still writes them ({"isPresent":true,"value":NaN}).OptionalIntandOptionalLongare untouched. EmptyOptionalDoublestill writes{"isPresent":false,"value":0.0}.Verification
mvn -pl gson test -Dtest=JdkOptionalTest— Tests run: 16, Failures: 1 (testOptionalDoubleSpecialValuesRejectedByDefault:181 expected java.lang.IllegalArgumentException to be thrown, but nothing was thrown).gsonmodule suite after the fix: Tests run: 4748, Failures: 0, Errors: 0, Skipped: 23 (pre-existing skips).