jackson-databind-nullable icon indicating copy to clipboard operation
jackson-databind-nullable copied to clipboard

Usage of jackson-databind-nullable with openapi generator plugin

Open MRDcoder opened this issue 4 years ago • 4 comments

Hi Team,

Sorry if my question is redundant, I would like to know we can use this module with openapi generator plugin from swagger. Suppose I have field email and I want it to be JsonNullable as its optional field and would like to see whether its present or not.

/Mudassar

MRDcoder avatar Jun 16 '20 07:06 MRDcoder

@jmini Can you please help me out here?

MRDcoder avatar Jun 18 '20 12:06 MRDcoder

i'm trying to understand the same

tatarco avatar Aug 10 '20 10:08 tatarco

I'm doing some initial experimentation with openapi-generator 5.1.0 (not exactly the same) to create server-side models for a Spring Boot app from a 3.0 spec. Here's what I've seen empirically:

  1. For a property with default settings (not required, not nullable), the field in the model will be null when the property was absent in the request or present with a null value.
  2. For a required property, validation fails if the property is absent in the JSON, or present with a value of null
  3. For a nullable property, the field type is JsonNullable, and isPresent() reflects whether the property was in the JSON, and if so, the wrapped value is the value in JSON (null or otherwise)
  4. For a required AND nullable property, the field type in the model is JsonNullable. This is similar to above, except that it fails validation when the property is present in the JSON with a value of null (which would be incorrect behavior since the property was nullable in the spec - the constraint added to the model property should be requiring presence instead I think)

So it seems that JsonNullable is intended only for cases where a property is nullable, rather than not required/optional. This was a bit of a surprise to me, though I supposed it shouldn't have been - the name of the class is JsonNullable after all.

Cases 2. and 3. above seem straightforward. But it does seem like there's some loss of information in the first case - I see a null value in the model whether the property was present in the JSON or if the JSON had a null value for it. I guess the argument would be that since the property is not nullable, one doesn't need to make the distinction. But this seems to assume that the incoming JSON would conform to the spec. If I received JSON with an explicit null value for a property that is not nullable, then I expect the request to be invalid. But there isn't enough information to do that.

I was expecting something more like a JsonOptional class (which would be used the model field type when a property is not marked required), but maybe there are issues with that approach too. Maybe to have precise validation/error reporting every single field would need to use a wrapper type.

The spring generator does have a useOptional config option documented, though I didn't see any effect on the generated model class.

newmodelcoder avatar May 01 '21 16:05 newmodelcoder

I also ran into the issue that the code generated by openapi-generator does not validate required nullable fields correctly. I posted a workaround here.

WIStudent avatar Mar 31 '22 10:03 WIStudent