karlvr / openapi-generator-plus-generators

Other
21 stars 8 forks source link

typescript-fetch-client2 - Type 'application/json' is not assignable to type 'application/xml' #33

Open taras opened 2 years ago

taras commented 2 years ago

Related #32

Using the same the petstore swagger.json, the generated TypeScript includes the following code:

        loginUser(__params: UserApi.LoginUserRequest, options?: RequestInit): (fetch?: FetchAPI, basePath?: string) => Promise<UserApi.LoginUserResponse> {
            const localVarFetchArgs = UserApiFetchParamCreator(configuration).loginUser(__params, options);
            return async (fetch: FetchAPI = defaultFetch, basePath: string = BASE_PATH) => {
                const response = await fetch(basePath + localVarFetchArgs.url, localVarFetchArgs.options)
                const contentType = response.headers.get('Content-Type');
                const mimeType = contentType ? contentType.replace(/;.*/, '') : undefined;

                if (response.status === 200) {
                    if (mimeType === 'application/json') {
                        return {
                            status: 200,
                            contentType: 'application/json',
                            body: await response.json() as string,
                            headers: {
                                xExpiresAfter: response.headers.get('X-Expires-After'),
                                xRateLimit: response.headers.get('X-Rate-Limit'),
                            },
                        }
                    }
                    if (mimeType === 'application/xml') {
                        return {
                            status: 200,
                            contentType: 'application/xml',
                            body: await response.text(),
                            headers: {
                                xExpiresAfter: response.headers.get('X-Expires-After'),
                                xRateLimit: response.headers.get('X-Rate-Limit'),
                            },
                        }
                    }
                    throw response;
                }
                if (response.status === 400) {
                    return {
                        status: 400,
                        /* No content */
                    }
                }
                throw response;
            };
        },

It doesn't compile because of the following error,

image
karlvr commented 2 years ago

@taras thank you for reporting this... could you please post what was generated for UserApi.LoginUserResponse?

taras commented 2 years ago

@karlvr thank you for looking into it. I pushed the generated code to a repo. You can find it here https://github.com/taras/backstage-openapi-client-scaffolding/blob/main/plugins/petstore-client-common/src/__generated__/api.ts#L1691