[java][jackson] fix: conditionally add jackson JsonIgnoreProperties for base class polymorphism fixes #12777 #22527#22528
Conversation
…12777 # Conflicts: # modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java # modules/openapi-generator/src/test/java/org/openapitools/codegen/java/AbstractJavaCodegenTest.java
|
CI issue not related to this change (samples already updated in latest master) |
|
thanks for the PR let's give it a try |
|
Thank you for doing all the work, I just have one suggestion/problem with this solution: Pet:
type: object
discriminator:
propertyName: petIntType
mapping:
0: '#/components/schemas/Dog'
1: '#/components/schemas/Cat'
properties:
name:
type: string
petIntType:
type: integer
required:
- name
Cat:
description: A representation of a cat
allOf:
- $ref: '#/components/schemas/Pet'
- type: object
properties:
huntingSkill:
type: string
required:
- huntingSkill
Dog:
description: A representation of a dog
allOf:
- $ref: '#/components/schemas/Pet'
- type: object
properties:
packSize:
type: integer
format: int32
required:
- packSize
With this simple Javacode: final var response = new Dog().petIntType(0).packSize(10).name("Dogo");
String ser = new ObjectMapper().writeValueAsString(response);
System.out.println("Seriliazied: " + ser)Results in this: My suggestion would be to use JsonTypeInfo.As.EXISTING_PROPERTY when disableDiscriminatorFieldIgnore is set to true. Something like this: {{^disableDiscriminatorJsonIgnoreProperties}}
@JsonIgnoreProperties(
value = "{{{discriminator.propertyBaseName}}}", // ignore manually set {{{discriminator.propertyBaseName}}}, it will be automatically generated by Jackson during serialization
allowSetters = true // allows the {{{discriminator.propertyBaseName}}} to be set during deserialization
)
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "{{{discriminator.propertyBaseName}}}", visible = true)
{{/disableDiscriminatorJsonIgnoreProperties}}
{{#disableDiscriminatorJsonIgnoreProperties}}
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "{{{discriminator.propertyBaseName}}}", visible = true)
{{/disableDiscriminatorJsonIgnoreProperties}}This resolved the issue for me and I think it would not cause any problems, but I am happy to here any feedback! |
Adresses issues:
Flag added to config:
disableDiscriminatorFieldIgnore -> if true, it ignores, does not creates jakarta @JsonIgnoreProperties in parent class like so:
@bbdouglas (2017/07) @sreeshas (2017/08) @jfiala (2017/08) @lukoyanov (2017/09) @cbornet (2017/09) @jeff9finger (2018/01) @karismann (2019/03) @Zomzog (2019/04) @lwlee2608 (2019/10) @martin-mfg (2023/08)
PR checklist
Commit all changed files.
This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
These must match the expectations made by your contribution.
You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example
./bin/generate-samples.sh bin/configs/java*.IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
master(upcoming7.x.0minor release - breaking changes with fallbacks),8.0.x(breaking changes without fallbacks)"fixes #123"present in the PR description)