OpenAPITools / OpenAPITools/openapi-generator
[REQ] Feature Request Description
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Is your feature request related to a problem? Please describe.
Whenever I generate a typescript-node API client, the generated code does not respect different return types for different status codes. For example, if 200 returns a user and 400 returns an error, the error gets parsed as a user and thus the error message is lost!
Describe the solution you'd like
There's a generated bit of code that looks like this:
if (error) {
reject(error);
} else {
body = ObjectSerializer.deserialize(body, "OrganizationMember");
if (response.statusCode && response.statusCode >= 200 && response.statusCode <= 299) {
resolve({ response: response, body: body });
} else {
reject(new HttpError(response, body, response.statusCode));
}
}
Ideally, it should follow the json specifications where different status codes return different types.
"200": {
"description": "Success",
"content": {
"text/plain": {
"schema": {
"$ref": "#/components/schemas/RefreshTokenSuccessResponse"
}
},
"application/json": {
"schema": {
"$ref": "#/components/schemas/RefreshTokenSuccessResponse"
}
},
"text/json": {
"schema": {
"$ref": "#/components/schemas/RefreshTokenSuccessResponse"
}
}
}
},
"400": {
"description": "Bad Request",
"content": {
"text/plain": {
"schema": {
"$ref": "#/components/schemas/ProblemDetails"
}
},
"application/json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetails"
}
},
"text/json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetails"
}
}
}
},
"404": {
"description": "Not Found",
"content": {
"text/plain": {
"schema": {
"$ref": "#/components/schemas/ProblemDetails"
}
},
"application/json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetails"
}
},
"text/json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetails"
}
}
}
},
"500": {
"description": "Server Error",
"content": {
"text/plain": {
"schema": {
"$ref": "#/components/schemas/ProblemDetails"
}
},
"application/json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetails"
}
},
"text/json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetails"
}
}
}
}
This can be a configurable mode called "strictStatusTypes" and resolve like so:
if (error) {
reject(error);
} else if (response.statusCode === 200)
resolve({ response: response, body: ObjectSerializer.deserialize(body, "OrganizationMember") });
} else if (response.statusCode === 400) {
reject(new HttpError(response, ObjectSerializer.deserialize(body, "ProblemDetails"), response.statusCode));
} else if ....
Describe alternatives you've considered
An easier solution to the issue would be to just leave the body alone when there's an error and use a raw JSON object without the parse.
if (error) {
reject(error);
} else {
if (response.statusCode && response.statusCode >= 200 && response.statusCode <= 299) {
body = ObjectSerializer.deserialize(body, "OrganizationMember");
resolve({ response: response, body: body });
} else {
reject(new HttpError(response, body, response.statusCode));
}
}
Additional context
Whenever the success status has no body (e.g. 201, 202, ...) the error body is left untouched which makes this behavior really inconsistent!
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 with the typescript-node generator path that emits the shown response-handling code, then trace how OpenAPI response status codes and schemas are represented. Done means generated clients can optionally use a strictStatusTypes mode that deserializes each status code with its specified response type, while preserving the proposed behavior for unspecified modes.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- nodejs, typescript
- Domain
- api, tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100