Skip to content

Commit

Permalink
fix: invalid dependency when using chaining with tag group strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
codingmatty authored and astahmer committed Feb 25, 2024
1 parent def55df commit 197316b
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/two-pears-tap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-zod-client": patch
---

Fix invalid output when using array types as the endpoint body with minItems or maxItems and using the tag-file group-strategy.
6 changes: 4 additions & 2 deletions lib/src/template-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ export const getZodClientTemplateContext = (
const addDependencyIfNeeded = (schemaName: string) => {
if (!schemaName) return;
if (schemaName.startsWith("z.")) return;
dependencies.add(schemaName);
// Sometimes the schema includes a chain that should be removed from the dependency
const [normalizedSchemaName] = schemaName.split(".");
dependencies.add(normalizedSchemaName!);
};

addDependencyIfNeeded(endpoint.response);
Expand Down Expand Up @@ -394,7 +396,7 @@ export type TemplateContextOptions = {
* When true, returns a "responses" array with all responses (both success and errors)
*/
withAllResponses?: boolean;

/**
* When true, prevents using the exact same name for the same type
* For example, if 2 schemas have the same type, but different names, export each as separate schemas
Expand Down
92 changes: 92 additions & 0 deletions lib/tests/array-body-with-chains-tag-group-strategy.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import type { OpenAPIObject } from "openapi3-ts";
import { expect, test } from "vitest";
import { generateZodClientFromOpenAPI } from "../src";

test("array-body-with-chains-tag-group-strategy", async () => {
const openApiDoc: OpenAPIObject = {
openapi: "3.0.0",
info: { title: "Test", version: "1.0.1" },
paths: {
"/test": {
put: {
summary: "Test",
description: "Test",
tags: ["Test"],
requestBody: {
content: {
"application/json": {
schema: {
type: "array",
items: {
type: "object",
properties: {
testItem: {
type: "string",
},
},
additionalProperties: false,
},
minItems: 1,
maxItems: 10,
},
},
},
},
parameters: [],
responses: {
"200": {
description: "Success",
content: { "application/json": {} },
},
},
},
},
},
components: {},
tags: [],
};

const output = await generateZodClientFromOpenAPI({
disableWriteToFile: true,
openApiDoc,
options: { groupStrategy: "tag-file" },
});
expect(output).toMatchInlineSnapshot(`
{
"Test": "import { makeApi, Zodios, type ZodiosOptions } from "@zodios/core";
import { z } from "zod";
const putTest_Body = z.array(z.object({ testItem: z.string() }).partial());
export const schemas = {
putTest_Body,
};
const endpoints = makeApi([
{
method: "put",
path: "/test",
description: \`Test\`,
requestFormat: "json",
parameters: [
{
name: "body",
type: "Body",
schema: putTest_Body.min(1).max(10),
},
],
response: z.void(),
},
]);
export const TestApi = new Zodios(endpoints);
export function createApiClient(baseUrl: string, options?: ZodiosOptions) {
return new Zodios(baseUrl, endpoints, options);
}
",
"__index": "export { TestApi } from "./Test";
",
}
`);
});

0 comments on commit 197316b

Please sign in to comment.