Skip to content

Commit 04dd1b5

Browse files
codingmattyastahmer
authored andcommitted
fix: bug with exportAllNamedSchemas where schemas will reuse wrong name
1 parent 89b41b5 commit 04dd1b5

File tree

5 files changed

+37
-6
lines changed

5 files changed

+37
-6
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"openapi-zod-client": patch
3+
---
4+
5+
Fix bug with `exportAllNamedSchemas` option where schemas will reuse last schema name with matching schema rather than it's own name that has already been used before.

lib/src/CodeMeta.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { getSchemaComplexity } from "./schema-complexity";
77
export type ConversionTypeContext = {
88
resolver: DocumentResolver;
99
zodSchemaByName: Record<string, string>;
10-
schemaByName: Record<string, string>;
10+
schemaByName: Record<string, string[]>;
1111
};
1212

1313
export type CodeMetaData = {

lib/src/getZodiosEndpointDefinitionList.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,9 @@ test("getZodiosEndpointDefinitionList /pet without schema ref", () => {
439439
"resolveSchemaName": [Function],
440440
},
441441
"schemaByName": {
442-
"Pet.and(Reason)": "updatePet_Body",
442+
"Pet.and(Reason)": [
443+
"updatePet_Body",
444+
],
443445
},
444446
"zodSchemaByName": {
445447
"Category": "z.object({ id: z.number().int(), name: z.string() }).partial().passthrough()",

lib/src/getZodiosEndpointDefinitionList.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export const getZodiosEndpointDefinitionList = (doc: OpenAPIObject, options?: Te
8484

8585
// if schema is already assigned to a variable, re-use that variable name
8686
if (!options?.exportAllNamedSchemas && ctx.schemaByName[result]) {
87-
return ctx.schemaByName[result]!;
87+
return ctx.schemaByName[result]![0]!;
8888
}
8989

9090
// result is complex and would benefit from being re-used
@@ -95,8 +95,8 @@ export const getZodiosEndpointDefinitionList = (doc: OpenAPIObject, options?: Te
9595
let isVarNameAlreadyUsed = false;
9696
while ((isVarNameAlreadyUsed = Boolean(ctx.zodSchemaByName[formatedName]))) {
9797
if (isVarNameAlreadyUsed) {
98-
if (options?.exportAllNamedSchemas && ctx.schemaByName[result]) {
99-
return ctx.schemaByName[result]!;
98+
if (options?.exportAllNamedSchemas && ctx.schemaByName[result]?.includes(formatedName)) {
99+
return formatedName;
100100
} else if (ctx.zodSchemaByName[formatedName] === safeName) {
101101
return formatedName;
102102
} else {
@@ -107,7 +107,13 @@ export const getZodiosEndpointDefinitionList = (doc: OpenAPIObject, options?: Te
107107
}
108108

109109
ctx.zodSchemaByName[formatedName] = result;
110-
ctx.schemaByName[result] = formatedName;
110+
111+
if (options?.exportAllNamedSchemas) {
112+
ctx.schemaByName[result] = (ctx.schemaByName[result] ?? []).concat(formatedName);
113+
} else {
114+
ctx.schemaByName[result] = [formatedName];
115+
}
116+
111117
return formatedName;
112118
}
113119

lib/tests/export-all-named-schemas.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ test("export-all-named-schemas", async () => {
5858
},
5959
},
6060
parameters: [
61+
{
62+
name: "sameSchemaDifferentName",
63+
in: "query",
64+
schema: { type: "string", enum: ["xxx", "yyy", "zzz"] },
65+
},
6166
{
6267
name: "sameSchemaSameName",
6368
in: "query",
@@ -128,6 +133,11 @@ test("export-all-named-schemas", async () => {
128133
"errors": [],
129134
"method": "delete",
130135
"parameters": [
136+
{
137+
"name": "sameSchemaDifferentName",
138+
"schema": "sameSchemaDifferentName",
139+
"type": "Query",
140+
},
131141
{
132142
"name": "sameSchemaSameName",
133143
"schema": "sameSchemaSameName",
@@ -160,6 +170,7 @@ test("export-all-named-schemas", async () => {
160170
"withAlias": false,
161171
},
162172
"schemas": {
173+
"sameSchemaDifferentName": "z.enum(["xxx", "yyy", "zzz"]).optional()",
163174
"sameSchemaSameName": "z.enum(["xxx", "yyy", "zzz"]).optional()",
164175
"schemaNameAlreadyUsed": "z.enum(["aaa", "bbb", "ccc"]).optional()",
165176
"schemaNameAlreadyUsed__2": "z.enum(["ggg", "hhh", "iii"]).optional()",
@@ -180,11 +191,13 @@ test("export-all-named-schemas", async () => {
180191
181192
const sameSchemaSameName = z.enum(["xxx", "yyy", "zzz"]).optional();
182193
const schemaNameAlreadyUsed = z.enum(["aaa", "bbb", "ccc"]).optional();
194+
const sameSchemaDifferentName = z.enum(["xxx", "yyy", "zzz"]).optional();
183195
const schemaNameAlreadyUsed__2 = z.enum(["ggg", "hhh", "iii"]).optional();
184196
185197
export const schemas = {
186198
sameSchemaSameName,
187199
schemaNameAlreadyUsed,
200+
sameSchemaDifferentName,
188201
schemaNameAlreadyUsed__2,
189202
};
190203
@@ -220,6 +233,11 @@ test("export-all-named-schemas", async () => {
220233
path: "/export-all-named-schemas",
221234
requestFormat: "json",
222235
parameters: [
236+
{
237+
name: "sameSchemaDifferentName",
238+
type: "Query",
239+
schema: sameSchemaDifferentName,
240+
},
223241
{
224242
name: "sameSchemaSameName",
225243
type: "Query",

0 commit comments

Comments
 (0)