-
-
Notifications
You must be signed in to change notification settings - Fork 356
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
204 No content generated code throws JSON parsing error #1656
Comments
There is no solution for this yet, but you can work-around it by using a custom fetch like below: const getBody = async <T>(c: Response | Request): Promise<T> => {
const text = await c.text();
if (!text) {
return {} as T;
}
const contentType = c.headers.get("content-type");
if (contentType?.includes("application/json")) {
return JSON.parse(text);
}
return text as unknown as T;
};
export const customFetch = async <T>(
url: string,
options: RequestInit,
): Promise<T> => {
const response = await fetch();
const data = await getBody<T>(response);
return { status: response.status, data } as T;
}; |
Hello, I'm facing the same issue. My
I tried the fix from #1656 (comment) as it is and registered it in
However, it doesn't work for me as it breaks other APIs (which require a |
Among the automatically generated custom hooks, custom fetch is called. Are your settings correct? |
Yes, the custom fetch will be called. |
@nimo23 |
@soartec-lab thanks, With the help of https://github.com/orval-labs/orval/blob/master/samples/next-app-with-fetch/custom-fetch.ts I get it working. |
@nimo23 |
@soartec-lab Since orval should aim to adhere to the definitions set in The simple fact is that something like |
I agree, I think it's good to support |
The main problem is that attempting to call |
Something like
or using something like
|
OK, I'll think about this, so give me some time and this issue be reopened. |
when there's no content Orval with fetch client triggers Json parse error orval-labs/orval#1656
I have a couple ideas: 1. Check
|
To me personally, it makes sense to only use option with the |
Thank you for your reply. I agree with your opinion. I will wait for more options to gather. |
I think the 5 possible states should be handled like this:
The following status codes should not have bodies according to the spec: 1xx, 204, 205, 304. When these codes are used, the body should always be ignored, even if present (state 1). As @zZHorizonZz noted, some APIs omit bodies even when they're technically required. This should be accepted (state 2). @nimo23 mentions in #1778 (comment) that with the current suggestion, a present but empty body will fail. I think that is correct behavior, since it is invalid JSON (state 3). States 4 and 5 represent the expected behavior. I therefore propose handling them explicitly, while treating the others as mentioned: export const listPets = async (
params?: ListPetsParams,
options?: RequestInit,
): Promise<listPetsResponse> => {
const res = await fetch(getListPetsUrl(params), {
...options,
method: 'GET',
});
- const data: Pets = await res.json();
+ const data: Pets = (res.body && ![204, 205, 304].includes(res.status))
+ ? await res.json()
+ : {};
return { status: res.status, data, headers: res.headers };
}; |
Excellent summary @clemeth |
Thank you for putting this together. I understand that you have merged both of my ideas. |
What are the steps to reproduce this issue?
fetch
client from openapi scheme which returns 204 (No content).What happens?
Basically when there is no response in fetch client response there is no content. Which mean the res.json() will throw error.
Generated code looks like this:
What were you expecting to happen?
There shouldn't be a error.
Any logs, error output, etc?
Any other comments?
I think when there is 204 in specs or there is no content in openapi specification the const data should not be generated and data should not be required in generated responses as there are none.
What versions are you using?
The text was updated successfully, but these errors were encountered: