diff --git a/packages/openapi-generator/src/optimize.ts b/packages/openapi-generator/src/optimize.ts index 8cb2b929..e70d124c 100644 --- a/packages/openapi-generator/src/optimize.ts +++ b/packages/openapi-generator/src/optimize.ts @@ -127,7 +127,11 @@ export function optimize(schema: Schema): Schema { return schemaObject; } else if (schema.type === 'intersection') { - return foldIntersection(schema, optimize); + const newSchema = foldIntersection(schema, optimize); + if (schema.comment) { + return { ...newSchema, comment: schema.comment }; + } + return newSchema; } else if (schema.type === 'union') { const simplified = simplifyUnion(schema, optimize); if (schema.comment) { diff --git a/packages/openapi-generator/test/openapi.test.ts b/packages/openapi-generator/test/openapi.test.ts index 9ee178d0..9092e1d4 100644 --- a/packages/openapi-generator/test/openapi.test.ts +++ b/packages/openapi-generator/test/openapi.test.ts @@ -2942,7 +2942,8 @@ export const route = h.httpRoute({ request: h.httpRequest({}), response: { 200: SimpleRouteResponse, - 400: ApiError + 400: ApiError, + 401: InvalidError }, }); @@ -2954,6 +2955,14 @@ const SimpleRouteResponse = t.type({ test: t.string, }); +/** + * Human readable description of the InvalidError schema + * @title Human Readable Invalid Error Schema + */ +const InvalidError = t.intersection([ + ApiError, + t.type({ error: t.literal('invalid') })]); + /** * Human readable description of the ApiError schema * @title Human Readable Api Error Schema @@ -2998,6 +3007,16 @@ testCase('route with api error schema', ROUTE_WITH_SCHEMA_WITH_COMMENT, { } }, description: 'Bad Request' + }, + '401': { + description: 'Unauthorized', + content: { + 'application/json': { + schema: { + $ref: '#/components/schemas/InvalidError' + } + } + } } } } @@ -3029,10 +3048,33 @@ testCase('route with api error schema', ROUTE_WITH_SCHEMA_WITH_COMMENT, { 'test' ], title: 'Human Readable Simple Route Response', - type: 'object' - } - }, - }, + type: 'object', + }, + InvalidError: { + title: 'Human Readable Invalid Error Schema', + description: 'Human readable description of the InvalidError schema', + allOf: [ + { + type: 'object', + properties: { + error: { + type: 'string', + enum: [ + 'invalid' + ] + } + }, + required: [ + 'error' + ] + }, + { + $ref: '#/components/schemas/ApiError' + } + ], + }, + } + } }); const ROUTE_WITH_SCHEMA_WITH_DEFAULT_METADATA = `