OpenAPITools / OpenAPITools/openapi-generator

[BUG] [typescript-axios] Raw integer parameter incorrectly serialized

Open
#10,991 2 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
  • [x ] Have you provided a full/minimal spec to reproduce the issue?
  • [ x] Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • [x ] Have you searched for related issues/PRs?
  • [ x] What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

When generating a API with only a raw integer value as the parameter the generator incorrectly serializes the data.

openapi-generator version

5.3.0

OpenAPI declaration file content or url
{
  "swagger" : "2.0",
  "info" : {
    "description" : "This provides the API",
    "version" : "v2",
    "title" : "API",
    "license" : {
      "name" : "Apache 2.0",
      "url" : "http://www.apache.org/licenses/LICENSE-2.0.html"
    }
  },
  "basePath" : "/any",
  "tags" : [  ],
  "schemes" : [ "http", "https" ],
  "paths" : {
    "/test" : {
      "put" : {
        "tags" : [ "stuff" ],
        "summary" : "Does a put operation",
        "description" : "Needs to be accepted as a raw integer",
        "operationId" : "rawInteger",
        "produces" : [ "application/json" ],
        "parameters" : [  {
          "in" : "body",
          "name" : "body",
          "description" : "The integer parameter",
          "required" : true,
          "schema" : {
            "type" : "integer",
            "format" : "int32",
            "default" : 0
          }
        } ],
        "responses" : {
          "412" : {
            "description" : "Failed Reason : invalid"
          }
        }
      }
    }
  }
}

Generation Details

openapi-generator generate -i swagger.json -g typescript-axios

Steps to reproduce

Run the above command with the provided swagger and you will see a serialization method generated which is like the following.

export const serializeDataIfNeeded = function (value: any, requestOptions: any, configuration?: Configuration) {
    const nonString = typeof value !== 'string';
    const needsSerialization = nonString && configuration && configuration.isJsonMime
        ? configuration.isJsonMime(requestOptions.headers['Content-Type'])
        : nonString;
    return needsSerialization
        ? JSON.stringify(value !== undefined ? value : {})
        : (value || "");
}

The serializeDataIfNeeded method is incorrect since the parameter should not be stringified. I think the fix would be to only serialize if the typeof value is equal to object rather than checking if it is not equal to string.

Related issues/PRs

I didn't see any

Suggest a fix

Adjust this template https://github.com/OpenAPITools/openapi-generator/blob/124511e7be0e4a77263b93934de50f8bedf2c3c3/modules/openapi-generator/src/main/resources/typescript-axios/common.mustache#L100-L108 to only serialize if the type is equal to object rather than if it is not equal to string.

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 modules/openapi-generator/src/main/resources/typescript-axios/common.mustache, especially the serializeDataIfNeeded template referenced in the issue. Generate a typescript-axios client from the supplied Swagger document and inspect the raw integer body handling; done means the generated client preserves that integer correctly without incorrect serialization.

Written by the indexing model from the issue text.

Assessment

Tech stack
openapi, typescript
Domain
tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.