swagger-api / swagger-api/swagger-codegen

[ALL] Wrong Schema generation

Open
#9,007 1 comment 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

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.

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 test.yaml and the Java generation command, then trace DefaultCodegenConfig.getTypeOfSchema() through getSchemaType(). Compare the generated signature in java-petstore/src/main/java/io/swagger/client/api/PetApi.java with the schema's integer/int64 values. Done means deletePet() uses Long for petId and regression coverage verifies the mapping.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.