swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
16.92k stars 6.03k forks source link

[ALL] Wrong Schema generation #9007

Open skrysmanski opened 5 years ago

skrysmanski commented 5 years ago
Description

Using Codegen 3 against a Swagger 2.0 file "misinterprets" some data type (at least int, but there may be others).

Swagger-codegen version

3.0.3

Swagger declaration file content or url

test.yaml

Command line used for generation

java -Dapis -cp swagger-codegen-cli-3.0.3.jar io.swagger.codegen.v3.cli.SwaggerCodegen generate --input-spec test.yaml --lang java --output generated/java-petstore

Steps to reproduce
  1. Generate Java client from file mentioned above
  2. Open generated file java-petstore\src\main\java\io\swagger\client\api\PetApi.java
  3. Notice the signature of the method deletePet() is public void deletePet(Integer petId) but should actually be public void deletePet(Long petId).

The underlying problem is that the Schema instance generated for parameter petId is of type Schema rather than IntegerSchema. The schema does, however, have the correct values for type and format:

class Schema {
    title: null
    multipleOf: null
    maximum: null
    exclusiveMaximum: null
    minimum: null
    exclusiveMinimum: null
    maxLength: null
    minLength: null
    pattern: null
    maxItems: null
    minItems: null
    uniqueItems: null
    maxProperties: null
    minProperties: null
    required: null
    type: integer
    not: null
    properties: null
    additionalProperties: null
    description: null
    format: int64
    $ref: null
    nullable: null
    readOnly: null
    writeOnly: null
    example: null
    externalDocs: null
    deprecated: null
    discriminator: null
    xml: null
}

Thus, the method DefaultCodegenConfig.getTypeOfSchema() (used through getSchemaType()) runs into the else branch and then just returns schema.getType() - which returns "integer" rather than the expected "long".

Related issues/PRs

None I could find.

Suggest a fix/enhancement

None.

skrysmanski commented 5 years ago

As a side note: I just converted the Swagger/2.0 spec into OpenAPI/3.0 and with this Codegen generates Long correctly.