Skip to content
Open
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
9 changes: 8 additions & 1 deletion packages/openapi-generator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,11 @@ field, which marks the field to be stripped out in a preprocessing
[step](https://github.com/BitGo/dev-portal/blob/master/scripts/filterSpec.js) on
dev-portal.

Items inside a private route (parameters, request body fields, and response fields)
inherit the route's privacy: field-level `@private`/`@public` tags on them produce no
`x-internal` marker, since the whole route is already internal. Field-level tags still
apply on public routes, and in reusable schemas under `components`.

```typescript
/**
* This is the summary
Expand Down Expand Up @@ -491,7 +496,9 @@ const SampleSchema = t.type({
These are some tags that you can use in your schema JSDocs are custom to this generator.

- `@private` allows you to mark any field in any schema as private. The final spec will
have `x-internal: true` for schemas with the `@private` tag.
have `x-internal: true` for schemas with the `@private` tag. On routes already marked
`@private`, these tags are omitted from the generated spec: items inherit the route's
`x-internal: true`.
- `@public` allows you to explicitly mark any field in any schema as public. The final
spec will have `x-internal: false` for schemas with the `@public` tag. Using both
`@public` and `@private` on the same field will cause an error.
Expand Down
51 changes: 40 additions & 11 deletions packages/openapi-generator/src/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ export type ComponentNameMapping = Record<string, Record<string, string>>;
export function schemaToOpenAPI(
schema: Schema,
componentNameMapping?: ComponentNameMapping,
// Items inherit a @private route's privacy; per-item markers are dropped.
inheritInternal = false,
): OpenAPIV3.SchemaObject | OpenAPIV3.ReferenceObject | undefined {
schema = optimize(schema);

const createOpenAPIObject = (
schema: Schema,
): OpenAPIV3.SchemaObject | OpenAPIV3.ReferenceObject | undefined => {
const defaultOpenAPIObject = buildDefaultOpenAPIObject(schema);
const defaultOpenAPIObject = buildDefaultOpenAPIObject(schema, inheritInternal);

switch (schema.type) {
case 'boolean':
Expand Down Expand Up @@ -74,7 +76,11 @@ export function schemaToOpenAPI(
...defaultOpenAPIObject,
};
case 'array':
const innerSchema = schemaToOpenAPI(schema.items, componentNameMapping);
const innerSchema = schemaToOpenAPI(
schema.items,
componentNameMapping,
inheritInternal,
);
if (innerSchema === undefined) {
return undefined;
}
Expand Down Expand Up @@ -116,7 +122,11 @@ export function schemaToOpenAPI(
...defaultOpenAPIObject,
properties: Object.entries(schema.properties).reduce(
(acc, [name, prop]) => {
const innerSchema = schemaToOpenAPI(prop, componentNameMapping);
const innerSchema = schemaToOpenAPI(
prop,
componentNameMapping,
inheritInternal,
);
if (innerSchema === undefined) {
return acc;
}
Expand All @@ -130,7 +140,11 @@ export function schemaToOpenAPI(
case 'intersection':
return {
allOf: schema.schemas.flatMap((s) => {
const innerSchema = schemaToOpenAPI(s, componentNameMapping);
const innerSchema = schemaToOpenAPI(
s,
componentNameMapping,
inheritInternal,
);
if (innerSchema === undefined) {
return [];
}
Expand Down Expand Up @@ -162,6 +176,7 @@ export function schemaToOpenAPI(
...(nullSchema ? { nullable: true } : {}),
},
componentNameMapping,
inheritInternal,
);
}

Expand All @@ -180,6 +195,7 @@ export function schemaToOpenAPI(
...(nullSchema ? { nullable: true } : {}),
},
componentNameMapping,
inheritInternal,
);
} else if (nonUnknownSchemas.length > 1) {
return schemaToOpenAPI(
Expand All @@ -190,6 +206,7 @@ export function schemaToOpenAPI(
...(nullSchema ? { nullable: true } : {}),
},
componentNameMapping,
inheritInternal,
);
}
}
Expand All @@ -199,7 +216,7 @@ export function schemaToOpenAPI(
nullable = true;
continue;
}
const innerSchema = schemaToOpenAPI(s, componentNameMapping);
const innerSchema = schemaToOpenAPI(s, componentNameMapping, inheritInternal);
if (innerSchema !== undefined) {
oneOf.push(innerSchema);
}
Expand Down Expand Up @@ -233,13 +250,15 @@ export function schemaToOpenAPI(
const additionalProperties = schemaToOpenAPI(
schema.codomain,
componentNameMapping,
inheritInternal,
);
if (additionalProperties === undefined) return undefined;

if (schema.domain !== undefined) {
const keys = schemaToOpenAPI(
schema.domain,
componentNameMapping,
inheritInternal,
) as OpenAPIV3.SchemaObject;
if (keys.type === 'string' && keys.enum !== undefined) {
const properties = keys.enum.reduce((acc, key) => {
Expand Down Expand Up @@ -291,7 +310,10 @@ export function schemaToOpenAPI(
}
};

function buildDefaultOpenAPIObject(schema: Schema): OpenAPIV3.SchemaObject {
function buildDefaultOpenAPIObject(
schema: Schema,
inheritInternal: boolean,
): OpenAPIV3.SchemaObject {
const emptyBlock: Block = { description: '', tags: [], source: [], problems: [] };
const jsdoc = parseCommentBlock(schema.comment ?? emptyBlock);

Expand Down Expand Up @@ -347,7 +369,13 @@ export function schemaToOpenAPI(
...(writeOnly ? { writeOnly: true } : {}),
...(format ? { format } : {}),
...(title ? { title } : {}),
...(isPrivate ? { 'x-internal': true } : isPublic ? { 'x-internal': false } : {}),
...(inheritInternal
? {}
: isPrivate
? { 'x-internal': true }
: isPublic
? { 'x-internal': false }
: {}),
};

return defaultOpenAPIObject;
Expand Down Expand Up @@ -403,7 +431,7 @@ function routeToOpenAPI(
requestBody: {
content: {
'application/json': {
schema: schemaToOpenAPI(route.body, componentNameMapping),
schema: schemaToOpenAPI(route.body, componentNameMapping, isInternal),
},
},
},
Expand All @@ -428,7 +456,7 @@ function routeToOpenAPI(
: {}),
parameters: route.parameters.map((p) => {
// Array types not allowed here
const schema = schemaToOpenAPI(p.schema, componentNameMapping);
const schema = schemaToOpenAPI(p.schema, componentNameMapping, isInternal);

if (schema && 'description' in schema) {
delete schema.description;
Expand Down Expand Up @@ -462,7 +490,7 @@ function routeToOpenAPI(
description,
content: {
'application/json': {
schema: schemaToOpenAPI(response, componentNameMapping),
schema: schemaToOpenAPI(response, componentNameMapping, isInternal),
...(example !== undefined ? { example } : undefined),
},
},
Expand Down Expand Up @@ -492,7 +520,8 @@ export function convertRoutesToOpenAPI(

const openapiSchemas = Object.entries(schemas).reduce(
(acc, [name, schema]) => {
const openapiSchema = schemaToOpenAPI(schema, componentNameMapping);
// Components are emitted without route context; per-item markers stay.
const openapiSchema = schemaToOpenAPI(schema, componentNameMapping, false);
if (openapiSchema === undefined) {
return acc;
} else if ('$ref' in openapiSchema) {
Expand Down
129 changes: 129 additions & 0 deletions packages/openapi-generator/test/openapi/jsdoc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1246,6 +1246,135 @@ testCase(
},
);

const ROUTE_PRIVATE_WITH_PRIVATE_PROPERTIES = `
import * as t from 'io-ts';
import * as h from '@api-ts/io-ts-http';

const SampleType = t.type({
foo: t.string,
/** @private */
bar: t.string,
/** @public */
baz: t.string,
});

/**
* @private
*/
export const route = h.httpRoute({
path: '/foo',
method: 'GET',
request: h.httpRequest({
params: {
/** @private */
path: t.string
},
query: {
/** @private */
query: t.string
},
body: SampleType
}),
response: {
200: SampleType
},
});
`;

testCase(
'private route omits field-level x-internal markers on its items',
ROUTE_PRIVATE_WITH_PRIVATE_PROPERTIES,
{
openapi: '3.0.3',
info: {
title: 'Test',
version: '1.0.0',
},
paths: {
'/foo': {
get: {
'x-internal': true,
parameters: [
{
description: '',
in: 'query',
name: 'query',
required: true,
schema: {
type: 'string',
},
},
{
description: '',
in: 'path',
name: 'path',
required: true,
schema: {
type: 'string',
},
},
],
requestBody: {
content: {
'application/json': {
schema: {
properties: {
bar: {
type: 'string',
},
baz: {
type: 'string',
},
foo: {
type: 'string',
},
},
required: ['foo', 'bar', 'baz'],
type: 'object',
},
},
},
},
responses: {
'200': {
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/SampleType',
},
},
},
description: 'OK',
},
},
},
},
},
components: {
schemas: {
SampleType: {
properties: {
bar: {
'x-internal': true,
type: 'string',
},
baz: {
'x-internal': false,
type: 'string',
},
foo: {
type: 'string',
},
},
required: ['foo', 'bar', 'baz'],
title: 'SampleType',
type: 'object',
},
},
},
},
);

const ROUTE_WITH_ANY_AND_DESCRIPTION = `
import * as t from 'io-ts';
import * as h from '@api-ts/io-ts-http';
Expand Down