-
-
Notifications
You must be signed in to change notification settings - Fork 381
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fetch: generator does not seem to support multiple response types #1588
Comments
I wonder if fetch needs |
Hello @soartec-lab do you have any guidance on how to work on this issue? Currently it is hard to handle non-200 responses and errors in the generated code without this case. |
There are no plans at the moment. |
@soartec-lab pretty sure that's exactly what he means. I have similar case "/books/{id}": {
"get": {
"operationId": "BooksController_getBook",
"parameters": [
{
"name": "id",
"required": true,
"in": "path",
"description": "Id",
"schema": {
"format": "uuid",
"example": "00000000-0000-0000-0000-000000000000",
"type": "string"
}
}
],
"responses": {
"200": {
"description": "Book",
"content": {
"application/json": {
"schema": {
"oneOf": [
{
"$ref": "#/components/schemas/BookResponse"
}
]
}
}
}
},
"400": {
"description": "Request failed",
"content": {
"application/json": {
"schema": {
"oneOf": [
{
"$ref": "#/components/schemas/BookNotFoundError"
},
{
"$ref": "#/components/schemas/ValidationError"
}
]
}
}
}
},
"500": {
"description": "Internal server error",
"content": {
"application/json": {
"schema": {
"oneOf": [
{
"$ref": "#/components/schemas/UnknownError"
}
]
}
}
}
}
},
"summary": "Get book by id",
"tags": [
"books"
]
}
}, I expect to get any of these responses from fetch, but it generates only success 200 response export const booksControllerGetBook = async (id: string, options?: RequestInit): Promise<BookResponse> => {
const res = await fetch(getBooksControllerGetBookUrl(id), { ...options, method: 'GET' });
const data = await res.json();
return data as BookResponse;
}
export interface BookResponse {
author: string;
description: string;
discountPrice: BookResponseDiscountPrice;
id: string;
imagePath: string;
name: string;
price: number;
} It would be great to add possibility to generate types like this by some config flag and use them instead of export type BooksControllerGetBookResponse = BookResponse | BookNotFoundError | ValidationError | UnknownError;
// or
export type BooksControllerGetBookResponse = BookResponse | BooksControllerGetBook400 | UnknownError; |
Thanks. well, I think that makes sense. |
Description
If you use an openAPI schema with multiple responses like this:
The generated fetch will only have the
200
response type (in this example UserResponse). It does not seem to generate a union type for the method return type.I tried to work around this using customFetch so I can union with the alternative response type:
https://github.com/orval-labs/orval/blob/master/samples/next-app-with-fetch/custom-fetch.ts#L56
However, this wouldn't work as the result type is generated by Orval.
Some maybe loosely related issues: #420, #935
The text was updated successfully, but these errors were encountered: