swagger-api / swagger-api/swagger-codegen

[JAVA] Cannot generate API because of requestBody that contains $ref based on importMappings

Open
#8,957 1 comment 3 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

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

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 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.