OpenAPITools / OpenAPITools/openapi-generator
[BUG] [typescript-axios] Query parameter passed as "null" when value is null
Nobody has claimed this yet.
- 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
When using the typescript-axios generator to generate an API from a spec with optional query parameters, I've noticed that passing null as a value for such parameters, this results in a query string containing like ?queryParamName=null where I would have expected ?queryParamName= or for it to be omitted from the query string entirely.
This is causing problems as it's resulting in "null" strings being passed into the server unexpectedly, requiring workarounds in some cases to stop "null" being persisted or transmitted internally.
This becomes increasingly likely to happen as the number of parameters grows or when a client user wants to specify Axios config options, requiring them to skip some arguments.
Those importing the client library as TypeScript (or at least, using the TypeScript definitions output file) are unlikely to have this issue as they'll see that the value cannot be null and can be expected to pass undefined instead, however, those using the compiled JavaScript output can and should be expected to pass null for an optional method argument to skip it.
Although I haven't tested with v6 (beta) it looks like the problem exists there too based on what I can see in master.
openapi-generator version
5.4.0
OpenAPI declaration file content or url
openapi: "3.0.0"
info:
title: Swagger Petstore
version: "1.0.0"
servers:
- url: https://petstore.swagger.io
paths:
/pet:
get:
summary: Finds Pets
operationId: findPets
parameters:
- name: "status"
in: "query"
required: false
schema:
type: "string"
enum:
- "available"
- "pending"
- "sold"
responses:
"200":
description: "successful operation"
content:
application/json:
schema:
type: "array"
items:
$ref: "#/components/schemas/Pet"
"400":
description: "Invalid status value"
content:
application/json: {}
components:
schemas:
Category:
type: "object"
properties:
id:
type: "integer"
format: "int64"
name:
type: "string"
Tag:
type: "object"
properties:
id:
type: "integer"
format: "int64"
name:
type: "string"
Pet:
type: "object"
required:
- "name"
- "photoUrls"
properties:
id:
type: "integer"
format: "int64"
category:
$ref: "#/components/schemas/Category"
name:
type: "string"
example: "doggie"
photoUrls:
type: "array"
items:
type: "string"
tags:
type: "array"
items:
$ref: "#/components/schemas/Tag"
status:
type: "string"
enum:
- "available"
- "pending"
- "sold"
Generation Details
The below configuration is being used:
generatorName: typescript-axios
outputDir: ./test-output
inputSpec: ./test-input/spec.yaml
packageName: test
additionalProperties:
packageVersion: "1.0.0"
npmName: "test"
npmVersion: "1.0.0"
Steps to reproduce
Generate the configuration provided previously and then pass null as the optional query string parameter:
import { DefaultApi } from 'test';
export function findPets() {
const api = new DefaultApi();
return api.findPets(null);
}
Related issues/PRs
None.
Suggest a fix
The problem appears to be that null values are being passed to URLSearchParams#set which results in the named parameter being serialised with a "null" value. This is contrary to querystring.stringify which treats null in the same way as undefined and results the parameter being serialised with no value (i.e. empty).
While this issue is being reported against the typescript-axios generator as that's the one I'm using, I've noticed that it's also apparent in some, but not all, other TypeScript generators. Those that used querystring directly or indirectly, do not suffer from this issue. However, for typescript-axios, I believe that the fix would be to simply add a null check to the generated setSearchParams function here.
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 at modules/openapi-generator/src/main/resources/typescript-axios/common.mustache around line 91, where the generated setSearchParams function handles query values. Reproduce the issue by generating the typescript-axios client from the provided spec and calling findPets(null). Done means the generated client no longer serializes null as the literal string "null", with behavior matching the intended empty or omitted query parameter handling.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 35/100