From ed26234e787888324cd3f4839612743202f019d4 Mon Sep 17 00:00:00 2001 From: Daniel Peng Date: Tue, 24 Nov 2026 22:57:28 -0500 Subject: [PATCH] fix(openapi): drop per-item x-internal markers on private routes Ticket: WCN-4282 --- packages/openapi-generator/README.md | 9 +- packages/openapi-generator/src/openapi.ts | 51 +++++-- .../test/openapi/jsdoc.test.ts | 129 ++++++++++++++++++ 3 files changed, 177 insertions(+), 12 deletions(-) diff --git a/packages/openapi-generator/README.md b/packages/openapi-generator/README.md index 8c03fe93..0ef5e789 100644 --- a/packages/openapi-generator/README.md +++ b/packages/openapi-generator/README.md @@ -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 @@ -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. diff --git a/packages/openapi-generator/src/openapi.ts b/packages/openapi-generator/src/openapi.ts index 3b4e6d5f..79a6a1db 100644 --- a/packages/openapi-generator/src/openapi.ts +++ b/packages/openapi-generator/src/openapi.ts @@ -12,13 +12,15 @@ export type ComponentNameMapping = Record>; 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': @@ -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; } @@ -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; } @@ -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 []; } @@ -162,6 +176,7 @@ export function schemaToOpenAPI( ...(nullSchema ? { nullable: true } : {}), }, componentNameMapping, + inheritInternal, ); } @@ -180,6 +195,7 @@ export function schemaToOpenAPI( ...(nullSchema ? { nullable: true } : {}), }, componentNameMapping, + inheritInternal, ); } else if (nonUnknownSchemas.length > 1) { return schemaToOpenAPI( @@ -190,6 +206,7 @@ export function schemaToOpenAPI( ...(nullSchema ? { nullable: true } : {}), }, componentNameMapping, + inheritInternal, ); } } @@ -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); } @@ -233,6 +250,7 @@ export function schemaToOpenAPI( const additionalProperties = schemaToOpenAPI( schema.codomain, componentNameMapping, + inheritInternal, ); if (additionalProperties === undefined) return undefined; @@ -240,6 +258,7 @@ export function schemaToOpenAPI( const keys = schemaToOpenAPI( schema.domain, componentNameMapping, + inheritInternal, ) as OpenAPIV3.SchemaObject; if (keys.type === 'string' && keys.enum !== undefined) { const properties = keys.enum.reduce((acc, key) => { @@ -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); @@ -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; @@ -403,7 +431,7 @@ function routeToOpenAPI( requestBody: { content: { 'application/json': { - schema: schemaToOpenAPI(route.body, componentNameMapping), + schema: schemaToOpenAPI(route.body, componentNameMapping, isInternal), }, }, }, @@ -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; @@ -462,7 +490,7 @@ function routeToOpenAPI( description, content: { 'application/json': { - schema: schemaToOpenAPI(response, componentNameMapping), + schema: schemaToOpenAPI(response, componentNameMapping, isInternal), ...(example !== undefined ? { example } : undefined), }, }, @@ -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) { diff --git a/packages/openapi-generator/test/openapi/jsdoc.test.ts b/packages/openapi-generator/test/openapi/jsdoc.test.ts index 1d873ef2..aa96d185 100644 --- a/packages/openapi-generator/test/openapi/jsdoc.test.ts +++ b/packages/openapi-generator/test/openapi/jsdoc.test.ts @@ -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';