Bug Report Checklist
Description
In #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
#20983
Suggest a fix
I would expect that the default case should be
default:
return SubRequestToJSONTyped(value, true);
Bug Report Checklist
Description
In #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
@typeis used.In typescript this is a field
typewhich is serialized to@typeduringToJSONTypedand thus is not serialized.openapi-generator version
I am using
7.15, I don't know for which versions it workedOpenAPI 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.tsmodel contains the following methodwhile the
Request.tscontainsIf we now serialize a
SubRequestusingSubRequestToJSONTypedwe obtain{ "type": "SubRequest", "id": "id" }while we would expect
{ "@type": "SubRequest", "id": "id" }Related issues/PRs
#20983
Suggest a fix
I would expect that the default case should be
default: return SubRequestToJSONTyped(value, true);