Skip to content

Commit

Permalink
fix: use optional property rather than change existing property
Browse files Browse the repository at this point in the history
  • Loading branch information
codingmatty authored and astahmer committed Feb 25, 2024
1 parent 04dd1b5 commit 989320e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
3 changes: 2 additions & 1 deletion lib/src/CodeMeta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import { getSchemaComplexity } from "./schema-complexity";
export type ConversionTypeContext = {
resolver: DocumentResolver;
zodSchemaByName: Record<string, string>;
schemaByName: Record<string, string[]>;
schemaByName: Record<string, string>;
schemasByName?: Record<string, string[]>;
};

export type CodeMetaData = {
Expand Down
4 changes: 1 addition & 3 deletions lib/src/getZodiosEndpointDefinitionList.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -439,9 +439,7 @@ test("getZodiosEndpointDefinitionList /pet without schema ref", () => {
"resolveSchemaName": [Function],
},
"schemaByName": {
"Pet.and(Reason)": [
"updatePet_Body",
],
"Pet.and(Reason)": "updatePet_Body",
},
"zodSchemaByName": {
"Category": "z.object({ id: z.number().int(), name: z.string() }).partial().passthrough()",
Expand Down
21 changes: 14 additions & 7 deletions lib/src/getZodiosEndpointDefinitionList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ export const getZodiosEndpointDefinitionList = (doc: OpenAPIObject, options?: Te
.otherwise((fn) => fn);

const ctx: ConversionTypeContext = { resolver, zodSchemaByName: {}, schemaByName: {} };
if (options?.exportAllNamedSchemas) {
ctx.schemasByName = {};
}

const complexityThreshold = options?.complexityThreshold ?? 4;
const getZodVarName = (input: CodeMeta, fallbackName?: string) => {
const result = input.toString();
Expand All @@ -84,7 +88,7 @@ export const getZodiosEndpointDefinitionList = (doc: OpenAPIObject, options?: Te

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

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

ctx.zodSchemaByName[formatedName] = result;
ctx.schemaByName[result] = formatedName;

if (options?.exportAllNamedSchemas) {
ctx.schemaByName[result] = (ctx.schemaByName[result] ?? []).concat(formatedName);
} else {
ctx.schemaByName[result] = [formatedName];
if (options?.exportAllNamedSchemas && ctx.schemasByName) {
ctx.schemasByName[result] = (ctx.schemasByName[result] ?? []).concat(formatedName);
}

return formatedName;
Expand Down Expand Up @@ -315,7 +318,11 @@ export const getZodiosEndpointDefinitionList = (doc: OpenAPIObject, options?: Te
}

if (endpointDefinition.responses !== undefined) {
endpointDefinition.responses.push({ statusCode, schema: schemaString ?? voidSchema, description: responseItem.description });
endpointDefinition.responses.push({
statusCode,
schema: schemaString ?? voidSchema,
description: responseItem.description,
});
}

if (schemaString) {
Expand Down

0 comments on commit 989320e

Please sign in to comment.