jackson-module-jsonSchema icon indicating copy to clipboard operation
jackson-module-jsonSchema copied to clipboard

Optional Support

Open yehuda-c opened this issue 8 years ago • 5 comments

Would be great if there was a way to configure this to serialize / deserialize json schema fields using Java 8 Optionals, so that fields of type T with required=false are deserialized to Optional<T> and fields with required=true are deserialized to T rather than needing to use @JsonAttribute annotations.

yehuda-c avatar Feb 08 '17 22:02 yehuda-c

I am not sure how this is related to what this module does, JSON Schema generation?

cowtowncoder avatar Feb 09 '17 00:02 cowtowncoder

JSON Schema generation will have a required field set to true or false. If this is settable based on whether the POJO contains an Optional, that would be amazing. For example:

{
  "type": "object",
  "properties": {
    "street_address": { "type": "string" },
    "city":           { "type": "string" },
    "state":          { "type": "string" }
  },
  "required": ["state"]
}

Would produce the pojo

public class Example {
    Optional<String> streetAddress;
    Optional<String> city;
    String state;
}

Does that explain this better?

yehuda-c avatar Feb 09 '17 05:02 yehuda-c

@YehudaCohen Yes, that helps. Now, I was under impression that Optional (and other "reference types") did indeed set required to false, but if this is not the case it should be changed to do so. Reverse (non-Optional marked as required) can not be made to work easily, although explicit required=true for @JsonProperty does do that.

Which version have you tried this with? Just want to verify that behavior you see is the latest one with 2.8.6.

cowtowncoder avatar Feb 09 '17 07:02 cowtowncoder

I'm looking for this feature (generate Optional generic) as well to be able to differentiate between a field which is set to null (default initialized) and not sent (optional).

owulff avatar Sep 04 '18 09:09 owulff

At this point I would need to know exactly what THIS module would need to do differently. Since module does not serialize/deserialize anything, either directly, or via jackson-databind, I wonder if this belongs to some other tool like "schema to pojo" generators.

cowtowncoder avatar Sep 05 '18 17:09 cowtowncoder