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

Path params are not using Schema Types

Open WilliamABradley opened this issue 1 year ago • 3 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(),
                }),
            },
        ],
    },
]);

WilliamABradley avatar Jun 20 '23 00:06 WilliamABradley

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.

WilliamABradley avatar Jun 20 '23 01:06 WilliamABradley

hey, yeah pretty much the same issue as the other one (https://github.com/astahmer/openapi-zod-client/issues/158). how would you solve that ? feel free to make a PR if you come up with a good solution

astahmer avatar Jun 20 '23 09:06 astahmer

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 https://github.com/astahmer/openapi-zod-client/pull/177. just linking for completeness

WickyNilliams avatar Jul 12 '23 12:07 WickyNilliams