swagger-api / swagger-api/swagger-codegen

[JAVA] generator cannot handle use of allOf or $ref for defined simple types

Open
#8,498 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

When I define a common simple type like for example:

definitions:
...
  Guid:
    type: string
    format: guid
    pattern: '^[0-9A-Fa-f]{8}(-[0-9A-Fa-f]{4}){3}-[0-9A-Fa-f]{12}$'
    description: |
      A guid identifier. No value can be set as "000000000-0000-0000-0000-00000000000".
    example: '5e0fe30e-3724-42c2-b5b9-0b1594b0b7a3'
    x-example: "5e0fe30e-3724-42c2-b5b9-0b1594b0b7a3"

And then I use this type as follows in a defined object type:

  MyObjectType:
    type: object
    required:
      - id
    properties:
      id:
        description: |
          the id of the object
        allOf:
          - $ref: "#/definitions/Guid"

or even...

  MyObjectType:
    type: object
    required:
      - id
    properties:
      id:
        $ref: "#/definitions/Guid"

Then the generated code is not correct.
In the first case the property "id" is simply missing from the "MyObjectType" class.
And in the second case the property is added as a class object property of Class type "Guid"

public class MyObjectType{

  @JsonProperty("id")
  private Guid id= null;

but the class "Guid" does not exist, and its should be a String.

This is they way to define commonly used simple types in Swagger.
Could the generator be changed to be able to handle this?

Swagger-codegen version

v2.2.3

Swagger declaration file content or url
Command line used for generation
        main = 'io.swagger.codegen.SwaggerCodegen'
        args = ["generate",
                "-l", "java",                                                       // language
                "-i", "${project.ext.inputDir}${version}/${project.ext.inputFile}", // input
                "-c", configJson.getAbsolutePath(),                                 // config
                "-o", "${project.ext.sourceset}",                                   // output
        ]
        jvmArgs = ['-Dmodels']                                                      // generate models only
Steps to reproduce
Related issues/PRs
Suggest a fix/enhancement

When handling a "$ref" or "allOf" reference the type (object, string,...) of the referenced type should be take into account. So not always a class (object) reference is assumed.

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 Java model-only generation command with the provided Guid and MyObjectType definitions. Trace how the generator resolves $ref and allOf for model properties. Done means the id property is generated as a String in MyObjectType for both forms, without generating or referencing a Guid class.

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.