eclipse-ee4j / eclipse-ee4j/yasson
Can not cope with Optional.empty() arguments in @JsonbCreator method/constructor
- Dominant language
- Java
- Stars
- 218
- Forks
- 109
- Avg merge
- 1d 5h
- Merged PRs (30d)
- 9
Description
Just like in #237, I also like to stick to immutable pojos. However I tried a different approach using `Optional<>` fields. In comparisons [like this on](https://developer.ibm.com/articles/j-javaee8-json-binding-4/) with other Json serialization frameworks (Jackson and/or Gson) it is stated that JSON-B has improved handling of `Optional<>` fields.
However the examples mostly show only serialization works. How about deserialization optional fields. Especially when using immutable pojos. In the following example I used a contructor with `Optional<>` arguments:
``` java
import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
import javax.json.bind.annotation.JsonbCreator;
import javax.json.bind.annotation.JsonbProperty;
import java.util.Optional;
public class OptionalInConstructor {
public static class MyModel {
private final String mandatory;
private final Optional optional;
@JsonbCreator
public MyModel( @JsonbProperty("mandatory") String mandatory, @JsonbProperty("optional") Optional optional ) {
this.mandatory = mandatory;
this.optional = optional;
}
@JsonbProperty("mandatory")
public String getMandatory() {
return mandatory;
}
@JsonbProperty("optional")
public Optional getOptional() {
return optional;
}
}
public static void main( String[] args ) {
MyModel model = new MyModel( "mandatory", Optional.empty() );
// From Object -->Json
Jsonb jsonb = JsonbBuilder.create();
String serializedModel = jsonb.toJson( model );
System.out.println( "Json: " + serializedModel );
// From Json --> Object
MyModel deserialized = jsonb.fromJson( serializedModel, MyModel.class );
}
}
```
Unfortunately this lead to the following error:
```
Json: {"mandatory":"mandatory"}
Jun 24, 2019 11:29:14 PM org.eclipse.yasson.internal.Unmarshaller deserializeItem
SEVERE: JsonbCreator parameter optional is missing in json document.
Exception in thread "main" javax.json.bind.JsonbException: JsonbCreator parameter optional is missing in json document.
at org.eclipse.yasson.internal.serializer.ObjectDeserializer.createInstance(ObjectDeserializer.java:120)
at org.eclipse.yasson.internal.serializer.ObjectDeserializer.getInstance(ObjectDeserializer.java:93)
at org.eclipse.yasson.internal.serializer.AbstractContainerDeserializer.deserialize(AbstractContainerDeserializer.java:61)
at org.eclipse.yasson.internal.Unmarshaller.deserializeItem(Unmarshaller.java:70)
at org.eclipse.yasson.internal.Unmarshaller.deserialize(Unmarshaller.java:56)
at org.eclipse.yasson.internal.JsonBinding.deserialize(JsonBinding.java:53)
at org.eclipse.yasson.internal.JsonBinding.fromJson(JsonBinding.java:60)
at OptionalInConstructor.main(OptionalInConstructor.java:39)
```
When the `Optional<>` argument is present it works correctly.
Should this be possible? Or is there another way (without making my pojo mutable) to achieve this?
Contributor guide
Research direction
Reproduce the OptionalInConstructor example, then inspect the stack-trace entry points in ObjectDeserializer.java, especially createInstance, and the surrounding Unmarshaller flow. Compare the missing and present optional-argument cases; done means the intended deserialization behavior is established and covered by a regression test.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100