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 18 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,
'x-discontinued': 'post test x-discontinued',
summary: 'post test summary',
description: 'post test description',
},
Expand All @@ -66,6 +67,7 @@ describe('Router', () => {
isVersioned: false,
options: {
deprecated: true,
'x-discontinued': 'post test x-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,
'x-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,
"x-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
*/
'x-discontinued'?: string;
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: What do you think about defining route definitions and kbn/config-schema meta as { discontinued: '...' } instead of { 'x-discontinued': '...' }. Just saves a few keystrokes and slightly awkward string syntax 'my-field-name'. Then when we render it out we just map it to x-discontinued.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I prefer consistency with the final output but can add a brief explanation to the field's description.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done in 94bcf57 & eb701a1

}

/**
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
*/
'x-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, 'x-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
*/
'x-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['x-discontinued'])
schema = schema.meta({ [META_FIELD_X_OAS_DISCONTINUED]: options.meta['x-discontinued'] });
}

// Attach generic error handler only if it hasn't been attached yet since
Expand Down
20 changes: 20 additions & 0 deletions packages/kbn-router-to-openapispec/openapi-types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

// eslint-disable-next-line import/no-extraneous-dependencies
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.
'x-discontinued'?: string;
}
}
}

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,
'x-discontinued': 'route x-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,
'x-discontinued': 'route x-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 } })
),
'x-discontinuedFoo': schema.maybe(
schema.string({
meta: {
description: 'x-discontinued foo',
deprecated: true,
'x-discontinued': 'route x-discontinued version or date',
},
})
),
}),
},
response: {
Expand Down
2 changes: 1 addition & 1 deletion packages/kbn-router-to-openapispec/src/generate_oas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import type { OpenAPIV3 } from 'openapi-types';
import type { CoreVersionedRouter, Router } from '@kbn/core-http-router-server-internal';
import type { OpenAPIV3 } from 'openapi-types';
import { OasConverter } from './oas_converter';
import { createOperationIdCounter } from './operation_id_counter';
import { processRouter } from './process_router';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

import Joi from 'joi';
import joiToJsonParse from 'joi-to-json';
import type { OpenAPIV3 } from 'openapi-types';
import { omit } from 'lodash';
import type { OpenAPIV3 } from 'openapi-types';
import { createCtx, postProcessMutations } from './post_process_mutations';
import type { IContext } from './post_process_mutations';

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 @@ -7,8 +7,8 @@
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import type { OpenAPIV3 } from 'openapi-types';
import { metaFields } from '@kbn/config-schema';
import type { OpenAPIV3 } from 'openapi-types';
import { deleteField, stripBadDefault } from './utils';

const { META_FIELD_X_OAS_OPTIONAL } = metaFields;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import type { OpenAPIV3 } from 'openapi-types';
import { metaFields } from '@kbn/config-schema';
import type { OpenAPIV3 } from 'openapi-types';

export const stripBadDefault = (schema: OpenAPIV3.SchemaObject): void => {
if (schema.default?.special === 'deep') {
Expand All @@ -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['x-discontinued'] = schema[metaFields.META_FIELD_X_OAS_DISCONTINUED] as string;
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
*/

import { z, isZod } from '@kbn/zod';
import type { OpenAPIV3 } from 'openapi-types';
// eslint-disable-next-line import/no-extraneous-dependencies
import zodToJsonSchema from 'zod-to-json-schema';
import type { OpenAPIV3 } from 'openapi-types';

import { KnownParameters } from '../../type';
import { validatePathParameters } from '../common';

Expand Down
3 changes: 3 additions & 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,9 @@ 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['x-discontinued']
? { 'x-discontinued': route.options['x-discontinued'] }
: {}),
requestBody: !!validationSchemas?.body
? {
content: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ 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['x-discontinued']
? { 'x-discontinued': route.options['x-discontinued'] }
: {}),
requestBody: hasBody
? {
content: hasVersionFilter
Expand Down
4 changes: 2 additions & 2 deletions packages/kbn-router-to-openapispec/src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import type { OpenAPIV3 } from 'openapi-types';

import type { OpenAPIV3 } from '../openapi-types';
export type { OpenAPIV3 } from '../openapi-types';
export interface KnownParameters {
[paramName: string]: { optional: boolean };
}
Expand Down
1 change: 1 addition & 0 deletions src/dev/precommit_hook/casing_check_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export const IGNORE_FILE_GLOBS = [
'test/package/Vagrantfile',
'x-pack/plugins/security_solution/scripts/endpoint/common/vagrant/Vagrantfile',
'**/test/**/fixtures/**/*',
'packages/kbn-router-to-openapispec/openapi-types.d.ts',
TinaHeiligers marked this conversation as resolved.
Show resolved Hide resolved

// Required to match the name in the docs.elastic.dev repo.
'dev_docs/nav-kibana-dev.docnav.json',
Expand Down