Skip to content

Commit 26f18f1

Browse files
committed
feat(openapi-generator): add support for inline type descriptions
DX-427
1 parent 5336a24 commit 26f18f1

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

packages/openapi-generator/src/openapi.ts

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,27 @@ function schemaToOpenAPI(
1414
const createOpenAPIObject = (
1515
schema: Schema,
1616
): OpenAPIV3.SchemaObject | OpenAPIV3.ReferenceObject | undefined => {
17+
const description = schema.comment?.description;
18+
19+
const defaultObject = {
20+
...(description ? { description } : {}),
21+
};
22+
1723
switch (schema.type) {
1824
case 'boolean':
1925
case 'string':
2026
case 'number':
21-
return { type: schema.type, ...(schema.enum ? { enum: schema.enum } : {}) };
27+
return {
28+
type: schema.type,
29+
...(schema.enum ? { enum: schema.enum } : {}),
30+
...defaultObject,
31+
};
2232
case 'integer':
23-
return { type: 'number', ...(schema.enum ? { enum: schema.enum } : {}) };
33+
return {
34+
type: 'number',
35+
...(schema.enum ? { enum: schema.enum } : {}),
36+
...defaultObject,
37+
};
2438
case 'null':
2539
// TODO: OpenAPI v3 does not have an explicit null type, is there a better way to represent this?
2640
// Or should we just conflate explicit null and undefined properties?
@@ -32,7 +46,7 @@ function schemaToOpenAPI(
3246
if (innerSchema === undefined) {
3347
return undefined;
3448
}
35-
return { type: 'array', items: innerSchema };
49+
return { type: 'array', items: innerSchema, ...defaultObject };
3650
case 'object':
3751
return {
3852
type: 'object',
@@ -146,13 +160,25 @@ function routeToOpenAPI(route: Route): [string, string, OpenAPIV3.OperationObjec
146160
{},
147161
);
148162

163+
const stripTopLevelComment = (schema: Schema) => {
164+
const copy = { ...schema };
165+
166+
if (copy.comment?.description !== undefined && copy.comment?.description !== '') {
167+
copy.comment.description = '';
168+
}
169+
170+
return copy;
171+
};
172+
173+
const topLevelStripped = stripTopLevelComment(route.body!);
174+
149175
const requestBody =
150176
route.body === undefined
151177
? {}
152178
: {
153179
requestBody: {
154180
content: {
155-
'application/json': { schema: schemaToOpenAPI(route.body) },
181+
'application/json': { schema: schemaToOpenAPI(topLevelStripped) },
156182
},
157183
},
158184
};
@@ -195,7 +221,7 @@ function routeToOpenAPI(route: Route): [string, string, OpenAPIV3.OperationObjec
195221
description,
196222
content: {
197223
'application/json': {
198-
schema: schemaToOpenAPI(response),
224+
schema: schemaToOpenAPI(stripTopLevelComment(response)),
199225
...(example !== undefined ? { example } : undefined),
200226
},
201227
},

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ testCase('simple route', SIMPLE, {
161161
description: 'foo param',
162162
required: true,
163163
schema: {
164+
description: 'foo param',
164165
type: 'string',
165166
},
166167
},
@@ -927,6 +928,7 @@ testCase('schema parameter with title tag', TITLE_TAG, {
927928
description: 'bar param',
928929
required: true,
929930
schema: {
931+
description: 'bar param',
930932
title: 'this is a bar parameter',
931933
type: 'string',
932934
},

0 commit comments

Comments
 (0)