question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Model not generated with the right type (allOf)

See original GitHub issue
Description

A member of a model is not using the model type requested by the spec. Please check the example for a better explanation

openapi-generator version

Master (3.1.1-SNAPSHOT)

OpenAPI declaration file content or url

This is a self contained file that reproduce the issue

swagger: "2.0"
info:
  description: "dfsdfsdfs"
  version: "1.0.0"
  title: "Swagger Petstore"
  termsOfService: "http://swagger.io/terms/"
  contact:
    email: "apiteam@swagger.io"
  license:
    name: "Apache 2.0"
    url: "http://www.apache.org/licenses/LICENSE-2.0.html"
host: "petstore.swagger.io"
basePath: "/v2"
schemes:
- "https"
- "http"
paths:
  /lifecycles:
    put:
      tags:
        - Device
      summary: xxxxxxx
      operationId: editDeviceLifecycles
      parameters:
      - in: body
        name: device-lifecycle-update-info
        required: true
        schema:
          $ref: "#/definitions/DeviceLifecycleCore"
      responses:
        200:
          description: OK
          schema:
            $ref: "#/definitions/DeviceLifecycleCore"
definitions:
  DeviceLifecycleCore:
    type: object
    description: "DeviceLifecycleCore"
    properties:
      aaa:
        type: string
        description: "aaa"
      bbb:
        type: string
        description: "bbb"
      ccc:
        type: string
        description: "ccc"
      returnSoftwareStatus:
        $ref: "#/definitions/DeviceLifecycleStatus"
  DeviceLifecycleStatus:
    type: object
    description: "DeviceLifecycleStatus"
    allOf:
      - $ref: "#/definitions/DeviceLifecycleStatusCore"
      - type: object
        required:
          - id
        properties:
          id:
            type: integer
            format: int64
            description: "Id associated with the status"
            example: 123
  DeviceLifecycleStatusCore:
    type: object
    description: "DeviceLifecycleStatusCore"
    properties:
      name:
        type: string
        description: "Name"
      details:
        type: string
        description: "Any additional information needed for the status"
        example: "Something is wrong"

As you can see returnSoftwareStatus is supposed to be of type DeviceLifecycleStatus. If you look in the generated code, you will see that it is of type DeviceLifecycleStatusCore instead:

public DeviceLifecycleCore returnSoftwareStatus(DeviceLifecycleStatusCore returnSoftwareStatus) {
    this.returnSoftwareStatus = returnSoftwareStatus;
    return this;
  }

   /**
   * DeviceLifecycleStatus
   * @return returnSoftwareStatus
  **/
  @Valid
  public DeviceLifecycleStatusCore getReturnSoftwareStatus() {
    return returnSoftwareStatus;
  }

  public void setReturnSoftwareStatus(DeviceLifecycleStatusCore returnSoftwareStatus) {
    this.returnSoftwareStatus = returnSoftwareStatus;
  }
Command line used for generation

java -jar openapi-generator-cli.jar generate -i ./swagger.yaml --generator-name java-play-framework -o generatedServer -DhideGenerationTimestamp=true,booleanGetterPrefix=is

Steps to reproduce

Just run the command above

Related issues/PRs

None

Suggest a fix/enhancement

?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
rubmscommented, Jul 30, 2018

I am also affected by this bug.

The problem comes from the https://github.com/OpenAPITools/openapi-generator/commit/a897feef50989d8b0d0238c067eebaef4f7f6c70 commit in the https://github.com/OpenAPITools/openapi-generator/pull/360 pull request. More precisely, from this change https://github.com/OpenAPITools/openapi-generator/commit/a897feef50989d8b0d0238c067eebaef4f7f6c70#diff-7cb46fa53f89a458a7b7cb201d2214a8R1527.

Before this commit, when these properties where not un-aliased, a Schema instance was used to compose the model property of composed models (models containing allOf). This Schema instance contained the appropriate $ref field indicating the original datatype name, from which the data type name was correctly extracted via DefaultCodeGen::getSchemaType().

Now, with this un-aliasing, instead of a Schema with the proper $ref field, a ComposedSchema is used to compose the model properties of composed datatypes. In those ComposedSchemas the original data type name is lost and DefaultCodeGen::getSchemaType() returns the datatype of the first of the models involved in the allOff clause:

    public String getSchemaType(Schema schema) {
        if (schema instanceof ComposedSchema) {
            ComposedSchema cs = (ComposedSchema)schema;
            if (cs.getAllOf() != null) {
                Iterator var3 = cs.getAllOf().iterator();

                while(var3.hasNext()) {
                    Schema s = (Schema)var3.next();
                    if (s != null) {
                        schema = s;
                        break;
                    }
                }
            }
        }
...

Though the https://github.com/OpenAPITools/openapi-generator/pull/360 pull request fixed the issues #255 and #191, it originated this #582 issue.

@wing328, do you think it would be possible to keep the fixes brought by https://github.com/OpenAPITools/openapi-generator/pull/360 and at the same time have a proper generation of composed schemas?

Maybe something like this:

    public static Schema unaliasSchema(Map<String, Schema> allSchemas, Schema schema) {
        if (schema != null && StringUtils.isNotEmpty(schema.get$ref())) {
            Schema ref = allSchemas.get(ModelUtils.getSimpleRef(schema.get$ref()));
            if (ref == null) {
                LOGGER.warn("{} is not defined", schema.get$ref());
                return schema;
            } else if (isObjectSchema(ref)) { // model
                return schema;
            } else if (isStringSchema(ref) && (ref.getEnum() != null && !ref.getEnum().isEmpty())) {
                // top-level enum class
                return schema;
            } else if (isMapSchema(ref) || isArraySchema(ref) || isComposedSchema(ref)) { // map/array def should be created as models
                return schema;
            } else {
                return unaliasSchema(allSchemas, allSchemas.get(ModelUtils.getSimpleRef(schema.get$ref())));
            }
        }
        return schema;
    }

You can notice I added || isComposedSchema(ref) to the final else if. This way composed models would not get broken when creating properties for models.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Open API generator allOf not generating model correctly
I want to generate our models based on an Openapi spec 3.0 yml definition. In my spec I have a definition using allOf...
Read more >
oneOf, anyOf, allOf, not - Swagger
OpenAPI lets you combine and extend model definitions using the allOf keyword. allOf takes an array of object definitions that are used for...
Read more >
Schema generation rules · GitBook - Goswagger.Io
For each schema, go-swagger will generate one or more model types in go. ... Generated models uses no reflection except for enum and...
Read more >
OpenAPI Specification v3.1.0 | Introduction, Definitions, & More
Media type definitions are spread across several resources. ... integer format: int32 responses: NotFound: description: Entity not found.
Read more >
RE: REST Builder (OpenAPI) YAML "allOf" inheritance is not ...
But when buildREST is generating the stubs for this allOf schema, we do see two different DTO classes without an extends. Parent Class...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found