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

Fail gracefully when a primitive value is absent. #1445

Merged
merged 1 commit into from Dec 5, 2021

Conversation

swankjesse
Copy link
Member

Without this we get an AssertionError, which is the wrong exception
type for a JSON schema mismatch:

java.lang.AssertionError: java.lang.NullPointerException: Cannot invoke "java.lang.Number.intValue()" because the return value of "sun.invoke.util.ValueConversions.primitiveConversion(sun.invoke.util.Wrapper, Object, boolean)" is null
  at com.squareup.moshi.RecordJsonAdapter.fromJson(RecordJsonAdapter.java:168)
  at com.squareup.moshi.internal.NullSafeJsonAdapter.fromJson(NullSafeJsonAdapter.java:41)
  at com.squareup.moshi.JsonAdapter.fromJson(JsonAdapter.java:70)
  at com.squareup.moshi.records.RecordsTest.absentPrimitiveFails(RecordsTest.java:257)
Caused by: java.lang.NullPointerException: Cannot invoke "java.lang.Number.intValue()" because the return value of "sun.invoke.util.ValueConversions.primitiveConversion(sun.invoke.util.Wrapper, Object, boolean)" is null
  at java.base/sun.invoke.util.ValueConversions.unboxInteger(ValueConversions.java:81)
  at java.base/java.lang.invoke.MethodHandle.invokeWithArguments(MethodHandle.java:732)
  at com.squareup.moshi.RecordJsonAdapter.fromJson(RecordJsonAdapter.java:156)
  ... 46 more

Without this we get an AssertionError, which is the wrong exception
type for a JSON schema mismatch:

    java.lang.AssertionError: java.lang.NullPointerException: Cannot invoke "java.lang.Number.intValue()" because the return value of "sun.invoke.util.ValueConversions.primitiveConversion(sun.invoke.util.Wrapper, Object, boolean)" is null
      at com.squareup.moshi.RecordJsonAdapter.fromJson(RecordJsonAdapter.java:168)
      at com.squareup.moshi.internal.NullSafeJsonAdapter.fromJson(NullSafeJsonAdapter.java:41)
      at com.squareup.moshi.JsonAdapter.fromJson(JsonAdapter.java:70)
      at com.squareup.moshi.records.RecordsTest.absentPrimitiveFails(RecordsTest.java:257)
    Caused by: java.lang.NullPointerException: Cannot invoke "java.lang.Number.intValue()" because the return value of "sun.invoke.util.ValueConversions.primitiveConversion(sun.invoke.util.Wrapper, Object, boolean)" is null
      at java.base/sun.invoke.util.ValueConversions.unboxInteger(ValueConversions.java:81)
      at java.base/java.lang.invoke.MethodHandle.invokeWithArguments(MethodHandle.java:732)
      at com.squareup.moshi.RecordJsonAdapter.fromJson(RecordJsonAdapter.java:156)
      ... 46 more
Copy link
Collaborator

@ZacSweers ZacSweers left a comment

Choose a reason for hiding this comment

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

Nice catch!

@swankjesse swankjesse merged commit 3cbd5c3 into master Dec 5, 2021
@swankjesse swankjesse deleted the jwilson.1204.fail_nicely_on_absent_primitives branch December 5, 2021 00:10
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.

None yet

2 participants