openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[JAVA] Bug Including Non Null fields in response

Open hwhrag opened this issue 6 years ago • 4 comments
trafficstars

Description

Expect default nullable=false to generate @JsonInclude(JsonInclude.Include.NON_NULL) excluding non-null fields from being returned. This is the default behavior in swagger-codegen.

openapi-generator version

4.1.2

OpenAPI declaration file content or url
type: integer
nullable: false
Command line used for generation

mvn clean install

Steps to reproduce

Define API response in yml using OpenAPI 3 spec. Run maven to create generated classes.

Related issues/PRs

None.

hwhrag avatar Oct 14 '19 13:10 hwhrag

@hwhrag may I know if you've time to contribute a fix?

wing328 avatar Oct 15 '19 16:10 wing328

In https://github.com/OpenAPITools/openapi-generator/pull/3441 we had a long discussion with @Fjolnir-Dvorak and @cbornet about Jackson configuration.

You can do it:

  • at mapper level
  • at model level with annotations

When you do it at model level this wins over the mapper configuration. Right now we tend to prefer using mapper configuration, because this is more flexible.


In addition your issue description is not clear to me. Are you generating a client or a server? Which generator are you using?

jmini avatar Oct 18 '19 05:10 jmini

Just ran into this myself, migrating a Spring Boot 2 app to 3 which includes switching from swagger-codegen to openapi-generator. In my case I'm generating a java client & library is resttemplate.

I can work around this issue by replacing/adjusting https://github.com/OpenAPITools/openapi-generator/blob/v6.5.0/modules/openapi-generator/src/main/resources/Java/jackson_annotations.mustache#L8, but I'd prefer a proper fix. Possibly some option to retain the current behavior by default?

famod avatar May 05 '23 08:05 famod

I think that the opposite behaviour should actually be implemented. If a field is specified with nullable: true in the OpenAPI declaration, the generator should generate @JsonInclude(JsonInclude.Include.NON_NULL) on the related field. If a field is not nullable in the declaration, it will be never null anyway. Therefore, it doesn't matter which JsonInclude value is used.

If a field is nullable, the generator would currently add it during serialization anyway, e. g.: "myField": null

Currently we call a 3rd party API that unfortunately can't handle null values corretly in the received json. It assumes that the field should either have a value or not be present at all. I know that this is rather a problem in the implementation of the 3rd party API, but if the generator used @JsonInclude(JsonInclude.Include.NON_NULL), we wouldn't have such problems.

devjeff avatar Jun 26 '24 13:06 devjeff