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

Adds 'discontinued' to OAS meta #192331

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
1caca5a
Adds 'discontinued' to OAS meta
TinaHeiligers Sep 8, 2024
dcaa0ef
Support discontinued in router and versioned router
TinaHeiligers Sep 8, 2024
f8fc148
Merge branch 'main' into kbn-192292-OAS-add-discontinued
elasticmachine Sep 8, 2024
97febf0
Apply suggestions from code review
TinaHeiligers Sep 9, 2024
529f328
types
TinaHeiligers Sep 9, 2024
186a3c8
fix 'discontinued' schema type
TinaHeiligers Sep 9, 2024
31041fc
Update packages/kbn-router-to-openapispec/src/oas_converter/kbn_confi…
TinaHeiligers Sep 10, 2024
4427493
Add discontinued to OpenAPIV3.SchemaObject
TinaHeiligers Sep 10, 2024
7c94e04
test
TinaHeiligers Sep 10, 2024
207d598
test
TinaHeiligers Sep 10, 2024
dc92ed7
Ignore kebab-cased file
TinaHeiligers Sep 10, 2024
3e5519e
Merge branch 'main' into kbn-192292-OAS-add-discontinued
elasticmachine Sep 10, 2024
35eb77d
Merge branch 'main' into kbn-192292-OAS-add-discontinued
elasticmachine Sep 11, 2024
c487da2
Merge branch 'main' into kbn-192292-OAS-add-discontinued
elasticmachine Sep 11, 2024
a596161
Append 'x-' to custom discontinued field
TinaHeiligers Sep 11, 2024
ad70d21
Use consistent name format
TinaHeiligers Sep 11, 2024
2adbdea
fix types, fixture and snapshot
TinaHeiligers Sep 11, 2024
a1da436
update snapshot
TinaHeiligers Sep 12, 2024
6767e56
Apply suggestions from code review
TinaHeiligers Sep 12, 2024
94bcf57
change route option name
TinaHeiligers Sep 12, 2024
eb701a1
Convert route option discontinued to OAS schema 'x-discontinued'
TinaHeiligers Sep 12, 2024
3c0bf0d
fix type
TinaHeiligers Sep 12, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ describe('Router', () => {
validate: { body: validation, query: validation, params: validation },
options: {
deprecated: true,
discontinued: 'post test discontinued',
summary: 'post test summary',
description: 'post test description',
},
Expand All @@ -66,6 +67,7 @@ describe('Router', () => {
isVersioned: false,
options: {
deprecated: true,
discontinued: 'post test discontinued',
summary: 'post test summary',
description: 'post test description',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ describe('Versioned router', () => {

it('provides the expected metadata', () => {
const versionedRouter = CoreVersionedRouter.from({ router });
versionedRouter.get({ path: '/test/{id}', access: 'internal', deprecated: true });
versionedRouter.get({
path: '/test/{id}',
access: 'internal',
deprecated: true,
discontinued: 'x.y.z',
});
versionedRouter.post({
path: '/test',
access: 'internal',
Expand All @@ -49,6 +54,7 @@ describe('Versioned router', () => {
"options": Object {
"access": "internal",
"deprecated": true,
"discontinued": "x.y.z",
},
"path": "/test/{id}",
},
Expand Down
8 changes: 8 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 @@ -207,6 +207,14 @@ export interface RouteConfigOptions<Method extends RouteMethod> {
* @remarks This will be surfaced in OAS documentation.
*/
deprecated?: boolean;

/**
* Release version or date that this route will be removed
* Use with `deprecated: true`
*
* @remarks This will be surfaced in OAS documentation.
TinaHeiligers marked this conversation as resolved.
Show resolved Hide resolved
*/
discontinued?: string;
}

/**
Expand Down
8 changes: 8 additions & 0 deletions packages/core/http/core-http-server/src/versioning/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ export type VersionedRouteConfig<Method extends RouteMethod> = Omit<
* @default false
*/
deprecated?: boolean;

/**
* Release version or date that this route will be removed
* Use with `deprecated: true`
*
* @default undefined
*/
discontinued?: string;
};

/**
Expand Down
2 changes: 2 additions & 0 deletions packages/kbn-config-schema/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ export const schema = {
export type Schema = typeof schema;

import {
META_FIELD_X_OAS_DISCONTINUED,
META_FIELD_X_OAS_ANY,
META_FIELD_X_OAS_OPTIONAL,
META_FIELD_X_OAS_DEPRECATED,
Expand All @@ -444,6 +445,7 @@ import {
} from './src/oas_meta_fields';

export const metaFields = Object.freeze({
META_FIELD_X_OAS_DISCONTINUED,
META_FIELD_X_OAS_ANY,
META_FIELD_X_OAS_OPTIONAL,
META_FIELD_X_OAS_DEPRECATED,
Expand Down
1 change: 1 addition & 0 deletions packages/kbn-config-schema/src/oas_meta_fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ export const META_FIELD_X_OAS_GET_ADDITIONAL_PROPERTIES =
'x-oas-get-additional-properties' as const;
export const META_FIELD_X_OAS_DEPRECATED = 'x-oas-deprecated' as const;
export const META_FIELD_X_OAS_ANY = 'x-oas-any-type' as const;
export const META_FIELD_X_OAS_DISCONTINUED = 'x-oas-discontinued' as const;
15 changes: 14 additions & 1 deletion packages/kbn-config-schema/src/types/type.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import { get } from 'lodash';
import { internals } from '../internals';
import { Type, TypeOptions } from './type';
import { META_FIELD_X_OAS_DEPRECATED } from '../oas_meta_fields';
import { META_FIELD_X_OAS_DEPRECATED, META_FIELD_X_OAS_DISCONTINUED } from '../oas_meta_fields';

class MyType extends Type<any> {
constructor(opts: TypeOptions<any> = {}) {
Expand All @@ -26,6 +26,19 @@ describe('meta', () => {
const meta = type.getSchema().describe();
expect(get(meta, 'flags.description')).toBe('my description');
expect(get(meta, `metas[0].${META_FIELD_X_OAS_DEPRECATED}`)).toBe(true);
expect(get(meta, `metas[1].${META_FIELD_X_OAS_DISCONTINUED}`)).toBeUndefined();
});

it('sets meta with all fields provided', () => {
const type = new MyType({
meta: { description: 'my description', deprecated: true, discontinued: '9.0.0' },
});
const meta = type.getSchema().describe();
expect(get(meta, 'flags.description')).toBe('my description');

expect(get(meta, `metas[0].${META_FIELD_X_OAS_DEPRECATED}`)).toBe(true);

expect(get(meta, `metas[1].${META_FIELD_X_OAS_DISCONTINUED}`)).toBe('9.0.0');
});

it('does not set meta when no provided', () => {
Expand Down
8 changes: 7 additions & 1 deletion packages/kbn-config-schema/src/types/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
type WhenOptions,
CustomHelpers,
} from 'joi';
import { META_FIELD_X_OAS_DEPRECATED } from '../oas_meta_fields';
import { META_FIELD_X_OAS_DEPRECATED, META_FIELD_X_OAS_DISCONTINUED } from '../oas_meta_fields';
import { SchemaTypeError, ValidationError } from '../errors';
import { Reference } from '../references';

Expand All @@ -33,6 +33,10 @@ export interface TypeMeta {
* Whether this field is deprecated.
*/
deprecated?: boolean;
/**
* Release version or date that this route will be removed
TinaHeiligers marked this conversation as resolved.
Show resolved Hide resolved
*/
discontinued?: string;
}

export interface TypeOptions<T> {
Expand Down Expand Up @@ -129,6 +133,8 @@ export abstract class Type<V> {
if (options.meta.deprecated) {
schema = schema.meta({ [META_FIELD_X_OAS_DEPRECATED]: true });
}
if (options.meta.deprecated && options.meta.discontinued)
schema = schema.meta({ [META_FIELD_X_OAS_DISCONTINUED]: options.meta.discontinued });
}

// Attach generic error handler only if it hasn't been attached yet since
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const sharedOas = {
'/bar': {
get: {
deprecated: true,
discontinued: 'route discontinued version or date',
operationId: '%2Fbar#0',
parameters: [
{
Expand Down
10 changes: 10 additions & 0 deletions packages/kbn-router-to-openapispec/src/generate_oas.test.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export const getVersionedRouterDefaults = (bodySchema?: RuntimeSchema) => ({
summary: 'versioned route',
access: 'public',
deprecated: true,
discontinued: 'route discontinued version or date',
options: {
tags: ['ignore-me', 'oas-tag:versioned'],
},
Expand All @@ -81,6 +82,15 @@ export const getVersionedRouterDefaults = (bodySchema?: RuntimeSchema) => ({
deprecatedFoo: schema.maybe(
schema.string({ meta: { description: 'deprecated foo', deprecated: true } })
),
discontinuedFoo: schema.maybe(
schema.string({
meta: {
description: 'discontinued foo',
deprecated: true,
discontinued: 'foo discontinued version or date',
},
})
),
}),
},
response: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Joi from 'joi';
import { metaFields } from '@kbn/config-schema';
import type { OpenAPIV3 } from 'openapi-types';
import { parse } from '../../parse';
import { deleteField, stripBadDefault, processDeprecated } from './utils';
import { deleteField, stripBadDefault, processDeprecated, processDiscontinued } from './utils';
import { IContext } from '../context';

const {
Expand Down Expand Up @@ -58,13 +58,14 @@ export const processMap = (ctx: IContext, schema: OpenAPIV3.SchemaObject): void

export const processAllTypes = (schema: OpenAPIV3.SchemaObject): void => {
processDeprecated(schema);
processDiscontinued(schema);
stripBadDefault(schema);
};

export const processAnyType = (schema: OpenAPIV3.SchemaObject): void => {
// Map schema to an empty object: `{}`
for (const key of Object.keys(schema)) {
deleteField(schema as Record<any, unknown>, key);
deleteField(schema as unknown as Record<any, unknown>, key);
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ export const processDeprecated = (schema: OpenAPIV3.SchemaObject): void => {
}
};

export const processDiscontinued = (schema: OpenAPIV3.SchemaObject): void => {
if (metaFields.META_FIELD_X_OAS_DISCONTINUED in schema) {
schema.discontinued = 'versionOrDate';
TinaHeiligers marked this conversation as resolved.
Show resolved Hide resolved
deleteField(schema, metaFields.META_FIELD_X_OAS_DISCONTINUED);
}
};

/** Just for type convenience */
export const deleteField = (schema: Record<any, unknown>, field: string): void => {
delete schema[field];
Expand Down
1 change: 1 addition & 0 deletions packages/kbn-router-to-openapispec/src/process_router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export const processRouter = (
tags: route.options.tags ? extractTags(route.options.tags) : [],
...(route.options.description ? { description: route.options.description } : {}),
...(route.options.deprecated ? { deprecated: route.options.deprecated } : {}),
...(route.options.discontinued ? { discontinued: route.options.discontinued } : {}),
requestBody: !!validationSchemas?.body
? {
content: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export const processVersionedRouter = (
tags: route.options.options?.tags ? extractTags(route.options.options.tags) : [],
...(route.options.description ? { description: route.options.description } : {}),
...(route.options.deprecated ? { deprecated: route.options.deprecated } : {}),
...(route.options.discontinued ? { discontinued: route.options.discontinued } : {}),
TinaHeiligers marked this conversation as resolved.
Show resolved Hide resolved
requestBody: hasBody
? {
content: hasVersionFilter
Expand Down