swagger-api / swagger-api/swagger-codegen
Model names shadowing language provided types collide in generated code
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
Description
When using a reference in the Swagger schema where the referenced definition shares its name with a language-provided type, generated code does not make a distinction between the language type and the generated model.
This results in an inability to properly (de)serialize params. This applies to multiple languages, but as an example, when the below Swagger file is generated as Spring and the /test/v1/integer endpoint is POSTed to with a valid #/definitions/integer object, the server prints
Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Can not deserialize instance of java.lang.Integer out of START_OBJECT token
at [Source: java.io.PushbackInputStream@6161c1c; line: 1, column: 1]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.Integer out of START_OBJECT token
at [Source: java.io.PushbackInputStream@6161c1c; line: 1, column: 1]
I'd expect this to deserialize to a model.Integer, not a java.lang.Integer.
This extends to more types than Integer - for example, in Java, a model named BigDecimal is also affected.
Swagger-codegen version
[angusi@dev-vm swagger-codegen]$ java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar version
2.2.2-SNAPSHOT
[angusi@dev-vm swagger-codegen]$ git reflog
014cfe7 HEAD@{0}: clone: from https://github.com/swagger-api/swagger-codegen
Swagger declaration file content or url
swagger: '2.0'
info:
title: Swagger Integer Model Test Schema
description: Simple schema for testing Swagger's code generation with a type that conflicts with an internal type.
version: 1.0.0
contact:
url: 'https://github.com/angusi'
schemes:
- http
basePath: /test/v1
consumes:
- application/json
produces:
- application/json
paths:
'/integer':
post:
parameters:
- name: testObject
in: body
description: A test object called "integer", which collides with the language-provided Integer type.
required: true
schema:
$ref: '#/definitions/integer'
summary: Test the colliding types
description: Post an "integer" to see if it collides and raises an error
operationId: PostInteger
responses:
'200':
description: Result data
schema:
$ref: '#/definitions/integer'
definitions:
integer:
type: object
description: Apparently an integer!
properties:
someString:
type: string
example: "Hello, I'm an Integer"
Command line used for generation
Issue exists in multiple generated languages, but e.g.
java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate -i ../minimal_bad_swagger.yaml -l spring -o ../springout
Steps to reproduce
- Generate code (e.g. Spring) from above embedded definition,
- Build and run generated code
- POST to /test/v1/integer with the body
{ "some_string": "Hello world!" } - Observe deserialization failure
Related issues
I tried hunting, but didn't find anything I felt was relevant.
Suggest a Fix
It's possible that these lines from DefaultCodegen:2480 may be relevant, in particular the typeMapping.get(name).
Model sub = bp.getSchema();
if (sub instanceof RefModel) {
String name = ((RefModel) sub).getSimpleRef();
if (typeMapping.containsKey(name)) {
name = typeMapping.get(name);
} else {
name = toModelName(name);
if (defaultIncludes.contains(name)) {
imports.add(name);
}
imports.add(name);
name = getTypeDeclaration(name);
}
p.dataType = name;
p.baseType = name;
}
Alternatively, or in conjunction with the above, qualify the generated model name when it's used in the output code, i.e. use models.Integer instead of simply Integer.
The only other solution I see is to vastly extend the list of reserved words in affected languages to include many more language-provided types (which seems infeasible!).
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java around the referenced typeMapping logic, then reproduce with the embedded Swagger schema and the Spring generation command. Trace how a reference named integer becomes a generated parameter type. Done means the generated model is selected instead of the language-provided type and the POST body deserializes successfully.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, spring
- Domain
- api, backend, tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100