astahmer / typed-openapi

Generate a headless Typescript API client from an OpenAPI spec
https://typed-openapi-web.vercel.app/
195 stars 24 forks source link

[Bug] When an `operationId` is long, the generated endpoint schema is wrapped with `z.object()` and results in wrong types. #41

Closed tui95 closed 2 months ago

tui95 commented 2 months ago

Version

0.6.0

Steps to reproduce

When an operationId is long, the generated endpoint schema is wrapped with z.object(). This happens when the generator is zod. I'm not sure about the others.

As a result, this makes the type errors occur in the methods of ApiClient.

Given an openapi endpoint schema with a very long operationId

/category/{categoryId}:
  delete:
    operationId: very_very_very_very_very_very_very_very_very_very_long
    parameters:
      - name: categoryId
        in: path
        description: Category id to delete
        required: true
        schema:
          type: integer
          format: int64
    responses:
      "400":
        description: Invalid category id

The link for the typed-openapi playground can be found here

Expected behavior

The generated endpoint schema should be

export const delete_Very_very_very_very_very_very_very_very_very_very_long = {
  method: z.literal("DELETE"),
  path: z.literal("/category/{categoryId}"),
  parameters: z.object({
    path: z.object({
      categoryId: z.number(),
    }),
  }),
  response: z.unknown(),
};

Actual behavior

The generated endpoint schema is

export const delete_Very_very_very_very_very_very_very_very_very_very_long = z.object({
  method: z.literal("DELETE"),
  path: z.literal("/category/{categoryId}"),
  parameters: z.object({
    path: z.object({
      categoryId: z.number(),
    }),
  }),
  response: z.unknown(),
});

and I got the type errors inside the ApiClient methods.

Type 'TEndpoint["parameters"]' does not satisfy the constraint 'ZodType<any, any, any>'.
  Type '{ "/category/{categoryId}": ZodObject<{ method: ZodLiteral<"DELETE">; path: ZodLiteral<"/category/{categoryId}">; parameters: ZodObject<{ path: ZodObject<{ categoryId: ZodNumber; }, "strip", ZodTypeAny, { ...; }, { ...; }>; }, "strip", ZodTypeAny, { ...; }, { ...; }>; response: ZodUnknown; }, "strip", ZodTypeAny, { ...' is not assignable to type 'ZodType<any, any, any>'.ts(2344)
Type '"parameters"' cannot be used to index type 'TEndpoint'.ts(2536)
Screenshot 2567-08-07 at 21 30 02