openapi-zod-client icon indicating copy to clipboard operation
openapi-zod-client copied to clipboard

Creates invalid types and lazy

Open drexxdk opened this issue 1 year ago • 1 comments

Im using https://activitybff.dev.alineadigital.dk/swagger/v1/swagger.json

pnpx openapi-zod-client 'https://activitybff.dev.alineadigital.dk/swagger/v1/swagger.json' -o './src/schemas/activitybff.ts' --base-url https://activitybff.dev.alineadigital.dk --export-schemas

It creates

const AccordionItemDTO: z.ZodType<AccordionItemDTO> = z.lazy(() =>
  z.object({
    heading: z.string(),
    sections: z.array(
      z.union([
        AudioAssetSectionDTO,
        ContentSectionDTO,
        DocumentSectionDTO,
        GeogebraAssetSectionDTO,
        HeadlineSectionDTO,
        ImageAssetSectionDTO,
        LinkSectionDTO,
        QuoteSectionDTO,
        SlideshowSectionDTO,
        TableAssetSectionDTO,
        VideoAssetSectionDTO,
        AssignmentSectionDTO,
        BoxSectionDTO,
        AccordionSectionDTO,
      ])
    ),
    defaultOpen: z.boolean(),
  })
);

the type:

type AccordionItemDTO = {
  heading: string;
  sections: Array<
    | 'AudioAssetSectionDTO'
    | 'ContentSectionDTO'
    | 'DocumentSectionDTO'
    | 'GeogebraAssetSectionDTO'
    | 'HeadlineSectionDTO'
    | 'ImageAssetSectionDTO'
    | 'LinkSectionDTO'
    | 'QuoteSectionDTO'
    | 'SlideshowSectionDTO'
    | 'TableAssetSectionDTO'
    | 'VideoAssetSectionDTO'
    | 'AssignmentSectionDTO'
    | 'BoxSectionDTO'
    | 'AccordionSectionDTO'
  >;
  defaultOpen: boolean;
};

How am i supposed to use that? The two dont match and its using z.lazy that means it thinks it can be undefined which it cant.

Typescript thinks sections are array of strings with specific names. That is wrong. Its array of different objects.

Is there a trick to get it to be accurate?

drexxdk avatar May 02 '23 06:05 drexxdk

Screenshot 2023-05-02 at 15 26 39

I think I fixed the 1st issue (TS type reference sometimes would output strings) that you described, there still seems to be a problem with .lazy in your case, I haven't dug deeper, feel free to do so from here

astahmer avatar May 02 '23 13:05 astahmer