Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[REQ] [Java] Add JsonIgnoreProperties to model #3438

Open
Fjolnir-Dvorak opened this issue Jul 24, 2019 · 11 comments · May be fixed by #3441
Open

[REQ] [Java] Add JsonIgnoreProperties to model #3438

Fjolnir-Dvorak opened this issue Jul 24, 2019 · 11 comments · May be fixed by #3441

Comments

@Fjolnir-Dvorak
Copy link
Contributor

Is your feature request related to a problem? Please describe.

We have the scenario that we have a service with multiple client APIs and a lot of other interfaces for other systems like databases which are also working with json.
The project is using one single JsonMapper for everything. The problem is: The JsonMapper is not iognoring unknown fields per se.

Describe the solution you'd like

To acommodate this issue we are adding the annotation

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)

to each model each time we need to regenerate the API per hand (to be honest, sed, but even sed can be considered a manual intervention).

Would this addition destroy anything? No, the generator provides its own JsonMapper which does exactly the same, but implicit, not explicit. This addition would (in my opinion) add some beautiful flavour of further documentation to each model making it even possible to use your own mapper without any further configuration or intervention.

There will be a new flag to turn off this new feature, but it will be on by default

Pull Request will follow...

Fjolnir-Dvorak added a commit to Fjolnir-Dvorak/openapi-generator that referenced this issue Jul 24, 2019
…alFieldsAnnotation" to turn off this feature
@Fjolnir-Dvorak Fjolnir-Dvorak linked a pull request Jul 24, 2019 that will close this issue
4 tasks
@jmini
Copy link
Member

jmini commented Aug 13, 2019

Which client/server are you using?

The jersey2 java-client is configuring the jackson mapper to ignore the missing properties:

mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

So is the jaxrs-jersey server:


I think we need to define if we would like to add support at model level (using the jackson annotation) or at jackson mapper level:

Related issue: #3573 (both options mapper and annotation are possible)

@cbornet
Copy link
Member

cbornet commented Aug 28, 2019

To me the root issue is that you use the same mapper for everything. Why do you have to do so ? It's a design smell IMO since it introduces coupling. If you're shipping the client, can't you just include your own mapper configured as you like ?

@wangq8
Copy link

wangq8 commented Aug 6, 2020

Is this resolved?

@brettryan
Copy link

@cbornet it shouldn't be assumed that the same classes are used for everything. One reason for this is for forwards compatibility where backend model classes are enriched with enhancements over time but some software is using an older yet compatible version of the API that does not need access to these properties.

@brettryan
Copy link

One option that could be used is to utilise additionalModelTypeAnnotations. I've attempted this by putting the fllowing in my configuration file:

  "additionalModelTypeAnnotations": "@JsonIgnoreProperties(ignoreUnknown = true)",

What happened though is my annotation got translated with escaped equals sign.

@JsonIgnoreProperties(ignoreUnknown = true)

@ericnjogu
Copy link

Building on the post by @brettryan, the following worked okay:

<configuration>
	<configOptions>
		<additionalModelTypeAnnotations>@JsonIgnoreProperties(ignoreUnknown = true)</additionalModelTypeAnnotations>
	</configOptions>
</configuration>

@iliassk
Copy link

iliassk commented Dec 20, 2021

Hi @ericnjogu which version are you using? I'm using the latest version 5.3.0 but the <additionalModelTypeAnnotations>@JsonIgnoreProperties(ignoreUnknown = true)</additionalModelTypeAnnotations> is not working for me, the configuration is not translated in the model to @JsonIgnoreProperties(ignoreUnknown = true)

@nrbw
Copy link

nrbw commented Jan 20, 2022

@ericnjogu that's exactly what I needed, I pimped it a bit to avoid importing JsonIgnoreProperties
<additionalModelTypeAnnotations>@com.fasterxml.jackson.annotation.JsonIgnoreProperties(ignoreUnknown = true)</additionalModelTypeAnnotations>

@iliassk I used 5.3.0 as well, the configuration @ericnjogu gave us is valid for the maven plugin.

@Ferioney
Copy link

Ferioney commented May 5, 2022

I have the same issue. When API has new fields, generated model can't parse these fields.

As for me, @Fjolnir-Dvorak proposes good solution to add @JsonIgnoreProperties(ignoreUnknown = true) annotation for each model. In this case all models will be backward compatible "from the box" and will not depend on mapper configuration.

Is it possible to actualize and merge this pull request?

@Fjolnir-Dvorak
Copy link
Contributor Author

@Ferioney I will look into it to get it running at least.

@gabel
Copy link

gabel commented Dec 5, 2022

The workaround using the <additionalModelTypeAnnotations> stopped working when using discriminator properly as it is then generating an additional @ JsonIgnoreProperties.

For me this REQ would still make a lot of sense as I do want that definition on model side and not in every using object mapper.

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants