OpenAPITools / OpenAPITools/openapi-generator

[BUG][JAVA] ImportMapping do not work for model composition

Open
#15,357 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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 use of importMapping doesn't generate valid code in case of model composition definition.
Generating code using the following OAS YAML and the maven pom configuration provided, the generator produces the Message class as expected while the generated ExtendedBody is wrong.

Actual output
For the class Message, the body field is rightly mapped to BaseMessageBody

public class Message {

  @JsonProperty("header")
  private MessageHeader header;

  @JsonProperty("body")
  private BaseMessageBody body;
......
}

The class ExtendedBody extends MessageBody instead of BaseMessageBody as expected.

public class ExtendedBody extends MessageBody {

  @JsonProperty("additionalInfo")
  private String additionalInfo;

  @JsonProperty("sender")
  private String sender;
....
}

Expected output

public class ExtendedBody extends BaseMessageBody {

  @JsonProperty("additionalInfo")
  private String additionalInfo;

  @JsonProperty("sender")
  private String sender;
....
}
openapi-generator version

v6.4.0

OpenAPI declaration file content
openapi: 3.0.0
info:
  version: 1.0.0
  title: Example
  description: example
security:
  - basicAuth: []   # Use OAuth with a different scope
servers:
  - url: https://api.example.com/api    # The "url: " prefix is required
tags:
  - name: example

paths:
  /test:
    get:
      operationId: test
      tags:
        - "test"
      responses:
        200:
          description: OK
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Message"

components:
  securitySchemes:
    basicAuth:
      type: http
      scheme: basic
  
  schemas:
    MessageHeader:
      description: This is the header
      type: object
      properties:
        id:
          type: string
        type:
          type: string
        producer:
          type: string
        headerType:
          type: string
        date:
          type: string
          format: date-time
        version:
          type: string
      required:
        - id
        - type
        - producer
        - date
        - version
      discriminator:  
        propertyName: headerType

    MessageBody:
      description: This is the body 
      type: object
      properties:
        bodyType:
          type: string
      discriminator:  
        propertyName: bodyType

    Message:
      description: This is the message
      properties:
        header:
          $ref: '#/components/schemas/MessageHeader'
        body:
          $ref: '#/components/schemas/MessageBody'

    ExtendedBody:
      allOf:
        - $ref: '#/components/schemas/MessageBody'
        - type: object
          properties:
            additionalInfo: 
              type: string
            sender: 
              type: string


Generation Details

The following is the plugin section to generate the code in a maven project

      <plugin>
        <groupId>org.openapitools</groupId>
        <artifactId>openapi-generator-maven-plugin</artifactId>
        <version>6.4.0</version>
        <executions>
          <execution>
            <id>example</id>
            <goals>
              <goal>generate</goal>
            </goals>
            <configuration>
              <inputSpec>${project.basedir}/open-api/example-api.yaml</inputSpec>
              <generatorName>spring</generatorName>
              <modelNameSuffix></modelNameSuffix>
              <generateApiTests>false</generateApiTests>
              <generateModelTests>false</generateModelTests>
              <configOptions>
                <basePackage>org.organization.application.test</basePackage>
                <modelPackage>org.organization.application.test.model</modelPackage>
                <apiPackage>org.organization.application.test.api</apiPackage>
                <configPackage>org.organization.application.test.config</configPackage>
                <dateLibrary>java11</dateLibrary>
                <booleanGetterPrefix>is</booleanGetterPrefix>
                <delegatePattern>true</delegatePattern>
                <interfaceOnly>true</interfaceOnly>
                <useTags>true</useTags>
              </configOptions>
              <typeMappings>
                <typeMapping>MessageBody=BaseMessageBody</typeMapping>
              </typeMappings>
              <importMappings>
                <importMapping>BaseMessageBody=org.organization.application.test.custom.BaseMessageBody</importMapping>
              </importMappings>
            </configuration>
          </execution>
        </executions>
      </plugin>

Here the full zip
openapi-generator-maven-plugin-bug.zip

Steps to reproduce

Generate the code

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the supplied OpenAPI YAML and Maven plugin configuration, then trace how the Java generator handles model composition with typeMappings and importMappings. Generate the models from the reproduction archive and verify that ExtendedBody extends BaseMessageBody while Message continues to map its body field correctly.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.