Search code examples
java-17openapi-generator-maven-plugin

openapi-generator-maven-plugin instead of inheritance adds all properties to generated java class


I'm using openapi-generator to create java classes from yaml. I get a problem that some classes instead of inheritance have the properties from base classes included. Here is my plugin configuration, api yaml, actual and expected result in java:

            <plugin>
                <groupId>org.openapitools</groupId>
                <artifactId>openapi-generator-maven-plugin</artifactId>
                <version>7.7.0</version>
                <configuration>
                    <generatorName>spring</generatorName>
                    <configOptions>
                       <sourceFolder>src/gen/java</sourceFolder>
                       <interfaceOnly>true</interfaceOnly>
                       <skipDefaultInterface>true</skipDefaultInterface>
                       <useJakartaEe>true</useJakartaEe>
                       <useBeanValidation>false</useBeanValidation>
                       <performBeanValidation>false</performBeanValidation>
                       <openApiNullable>false</openApiNullable>
                       <booleanGetterPrefix>is</booleanGetterPrefix>
                       <useSpringBoot3>true</useSpringBoot3>
                    </configOptions>
                    <modelPackage>my.package.model</modelPackage>
                    <apiPackage>my.package.api</apiPackage>
                    <output>${project.basedir}</output>
                    <generateApiDocumentation>false</generateApiDocumentation>
                    <generateModelDocumentation>false</generateModelDocumentation>
                    <generateSupportingFiles>false</generateSupportingFiles>
                    <generateApiTests>false</generateApiTests>
                    <generateModelTests>false</generateModelTests>
                </configuration>
                <executions>
                    <execution>
                        <id>generate-testdeleteme</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>${project.basedir}/src/main/resources/openapi/testdeleteme-api-def-single.yaml</inputSpec>
                    </execution>
                </executions>
            </plugin>
openapi: 3.0.1
info:
  title: deletemetest API
  description: delete me test
  version: 1.0.0
servers:
- url: https://myhost.net/
paths: {}
components:
  schemas:
    DeleteMePO:
      allOf:
      - $ref: '#/components/schemas/BasePO'
      - type: object
        properties:
          address:
            type: string
    BasePO:
      type: object
      properties:
        id:
          type: integer
          format: int64
x-original-swagger-version: "2.0"

produces two classes:

@Generated(value = "org.openapitools.codegen.languages.SpringCodegen", date = "...", comments = "Generator version: 7.7.0")
public class BasePO {

  private Long id;
...
@Generated(value = "org.openapitools.codegen.languages.SpringCodegen", date = "...", comments = "Generator version: 7.7.0")
public class DeleteMePO {

  private Long id;

  private String address;

and I would expect to have inheritance in DeleteMePO (DeleteMePO extends BasePO)

public class DeleteMePO extends BasePO {

  private String address;

Any idea why this happens and how to force openapi-generator to create classes with inheritance?


Solution

  • There are two ways to accomplish that, either add to configuration <openapiNormalizer>REF_AS_PARENT_IN_ALLOF=true</openapiNormalizer> to handle every allOf as inheritance:

                <plugin>
                    <groupId>org.openapitools</groupId>
                    <artifactId>openapi-generator-maven-plugin</artifactId>
                    <version>7.7.0</version>
                    <configuration>
                        <generatorName>spring</generatorName>
                        <openapiNormalizer>REF_AS_PARENT_IN_ALLOF=true</openapiNormalizer>
                        ...
    
    

    or add x-parent: true to parent yaml definition:

        ...
        BasePO:
          type: object
          x-parent: true
        ...