Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fixed an error when requestBody was of type "object" #179

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/src/getZodiosEndpointDefinitionList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export const getZodiosEndpointDefinitionList = (doc: OpenAPIObject, options?: Te

ctx.zodSchemaByName[formatedName] = result;
ctx.schemaByName[result] = formatedName;
return formatedName;
return result;
}

// result is a reference to another schema
Expand Down
89 changes: 89 additions & 0 deletions lib/tests/request-body-object.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import type { OpenAPIObject } from "openapi3-ts";
import { expect, test } from "vitest";
import { generateZodClientFromOpenAPI } from "../src";


test("request-body-object", async () => {
const openApiDoc: OpenAPIObject = {
openapi: "3.0.3",
info: {
title: "Pets",
version: "1.0.0",
},
paths: {
"/pets": {
post: {
summary: "Post pets.",
operationId: "PostPets",
requestBody: {
content: {
"application/json": {
schema: {
type: "object",
properties: {
data: {
$ref: "#/components/schemas/PostPetsRequest",
},
},
},
},
},
},
responses: {},
},
},
},
components: {
schemas: {
PostPetsRequest: {
type: "object",
properties: {
id: {
type: "string",
},
},
},
},
},
};

const output = await generateZodClientFromOpenAPI({ disableWriteToFile: true, openApiDoc });
expect(output).toMatchInlineSnapshot(`
"import { makeApi, Zodios, type ZodiosOptions } from "@zodios/core";
import { z } from "zod";

const PostPetsRequest = z.object({ id: z.string() }).partial().passthrough();
const PostPets_Body = z
.object({ data: PostPetsRequest })
.partial()
.passthrough();

export const schemas = {
PostPetsRequest,
PostPets_Body,
};

const endpoints = makeApi([
{
method: "post",
path: "/pets",
requestFormat: "json",
parameters: [
{
name: "body",
type: "Body",
schema: z.object({ data: PostPetsRequest }).partial().passthrough(),
},
],
response: z.void(),
},
]);

export const api = new Zodios(endpoints);

export function createApiClient(baseUrl: string, options?: ZodiosOptions) {
return new Zodios(baseUrl, endpoints, options);
}
"
`);
});