Skip to content

[BUG][JAVA] AllOf with mapping generates wrong JsonSubTypes annotations #14917

@leonard84

Description

@leonard84

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

The generator adds subtypes where they don't belong and doesn't respect the mapping.

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "petType", visible = true)
@JsonSubTypes({
  @JsonSubTypes.Type(value = Cat.class, name = "Cat"),        // <--- should not be generated
  @JsonSubTypes.Type(value = Dog.class, name = "Dog"),        // <--- should not be generated
  @JsonSubTypes.Type(value = Lizard.class, name = "Lizard"),  // <--- should not be generated
  @JsonSubTypes.Type(value = Cat.class, name = "cat"),
  @JsonSubTypes.Type(value = Dog.class, name = "dog"),
  @JsonSubTypes.Type(value = Lizard.class, name = "lizard"),
})

public class Pet
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "petType", visible = true)
// vvvvvv This should not be generated at all vvvv
@JsonSubTypes({
  @JsonSubTypes.Type(value = Cat.class, name = "cat"),
  @JsonSubTypes.Type(value = Dog.class, name = "dog"),
  @JsonSubTypes.Type(value = Lizard.class, name = "lizard"),
})

public class Cat extends Pet {

similarly the static mapper is wrong:

Pet:

static {
  // Initialize and register the discriminator mappings.
  Map<String, Class<?>> mappings = new HashMap<String, Class<?>>();
  mappings.put("Cat", Cat.class);
  mappings.put("Dog", Dog.class);
  mappings.put("Lizard", Lizard.class);
  mappings.put("cat", Cat.class);
  mappings.put("dog", Dog.class);
  mappings.put("lizard", Lizard.class);
  mappings.put("Pet", Pet.class);
  JSON.registerDiscriminator(Pet.class, "petType", mappings);
}

Cat:

static {
  // Initialize and register the discriminator mappings.
  Map<String, Class<?>> mappings = new HashMap<String, Class<?>>();
  mappings.put("cat", Cat.class);
  mappings.put("dog", Dog.class);
  mappings.put("lizard", Lizard.class);
  mappings.put("Cat", Cat.class);
  JSON.registerDiscriminator(Cat.class, "petType", mappings);
}

This causes broken serialization as can be seen here https://github.com/gradle/gradle-enterprise-api-samples/blob/de89e6b8d702d1e08df1f09fea419e1c5e87d543/src/test/java/Test.java#L36-L40

openapi-generator version

6.4.0

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  title: test
  description: >-
    test schema
  version: 1.0.0
servers:
  - url: 'http://test.com'
    description: stage
paths:
  /demo:
    get:
      summary: placeholder summary
      responses:
        '200':
          description: Success
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Pet'
components:
  schemas:
    AllPets:
      type: object
      properties:
        pets:
          type: array
          items:
            $ref: '#/components/schemas/Pet'
    Pet:
      type: object
      required:
        - petType
      properties:
        petType:
          type: string
      discriminator:
        propertyName: petType
        mapping:
          cat: '#/components/schemas/Cat'
          dog: '#/components/schemas/Dog'
          lizard: '#/components/schemas/Lizard'
    Cat:
      allOf:
        - $ref: '#/components/schemas/Pet'
        - type: object
          # all other properties specific to a `Cat`
          properties:
            name:
              type: string
    Dog:
      allOf:
        - $ref: '#/components/schemas/Pet'
        - type: object
          # all other properties specific to a `Dog`
          properties:
            bark:
              type: string
    Lizard:
      allOf:
        - $ref: '#/components/schemas/Pet'
        - type: object
          # all other properties specific to a `Lizard`
          properties:
            lovesRocks:
              type: boolean
Generation Details

https://github.com/gradle/gradle-enterprise-api-samples/blob/de89e6b8d702d1e08df1f09fea419e1c5e87d543/build.gradle.kts#L36-L58

Steps to reproduce

Checkout https://github.com/gradle/gradle-enterprise-api-samples/tree/openapi-generator-issue_allOf_Subtypes
Run ./gradlew :test

Related issues/PRs
Suggest a fix

Remove the typeinfo on subclasses and filter out the duplicate type info for mappings.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions