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

Usage of jackson-databind-nullable with openapi generator plugin #17

Open
MRDcoder opened this issue Jun 16, 2020 · 4 comments
Open

Usage of jackson-databind-nullable with openapi generator plugin #17

MRDcoder opened this issue Jun 16, 2020 · 4 comments

Comments

@MRDcoder
Copy link

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
Copy link
Author

@jmini Can you please help me out here?

@tatarco
Copy link

tatarco commented Aug 10, 2020

i'm trying to understand the same

@newmodelcoder
Copy link

newmodelcoder commented May 1, 2021

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.

@WIStudent
Copy link

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.

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

No branches or pull requests

4 participants