Skip to content

Commit

Permalink
fix(#49): missing .and for allOf
Browse files Browse the repository at this point in the history
  • Loading branch information
astahmer committed Dec 11, 2022
1 parent d4ae7d1 commit fb2fc0c
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/src/openApiToZod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export function getZodSchema({ schema, ctx, meta: inheritedMeta, options }: Conv
const rest = types
.slice(1)
.map((type) => `and(${type.toString()})`)
.join("");
.join(".");

return code.assign(`${first.toString()}.${rest}`);
}
Expand Down
60 changes: 60 additions & 0 deletions lib/tests/allOf-missing-and.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import type { OpenAPIObject } from "openapi3-ts";
import { expect, test } from "vitest";
import { generateZodClientFromOpenAPI } from "../src";

// https://github.com/astahmer/openapi-zod-client/issues/49
test("allOf-missing-and", async () => {
const openApiDoc: OpenAPIObject = {
openapi: "3.0.3",
info: { title: "Swagger Petstore - OpenAPI 3.0", version: "1.0.11" },
paths: {
"/pet": {
put: {
responses: {
"200": {
description: "Successful operation",
content: { "application/json": { schema: { $ref: "#/components/schemas/test4" } } },
},
},
},
},
},
components: {
schemas: {
test1: { type: "object", properties: { text1: { type: "string" } } },
test2: { type: "object", properties: { text2: { type: "number" } } },
test3: { type: "object", properties: { text3: { type: "boolean" } } },
test4: {
allOf: [
{ $ref: "#/components/schemas/test1" },
{ $ref: "#/components/schemas/test2" },
{ $ref: "#/components/schemas/test3" },
],
},
},
},
};

const output = await generateZodClientFromOpenAPI({ disableWriteToFile: true, openApiDoc });
expect(output).toMatchInlineSnapshot(`
"import { makeApi, Zodios } from "@zodios/core";
import { z } from "zod";
const test1 = z.object({ text1: z.string() }).partial();
const test2 = z.object({ text2: z.number() }).partial();
const test3 = z.object({ text3: z.boolean() }).partial();
const test4 = test1.and(test2).and(test3);
const endpoints = makeApi([
{
method: "put",
path: "/pet",
requestFormat: "json",
response: test4,
},
]);
export const api = new Zodios(endpoints);
"
`);
});

0 comments on commit fb2fc0c

Please sign in to comment.