OpenAPITools / OpenAPITools/openapi-generator

[BUG][Java][Spring] inheritance not working using java/spring generator with multi file opeanpi 3.1.0 spec

Open
#18,686 1 comment 5 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

Composition instead of inheritance is generated when using java/spring generator with openap:i 3.1.0 declaration split into multiple files. With openapi: 3.0.2 it works fine.

openapi-generator version

openapi-generator-maven-plugin:7.5.0

OpenAPI declaration file content or url

openapi.yaml:

openapi: 3.1.0
info:
  title: multi-file-openapi
  version: 1.0.0
servers:
  - url: 'http://localhost:8080/rest'
paths:
  /dummy-path:
    get:
      operationId: getDummy
      responses:
        '200':
          description: Get dummy.
          content:
            application/json:
              schema:
                $ref: './child.yaml'

child.yaml:

title: Child
allOf:
  - $ref: './parent.yaml'
  - properties:
      propB:
        type: string

parent.yaml:

title: Parent
discriminator:
  propertyName: propA
properties:
  propA:
    type: string
required:
  - propA
Generation Details
<plugin>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>
    <executions>
        <execution>
            <phase>generate-sources</phase>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <generatorName>spring</generatorName>
                <inputSpec>${project.basedir}/src/main/resources/openapi-multiple-files/openapi.yaml</inputSpec>
                <output>${project.basedir}</output>
                <generateApis>false</generateApis>
                <generateApiTests>false</generateApiTests>
                <generateApiDocumentation>false</generateApiDocumentation>
                <generateModelTests>false</generateModelTests>
                <generateModelDocumentation>false</generateModelDocumentation>
                <generateSupportingFiles>false</generateSupportingFiles>
                <configOptions>
                    <library>spring-boot</library>
                    <serializableModel>true</serializableModel>
                    <groupId>${project.groupId}</groupId>
                    <artifactId>${project.artifactId}</artifactId>
                    <version>${project.version}</version>
                    <useJakartaEe>true</useJakartaEe>
                </configOptions>
            </configuration>
        </execution>
    </executions>
</plugin>
Steps to reproduce
  1. Generate code using openapi-generator-maven-plugin:7.5.0 and provided openapi declaration file.
Actual behavior (with openapi: 3.1.0)

A single class Child with both propA and propB is generated:

package org.openapitools.model;

import java.net.URI;
import java.util.Objects;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import org.openapitools.jackson.nullable.JsonNullable;
import java.time.OffsetDateTime;
import javax.validation.Valid;
import javax.validation.constraints.*;
import io.swagger.v3.oas.annotations.media.Schema;


import java.util.*;
import javax.annotation.Generated;

/**
 * Child
 */


@Generated(value = "org.openapitools.codegen.languages.SpringCodegen", date = "2024-05-16T12:43:04.523358872Z[Etc/UTC]", comments = "Generator version: 7.6.0-SNAPSHOT")
public class Child {

  private String propA;

  private String propB;

  // generated constructor, getters, setters, etc.
}
Expected behavior (as it is with openapi: 3.0.2):

Two classes are generated:

  • Parent class with propA property
package org.openapitools.model;

import java.net.URI;
import java.util.Objects;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeName;
import org.openapitools.jackson.nullable.JsonNullable;
import java.time.OffsetDateTime;
import javax.validation.Valid;
import javax.validation.constraints.*;
import io.swagger.v3.oas.annotations.media.Schema;


import java.util.*;
import javax.annotation.Generated;

/**
 * Parent
 */

@JsonIgnoreProperties(
  value = "propA", // ignore manually set propA, it will be automatically generated by Jackson during serialization
  allowSetters = true // allows the propA to be set during deserialization
)
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "propA", visible = true)
@JsonSubTypes({
  @JsonSubTypes.Type(value = Child.class, name = "child")
})

@Generated(value = "org.openapitools.codegen.languages.SpringCodegen", date = "2024-05-16T13:05:57.171195852Z[Etc/UTC]", comments = "Generator version: 7.6.0-SNAPSHOT")
public class Parent {

  private String propA;

  // generated constructor, getters, setters, etc.
}
  • Child class with propB property, extending Parent class
package org.openapitools.model;

import java.net.URI;
import java.util.Objects;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeName;
import org.openapitools.model.Parent;
import org.openapitools.jackson.nullable.JsonNullable;
import java.time.OffsetDateTime;
import javax.validation.Valid;
import javax.validation.constraints.*;
import io.swagger.v3.oas.annotations.media.Schema;


import java.util.*;
import javax.annotation.Generated;

/**
 * Child
 */


@JsonTypeName("child")
@Generated(value = "org.openapitools.codegen.languages.SpringCodegen", date = "2024-05-16T13:05:57.171195852Z[Etc/UTC]", comments = "Generator version: 7.6.0-SNAPSHOT")
public class Child extends Parent {

  private String propB;

  // generated constructor, getters, setters, etc.
}


Related issues/PRs

N/A

Suggest a fix

N/A

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 Maven generator configuration and reproduce the result using openapi.yaml, child.yaml, and parent.yaml with OpenAPI 3.1.0, then compare it with the 3.0.2 output. Trace how the Spring generator resolves the split allOf and discriminator schemas; done means the generated Parent and Child classes preserve inheritance and the regression is covered by a test.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi, spring
Domain
backend, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.