Skip to content

Both data and error should not be undefiend when the openapischema doesn't define non application/json and non 204/304 responses #2455

@ch0c0l8ra1n

Description

@ch0c0l8ra1n

Description

I have an openapi schema that looks like this:

"/country/{country}/product/{productId}": {
      "get": {
        "operationId": "get_country_country_product_productId",
        "parameters": [
          {
            "name": "country",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "productId",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Generated response for status 200",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "country": {
                      "type": "string"
                    },
                    "productId": {
                      "type": "string"
                    },
                    "availability": {
                      "type": "string"
                    },
                    "localizedPriceInCents": {
                      "type": "number"
                    }
                  },
                  "required": [
                    "country",
                    "productId",
                    "availability",
                    "localizedPriceInCents"
                  ]
                }
              }
            }
          }
        }
      },

When I try to use it with openapi-fetch like this:

const client = createFetchClient<paths>({
    baseUrl: "http://localhost:3000",
    fetch: fetch,
});

const { data, error } = await client.GET("/country/{country}/product/{productId}", {
    params: {
        path: {
            country: "US",
            productId: "123",
        },
    },
});

Data and error can both be undefined at the same time, even though we don't define a non json response or 204/304 response on the schema.

This feels counter-intuitive and the user has to still have to check if the data is undefined or not even though the openapi schema guarantees that it can't be undefined for a successful response.

Proposal

When the schema doens't contain 204/304 responses and only specifies application/json as content-type, data and error should not be undefined at the same time.

Extra

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestopenapi-fetchRelevant to the openapi-fetch library

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions