Open neocotic opened 2 years ago
Why don't you pass undefined? Null is a value whereas in "?queryParamName=" has no value
@adrianwix I would happily do this, however, the problem is that, like a lot of other users of the generator, I simply generate the client but do not use it myself heavily. I can understand why any developer would pass null
to skip this argument. It has been a pattern for a very long time with optional function arguments in JS.
A key problem I also see is inconsistency. As I mentioned, some other typescript-based generators don't have this problem as they use querystring. I'm under the impression that this behaviour is in fact somewhat accidental.
I see. I am not sure if the same logic applies. But there are some cases where null
is needed. For example, in PATCH request a property with null
can mean "set the value of the property to null" whereas if the property has the value undefined
it means that it was not defined (even if the developer would do property: undefined
since that is the same as not defining that property)
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.
By analogy, I may want to query some entities where a field is null
. So how would I do it, if setting a query to null
makes it to be omitted
While I agree about PATCH in principle, this could still be achieved via the request body. My bug report is specifically for query string parameters. I think this would be far more appropriate, personally.
By analogy, I may want to query some entities where a field is
null
. So how would I do it, if setting a query tonull
makes it to be omitted
You could simply pass "null"
string in such a case, which I believe is far more reasonable and deliberate than passing null
and it being serialised as a string. Even with typescript consumers in mind this can be achieved and simply cast to the desired type.
Is this still an issue if you set nullable: true
on the parameter?
Null
OpenAPI 3.0 does not have an explicit null type as in JSON Schema, but you can use nullable: true to specify that the value may be null. Note that null is different from an empty string "".
https://swagger.io/docs/specification/data-models/data-types/#Null
I'll give this a shot and let you know how I get on. From the generation code, I'm not sure I see how it would differ.
I also found this to be unintuitive. Luckily I am the creator and the consumer of the api-client so I can put undefined in even though it does feel a bit wrong to do so.
Bug Report Checklist
Description
When using the
typescript-axios
generator to generate an API from a spec with optional query parameters, I've noticed that passingnull
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 passundefined
instead, however, those using the compiled JavaScript output can and should be expected to passnull
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
Generation Details
The below configuration is being used:
Steps to reproduce
Generate the configuration provided previously and then pass
null
as the optional query string parameter: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 treatsnull
in the same way asundefined
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 usedquerystring
directly or indirectly, do not suffer from this issue. However, fortypescript-axios
, I believe that the fix would be to simply add a null check to the generatedsetSearchParams
function here.