Skip to content

Commit 1e36a48

Browse files
Merge pull request #812 from BitGo/DX-521-comments-for-intersections
2 parents c6436b6 + 366b7d0 commit 1e36a48

File tree

2 files changed

+52
-6
lines changed

2 files changed

+52
-6
lines changed

packages/openapi-generator/src/optimize.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,11 @@ export function optimize(schema: Schema): Schema {
127127

128128
return schemaObject;
129129
} else if (schema.type === 'intersection') {
130-
return foldIntersection(schema, optimize);
130+
const newSchema = foldIntersection(schema, optimize);
131+
if (schema.comment) {
132+
return { ...newSchema, comment: schema.comment };
133+
}
134+
return newSchema;
131135
} else if (schema.type === 'union') {
132136
const simplified = simplifyUnion(schema, optimize);
133137
if (schema.comment) {

packages/openapi-generator/test/openapi.test.ts

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2942,7 +2942,8 @@ export const route = h.httpRoute({
29422942
request: h.httpRequest({}),
29432943
response: {
29442944
200: SimpleRouteResponse,
2945-
400: ApiError
2945+
400: ApiError,
2946+
401: InvalidError
29462947
},
29472948
});
29482949
@@ -2954,6 +2955,14 @@ const SimpleRouteResponse = t.type({
29542955
test: t.string,
29552956
});
29562957
2958+
/**
2959+
* Human readable description of the InvalidError schema
2960+
* @title Human Readable Invalid Error Schema
2961+
*/
2962+
const InvalidError = t.intersection([
2963+
ApiError,
2964+
t.type({ error: t.literal('invalid') })]);
2965+
29572966
/**
29582967
* Human readable description of the ApiError schema
29592968
* @title Human Readable Api Error Schema
@@ -2998,6 +3007,16 @@ testCase('route with api error schema', ROUTE_WITH_SCHEMA_WITH_COMMENT, {
29983007
}
29993008
},
30003009
description: 'Bad Request'
3010+
},
3011+
'401': {
3012+
description: 'Unauthorized',
3013+
content: {
3014+
'application/json': {
3015+
schema: {
3016+
$ref: '#/components/schemas/InvalidError'
3017+
}
3018+
}
3019+
}
30013020
}
30023021
}
30033022
}
@@ -3029,10 +3048,33 @@ testCase('route with api error schema', ROUTE_WITH_SCHEMA_WITH_COMMENT, {
30293048
'test'
30303049
],
30313050
title: 'Human Readable Simple Route Response',
3032-
type: 'object'
3033-
}
3034-
},
3035-
},
3051+
type: 'object',
3052+
},
3053+
InvalidError: {
3054+
title: 'Human Readable Invalid Error Schema',
3055+
description: 'Human readable description of the InvalidError schema',
3056+
allOf: [
3057+
{
3058+
type: 'object',
3059+
properties: {
3060+
error: {
3061+
type: 'string',
3062+
enum: [
3063+
'invalid'
3064+
]
3065+
}
3066+
},
3067+
required: [
3068+
'error'
3069+
]
3070+
},
3071+
{
3072+
$ref: '#/components/schemas/ApiError'
3073+
}
3074+
],
3075+
},
3076+
}
3077+
}
30363078
});
30373079

30383080
const ROUTE_WITH_SCHEMA_WITH_DEFAULT_METADATA = `

0 commit comments

Comments
 (0)