OpenAPITools / OpenAPITools/openapi-generator

[BUG][Java] The client generated by version 3.3.4 hasn't properties generated as concrete Java class but as generic Object class

Open
#2,366 6 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)?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
Description

If i generate client with version 3.0.3 the Cat class is:

public class Cat extends Pet {
  public static final String SERIALIZED_NAME_HUNTS = "hunts";
  @SerializedName(SERIALIZED_NAME_HUNTS)
  private Boolean hunts = null;

  public static final String SERIALIZED_NAME_AGE = "age";
  @SerializedName(SERIALIZED_NAME_AGE)
  private Integer age = null;

  public static final String SERIALIZED_NAME_LINKS = "links";
  @SerializedName(SERIALIZED_NAME_LINKS)
  private CatLinks links = null;

  public Cat hunts(Boolean hunts) {
    this.hunts = hunts;
    return this;
  }

but if I generate client with version 3.3.4 than the Cat class is:

public class Cat extends Pet {
  public static final String SERIALIZED_NAME_HUNTS = "hunts";
  @SerializedName(SERIALIZED_NAME_HUNTS)
  private Boolean hunts;

  public static final String SERIALIZED_NAME_AGE = "age";
  @SerializedName(SERIALIZED_NAME_AGE)
  private Integer age;

  public static final String SERIALIZED_NAME_LINKS = "links";
  @SerializedName(SERIALIZED_NAME_LINKS)
  private Object links = null;

The expected output is that the links is a CatLinks class not Object class.

openapi-generator version

It is regression between 3.3.4 and 3.0.3 of the openapi-generator-maven-plugin.

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  title: My demo
  version: 0.1.0
servers:
  - url: /api/v2
paths:
  /pets:
    patch:
      requestBody:
        content:
          application/json:
            schema:
              oneOf:
                - $ref: '#/components/schemas/Cat'
                - $ref: '#/components/schemas/Dog'
              discriminator:
                propertyName: pet_type
      responses:
        '200':
          description: Updated
components:
  schemas:
    Pet:
      type: object
      required:
        - pet_type
      properties:
        pet_type:
          type: string
      discriminator:
        propertyName: pet_type
    Dog:     # "Dog" is a value for the pet_type property (the discriminator value)
      allOf: # Combines the main `Pet` schema with `Dog`-specific properties 
        - $ref: '#/components/schemas/Pet'
        - type: object
          # all other properties specific to a `Dog`
          properties:
            bark:
              type: boolean
            breed:
              type: string
              enum: [Dingo, Husky, Retriever, Shepherd]
    Cat:     # "Cat" is a value for the pet_type property (the discriminator value)
      allOf: # Combines the main `Pet` schema with `Cat`-specific properties 
        - $ref: '#/components/schemas/Pet'
        - type: object
          # all other properties specific to a `Cat`
          properties:
            hunts:
              type: boolean
            age:
              type: integer
            links:
              type: object
              example:
                self: "/api/v2/scrapers/1"
                owners: "/api/v2/scrapers/1/owners"
                members: "/api/v2/scrapers/1/members"
              properties:
                self:
                  $ref: "#/components/schemas/Link"
                members:
                  $ref: "#/components/schemas/Link"
                owners:
                  $ref: "#/components/schemas/Link"
    Link:
      type: string
      format: uri
      readOnly: true
      description: URI of resource.
Command line used for generation

Generate client via:

mvn org.openapitools:openapi-generator-maven-plugin:generate
Steps to reproduce

The maven plugin configuration:

<plugin>
                <groupId>org.openapitools</groupId>
                <artifactId>openapi-generator-maven-plugin</artifactId>
                <version>3.0.3</version>
                <configuration>
                    <inputSpec>${project.basedir}/src/main/resources/swagger.yml</inputSpec>
                    <generatorName>java</generatorName>
                    <configOptions>
                        <sourceFolder>java</sourceFolder>
                        <dateLibrary>java8</dateLibrary>
                    </configOptions>
                    <generateModelDocumentation>false</generateModelDocumentation>
                    <generateApis>false</generateApis>
                    <generateSupportingFiles>false</generateSupportingFiles>
                    <verbose>false</verbose>
                    <output>${project.basedir}/src/generated</output>
                    <addCompileSourceRoot>false</addCompileSourceRoot>
                    <modelPackage>com.example.api.model</modelPackage>
                </configuration>
            </plugin>
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 with the OpenAPI declaration in src/main/resources/swagger.yml and reproduce the Java client using the openapi-generator-maven-plugin configuration and Maven generate command. Compare the generated Cat model under src/generated with the expected output; done means links is generated as CatLinks rather than Object, with regression coverage for the provided schema.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
devtools, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.