Skip to content

Commit

Permalink
Merge pull request #776 from BitGo/DX-434-fix-description-outputs
Browse files Browse the repository at this point in the history
feat: add support for `descriptions` in OpenAPI specs
  • Loading branch information
bitgopatmcl authored May 31, 2024
2 parents 6f9543b + b93dfb0 commit e195d7c
Show file tree
Hide file tree
Showing 3 changed files with 615 additions and 4 deletions.
14 changes: 11 additions & 3 deletions packages/openapi-generator/src/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ function schemaToOpenAPI(
case 'object':
return {
type: 'object',
...defaultObject,
properties: Object.entries(schema.properties).reduce(
(acc, [name, prop]) => {
const innerSchema = schemaToOpenAPI(prop);
Expand All @@ -72,6 +73,7 @@ function schemaToOpenAPI(
}
return [innerSchema];
}),
...defaultObject,
};
case 'union':
let nullable = false;
Expand All @@ -95,10 +97,15 @@ function schemaToOpenAPI(
)[0] === '$ref'
)
// OpenAPI spec doesn't allow $ref properties to have siblings, so they're wrapped in an 'allOf' array
return { ...(nullable ? { nullable } : {}), allOf: oneOf };
else return { ...(nullable ? { nullable } : {}), ...oneOf[0] };
return {
...(nullable ? { nullable } : {}),
allOf: oneOf,
...defaultObject,
};
else
return { ...(nullable ? { nullable } : {}), ...oneOf[0], ...defaultObject };
} else {
return { ...(nullable ? { nullable } : {}), oneOf };
return { ...(nullable ? { nullable } : {}), oneOf, ...defaultObject };
}
case 'record':
const additionalProperties = schemaToOpenAPI(schema.codomain);
Expand All @@ -108,6 +115,7 @@ function schemaToOpenAPI(
return {
type: 'object',
additionalProperties,
...defaultObject,
};
case 'undefined':
return undefined;
Expand Down
23 changes: 22 additions & 1 deletion packages/openapi-generator/src/optimize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,13 @@ export function optimize(schema: Schema): Schema {
continue;
}
const [isOptional, filteredSchema] = filterUndefinedUnion(optimized);

if (prop.comment) {
filteredSchema.comment = prop.comment;
}

properties[key] = filteredSchema;

if (schema.required.indexOf(key) >= 0 && !isOptional) {
required.push(key);
}
Expand All @@ -123,11 +129,26 @@ export function optimize(schema: Schema): Schema {
} else if (schema.type === 'intersection') {
return foldIntersection(schema, optimize);
} else if (schema.type === 'union') {
return simplifyUnion(schema, optimize);
const simplified = simplifyUnion(schema, optimize);
if (schema.comment) {
return { ...simplified, comment: schema.comment };
}

return simplified;
} else if (schema.type === 'array') {
const optimized = optimize(schema.items);
if (schema.comment) {
return { type: 'array', items: optimized, comment: schema.comment };
}
return { type: 'array', items: optimized };
} else if (schema.type === 'record') {
if (schema.comment) {
return {
type: 'record',
codomain: optimize(schema.codomain),
comment: schema.comment,
};
}
return { type: 'record', codomain: optimize(schema.codomain) };
} else if (schema.type === 'tuple') {
const schemas = schema.schemas.map(optimize);
Expand Down
Loading

0 comments on commit e195d7c

Please sign in to comment.