Skip to content

Commit

Permalink
Optimise logic
Browse files Browse the repository at this point in the history
  • Loading branch information
samchungy committed Nov 6, 2024
1 parent 25ed761 commit b44a79f
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 18 deletions.
21 changes: 8 additions & 13 deletions src/create/schema/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,11 @@ export const createSchemaOrRef = <
return existingRef;
}

const previous =
zodSchema._def.zodOpenApi?.previous &&
(createSchemaOrRef(zodSchema._def.zodOpenApi.previous, state, true) as
| RefObject
| undefined);
const previous = zodSchema._def.zodOpenApi?.previous
? (createSchemaOrRef(zodSchema._def.zodOpenApi.previous, state, true) as
| RefObject
| undefined)
: undefined;

const current =
zodSchema._def.zodOpenApi?.current &&
Expand All @@ -236,14 +236,9 @@ export const createSchemaOrRef = <

const ref = zodSchema._def.zodOpenApi?.openapi?.ref ?? component?.ref;
if (ref) {
if (current) {
if (onlyRef) {
return current;
}
return createNewSchema({ zodSchema, previous: current, state });
}

return createNewRef({ ref, zodSchema, previous, state });
return current
? createNewSchema({ zodSchema, previous: current, state })
: createNewRef({ ref, zodSchema, previous, state });
}

if (onlyRef) {
Expand Down
50 changes: 46 additions & 4 deletions src/create/schema/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,46 @@ const createDescriptionMetadata = (
};
};

const isValueEqual = (value: unknown, previous: unknown): boolean => {
if (typeof value !== typeof previous) {
return false;
}

if (
typeof value === 'string' ||
typeof value === 'number' ||
typeof value === 'boolean'
) {
return value === previous;
}

if (Array.isArray(value) && Array.isArray(previous)) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const sorted = [...value].sort();
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const previousSorted = [...previous].sort();

return sorted.every((v, i) => isValueEqual(v, previousSorted[i]));
}

if (value === null || previous === null) {
return value === previous;
}

if (typeof value === 'object' && typeof previous === 'object') {
const keys = Object.keys(value);

return keys.every((key) =>
isValueEqual(
(value as Record<string, unknown>)[key],
(previous as Record<string, unknown>)[key],
),
);
}

return value === previous;
};

export const enhanceWithMetadata = (
schema: Schema,
metadata: oas31.SchemaObject | oas31.ReferenceObject,
Expand Down Expand Up @@ -75,10 +115,12 @@ export const enhanceWithMetadata = (
const diff = Object.entries({ ...schema.schema, ...values }).reduce(
(acc, [key, value]) => {
if (
['oneOf', 'allOf', 'anyOf'].includes(key) ||
(previous.schemaObject &&
!isReferenceObject(previous.schemaObject) &&
(previous.schemaObject as Record<string, unknown>)[key] === value)
previous.schemaObject &&
!isReferenceObject(previous.schemaObject) &&
isValueEqual(
(previous.schemaObject as Record<string, unknown>)[key],
value,
)
) {
return acc;
}
Expand Down
2 changes: 1 addition & 1 deletion src/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ export const isReferenceObject = (
| oas31.ReferenceObject
| oas30.SchemaObject
| oas30.ReferenceObject,
): schemaOrRef is oas31.ReferenceObject =>
): schemaOrRef is oas31.ReferenceObject | oas30.ReferenceObject =>
Boolean('$ref' in schemaOrRef && schemaOrRef.$ref);

0 comments on commit b44a79f

Please sign in to comment.