swagger-api / swagger-api/swagger-codegen

[JAVA] Generated models have incorrect type for referenced primitive property

Open
#10,029 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

I would like to cross-reference a primitive property from #/components/schemas/ObjectA/properties/property into object B and then have the model for object B declare the property type as the correct primitive type.

Currently, such a cross-reference validates but results in a generated model where the type is the non-existing 'ObjectApropertiesproperty' (note that this is equivalent to the last three elements of the path without the / character), resulting in unusable code.

Swagger-codegen version

3.0.16. Also observed in earlier versions.

Swagger declaration file content or url

Example:

openapi: 3.0.1
info:
  title: Swagger Petstore
  description: Cross-reference properties in java autogen code bug test
  version: 1.0.0
paths:
  /pet/:
    get:
      responses:
        200:
          description: successful operation
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Pet'
  /pet-summary/:
    get:
      responses:
        200:
          description: successful operation
          content:
            '*/*':
              schema:
                $ref: '#/components/schemas/Pet-Summary'
components:
  schemas:
    Pet:
      type: object
      properties:
        name:
          type: string
          example: doggie
    Pet-Summary:
      type: object
      properties:
        name:
          $ref: '#/components/schemas/Pet/properties/name'

Results in the following autogenerated model 'PetSummary'

@javax.annotation.Generated(value = "io.swagger.codegen.v3.generators.java.JavaClientCodegen", date = "2020-02-05T15:26:50.087-05:00[America/New_York]")
public class PetSummary {
  @JsonProperty("id")
  private Integer id = null;

  @JsonProperty("name")
  private Petpropertiesname name = null;
  // ...

Whereas the expected model would be:

@javax.annotation.Generated(value = "io.swagger.codegen.v3.generators.java.JavaClientCodegen", date = "2020-02-05T15:26:50.087-05:00[America/New_York]")
public class PetSummary {
  @JsonProperty("id")
  private Integer id = null;

  @JsonProperty("name")
  private String name = null;
  // ...
Command line used for generation

mvn -P generate-api

where the pom has the following profile:

      <profile>
            <id>generate-api</id>
            <properties>
                <packaging>pom</packaging>
                <sfdcapi-inputspec>src/main/resources/api.yml</sfdcapi-inputspec>
            </properties>
            <build>
                <defaultGoal>clean generate-sources</defaultGoal>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-clean-plugin</artifactId>
                        <configuration>
                            <excludeDefaultDirectories>true</excludeDefaultDirectories>
                            <filesets>
                                <fileset>
                                    <directory>src/main/java/com/test/api</directory>
                                </fileset>
                                <fileset>
                                    <directory>src/main/java/com/test/model</directory>
                                </fileset>
                            </filesets>
                        </configuration>
                    </plugin>

                    <plugin>
                        <groupId>io.swagger.codegen.v3</groupId>
                        <artifactId>swagger-codegen-maven-plugin</artifactId>
                        <version>3.0.16</version>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>generate</goal>
                                </goals>
                                <configuration>
                                    <language>java</language>
                                    <apiPackage>com.test.api</apiPackage>
                                    <modelPackage>com.test.model</modelPackage>
                                    <generateModels>true</generateModels>
                                    <generateApis>true</generateApis>
                                    <generateModelTests>false</generateModelTests>
                                    <generateApiTests>false</generateApiTests>
                                    <generateSupportingFiles>false</generateSupportingFiles>
                                    <generateModelDocumentation>false</generateModelDocumentation>
                                    <generateApiDocumentation>false</generateApiDocumentation>
                                    <addCompileSourceRoot>false</addCompileSourceRoot>
                                    <configOptions>
                                        <dateLibrary>java8</dateLibrary>
                                        <java8>true</java8>
                                        <library>resttemplate</library>
                                    </configOptions>
                                    <inputSpec>${sfdcapi-inputspec}</inputSpec>
                                    <output>${project.basedir}</output>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                    
                </plugins>
            </build>
        </profile>
Steps to reproduce
  1. Update POM as shown above
  2. Create api.yml in src/main/resources in a project directory
  3. Run mvn -P generate-api
Related issues/PRs

https://github.com/swagger-api/swagger-codegen/issues/6322

Suggest a fix/enhancement

When creating a model object that is defined in-part by referenced properties, respect the datatype of the referenced property.

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 api.yml example and the Maven generate-api profile, then reproduce the output for PetSummary. Trace the Java model generation and referenced-property type resolution; done means the generated name field uses String rather than Petpropertiesname and the reproduction still generates successfully.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
api, 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.