Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add comment block to intersection types #812

Merged
merged 2 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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