diff --git a/src/utils/find-common-openapi-schema-properties.ts b/src/utils/find-common-openapi-schema-properties.ts index 046bebf3..0de46ae1 100644 --- a/src/utils/find-common-openapi-schema-properties.ts +++ b/src/utils/find-common-openapi-schema-properties.ts @@ -3,31 +3,20 @@ import type { OpenapiSchema } from 'lib/openapi.js' export function findCommonOpenapiSchemaProperties( schemas: OpenapiSchema[], ): Record { - if (schemas.length === 0 || schemas[0]?.properties == null) { - return {} - } - const firstSchema = schemas[0] - if (firstSchema.properties == null) { + if (schemas.length === 0 || !firstSchema?.properties) { return {} } - const firstSchemaPropKeys = Object.keys(firstSchema.properties) - - const commonPropKeys = firstSchemaPropKeys.filter((propKey) => - schemas.every((schema) => + return Object.entries(firstSchema.properties).reduce< + Record + >((commonProps, [propKey, propValue]) => { + const isPropInAllSchemas = schemas.every((schema) => Object.keys(schema.properties ?? {}).includes(propKey), - ), - ) + ) - return commonPropKeys.reduce>( - (result, propKey) => { - const propValue = firstSchema.properties?.[propKey] - if (propValue != null) { - result[propKey] = propValue - } - return result - }, - {}, - ) + return isPropInAllSchemas + ? { ...commonProps, [propKey]: propValue } + : commonProps + }, {}) }