OpenAPITools / OpenAPITools/openapi-generator

[BUG] Swagger Code Generation Generates Incorrect Request Models

Open
#16,586 0 comments 1 reaction 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

I've encountered an issue with Swagger code generation where it generates incorrect request models for two different endpoints with identical request bodies.

In my Swagger specification, I have the following two endpoints:

Endpoint for retrieving OPS_ID by authorization serial number:
    Path: /iss/auth/get_ops_id_by_auth_serno
    Summary: Retrieve OPS_ID by authorization serial number in ONLINE.
    Operation ID: get_ops_id_by_auth_serno
    Request Body:
 {
      "auth_serno": {
        "description": "Authorization serial number",
        "type": "integer",
        "format": "int64"
      }
    }

Endpoint for retrieving backoffserno by authorization serial number:

Path: /iss/auth/get_backoffserno_by_authserno
Summary: Retrieve backoffserno by authorization serial number in PRIME.
Operation ID: get_backoffserno_by_authserno
Request Body:
{
         "auth_serno": {
            "description": "Authorization serial number",
            "type": "integer",
            "format": "int64"
          }
        }

However, when Swagger code generation is performed, it generates code like this:

@RequestLine("POST /iss/auth/get_ops_id_by_auth_serno")
@Headers({
  "Content-Type: application/json",
  "Accept: application/json",
  "x-request-id: {xRequestId}"
})

GetOpsIdByAuthSerno200Response getOpsIdByAuthSerno(@Param("xRequestId") String xRequestId, GetBackoffsernoByAuthsernoRequest getBackoffsernoByAuthsernoRequest);

openapi-generator version

7.0.0

As you can see, the generated code for the get_ops_id_by_auth_serno endpoint incorrectly uses the GetBackoffsernoByAuthsernoRequest model, which should be specific to the get_backoffserno_by_authserno endpoint.

I believe this behavior is due to Swagger code generation optimizing and reusing models with identical request bodies. Is there a way to prevent this automatic optimization and ensure that each endpoint gets its own distinct request model?

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 generated Java client for the two operations, get_ops_id_by_auth_serno and get_backoffserno_by_authserno, using the identical request bodies shown in the issue. Trace how Swagger code generation deduplicates request models; done means the first operation no longer receives GetBackoffsernoByAuthsernoRequest, or the supported way to prevent that reuse is clearly established.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
api, tooling
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.