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

Adapters not applied properly for properties of differing read/write type #309

Closed
aguibert opened this issue Aug 21, 2019 · 3 comments · Fixed by #312
Closed

Adapters not applied properly for properties of differing read/write type #309

aguibert opened this issue Aug 21, 2019 · 3 comments · Fixed by #312
Assignees
Labels
bug Something isn't working right
Milestone

Comments

@aguibert
Copy link
Member

If I register a JsonbAdapter for a type T, it will not be correctly applied to properties of type Optional upon serialization. For example:

Adapter class:

public class ThrowableAdapter implements JsonbAdapter<Throwable, Map<String, Object>> {

  @Override
  public Map<String, Object> adaptToJson(Throwable obj) throws Exception {
    HashMap<String, Object> output = new HashMap<>();
    output.put("message", obj.getMessage());
    output.put("type", obj.getClass().getName());

    return unmodifiableMap(output);
  }

  @Override
  public Throwable adaptFromJson(Map<String, Object> obj) throws Exception {
    throw new UnsupportedOperationException("not implemented");
  }
}

Property class:

public class ValueClass {

  public Instant getStartedAt() { ... }

  public Optional<Instant> getFinishedAt() { ... }

  public Optional<Throwable> getException() { ... }

  // setters ....
}

If we attempt to serialize an object of type ValueClass like this:

public class JsonbTypeAdapterTest {

  private final JsonbConfig config = new JsonbConfig()
      .withAdapters(
          new ThrowableAdapter()
      )
      .withFormatting(true);

  private final Jsonb jsonb = JsonbBuilder.newBuilder()
      .withConfig(config)
      .build();

  @Test
  public void testTypeAdapter() {
    ValueClass obj = new ValueClass();
    obj.setStartedAt(Instant.now());
    obj.setException(new RuntimeException("hit me"));
    
    String toJson = jsonb.toJson(obj); // fails with ClassCastException
  }
}

Stack trace:

javax.json.bind.JsonbException: Unable to serialize property 'exception' from org.example.testcase.jsonbtypeadapter.ValueClass
	at org.eclipse.yasson.internal.serializer.ObjectSerializer.serializeInternal(ObjectSerializer.java:68)
	at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serialize(AbstractContainerSerializer.java:64)
	at org.eclipse.yasson.internal.Marshaller.serializeRoot(Marshaller.java:148)
	at org.eclipse.yasson.internal.Marshaller.marshall(Marshaller.java:76)
	at org.eclipse.yasson.internal.Marshaller.marshall(Marshaller.java:102)
	at org.eclipse.yasson.internal.JsonBinding.toJson(JsonBinding.java:118)
	at org.example.testcase.jsonbtypeadapter.JsonbTypeAdapterTest.testTypeAdapter(JsonbTypeAdapterTest.java:31)
Caused by: javax.json.bind.JsonbException: Problem adapting object of type class java.lang.Throwable to java.util.Map<java.lang.String, java.lang.Object> in class class org.example.testcase.jsonbtypeadapter.ThrowableAdapter
	at org.eclipse.yasson.internal.serializer.AdaptedObjectSerializer.serialize(AdaptedObjectSerializer.java:72)
	at org.eclipse.yasson.internal.serializer.AbstractContainerSerializer.serializerCaptor(AbstractContainerSerializer.java:96)
	at org.eclipse.yasson.internal.serializer.ObjectSerializer.marshallProperty(ObjectSerializer.java:100)
	at org.eclipse.yasson.internal.serializer.ObjectSerializer.serializeInternal(ObjectSerializer.java:66)
	... 24 more
Caused by: java.lang.ClassCastException: java.util.Optional incompatible with java.lang.Throwable
	at java.base/java.lang.ClassCastException.<init>(ClassCastException.java:71)
	at org.example.testcase.jsonbtypeadapter.ThrowableAdapter.adaptToJson(ThrowableAdapter.java:9)
	at org.eclipse.yasson.internal.serializer.AdaptedObjectSerializer.serialize(AdaptedObjectSerializer.java:61)
	... 27 more
@aguibert aguibert self-assigned this Aug 21, 2019
@aguibert aguibert added the bug Something isn't working right label Aug 21, 2019
@aguibert
Copy link
Member Author

After investigating this more I realized that it has to do with a little more than just the getter methods on the JSON model class. This occurs when there is a field of the same name as a getter, but with different types, for example:

public class ValueClass {
   
  private Throwable exception;

  public Optional<Throwable> getException() { ... }
}

@bmarwell
Copy link
Contributor

Good finding! Actually, in my real application, I was using Immutables (https://github.com/immutables/immutables), i.e. a generated class. It indeed uses @Nullable Throwable exception internally, because Optional should of course never be used in a field.

That's omnited in the example from #309 (comment), but does it still produce the same error?

@aguibert
Copy link
Member Author

hi @bmhm, yes I was able to reproduce the issue with the generated class, and after attaching a debugger I realized that the root issue was the property field and corresponding getter method having different Java types.

I expect to have a PR soon which you could use to confirm the fix.

@aguibert aguibert changed the title Custom adapters of type T do not also apply to Optional<T> Adapters not applied properly for properties of differing read/write type Aug 26, 2019
@aguibert aguibert added this to the 1.0.5 milestone Sep 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working right
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants