Skip to content

[BUG][JAVA][SPRING] Fix serialization when there is a discriminator with mapping #14731

@jorgerod

Description

@jorgerod

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

When setting the modelNameSuffix option the name is being sanitized so the @JsonTypeName annotation is always added.

In the case of discriminator with mapping, when adding that @JsonTypeName the serialization is being done incorrectly.

openapi-generator version

6.3.0

OpenAPI declaration file content or url
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'
Generation Details

java -jar openapi-generator-cli.jar generate -i schema.yaml -o foo -g spring --model-name-suffix DTO

Steps to reproduce

Test

  @Test
  void testSerialization() throws JsonProcessingException {
    final ObjectMapper objectMapper = new ObjectMapper();

    final String foo = objectMapper.writeValueAsString(new FooDTO());
    assertThat(foo).contains("{\"type\":\"foo\"}");

  }

Output:

java.lang.AssertionError: 
Expecting actual:
  "{"type":"foo"}"
to contain:
  "{"type":"Foo"}" 

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