OpenAPITools / OpenAPITools/openapi-generator
[BUG] [typescript-fetch] Discriminated types and Create/Update requests
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
I have an api generated in AspNet Core 5 whereby model types and endpoints are declared and generated using NSwag using the inheritance mechanisms of NJsonSchema. The generated code can correctly discriminate and convert, however going the other way (for PUT/POST requests) does not seem to contain any generated mappings or calls to the generated ClassToJSON calls.
openapi-generator version
openapi-generator-cli 5.1.1
commit : 560bf7e
built : 2021-05-07T02:32:26Z
source : https://github.com/openapitools/openapi-generator
docs : https://openapi-generator.tech/
OpenAPI declaration file content or url
{
"x-generator": "NSwag v13.11.1.0 (NJsonSchema v10.4.3.0 (Newtonsoft.Json v12.0.0.0))",
"openapi": "3.0.0",
"info": {
"title": "My Title",
"version": "1.0.0"
},
"servers": [
{
"url": "http://localhost:5000"
}
],
"paths": {
"/": {
"get": {
"tags": [
"Home"
],
"operationId": "Home_GetPets",
"responses": {
"200": {
"description": "Gets all pets",
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Pet"
}
}
}
}
}
}
},
"post": {
"tags": [
"Home"
],
"operationId": "Home_AddPet",
"requestBody": {
"x-name": "pet",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Pet"
}
}
},
"required": true,
"x-position": 1
},
"responses": {
"201": {
"description": "Add a pet to the collection",
"content": {
"application/json": {
"schema": {
"type": "string",
"format": "guid"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"Pet": {
"type": "object",
"discriminator": {
"propertyName": "discriminator",
"mapping": {
"cat": "#/components/schemas/Cat",
"dog": "#/components/schemas/Dog",
"mouse": "#/components/schemas/Mouse"
}
},
"x-abstract": true,
"additionalProperties": false,
"required": [
"discriminator"
],
"properties": {
"id": {
"type": "string",
"format": "guid"
},
"name": {
"type": "string",
"nullable": true
},
"discriminator": {
"type": "string"
}
}
},
"Cat": {
"allOf": [
{
"$ref": "#/components/schemas/Pet"
},
{
"type": "object",
"additionalProperties": false,
"properties": {
"scaredy": {
"type": "boolean"
}
}
}
]
},
"Dog": {
"allOf": [
{
"$ref": "#/components/schemas/Pet"
},
{
"type": "object",
"additionalProperties": false,
"properties": {
"isCool": {
"type": "boolean"
}
}
}
]
},
"Mouse": {
"allOf": [
{
"$ref": "#/components/schemas/Pet"
},
{
"type": "object",
"additionalProperties": false,
"properties": {
"likesCheese": {
"type": "boolean"
}
}
}
]
}
}
}
}
Generation Details
(localhost:5000 is the hosted AspNetCore App)
openapi-generator generate -i http://localhost:5000/swagger/v1/swagger.json -o generated-client -g typescript-fetch
Steps to reproduce
- Download supplied spec (has been validated)
- Run tool as mentioned above
Related issues/PRs
Suggest a fix
Current:
export function PetFromJSONTyped(json: any, ignoreDiscriminator: boolean): Pet {
if ((json === undefined) || (json === null)) {
return json;
}
if (!ignoreDiscriminator) {
if (json['discriminator'] === 'cat') {
return CatFromJSONTyped(json, true);
}
if (json['discriminator'] === 'dog') {
return DogFromJSONTyped(json, true);
}
if (json['discriminator'] === 'mouse') {
return MouseFromJSONTyped(json, true);
}
}
return {
'id': !exists(json, 'id') ? undefined : json['id'],
'name': !exists(json, 'name') ? undefined : json['name'],
'discriminator': json['discriminator'],
};
}
export function PetToJSON(value?: Pet | null): any {
if (value === undefined) {
return undefined;
}
if (value === null) {
return null;
}
return {
'id': value.id,
'name': value.name,
'discriminator': value.discriminator,
};
}
Proposed:
// I like the idea of putting declared constants for type discriminator names somewhere
export const Types = {
Cat: 'cat',
Dog: 'dog',
Mouse: 'mouse'
}
export function PetFromJSON(json: any): Pet {
return PetFromJSONTyped(json, false);
}
export function PetFromJSONTyped(json: any, ignoreDiscriminator: boolean): Pet {
if ((json === undefined) || (json === null)) {
return json;
}
if (!ignoreDiscriminator) {
switch (json['discriminator']) {
case Types.Cat:
return CatFromJSONTyped(json, true);
case Types.Dog:
return DogFromJSONTyped(json, true);
case Types.Mouse:
return MouseFromJSONTyped(json, true);
}
}
return {
'id': !exists(json, 'id') ? undefined : json['id'],
'name': !exists(json, 'name') ? undefined : json['name'],
'discriminator': json['discriminator'],
};
}
export function PetToJSON(value?: Pet | null): any {
if (value === undefined) {
return undefined;
}
if (value === null) {
return null;
}
return {
...childToJson(value), // The blank here before led me to believe some part of the template didnt render?
'id': value.id,
'name': value.name,
'discriminator': value.discriminator,
};
}
function childToJson(value?: Pet | null): any {
switch (value.discriminator) {
case Types.Cat:
return CatToJSON(value);
case Types.Dog:
return DogToJSON(value);
case Types.Mouse:
return MouseToJSON(value);
default:
return {}; //Not sure on default here?
}
}
Sample repository can be found 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
Use the supplied OpenAPI JSON and the openapi-generator generate -i http://localhost:5000/swagger/v1/swagger.json -o generated-client -g typescript-fetch command as the entry point. Inspect the generated discriminator serialization for Pet and its Cat, Dog, and Mouse subtypes; done means generated POST request models preserve subtype fields through the corresponding ToJSON path.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- openapi, typescript
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100