OpenAPITools / OpenAPITools/openapi-generator
[BUG][typescript-fetch] Default case for oneOf serialization method does not serialize correctly
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?
- 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
In https://github.com/OpenAPITools/openapi-generator/pull/20983 was introduced that instead of throwing an error in the default case of a one of switch, the value itself is returned.
However this value is not serialized and instead directly returned.
This does lead to problem when for example a discriminator @type is used.
In typescript this is a field type which is serialized to @type during ToJSONTyped and thus is not serialized.
openapi-generator version
I am using 7.15, I don't know for which versions it worked
OpenAPI declaration file content or url
{
"components": {
"schemas": {
"AbstractRequest": {
"discriminator": {
"propertyName": "@type"
},
"properties": {
"@type": {
"type": "string"
}
},
"required": [
"@type"
]
},
"SubRequest": {
"allOf": [
{
"$ref": "#/components/schemas/AbstractRequest"
},
{
"properties": {
"@type": {
"type": "string"
},
"id": {
"type": "string"
}
},
"type": "object"
}
],
"discriminator": {
"propertyName": "@type"
},
"required": [
"@type",
"id"
]
}
}
},
"info": {
"title": "API",
"version": "0.1"
},
"openapi": "3.1.0"
}
The SubRequest.ts model contains the following method
export function SubRequestToJSONTyped(value?: SubRequest | null, ignoreDiscriminator: boolean = false): any {
if (value == null) {
return value;
}
if (!ignoreDiscriminator) {
switch (value['type']) {
default:
return value;
}
}
return {
...AbstractRequestToJSONTyped(value, true),
'@type': value['type'],
'id': value['id'],
};
}
while the Request.ts contains
export function AbstractRequestToJSONTyped(value?: AbstractRequest | null, ignoreDiscriminator: boolean = false): any {
if (value == null) {
return value;
}
if (!ignoreDiscriminator) {
switch (value['type']) {
case 'SubRequest':
return SubRequestToJSONTyped(value as SubRequest, ignoreDiscriminator);
default:
return value;
}
}
return {
'@type': value['type'],
};
}
If we now serialize a SubRequest using SubRequestToJSONTyped we obtain
{
"type": "SubRequest",
"id": "id"
}
while we would expect
{
"@type": "SubRequest",
"id": "id"
}
Related issues/PRs
https://github.com/OpenAPITools/openapi-generator/pull/20983
Suggest a fix
I would expect that the default case should be
default:
return SubRequestToJSONTyped(value, true);
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 by examining the generated SubRequest.ts and AbstractRequest.ts methods shown in the report, then review the related change in pull request 20983. Reproduce serialization with the supplied OpenAPI declaration and verify that the default discriminator path produces '@type' rather than 'type' while preserving 'id'.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 68/100