Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[OAS] Support setting availability #196647

Merged
merged 11 commits into from
Oct 21, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ describe('Router', () => {
discontinued: 'post test discontinued',
summary: 'post test summary',
description: 'post test description',
availability: {
since: '1.0.0',
stability: 'experimental',
},
},
},
(context, req, res) => res.ok()
Expand All @@ -72,6 +76,10 @@ describe('Router', () => {
discontinued: 'post test discontinued',
summary: 'post test summary',
description: 'post test description',
availability: {
since: '1.0.0',
stability: 'experimental',
},
},
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,46 @@ describe('Versioned route', () => {
jest.clearAllMocks();
});

describe('#getRoutes', () => {
it('returns the expected metadata', () => {
const versionedRouter = CoreVersionedRouter.from({ router });
versionedRouter
.get({
path: '/test/{id}',
access: 'public',
options: {
httpResource: true,
availability: {
since: '1.0.0',
stability: 'experimental',
},
excludeFromOAS: true,
tags: ['1', '2', '3'],
},
description: 'test',
summary: 'test',
enableQueryVersion: false,
})
.addVersion({ version: '2023-10-31', validate: false }, handlerFn);

expect(versionedRouter.getRoutes()[0].options).toMatchObject({
access: 'public',
enableQueryVersion: false,
description: 'test',
summary: 'test',
options: {
httpResource: true,
availability: {
since: '1.0.0',
stability: 'experimental',
},
excludeFromOAS: true,
tags: ['1', '2', '3'],
},
});
});
});

it('can register multiple handlers', () => {
const versionedRouter = CoreVersionedRouter.from({ router });
versionedRouter
Expand Down
16 changes: 16 additions & 0 deletions packages/core/http/core-http-server/src/router/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,22 @@ export interface RouteConfigOptions<Method extends RouteMethod> {
* @default false
*/
httpResource?: boolean;

/**
* Based on the the ES API specification (see https://github.com/elastic/elasticsearch-specification)
* Kibana APIs can also specify some metadata about API availability.
*
* @remark intended to be used for informational purposes only.
*
*/
availability?: {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ES specification has availability as either stack or serverless and we're missing that in the route config options.
It's probably not a deal breaker now but would be great to surface in the generated spec.

/** @default stable */
Copy link
Contributor

@TinaHeiligers TinaHeiligers Oct 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Route access defaults to 'internal'. WDYT about reflecting that here and setting the default stability as 'experimental'? That way there's less risk to adding new routes and forgetting to change the stability descriptor to 'experimental' pre-GA.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I considered this too, but then realised our existing public routes would default to "experimental" too unless we explicitly set them all to "stable". So, in practice, we default to "stable" already today.

I'm not opposed to changing the default, and I tend to agree with you starting as "experimental" is better I just wanted to avoid a wider effort. We could do it as follow up work, happy to open an issue.

I wasn't too concerned about internal routes bc they are in principle "not available", but I think we can capture that in the doc comment.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I considered this too, but then realised our existing public routes would default to "experimental" too unless we explicitly set them all to "stable".

We don't have that many public routes as we do internal. From a cost/benefit pov, changing public routes to stable is worth the effort of mitigating the risk of accidentally marking a route as stable when it's not.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not opposed to changing the default, and I tend to agree with you starting as "experimental" is better I just wanted to avoid a wider effort. We could do it as follow up work, happy to open an issue.

Sounds good, LMK when you've created an issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#197003 🙌🏻

stability?: 'experimental' | 'beta' | 'stable';
/**
* Must be a stack version
jloleysens marked this conversation as resolved.
Show resolved Hide resolved
*/
since?: string;
};
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export type VersionedRouteConfig<Method extends RouteMethod> = Omit<
> & {
options?: Omit<
RouteConfigOptions<Method>,
'access' | 'description' | 'deprecated' | 'discontinued' | 'security'
'access' | 'description' | 'summary' | 'deprecated' | 'discontinued' | 'security'
>;
/** See {@link RouteConfigOptions<RouteMethod>['access']} */
access: Exclude<RouteConfigOptions<Method>['access'], undefined>;
Expand Down
2 changes: 1 addition & 1 deletion packages/kbn-router-to-openapispec/openapi-types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export * from 'openapi-types';
declare module 'openapi-types' {
export namespace OpenAPIV3 {
export interface BaseSchemaObject {
// Custom OpenAPI field added by Kibana for a new field at the shema level.
// Custom OpenAPI field added by Kibana for a new field at the schema level.
TinaHeiligers marked this conversation as resolved.
Show resolved Hide resolved
'x-discontinued'?: string;
}
}
Expand Down
99 changes: 99 additions & 0 deletions packages/kbn-router-to-openapispec/src/generate_oas.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,4 +321,103 @@ describe('generateOpenApiDocument', () => {
expect(result.paths['/v2-1']!.get!.tags).toEqual([]);
});
});

describe('availability', () => {
it('creates the expected availability entries', () => {
const [routers, versionedRouters] = createTestRouters({
routers: {
testRouter1: {
routes: [
{
path: '/1-1/{id}/{path*}',
options: { availability: { stability: 'experimental' } },
},
{
path: '/1-2/{id}/{path*}',
options: { availability: { stability: 'beta' } },
},
{
path: '/1-3/{id}/{path*}',
options: { availability: { stability: 'stable' } },
},
],
},
testRouter2: {
routes: [{ path: '/2-1/{id}/{path*}' }],
},
},
versionedRouters: {
testVersionedRouter1: {
routes: [
{
path: '/v1-1',
options: {
access: 'public',
options: { availability: { stability: 'experimental' } },
},
},
{
path: '/v1-2',
options: {
access: 'public',
options: { availability: { stability: 'beta' } },
},
},
{
path: '/v1-3',
options: {
access: 'public',
options: { availability: { stability: 'stable' } },
},
},
],
},
testVersionedRouter2: {
routes: [{ path: '/v2-1', options: { access: 'public' } }],
},
},
});
const result = generateOpenApiDocument(
{
routers,
versionedRouters,
},
{
title: 'test',
baseUrl: 'https://test.oas',
version: '99.99.99',
}
);

// router paths
expect(result.paths['/1-1/{id}/{path*}']!.get).toMatchObject({
'x-state': 'Technical Preview',
});
expect(result.paths['/1-2/{id}/{path*}']!.get).toMatchObject({
'x-state': 'Beta',
});

expect(result.paths['/1-3/{id}/{path*}']!.get).not.toMatchObject({
'x-state': expect.any(String),
});
expect(result.paths['/2-1/{id}/{path*}']!.get).not.toMatchObject({
'x-state': expect.any(String),
});

// versioned router paths
expect(result.paths['/v1-1']!.get).toMatchObject({
'x-state': 'Technical Preview',
});
expect(result.paths['/v1-2']!.get).toMatchObject({
'x-state': 'Beta',
});

expect(result.paths['/v1-3']!.get).not.toMatchObject({
'x-state': expect.any(String),
});
expect(result.paths['/v2-1']!.get).not.toMatchObject({
'x-state': expect.any(String),
});
});
});
});
6 changes: 5 additions & 1 deletion packages/kbn-router-to-openapispec/src/process_router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ import {
getVersionedHeaderParam,
mergeResponseContent,
prepareRoutes,
setXState,
} from './util';
import type { OperationIdCounter } from './operation_id_counter';
import type { GenerateOpenApiDocumentOptionsFilters } from './generate_oas';
import type { CustomOperationObject } from './type';

export const processRouter = (
appRouter: Router,
Expand Down Expand Up @@ -61,7 +63,7 @@ export const processRouter = (
parameters.push(...pathObjects, ...queryObjects);
}

const operation: OpenAPIV3.OperationObject = {
const operation: CustomOperationObject = {
summary: route.options.summary ?? '',
tags: route.options.tags ? extractTags(route.options.tags) : [],
...(route.options.description ? { description: route.options.description } : {}),
Expand All @@ -81,6 +83,8 @@ export const processRouter = (
operationId: getOpId(route.path),
};

setXState(route.options.availability, operation);

const path: OpenAPIV3.PathItemObject = {
[route.method]: operation,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
extractTags,
mergeResponseContent,
getXsrfHeaderForMethod,
setXState,
} from './util';

export const processVersionedRouter = (
Expand Down Expand Up @@ -112,6 +113,9 @@ export const processVersionedRouter = (
parameters,
operationId: getOpId(route.path),
};

setXState(route.options.options?.availability, operation);

const path: OpenAPIV3.PathItemObject = {
[route.method]: operation,
};
Expand Down
5 changes: 5 additions & 0 deletions packages/kbn-router-to-openapispec/src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,8 @@ export interface OpenAPIConverter {

is(type: unknown): boolean;
}

export type CustomOperationObject = OpenAPIV3.OperationObject<{
// Custom OpenAPI from ES API spec based on @availability
'x-state'?: 'Technical Preview' | 'Beta';
}>;
16 changes: 15 additions & 1 deletion packages/kbn-router-to-openapispec/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
type RouterRoute,
type RouteValidatorConfig,
} from '@kbn/core-http-server';
import { KnownParameters } from './type';
import { CustomOperationObject, KnownParameters } from './type';
import type { GenerateOpenApiDocumentOptionsFilters } from './generate_oas';

const tagPrefix = 'oas-tag:';
Expand Down Expand Up @@ -165,3 +165,17 @@ export const getXsrfHeaderForMethod = (
},
];
};

export function setXState(
availability: RouteConfigOptions<RouteMethod>['availability'],
operation: CustomOperationObject
): void {
if (availability) {
if (availability.stability === 'experimental') {
operation['x-state'] = 'Technical Preview';
}
if (availability.stability === 'beta') {
operation['x-state'] = 'Beta';
}
}
}
Comment on lines +169 to +181
Copy link
Contributor Author

@jloleysens jloleysens Oct 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the logic for adding x-state, added the types for since but we can implement it in another PR.