[BUG][TypeScript-Fetch] Incorrect code generation for oneOf
schema with number | string
types
#21259
Labels
oneOf
schema with number | string
types
#21259
Uh oh!
There was an error while loading. Please reload this page.
The TypeScript-Fetch generator produces incorrect code for a schema that uses
oneOf
to define a type as either anumber
or astring
. The generated*ToJSONTyped
function (e.g.,MyCustomSpeedToJSONTyped
) returns an empty object{}
instead of the actual value when the inputvalue
is not null. This leads to incorrect serialization of this type.The problematic generated code for a type
MyCustomSpeed = number | string;
is:openapi-generator version
The
openapitools.json
file indicates version7.13.0
ofgenerator-cli
.OpenAPI declaration file content or url
Here's a minimal OpenAPI 3.0 spec to reproduce the issue:
Generation Details
The generator is invoked via a script in
package.json
:Replace
path/to/your/openapi.yml
with the actual path to the OpenAPI spec andoutput_directory
with the desired output folder.Steps to reproduce
minimal-openapi.yml
.@openapitools/openapi-generator-cli
version7.13.0
(or potentially other affected versions).openapi-generator-cli generate -i minimal-openapi.yml -g typescript-fetch -o ./generated-sdk
generated-sdk/models/MyCustomSpeed.ts
.MyCustomSpeedToJSONTyped
returns{}
instead ofvalue
.MyCustomSpeedFromJSONTyped
returns{}
instead of attempting to parse the input.Related issues/PRs
(Please search existing issues on the OpenAPI Generator GitHub repository for similar problems before submitting. If you find any, link them here.)
Suggest a fix
The
MyCustomSpeedToJSONTyped
function should directly return thevalue
if it's not null, asnumber
andstring
are already valid JSON types.For
MyCustomSpeedFromJSONTyped
, it needs to correctly handle theoneOf
case. Sincenumber
andstring
are primitive types, a simple check should suffice, or it should at least return the inputjson
to be processed by the calling code, rather than an empty object. A more robust solution would be to attempt to match the type.The exact implementation for
FromJSONTyped
might vary based on how strictlyoneOf
for primitives should be handled, but returning{}
is incorrect. The simplest correct behavior would bereturn json;
.The text was updated successfully, but these errors were encountered: