orval-labs / orval

orval is able to generate client with appropriate type-signatures (TypeScript) from any valid OpenAPI v3 or Swagger v2 specification, either in yaml or json formats. 🍺
https://orval.dev
MIT License
3.01k stars 329 forks source link

headers:false still outputs headers on Response #1056

Closed cheryl-c-tirerack closed 11 months ago

cheryl-c-tirerack commented 11 months ago

What are the steps to reproduce this issue?

  1. in orval.config, for any mode, ie. 'tags'
  2. set output: { mode: 'tags', headers: false,

What happens?

React-Query still shows headers …export const createPartner = (partner: BodyType) => { return customInstance({ url: /api/partners, method: 'post', headers: { 'Content-Type': 'application/json' }, data: partner, }); };

What were you expecting to happen?

I do not want the tool to auto-generate the headers

Any logs, error output, etc?

Any other comments?

What versions are you using?

Operating System:. Mac Package Version: ^6.20.0 Browser Version: any

melloware commented 11 months ago

I just tried it with 6.20.0 and can't reproduce. Headers False does not create headers but headers:true does. Please provide and exact reproducer.

cheryl-c-tirerack commented 11 months ago

It is in propriety code and a large api. Can you share your config to produce no headers? Is mine in the right place? I will try to spin it up connecting to placeholder api.

melloware commented 11 months ago

Sure it looks like this..

module.exports = {
    myservice: {
        output: {
            target: "src/service/MyService.ts",
            client: "react-query",
            mock: false,
            prettier: false,
            headers: true,
            override: {
                useDates: false,
                mutator: {
                    path: "src/service/AxiosMutator.ts",
                    name: "useAxiosMutator",
                },
                query: {
                    useQuery: true,
                },
            },
        },
        input: {
            target: "./openapi.json",
        },
    },
};

And my JSON looks like this for OpenAPI.

"paths": {
        "/domino/dataset/file/content/{snapshotId}": {
            "get": {
                "tags": ["Domino"],
                "summary": "Get a single file content",
                "description": "Gets the contents of a single file as a string.",
                "parameters": [
                    {
                        "name": "X-Domino-Api-Key",
                        "in": "header",
                        "description": "Domino API Key",
                        "required": true,
                        "schema": {
                            "type": "string"
                        },
                        "example": "83b8568486a99e4a61a49583fd8bea78cbe51ccfbabed01fa04bedf9c875ff96"
                    },
                    {
                        "name": "X-Domino-Api-Url",
                        "in": "header",
                        "description": "Domino URL",
                        "required": true,
                        "schema": {
                            "type": "string"
                        }
                    },
                    {
                        "name": "snapshotId",
                        "in": "path",
                        "description": "Snapshot Identifier",
                        "required": true,
                        "schema": {
                            "type": "string"
                        }
                    },
                    {
                        "name": "filePath",
                        "in": "query",
                        "description": "File Path",
                        "required": true,
                        "schema": {
                            "type": "string"
                        }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "File Content",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "type": "string"
                                }
                            }
                        }
                    },
                    "403": {
                        "description": "Forbidden",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/ApiError"
                                }
                            }
                        }
                    },
                    "500": {
                        "description": "Unexpected error",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/ApiError"
                                }
                            }
                        }
                    }
                }
            }
        },
cheryl-c-tirerack commented 11 months ago

Thank you for sharing config-just wanted to make sure I had mine in the right place. Here is a minimum repo https://github.com/cheryl-c-tirerack/orval-debug

See api/partners/post.ts The createPartner is giving a header that conflicts with other type definitions. My backend says that is not coming from the json, shown in repo above. Thanks for your time.

melloware commented 11 months ago

Ohhh yours are Response output headers not Request input headers. That is what that property currently controls. Let me look.

melloware commented 11 months ago

Wait these are the Content-Type headers to tell the server you want application-json? Why do you want those excluded?

It should always generate headers: { "Content-Type": "application/json" } because your openAPI says

"responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Partner"
                }
              }
            }
          },
        }
cheryl-c-tirerack commented 11 months ago

I have headers set in my custom-instance, not shown in the reproduction. If headers are set in my custom-instance has several, but the generated code does not show all of them, I get an eslint fail on build. It would be easier if all headers were controlled by my axios settings.

melloware commented 11 months ago

Then I need a better example because that example above is working as expected? And can't you just override the headers with an Axios Mutator?

cheryl-c-tirerack commented 11 months ago

I've updated the example with my full custom-instance. The headers here do not match the headers in the generated code and so I get eslint / typescript errors on build. I have struggled a bit to understand this version of the axios instance vs interceptors and other methods I see online. So this could be a failure of understanding on my part. Everything worked perfectly until I hit an api endpoint that generates those for me.

melloware commented 11 months ago

oh I have always used Interceptors to inject headers.

import { AxiosInstance } from "axios";

/**
 * Axios utlity class for adding token handling and Date handling to all request/responses.
 */
export default class AxiosInterceptors {
    // hold instances of interceptor by their unique URL
    static requestInterceptors = new Map<string, number>();
    static responseInterceptors = new Map<string, number>();

    /**
     * Configures Axios request/reponse interceptors to add JWT token.
     *
     * @param {AxiosInstance} instance the Axios instance to remove interceptors
     * @param {string} apiKey the API Key to add to all Axios requests
     * @param {string} apiUrl the API URL to add to all Axios requests
     */
    static setupAxiosInstance = (instance: AxiosInstance, apiKey: string, apiUrl: string) => {
        const appKey = instance.defaults.baseURL!;
        EnvUtils.debug(`Configuring Axios request/response interceptors for: ${apiKey}`);
        // Axios request interceptor to add JWT token
        const tokenRequestInterceptor = instance.interceptors.request.use(
            (config) => {
                if (apiKey) {
                    const headers = config.headers || {};
                    headers["X-Domino-Api-Key"] = apiKey;
                    headers["X-Domino-Api-Url"] = apiUrl;
                }
                return config;
            },
            (error) => {
                return Promise.reject(error);
            },
        );
        EnvUtils.debug(`Axios Token Request Interceptor: ${tokenRequestInterceptor}`);
        AxiosInterceptors.requestInterceptors.set(appKey, tokenRequestInterceptor);
    };

    /**
     * Cleanup Axios on sign out of application.
     *
     * @param {AxiosInstance} instance the Axios instance to remove interceptors
     */
    static teardownAxiosInstance = (instance: AxiosInstance) => {
        const appKey = instance.defaults.baseURL!;
        EnvUtils.warn(`Cleaning up Axios removing all interceptors for: ${appKey}`);
        instance.interceptors.request.clear();
        instance.interceptors.response.clear();
    };
}

Then use it like this setting it up once.

AxiosInterceptors.setupAxiosInstance(AXIOS_INSTANCE, dominoApiKey, dominoApiInstance);
melloware commented 11 months ago

Going to close this for now as not an Orval issue

cheryl-c-tirerack commented 11 months ago

Thank you for your help, I simplified my custom-instance and used interceptors properly and it is solved, so I appreciate the exchange.

melloware commented 11 months ago

No problem!