swagger-codegen icon indicating copy to clipboard operation
swagger-codegen copied to clipboard

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

Open sgeeroms opened this issue 8 years ago • 3 comments

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.

sgeeroms avatar Jul 27 '18 13:07 sgeeroms

Use format uuid instead. Otherwise you will have to provide your own class that realises the Guid.

silkentrance avatar Jul 27 '18 21:07 silkentrance

On "the wire", i.e. in the JSON, this is actually a string, but if you define a format, then it will also use a native class that implements the UUID or Guid in your case, marshalling/unmarshalling from/to an instance of that class.

See https://swagger.io/docs/specification/data-models/oneof-anyof-allof-not/ for uses of allOf etc. and https://swagger.io/docs/specification/data-models in general on how to define properties on an object.

silkentrance avatar Jul 27 '18 21:07 silkentrance

Hi @silkentrance,

Thanks for you reply.

In the case of the Guid I can use the format "uuid", but it was a general example. I would like to be able to define custom "base types" that I can reference in different places. and have the definition only in place.

sgeeroms avatar Aug 16 '18 08:08 sgeeroms