Skip to content

Commit

Permalink
Merge pull request #812 from BitGo/DX-521-comments-for-intersections
Browse files Browse the repository at this point in the history
  • Loading branch information
anshchaturvedi authored Jun 26, 2024
2 parents c6436b6 + 366b7d0 commit 1e36a48
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 6 deletions.
6 changes: 5 additions & 1 deletion packages/openapi-generator/src/optimize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
52 changes: 47 additions & 5 deletions packages/openapi-generator/test/openapi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2942,7 +2942,8 @@ export const route = h.httpRoute({
request: h.httpRequest({}),
response: {
200: SimpleRouteResponse,
400: ApiError
400: ApiError,
401: InvalidError
},
});
Expand All @@ -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
Expand Down Expand Up @@ -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'
}
}
}
}
}
}
Expand Down Expand Up @@ -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 = `
Expand Down

0 comments on commit 1e36a48

Please sign in to comment.