Skip to content
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

Build setting allParamsOptional is not working as expected #1322

Open
wouterkroes opened this issue Apr 22, 2024 · 3 comments · May be fixed by #1361
Open

Build setting allParamsOptional is not working as expected #1322

wouterkroes opened this issue Apr 22, 2024 · 3 comments · May be fixed by #1361
Assignees
Labels
bug Something isn't working
Milestone

Comments

@wouterkroes
Copy link
Contributor

wouterkroes commented Apr 22, 2024

What are the steps to reproduce this issue?

  1. Generate with allParamsOptional set to true.

What happens?

Not all params are optional.

What were you expecting to happen?

All params are optional.

Any logs, error output, etc?

No

Any other comments?

// orval.config.ts
import { defineConfig } from "orval";

export default defineConfig({
  petstore: {
    input: {
      target: "https://petstore.swagger.io/v2/swagger.json",
    },
    output: {
      clean: true,
      target: "./generated",
      allParamsOptional: true,
    },
  },
});

Generated output

// generated/swaggerPetstore.ts

export const deleteOrder = <TData = AxiosResponse<unknown>>(
  orderId: number | undefined | null, // ✅ orderId is optional
  options?: AxiosRequestConfig
): Promise<TData> => {
  return axios.delete(`/store/order/${orderId}`, options);
};

export const findPetsByTags = <TData = AxiosResponse<Pet[]>>(
  params: FindPetsByTagsParams, // ❌ params is not optional
  options?: AxiosRequestConfig
): Promise<TData> => {
  return axios.get(`/pet/findByTags`, {
    ...options,
    params: { ...params, ...options?.params },
  });
};

export const updateUser = <TData = AxiosResponse<unknown>>(
  username: string | undefined | null,
  user: User, // ❌ user is not optional
  options?: AxiosRequestConfig
): Promise<TData> => {
  return axios.put(`/user/${username}`, user, options);
};

My guess is that params of type Object (and Array?) are not handled correctly.

What versions are you using?

Operating System: Windows

Package Version: 6.27.1

Browser Version: n/a

@melloware melloware added the bug Something isn't working label Apr 22, 2024
@melloware
Copy link
Collaborator

@wouterkroes if you want to investigate and provide a PR that would be great!

@wouterkroes
Copy link
Contributor Author

@melloware I gave it a try, #1361, what do you think?

@melloware melloware added this to the 6.28.0 milestone May 8, 2024
@Maxim-Mazurok
Copy link
Collaborator

I think this is by design.
We only make optional those params that are used to enable/disable the query, like here: https://github.com/anymaniax/orval/blob/master/samples/react-query/custom-client/src/api/endpoints/petstoreFromFileSpecWithTransformer.ts#L253

The query can't be enabled without those params, because they are path params (they go into URL like /v${version}/pets/${petId}

For us it doesn't make sense to be passing null or undefined values in there, so this is the reason for this feature to exist.

To fix this issue you can rename allParamsOptional to allPathParamsOptional and/or update documentation.

For other stuff it doesn't make sense to make them optional. Your swagger schema should define which properties are required, and which are optional. If it doesn't match reality - fix the BE to generate correct schema for your FE.

If you really need it - please create a separate flag, because this one isn't it.

Hopefully I remembered everything correctly, but feel free to double check.

Hope this helps, cheers!

@melloware melloware modified the milestones: 6.28.0, 6.29.0, 6.30.0 May 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants