OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.24k stars 6.43k forks source link

[BUG][jaxrs-spec] @JsonTypeName does not use specified discriminator mapping #10822

Open languitar opened 2 years ago

languitar commented 2 years ago

Bug Report Checklist

Description

https://swagger.io/specification/#discriminator-object describes how the mapping key in a discriminator object can be used to control the contents of the discriminator field in case of polymorphism. When providing a mapping, instead of the type name the declared map key has to be used per type as the content of the discriminator field.

The current code used to generate the @JsonTypeName annotation that is used to provide the discriminator value with jackson mapping in many generators (including jaxrs-spec) always uses the data type name, ignoring a potential mapping.

For the example from the linked specification:

    MyResponseType:
      oneOf:
      - $ref: '#/components/schemas/Cat'
      - $ref: '#/components/schemas/Dog'
      - $ref: '#/components/schemas/Lizard'
      - $ref: 'https://gigantic-server.com/schemas/Monster/schema.json'
      discriminator:
        propertyName: petType
        mapping:
          dog: '#/components/schemas/Dog'
          monster: 'https://gigantic-server.com/schemas/Monster/schema.json'

The generated Dog Java class should have the following annotation: @JsonTypeName("dog"). At the moment, it will always be @JsonTypeName("Dog").

openapi-generator version

5.3.x

OpenAPI declaration file content or url
components:
  schemas:
    MyResponseType:
      oneOf:
      - $ref: '#/components/schemas/Dog'
      discriminator:
        propertyName: petType
        mapping:
          dog: '#/components/schemas/Dog'
    Dog:
      type: object
      properties:
        test:
          type: string
Generation Details

nothing fancy, jaxrs-spec with library Quarkus and Jackson mapping is enough to trigger this.

Steps to reproduce

Just generate the code.

Related issues/PRs
Suggest a fix

I suspect there are some actual Java code changes required to compute the intended name from a potentially existing mapping.

celnan commented 2 years ago

Hi, I'm doing some experimentations with jaxrs-spec generator and I have the same issue.

As a workaround, at the moment I duplicated the mappings value ("dog" in the example above) adding the extension x-discriminator-value to the models (the Dog model in the example above) and I overrided the template to use it instead of the name.

I agree with languitar: I think that some java changes are required to pass the mapping to the pojo template.

PS: the issue happens also on the version 6.0.0 of the openapi-generator.

celnan commented 2 years ago

Hi, just for info: I managed to retrieve the correct mapping by switching to handlebars and accessing them from parent model

patrice-conil commented 9 months ago

In fact, if you remove @JsonTypeName from template everything works fine (Jackson seems to retrieve typeName from parent). You can use pojo.mustache and typeInfoAnnotation.mustache templates from JavaSpring generator.