openapi-ts icon indicating copy to clipboard operation
openapi-ts copied to clipboard

Transformers do properly not handle fields which are both required and nullable

Open yallxe opened this issue 8 months ago • 2 comments

Description

Transformers does not check whether a field is a null. As an example: Suppose we have a schema

export type Pet = {
  id?: bigint;
  name: string;
  createdAt: Date | null;
  photoUrls: Array<string>;
  /**
   * pet status in the store
   */
  status?: 'available' | 'pending' | 'sold';
};

Here is the transformer for it

const petSchemaResponseTransformer = (data: any) => {
  if (data.id) {
    data.id = BigInt(data.id.toString());
  }

  // May result in new Date(null), because createdAt is Date | null (but still required)
  data.createdAt = new Date(data.createdAt);
  return data;
};

Reproducible example or configuration

https://stackblitz.com/edit/hey-api-client-fetch-example-2simwjzc?file=src%2Fclient%2Ftransformers.gen.ts

OpenAPI specification (optional)

Inside the stackblitz link, I changed the swagger example.

System information (optional)

No response

yallxe avatar May 07 '25 10:05 yallxe

Thanks for reporting!

mrlubos avatar May 07 '25 10:05 mrlubos

@yallxe this has been fixed in a previous release, please upgrade to the latest 🙌

mrlubos avatar May 30 '25 21:05 mrlubos