From 1ea88b4f8fbc190b92e826fe284947e43ab23b10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8C=82=E8=8C=82?= <375642570@qq.com> Date: Fri, 7 Jul 2023 11:44:07 +0800 Subject: [PATCH] fix: Fixed an error when requestBody was of type "object" --- lib/src/getZodiosEndpointDefinitionList.ts | 2 +- lib/tests/request-body-object.test.ts | 89 ++++++++++++++++++++++ 2 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 lib/tests/request-body-object.test.ts diff --git a/lib/src/getZodiosEndpointDefinitionList.ts b/lib/src/getZodiosEndpointDefinitionList.ts index 0fccdb67..87886afb 100644 --- a/lib/src/getZodiosEndpointDefinitionList.ts +++ b/lib/src/getZodiosEndpointDefinitionList.ts @@ -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 diff --git a/lib/tests/request-body-object.test.ts b/lib/tests/request-body-object.test.ts new file mode 100644 index 00000000..b4e619a4 --- /dev/null +++ b/lib/tests/request-body-object.test.ts @@ -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); + } + " + `); +});