swagger-api / swagger-api/swagger-codegen
[JAVA] Cannot generate API because of requestBody that contains $ref based on importMappings
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
Description
For some reason, I cannot generate my API because of the patch request described below. I played around with the definition and found out that the requestBody is the source of the problem.
The generation works if I put the $ref inside a list :
requestBody:
description: The object
required: true
content:
application/json:
schema:
type: array
items:
$ref: "#/components/schemas/TestObject"
minItems: 1
or if the $ref is on a component defined in the api.yml itself instead of an import mapping.
Swagger-codegen version
3.2.3 and tested with 3.3.3 and it does not work either.
Swagger declaration file content
...
patch:
summary: Update a specific object
operationId: updateObject
parameters:
- name: objectId
in: path
required: true
description: object id
schema:
type: string
requestBody:
description: The object
required: true
content:
application/json:
schema:
$ref: "#/components/schemas/TestObject"
responses:
default:
description: Updated object
content:
application/json:
schema:
$ref: "#/components/schemas/TestObject"
....
Command line used for generation
Java, using the following gradle task, ran with Intellij :
openApiGenerate {
generatorName = "spring"
inputSpec = file("src/main/openapi/api.yml").toString()
outputDir = file("${buildDir}/openapi-generated-code").toString()
apiPackage = "com.test.api"
modelPackage = "com.test.model"
configOptions = [
dateLibrary : "java8",
java8 : true,
interfaceOnly : true,
serializableModel: true
]
importMappings = [
TestObject: "com.test.model.TestObject"
]
}
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 by reproducing the Spring generation from src/main/openapi/api.yml with the shown openApiGenerate Gradle configuration and importMappings. Trace how a requestBody schema containing a direct $ref is handled compared with the array case, then verify that generation succeeds and produces the expected API using the imported TestObject model.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, openapi
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100