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

[JAVA] Prioritize mapped discriminators over generated (relates to issue #12777) #15284

Merged

Conversation

robbertvanwaveren
Copy link
Contributor

@robbertvanwaveren robbertvanwaveren commented Apr 21, 2023

This PR introduces a slightly smarter ordering of the discriminators which prioritizes actual non-generated mappings as defined in the original spec above others as suggested here #12777 (comment).

The result is that the correct value is selected to populate the discriminator field during serialization based on the actual discriminator mapping is available in the schema.

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package 
    ./bin/generate-samples.sh
    ./bin/utils/export_docs_generators.sh
    
    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*.
    For Windows users, please run the script in Git BASH.
  • In case you are adding a new generator, run the following additional script :
    ./bin/utils/ensure-up-to-date.sh
    
    Commit all changed files.
  • File the PR against the correct branch: master (6.3.0) (minor release - breaking changes with fallbacks), 7.0.x (breaking changes without fallbacks)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

@robbertvanwaveren
Copy link
Contributor Author

@wing328 Could you have a look at this change?

(the failed check is inherited from the issue in master with the python-nextgen client and is not related)

@Qunit
Copy link

Qunit commented Apr 28, 2023

Helpful fix

@@ -128,7 +128,13 @@ public int compareTo(MappedModel other) {
} else if (other.getMappingName() == null) {
return -1;
}
return getMappingName().compareTo(other.getMappingName());

final boolean mappedEqualsModelName = getMappingName().equals(getModelName());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you please add a line or two explaining the change/code block?

does it aim to maintain the same order in the spec?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added the comment and rebased the branch with current master @wing328

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rebased branch to deal with removed python samples

@wing328 wing328 added this to the 7.0.0 milestone May 11, 2023
@robbertvanwaveren robbertvanwaveren force-pushed the prioritized-discriminators branch 2 times, most recently from 6d5ac19 to c251fb7 Compare May 12, 2023 09:02
@b3nk4n
Copy link

b3nk4n commented Jun 20, 2023

Thank you! This is a really helpful fix for us!

@wing328 wing328 modified the milestones: 7.0.0, 7.0.1 Aug 25, 2023
@mcleap
Copy link

mcleap commented Sep 8, 2023

Please merge it before 7.0.1 release, we really like this feature. :) We've just faced with this issue...

@wing328
Copy link
Member

wing328 commented Sep 12, 2023

Can someone please share a spec to reproduce the issue? I tested with the one in #12777 (comment) but the output remains the same with this patch/PR.

@robbertvanwaveren
Copy link
Contributor Author

In the original issue different people actually refer to different issues. This PR solves the most generic of those. Namely that objects are serialized using the proper value as per the key in the discriminator spec.

I don't have time to test it right now, but I believe this spec shows the problem

openapi: 3.0.1
info:
  title: Discriminator Test spec
  version: 0.0.1
paths:
  /random-parent:
    get:
      operationId: test
      responses:
        '200':
          description: Successful operation
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Parent'
components:
  schemas:
    Parent:
      type: object
      properties:
        parent:
          type: string
        child:
          oneOf:
            - $ref: '#/components/schemas/Child1'
            - $ref: '#/components/schemas/Child2'              
    Child1:
      allOf:
        - $ref: '#/components/schemas/Child'
        - type: object
          properties:
            problematic:
              type: boolean
              default: true
    Child2:
      allOf:
        - $ref: '#/components/schemas/Child'
        - type: object
          properties:
            sweet:
              type: boolean
              default: true
    Child:
      type: object
      properties:
        name:
          type: string
        type:
          type: string
      discriminator:
        propertyName: 'type'
        mapping:
          # ProblemChild/SweetChildOfMine come alphabetically behind Child1/Child2
          ProblemChild: '#/components/schemas/Child1'
          SweetChildOfMine: '#/components/schemas/Child2'

The resulting interface "ParentChild.java" should look like this with this patch:

@JsonSubTypes({
  @JsonSubTypes.Type(value = Child1.class, name = "ProblemChild"),
  @JsonSubTypes.Type(value = Child2.class, name = "SweetChildOfMine"),
  @JsonSubTypes.Type(value = Child1.class, name = "Child1"),
  @JsonSubTypes.Type(value = Child2.class, name = "Child2"),
})

whereas without it the ordering will be different, resulting in jackson picking the wrong values during serialization.

@wing328 wing328 modified the milestones: 7.0.1, 7.1.0 Sep 19, 2023
@wing328
Copy link
Member

wing328 commented Sep 27, 2023

FYI @OpenAPITools/generator-core-team

@wing328 wing328 merged commit beb67aa into OpenAPITools:master Sep 27, 2023
39 checks passed
AlanCitrix pushed a commit to AlanCitrix/openapi-generator that referenced this pull request Oct 26, 2023
…sue OpenAPITools#12777) (OpenAPITools#15284)

* prioritize mapped discriminators over generated

* update samples with new ordering

* explain reason behind discriminator prioritization

* add new samples

* prioritize explicit mapping over any generated mappings

* update examples to reflect new logic

* update tests to reflect explicit mappings
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants