OpenAPITools / OpenAPITools/openapi-generator

Spring 3 Generator, generated parent class don't contain @JsonTypeName on maven plugin 7.2.0

Open
#17,581 5 comments 1 reaction 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

Worked on maven plugin version 6.2.1.
Tried with latest release 7.2.0

The generated parent class "Animal.java" doesn't contain an annotation @JsonTypeName("Animal").

openapi-generator version
OpenAPI declaration file content or url
components:
  schemas:
    Dog:
      allOf:
        - $ref: '#/components/schemas/Animal'
        - $ref: '#/components/schemas/Dog_allOf'
    Cat:
      allOf:
        - $ref: '#/components/schemas/Animal'
        - $ref: '#/components/schemas/Cat_allOf'
    Animal:
      discriminator:
        propertyName: className
      properties:
        className:
          type: string
        color:
          default: red
          type: string
      required:
        - className
      type: object
    Dog_allOf:
      properties:
        breed:
          type: string
      type: object
    Cat_allOf:
      properties:
        declawed:
          type: boolean
      type: object

With following configuration of Maven plugin

	<plugin>
				<groupId>org.openapitools</groupId>
				<artifactId>openapi-generator-maven-plugin</artifactId>
				<version>7.2.0</version>
				<executions>
					<execution>
						<goals>
							<goal>generate</goal>
						</goals>
						<configuration>
							<inputSpec>src/main/resources/test-api.yaml</inputSpec>
							<modelNamePrefix>ABC</modelNamePrefix>
							<modelNameSuffix>DTO</modelNameSuffix>
							<apiPackage>com.test.api</apiPackage>
							<modelPackage>com.test.model</modelPackage>
							<enablePostProcessFile>false</enablePostProcessFile>
							<generatorName>spring</generatorName>
							<additionalProperties>removeEnumValuePrefix=false</additionalProperties>
							<generateSupportingFiles>false</generateSupportingFiles>
							<generateModelTests>false</generateModelTests>
							<generateModelDocumentation>false</generateModelDocumentation>
							<generateApiDocumentation>false</generateApiDocumentation>
							<generateApiTests>false</generateApiTests>
							<configOptions>
								<interfaceOnly>true</interfaceOnly>
								<skipDefaultInterface>true</skipDefaultInterface>
								<serializableModel>true</serializableModel>
								<snapshotVersion>true</snapshotVersion>
								<useTags>true</useTags>
								<useSpringBoot3>true</useSpringBoot3>
								<generatedConstructorWithRequiredArgs>false</generatedConstructorWithRequiredArgs>
							</configOptions>
						</configuration>
					</execution>
				</executions>
			</plugin>

The generated code of Animal DTO:

package com.test.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.io.Serializable;
import java.time.OffsetDateTime;
import jakarta.validation.Valid;
import jakarta.validation.constraints.*;
import io.swagger.v3.oas.annotations.media.Schema;


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

/**
 * ABCAnimalDTO
 */

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

@Generated(value = "org.openapitools.codegen.languages.SpringCodegen", date = "2024-01-10T18:28:22.136625800+01:00[Europe/Berlin]")
public class ABCAnimalDTO implements Serializable {

  private static final long serialVersionUID = 1L;

  private String className;

  private String color = "red";

  public ABCAnimalDTO className(String className) {
    this.className = className;
    return this;
  }

  /**
   * Get className
   * @return className
  */
  @NotNull 
  @Schema(name = "className", requiredMode = Schema.RequiredMode.REQUIRED)
  @JsonProperty("className")
  public String getClassName() {
    return className;
  }

  public void setClassName(String className) {
    this.className = className;
  }

  public ABCAnimalDTO color(String color) {
    this.color = color;
    return this;
  }

  /**
   * Get color
   * @return color
  */
  
  @Schema(name = "color", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
  @JsonProperty("color")
  public String getColor() {
    return color;
  }

  public void setColor(String color) {
    this.color = color;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }
    ABCAnimalDTO animal = (ABCAnimalDTO) o;
    return Objects.equals(this.className, animal.className) &&
        Objects.equals(this.color, animal.color);
  }

  @Override
  public int hashCode() {
    return Objects.hash(className, color);
  }

  @Override
  public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append("class ABCAnimalDTO {\n");
    sb.append("    className: ").append(toIndentedString(className)).append("\n");
    sb.append("    color: ").append(toIndentedString(color)).append("\n");
    sb.append("}");
    return sb.toString();
  }

  /**
   * Convert the given object to string with each line indented by 4 spaces
   * (except the first line).
   */
  private String toIndentedString(Object o) {
    if (o == null) {
      return "null";
    }
    return o.toString().replace("\n", "\n    ");
  }
}
Generation Details
Steps to reproduce
Related issues/PRs
Suggest a fix

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 by reproducing the supplied schema with the Spring generator through the Maven plugin configuration, then inspect how the generated Animal DTO handles Jackson annotations. Compare the parent and child model output, and consider the issue done when the generated parent includes the expected @JsonTypeName annotation without regressing discriminator handling.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi, spring
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 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.