astahmer / openapi-zod-client

Generate a zodios (typescript http client with zod validation) from an OpenAPI spec (json/yaml)

Home Page:openapi-zod-client.vercel.app

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Path params are not using Schema Types

WilliamABradley opened this issue · comments

It seems these get inlined instead of using the shared schema type.

Current:

const endpoints = makeApi([
    {
        method: "get",
        path: "/users/:id",
        requestFormat: "json",
        parameters: [
            {
                name: "id",
                type: "Path",
                schema: z.string().regex(/^user_[a-z0-9]{25,27}$/),
            },
        ],
        response: z.union([
            z.object({ success: z.literal(true), item: User }),
            z.object({
                success: z.literal(false),
                message: z.string().optional(),
                issues: z.array(APIIssue).optional(),
            }),
        ]),
        errors: [
            {
                status: 400,
                description: `Bad Request`,
                schema: z.object({
                    success: z.literal(false),
                    message: z.string().optional(),
                    issues: z.array(APIIssue).optional(),
                }),
            },
        ],
    },
]);

Expected:

const UserId = z.string().regex(/^user_[a-z0-9]{25,27}$/);

const User = z.object({
    _id: UserId,
    full_name: z.string().optional(),
    email: z.string().optional(),
});

const endpoints = makeApi([
    {
        method: "get",
        path: "/users/:id",
        requestFormat: "json",
        parameters: [
            {
                name: "id",
                type: "Path",
                schema: UserId,
            },
        ],
        response: z.union([
            z.object({ success: z.literal(true), item: User }),
            z.object({
                success: z.literal(false),
                message: z.string().optional(),
                issues: z.array(APIIssue).optional(),
            }),
        ]),
        errors: [
            {
                status: 400,
                description: `Bad Request`,
                schema: z.object({
                    success: z.literal(false),
                    message: z.string().optional(),
                    issues: z.array(APIIssue).optional(),
                }),
            },
        ],
    },
]);

Another bug found here is if you mess with the complexity threshold settings, you do get a separate schema called id, and it overwrites each other, if you have another parameter with id and complexity.

hey, yeah pretty much the same issue as the other one (#158).
how would you solve that ? feel free to make a PR if you come up with a good solution

i think this is also related to the problems i was experiencing around nullable. i think solving one would solve both. i have a PR with failing tests here, but no solution #177. just linking for completeness