anymaniax / 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
2.36k stars 270 forks source link

Build setting allParamsOptional is not working as expected #1322

Open wouterkroes opened 4 weeks ago

wouterkroes commented 4 weeks ago

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 commented 4 weeks ago

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

wouterkroes commented 1 week ago

@melloware I gave it a try, https://github.com/anymaniax/orval/pull/1361, what do you think?

Maxim-Mazurok commented 1 week ago

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!