OpenAPITools / OpenAPITools/openapi-generator

[BUG][JAVA] Incorrectly generated openapi.yml file when used query and path params with the same name

Open
#18,021 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

Incorrectly generated openapi.yml file when used query and path params with the same name.
For example, there are 2 endpoints:

  1. /api/v1/clusters
  2. /api/v1/clusters/{clusterName}
    In this case, both have the clusterName parameter, but in the first case it will be a query parameter, and in the second path
    After generation, in the openapi.yaml file, in the second endpoint clusterName has the type query & required=false, which is incorrect
openapi-generator version

6.2.0 and newer

OpenAPI declaration file content or url

test/
├── common/
│ └── clusterResponse.yaml
├── query/
│ ├── clusters.yaml
│ └── clusterName.yaml
└── path/
├── cluster.yaml
└── clusterName.yaml
test.yaml

Yaml files:
test.yaml

openapi: 3.0.0
info:
  description: REST service specification
  version: 1.0.0
  title: API Documentation
  contact: { }
tags:
  - name: Test-with-query-param
  - name: Test-with-path-param
servers:
  - url: http://localhost:8870

paths:
  #Test-with-query-param
  /api/v1/clusters:
    $ref: './test/query/clusters.yaml'
  #Test-with-path-param
  /api/v1/clusters/{clusterName}:
    $ref: './test/path/cluster.yaml'

test/common/clusterResponse.yaml

type: object
title: Cluster
description: Cluster
properties:
  id:
    type: integer
    description: Object id
    uniqueItems: true
  name:
    type: string
    description: Cluster name
    uniqueItems: true
required:
  - id
  - name

test/query/clusters.yaml

get:
  tags:
    - Test-with-query-param
  summary: Get clusters
  parameters:
    - $ref: './test/query/clusterName.yaml'
  operationId: getClusters
  responses:
    '200':
      description: OK
      content:
        application/json:
          schema:
            type: array
            items:
              $ref: '../common/clusterResponse.yaml'
  description: Get clusters

test/query/clusterName.yaml

name: clusterName
in: query
required: false
schema:
  type: string
description: Cluster name

test/path/cluster.yaml

get:
  parameters:
    - $ref: './test/path/clusterName.yaml'
  tags:
    - Cluster
  summary: Get cluster
  operationId: getCluster
  description: Get information about a specific cluster
  responses:
    '200':
      description: OK
      content:
        application/json:
          schema:
            $ref: '../common/clusterResponse.yaml'
    '404':
      description: No cluster

test/path/clusterName.yaml

name: clusterName
in: path
required: true
description: Cluster name
schema:
  type: string
Generation Details

openapi-generator-maven-plugin config

                    <execution>
                        <id>test</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>${project.basedir}/src/main/resources/test.yaml</inputSpec>
                            <generatorName>openapi-yaml</generatorName>
                            <configOptions>
                                <sourceFolder>src/java/main</sourceFolder>
                                <outputFile>test-output.yaml</outputFile>
                            </configOptions>
                            <output>${project.build.directory}/docs</output>
                            <generateModels>false</generateModels>
                            <generateApis>false</generateApis>
                            <generateModelTests>false</generateModelTests>
                            <generateApiTests>false</generateApiTests>
                            <generateApiDocumentation>true</generateApiDocumentation>
                            <generateSupportingFiles>true</generateSupportingFiles>
                        </configuration>
                    </execution>

Generated result:

openapi: 3.0.0
info:
  contact: {}
  description: REST service specification
  title: API Documentation
  version: 1.0.0
servers:
- url: http://localhost:8870
tags:
- name: Test-with-query-param
- name: Test-with-path-param
paths:
  /api/v1/clusters:
    get:
      description: Get clusters
      operationId: getClusters
      parameters:
      - description: Cluster name
        in: query
        name: clusterName
        required: false
        schema:
          type: string
      responses:
        "200":
          content:
            application/json:
              schema:
                items:
                  $ref: '#/components/schemas/clusterResponse'
                type: array
          description: OK
      summary: Get clusters
      tags:
      - Test-with-query-param
  /api/v1/clusters/{clusterName}:
    get:
      description: Get information about a specific cluster
      operationId: getCluster
      parameters:
      - description: Cluster name
        in: query
        name: clusterName
        required: false
        schema:
          type: string
      responses:
        "200":
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/clusterResponse'
          description: OK
        "404":
          description: No cluster
      summary: Get cluster
      tags:
      - Cluster
components:
  parameters:
    clusterName:
      description: Cluster name
      in: query
      name: clusterName
      required: false
      schema:
        type: string
  schemas:
    clusterResponse:
      description: Cluster
      properties:
        id:
          description: Object id
          type: integer
          uniqueItems: true
        name:
          description: Cluster name
          type: string
          uniqueItems: true
      required:
      - id
      - name
      title: Cluster
      type: object

Second endpoint (/api/v1/clusters/{clusterName}) has incorrect query param clusterName, it has to have required path param clusterName.

Suggest a fix

When using unique file names for parameters clusterName, e.g. clusterNameQuery.yaml & clusterNamePath.yaml, output result is correct.

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 referenced files under test/query/ and test/path/, then run the openapi-generator Maven configuration for the openapi-yaml generator. Compare the generated output for the two cluster endpoints, focusing on how same-named parameter references are resolved. Done means the path endpoint emits a required path parameter while the collection endpoint retains its optional query parameter.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
api, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.