Skip to content

Commit e195d7c

Browse files
authored
Merge pull request #776 from BitGo/DX-434-fix-description-outputs
feat: add support for `descriptions` in OpenAPI specs
2 parents 6f9543b + b93dfb0 commit e195d7c

File tree

3 files changed

+615
-4
lines changed

3 files changed

+615
-4
lines changed

packages/openapi-generator/src/openapi.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ function schemaToOpenAPI(
5050
case 'object':
5151
return {
5252
type: 'object',
53+
...defaultObject,
5354
properties: Object.entries(schema.properties).reduce(
5455
(acc, [name, prop]) => {
5556
const innerSchema = schemaToOpenAPI(prop);
@@ -72,6 +73,7 @@ function schemaToOpenAPI(
7273
}
7374
return [innerSchema];
7475
}),
76+
...defaultObject,
7577
};
7678
case 'union':
7779
let nullable = false;
@@ -95,10 +97,15 @@ function schemaToOpenAPI(
9597
)[0] === '$ref'
9698
)
9799
// OpenAPI spec doesn't allow $ref properties to have siblings, so they're wrapped in an 'allOf' array
98-
return { ...(nullable ? { nullable } : {}), allOf: oneOf };
99-
else return { ...(nullable ? { nullable } : {}), ...oneOf[0] };
100+
return {
101+
...(nullable ? { nullable } : {}),
102+
allOf: oneOf,
103+
...defaultObject,
104+
};
105+
else
106+
return { ...(nullable ? { nullable } : {}), ...oneOf[0], ...defaultObject };
100107
} else {
101-
return { ...(nullable ? { nullable } : {}), oneOf };
108+
return { ...(nullable ? { nullable } : {}), oneOf, ...defaultObject };
102109
}
103110
case 'record':
104111
const additionalProperties = schemaToOpenAPI(schema.codomain);
@@ -108,6 +115,7 @@ function schemaToOpenAPI(
108115
return {
109116
type: 'object',
110117
additionalProperties,
118+
...defaultObject,
111119
};
112120
case 'undefined':
113121
return undefined;

packages/openapi-generator/src/optimize.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,13 @@ export function optimize(schema: Schema): Schema {
106106
continue;
107107
}
108108
const [isOptional, filteredSchema] = filterUndefinedUnion(optimized);
109+
110+
if (prop.comment) {
111+
filteredSchema.comment = prop.comment;
112+
}
113+
109114
properties[key] = filteredSchema;
115+
110116
if (schema.required.indexOf(key) >= 0 && !isOptional) {
111117
required.push(key);
112118
}
@@ -123,11 +129,26 @@ export function optimize(schema: Schema): Schema {
123129
} else if (schema.type === 'intersection') {
124130
return foldIntersection(schema, optimize);
125131
} else if (schema.type === 'union') {
126-
return simplifyUnion(schema, optimize);
132+
const simplified = simplifyUnion(schema, optimize);
133+
if (schema.comment) {
134+
return { ...simplified, comment: schema.comment };
135+
}
136+
137+
return simplified;
127138
} else if (schema.type === 'array') {
128139
const optimized = optimize(schema.items);
140+
if (schema.comment) {
141+
return { type: 'array', items: optimized, comment: schema.comment };
142+
}
129143
return { type: 'array', items: optimized };
130144
} else if (schema.type === 'record') {
145+
if (schema.comment) {
146+
return {
147+
type: 'record',
148+
codomain: optimize(schema.codomain),
149+
comment: schema.comment,
150+
};
151+
}
131152
return { type: 'record', codomain: optimize(schema.codomain) };
132153
} else if (schema.type === 'tuple') {
133154
const schemas = schema.schemas.map(optimize);

0 commit comments

Comments
 (0)