Skip to content
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

Closed
jamesleeht opened this issue Aug 19, 2024 · 5 comments · Fixed by #1776
Closed

Fetch: generator does not seem to support multiple response types #1588

jamesleeht opened this issue Aug 19, 2024 · 5 comments · Fixed by #1776
Assignees
Labels
fetch Fetch client related issue

Comments

@jamesleeht
Copy link
Contributor

Description

If you use an openAPI schema with multiple responses like this:

  "/api/user": {
    "get": {
      "operationId": "get_user",
      "summary": "Get User",
      "parameters": [],
      "responses": {
        "200": {
          "description": "OK",
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/UserResponse" }
            }
          }
        },
        "400": {
          "description": "Bad Request",
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/ApiErrorResponse" }
            }
          }
        }

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

@melloware melloware added the fetch Fetch client related issue label Aug 19, 2024
@melloware melloware changed the title Fetch generator does not seem to support multiple response types Fetch: generator does not seem to support multiple response types Aug 19, 2024
@melloware
Copy link
Collaborator

I wonder if fetch needs generateEachHttpStatus similar to mock and zod #1380

@jamesleeht
Copy link
Contributor Author

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.

@soartec-lab
Copy link
Member

@jamesleeht

There are no plans at the moment.
Does this mean that you want to include patterns other than 200 such as 404 in the data of responseType?

@siranweb
Copy link

@soartec-lab pretty sure that's exactly what he means. I have similar case
There is a piece from my OpenAPI docs

 "/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 BookResponse

export type BooksControllerGetBookResponse = BookResponse | BookNotFoundError | ValidationError | UnknownError;
// or
export type BooksControllerGetBookResponse = BookResponse | BooksControllerGetBook400 | UnknownError;

@soartec-lab
Copy link
Member

Thanks. well, I think that makes sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fetch Fetch client related issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants