hey-api / openapi-ts

🚀 The OpenAPI to TypeScript codegen. Generate clients, SDKs, validators, and more. Support: @mrlubos
https://heyapi.dev
Other
1.39k stars 107 forks source link

Cookies are passed but ignored by Axios client #1308

Closed jakubman1 closed 2 days ago

jakubman1 commented 3 days ago

Description

Hello, I have cookies included in my OpenAPI specification (my openapi.json is generated using FastAPI).

The axios client generator generates the client function correctly as follows:

export const getDbTimeApiV1StatsDbTimeGet = (
  data: GetDbTimeApiV1StatsDbTimeGetData = {},
): CancelablePromise<GetDbTimeApiV1StatsDbTimeGetResponse> => {
  return __request(OpenAPI, {
    method: "GET",
    url: "/api/v1/stats/db-time",
    cookies: {
      session_token: data.sessionToken,
    }
  });
};

However, the cookies are never sent to the target server. I tried looking at the requests using Wireshark and there are no cookie headers included in the requests made by Axios.

I have the WITH_CREDENTIALS parameter set to true.

I looked through the generated request() function and from what I could understand, it never uses the cookies attribute of the options parameter.

Maybe there should be some implicit interceptor that handles cookies?

Reproducible example or configuration

Configuration:

import { defineConfig } from "@hey-api/openapi-ts";

export default defineConfig({
  client: "@hey-api/client-axios",
  input: "http://127.0.0.1:8000/api/v1/openapi.json",
  output: {
    format: "biome",
    lint: "biome",
    path: "app/client",
  },
});

Command:

openapi-ts --client=axios

OpenAPI specification (optional)

{
    "openapi": "3.1.0",
    "info": {
        "title": "Cookie example",
        "version": "0.1.0"
    },
    "paths": {
        "/api/v1/db-time": {
            "get": {
                "summary": "Get DB Time",
                "description": "Get current timestamp with timezone from the database.",
                "operationId": "get_db_time_api_v1_stats_db_time_get",
                "parameters": [
                    {
                        "required": false,
                        "schema": {
                            "type": "string",
                            "title": "Session Token"
                        },
                        "name": "session_token",
                        "in": "cookie"
                    }
                ],
                "responses": {
                    "200": {
                        "description": "Successful Response",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "type": "string",
                                    "format": "date-time",
                                    "title": "Response Get Db Time Api V1 Stats Db Time Get"
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

System information (optional)

@hey-api/client-axios@0.2.3
@hey-api/openapi-ts@0.52.11
axios@1.7.5
mrlubos commented 3 days ago

Hey @jakubman1, I am confused how you end up with that output. If you're using the Axios client package as suggested by your config, it wouldn't look like that. Can you confirm which client you're using? Is it the legacy Axios client or the npm package?

jakubman1 commented 3 days ago

Hey @jakubman1, I am confused how you end up with that output. If you're using the Axios client package as suggested by your config, it wouldn't look like that. Can you confirm which client you're using? Is it the legacy Axios client or the npm package?

Thank you for the answer. I am running it as npx openapi-ts --client=axios, maybe the --client parameter is overwriting the config? I thought that the axios client and the @hey-api/client-axios are the same. I'll try again without the --client parameter and let you know.

mrlubos commented 3 days ago

That's right, CLI takes precedence. They're not the same, the CLI one is the legacy client, renamed to legacy/axios in one of the recent releases to reduce the confusion

jakubman1 commented 2 days ago

I re-generated my client using the new axios client and now it seems correct and I can pass headers to the client functions. My bad. Thank you for the help.