-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
fix #13150 Do not add schema / class name mapping where custom mapping exists #13815
fix #13150 Do not add schema / class name mapping where custom mapping exists #13815
Conversation
…ustom mapping exists
@cachescrubber (2022/02) @welshm (2022/02) @MelleD (2022/02) @atextor (2022/02) @manedev79 (2022/02) @javisst (2022/02) @borsch (2022/02) @banlevente (2022/02) @Zomzog (2022/09) |
Optional.ofNullable(vendorExtensions) | ||
.map(ve -> ve.get("x-discriminator-value")) | ||
.map(discriminatorValue -> (String) discriminatorValue) | ||
.orElse(currentSchemaName); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This logic is changing - since this value previously would have been null
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, prior to this change currentSchemaName
was added as a mapping name on line 3365, and then another mapping was added on line 3371 if an x-discriminator-value
was set. With this change, it's one or the other, not both.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if this change is causing the issues you're seeing with Dart. I'd have to look more closely at how the schemas are being used later on, but this adds less schemas now; so perhaps some filterins logic is applied at some point?
I would try reverting this part of the change and see what the Dart output looks like.
Is this still required after #13958 ? |
They seem unrelated to me. The other is about adding additional annotations to the model class / interface. This one is about having singular mapping names for |
cc @OpenAPITools/generator-core-team as the change impacts default codegen. |
Hello @bernie-schelberg-mywave @wing328 What is the status of this issue? |
@jorgerod as far as I know, it is planned to merge this one into the version 7 branch. I don't really have any update on a timeline though, sorry. |
I have done a serialization test with your changes. The JsonSubTypes are no longer duplicated. @Test
void testSerialization() throws JsonProcessingException {
final ObjectMapper objectMapper = new ObjectMapper();
final String foo = objectMapper.writeValueAsString(new FooDTO());
assertThat(foo).contains("{\"type\":\"Foo\"}");
} The output is:
I'm not a Jackson expert, but it seems that in the serialization Jackson is using the value of What do you think? |
Hi @jorgerod, the error message in your comment doesn't match your test. In your test you say that you expect Also, what is |
Hi @bernie-schelberg-mywave, I have experienced a similar issue as @jorgerod. Regarding your question of the |
Hi @bernie-schelberg-mywave spec openapi: '3.0.0'
info:
version: '1.0.0'
title: 'FooService'
paths:
/parent:
put:
summary: put parent
operationId: putParent
parameters:
- name: name
in: path
required: true
description: Name of the account being updated.
schema:
type: string
requestBody:
description: The updated account definition to save.
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/Parent'
responses:
'200':
$ref: '#/components/responses/Parent'
components:
schemas:
Parent:
type: object
description: Defines an account by name.
properties:
name:
type: string
description: The account name.
type:
type: string
description: The account type discriminator.
required:
- name
- type
discriminator:
propertyName: type
mapping:
foo: '#/components/schemas/Foo'
Foo:
allOf:
- $ref: "#/components/schemas/Parent"
- type: object
properties:
fooType:
type: string
responses:
Parent:
description: The saved account definition.
content:
application/json:
schema:
$ref: '#/components/schemas/Parent' Test: @Test
void testSerialization() throws JsonProcessingException {
final ObjectMapper objectMapper = new ObjectMapper();
final String foo = objectMapper.writeValueAsString(new FooDTO());
assertThat(foo).contains("{\"type\":\"foo\"}");
} Output:
As @troehling says, Jackson use @JsonTypeName("Foo") for serialization. I'm testing and I think that modifying Currently there is this: openapi-generator/modules/openapi-generator/src/main/resources/JavaSpring/pojo.mustache Lines 16 to 20 in 12e76ec
It would be to put this: {{#jackson}}
{{#isClassnameSanitized}}
{{^hasDiscriminatorWithNonEmptyMapping}}
@JsonTypeName("{{name}}")
{{/hasDiscriminatorWithNonEmptyMapping}}
{{/isClassnameSanitized}}
{{/jackson}} If there is a discriminator with mapping, don't add @JsonTypeName I also think this bugfix should go in a separate PR from this one as they are different problems. How do you see it @bernie-schelberg-mywave @troehling @wing328 @welshm ? EDIT: With the change I mention, this issue would be solved. |
@jorgerod I think I understand now. I think it may be related, but a different issue to this one. I think the issue you describe is present in the master branch, is it not? |
Sorry this fell off my radar for a bit! Can you regenerate the samples to include with the PR? Or did no samples change? This looks fine to me from a SpringCodegen point of view - but the changes are in the default codegen so may need more reviewers - although @wing328 may be able to take a look as well |
@jorgerod I agree with your suggestion that the fix should be in a separate PR - it will also be quicker to merge and simpler to review as it will just be touching a Spring codegen mustache. |
@welshm I merged |
Might want to add some samples then that show this behavior being used. Not required though. |
@welshm I modified one of the examples to have a discriminator mapping, renaming |
Hi @bernie-schelberg-mywave @welshm |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for updating the samples (and modifying them to use excercise the code change!)
Minor suggestion on parameter naming, but otherwise I think this is good.
If you are not on the Slack, I would recommend joining - Link
You will need to get someone from the default codegen maintainers to review still
properties: | ||
className: | ||
type: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: I would not use type
here since it's the name of another property of the YAML - maybe animalType
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agree, it's confusing. I've changed it to species
.
@@ -21,6 +21,8 @@ | |||
import com.fasterxml.jackson.annotation.JsonTypeName; | |||
import com.fasterxml.jackson.annotation.JsonValue; | |||
import org.openapitools.client.model.Animal; | |||
import org.openapitools.client.model.Cat; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems weird to import itself, but separate problem
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that's weird. I've seen that some subclasses have the @JsonSubTypes
annotation as well (here for example), which I don't think is necessary (not because of this change, the master
branch shows this behaviour as well, but it wasn't in the samples before because the source schema didn't use discriminator values). Because of this annotation, every subclass refers to every other subclass. This may be the reason for the unnecessary import here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agreed it's something we can deal with later.
properties: | ||
className: | ||
type: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same nit - would probably call this animalType
or animalSpecies
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed to species
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i've cherry-pick some commits and created #14984 instead.
i didn't rename className
to something else
# Conflicts: # modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java
I've updated the target branch to 7.0.x as I think that's what this change is based on. |
closed via #14984 instead |
Having multiple names for a model mapping can cause issues with clients that are expecting a particular types to always have the same model name. Refer #13150.
PR checklist
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*
.For Windows users, please run the script in Git BASH.
master
(6.1.0) (minor release - breaking changes with fallbacks),7.0.x
(breaking changes without fallbacks)