Skip to content

Commit 989320e

Browse files
codingmattyastahmer
authored andcommitted
fix: use optional property rather than change existing property
1 parent 04dd1b5 commit 989320e

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

lib/src/CodeMeta.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ 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>;
11+
schemasByName?: Record<string, string[]>;
1112
};
1213

1314
export type CodeMetaData = {

lib/src/getZodiosEndpointDefinitionList.test.ts

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

lib/src/getZodiosEndpointDefinitionList.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ export const getZodiosEndpointDefinitionList = (doc: OpenAPIObject, options?: Te
6565
.otherwise((fn) => fn);
6666

6767
const ctx: ConversionTypeContext = { resolver, zodSchemaByName: {}, schemaByName: {} };
68+
if (options?.exportAllNamedSchemas) {
69+
ctx.schemasByName = {};
70+
}
71+
6872
const complexityThreshold = options?.complexityThreshold ?? 4;
6973
const getZodVarName = (input: CodeMeta, fallbackName?: string) => {
7074
const result = input.toString();
@@ -84,7 +88,7 @@ export const getZodiosEndpointDefinitionList = (doc: OpenAPIObject, options?: Te
8488

8589
// if schema is already assigned to a variable, re-use that variable name
8690
if (!options?.exportAllNamedSchemas && ctx.schemaByName[result]) {
87-
return ctx.schemaByName[result]![0]!;
91+
return ctx.schemaByName[result]!;
8892
}
8993

9094
// result is complex and would benefit from being re-used
@@ -95,7 +99,7 @@ export const getZodiosEndpointDefinitionList = (doc: OpenAPIObject, options?: Te
9599
let isVarNameAlreadyUsed = false;
96100
while ((isVarNameAlreadyUsed = Boolean(ctx.zodSchemaByName[formatedName]))) {
97101
if (isVarNameAlreadyUsed) {
98-
if (options?.exportAllNamedSchemas && ctx.schemaByName[result]?.includes(formatedName)) {
102+
if (options?.exportAllNamedSchemas && ctx.schemasByName?.[result]?.includes(formatedName)) {
99103
return formatedName;
100104
} else if (ctx.zodSchemaByName[formatedName] === safeName) {
101105
return formatedName;
@@ -107,11 +111,10 @@ export const getZodiosEndpointDefinitionList = (doc: OpenAPIObject, options?: Te
107111
}
108112

109113
ctx.zodSchemaByName[formatedName] = result;
114+
ctx.schemaByName[result] = formatedName;
110115

111-
if (options?.exportAllNamedSchemas) {
112-
ctx.schemaByName[result] = (ctx.schemaByName[result] ?? []).concat(formatedName);
113-
} else {
114-
ctx.schemaByName[result] = [formatedName];
116+
if (options?.exportAllNamedSchemas && ctx.schemasByName) {
117+
ctx.schemasByName[result] = (ctx.schemasByName[result] ?? []).concat(formatedName);
115118
}
116119

117120
return formatedName;
@@ -315,7 +318,11 @@ export const getZodiosEndpointDefinitionList = (doc: OpenAPIObject, options?: Te
315318
}
316319

317320
if (endpointDefinition.responses !== undefined) {
318-
endpointDefinition.responses.push({ statusCode, schema: schemaString ?? voidSchema, description: responseItem.description });
321+
endpointDefinition.responses.push({
322+
statusCode,
323+
schema: schemaString ?? voidSchema,
324+
description: responseItem.description,
325+
});
319326
}
320327

321328
if (schemaString) {

0 commit comments

Comments
 (0)