Skip to content

Commit 737e47b

Browse files
committed
[HTTP/OAS] Support deprecated field (elastic#181240)
(cherry picked from commit 0547800)
1 parent 60fb183 commit 737e47b

File tree

8 files changed

+53
-10
lines changed

8 files changed

+53
-10
lines changed

packages/kbn-config-schema/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ export type Schema = typeof schema;
246246
import {
247247
META_FIELD_X_OAS_REF_ID,
248248
META_FIELD_X_OAS_OPTIONAL,
249+
META_FIELD_X_OAS_DEPRECATED,
249250
META_FIELD_X_OAS_MAX_LENGTH,
250251
META_FIELD_X_OAS_MIN_LENGTH,
251252
META_FIELD_X_OAS_GET_ADDITIONAL_PROPERTIES,
@@ -254,6 +255,7 @@ import {
254255
export const metaFields = Object.freeze({
255256
META_FIELD_X_OAS_REF_ID,
256257
META_FIELD_X_OAS_OPTIONAL,
258+
META_FIELD_X_OAS_DEPRECATED,
257259
META_FIELD_X_OAS_MAX_LENGTH,
258260
META_FIELD_X_OAS_MIN_LENGTH,
259261
META_FIELD_X_OAS_GET_ADDITIONAL_PROPERTIES,

packages/kbn-config-schema/src/oas_meta_fields.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ export const META_FIELD_X_OAS_MAX_LENGTH = 'x-oas-max-length' as const;
1616
export const META_FIELD_X_OAS_GET_ADDITIONAL_PROPERTIES =
1717
'x-oas-get-additional-properties' as const;
1818
export const META_FIELD_X_OAS_REF_ID = 'x-oas-ref-id' as const;
19+
export const META_FIELD_X_OAS_DEPRECATED = 'x-oas-deprecated' as const;

packages/kbn-config-schema/src/types/type.test.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,29 @@
99
import { get } from 'lodash';
1010
import { internals } from '../internals';
1111
import { Type, TypeOptions } from './type';
12-
import { META_FIELD_X_OAS_REF_ID } from '../oas_meta_fields';
12+
import { META_FIELD_X_OAS_REF_ID, META_FIELD_X_OAS_DEPRECATED } from '../oas_meta_fields';
1313

1414
class MyType extends Type<any> {
1515
constructor(opts: TypeOptions<any> = {}) {
1616
super(internals.any(), opts);
1717
}
1818
}
1919

20-
test('meta', () => {
21-
const type = new MyType({ meta: { description: 'my description', id: 'foo' } });
22-
const meta = type.getSchema().describe();
23-
expect(get(meta, 'flags.description')).toBe('my description');
24-
expect(get(meta, `metas[0].${META_FIELD_X_OAS_REF_ID}`)).toBe('foo');
20+
describe('meta', () => {
21+
it('sets meta when provided', () => {
22+
const type = new MyType({
23+
meta: { description: 'my description', id: 'foo', deprecated: true },
24+
});
25+
const meta = type.getSchema().describe();
26+
expect(get(meta, 'flags.description')).toBe('my description');
27+
expect(get(meta, `metas[0].${META_FIELD_X_OAS_REF_ID}`)).toBe('foo');
28+
expect(get(meta, `metas[1].${META_FIELD_X_OAS_DEPRECATED}`)).toBe(true);
29+
});
30+
31+
it('does not set meta when no provided', () => {
32+
const type = new MyType();
33+
const meta = type.getSchema().describe();
34+
expect(get(meta, 'flags.description')).toBeUndefined();
35+
expect(get(meta, 'metas')).toBeUndefined();
36+
});
2537
});

packages/kbn-config-schema/src/types/type.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,23 @@
77
*/
88

99
import type { AnySchema, CustomValidator, ErrorReport } from 'joi';
10-
import { META_FIELD_X_OAS_REF_ID } from '../oas_meta_fields';
10+
import { META_FIELD_X_OAS_DEPRECATED, META_FIELD_X_OAS_REF_ID } from '../oas_meta_fields';
1111
import { SchemaTypeError, ValidationError } from '../errors';
1212
import { Reference } from '../references';
1313

14-
/** Meta fields used when introspecting runtime validation */
14+
/**
15+
* Meta fields used when introspecting runtime validation. Most notably for
16+
* generating OpenAPI spec.
17+
*/
1518
export interface TypeMeta {
1619
/**
1720
* A human-friendly description of this type to be used in documentation.
1821
*/
1922
description?: string;
23+
/**
24+
* Whether this field is deprecated.
25+
*/
26+
deprecated?: boolean;
2027
/**
2128
* A string that uniquely identifies this schema. Used when generating OAS
2229
* to create refs instead of inline schemas.
@@ -108,6 +115,9 @@ export abstract class Type<V> {
108115
if (options.meta.id) {
109116
schema = schema.meta({ [META_FIELD_X_OAS_REF_ID]: options.meta.id });
110117
}
118+
if (options.meta.deprecated) {
119+
schema = schema.meta({ [META_FIELD_X_OAS_DEPRECATED]: true });
120+
}
111121
}
112122

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

packages/kbn-router-to-openapispec/src/__snapshots__/generate_oas.test.ts.snap

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/kbn-router-to-openapispec/src/generate_oas.test.util.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,12 @@ const getVersionedRouterDefaults = () => ({
7979
fn: jest.fn(),
8080
options: {
8181
validate: {
82-
request: { body: schema.object({ foo: schema.string() }) },
82+
request: {
83+
body: schema.object({
84+
foo: schema.string(),
85+
deprecatedFoo: schema.maybe(schema.string({ meta: { deprecated: true } })),
86+
}),
87+
},
8388
response: {
8489
[200]: { body: schema.object({ fooResponse: schema.string() }) },
8590
},

packages/kbn-router-to-openapispec/src/oas_converter/kbn_config_schema/post_process_mutations/mutations/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Joi from 'joi';
1010
import { metaFields } from '@kbn/config-schema';
1111
import type { OpenAPIV3 } from 'openapi-types';
1212
import { parse } from '../../parse';
13-
import { deleteField, stripBadDefault } from './utils';
13+
import { deleteField, stripBadDefault, processDeprecated } from './utils';
1414
import { IContext } from '../context';
1515

1616
const {
@@ -62,6 +62,7 @@ export const processMap = (ctx: IContext, schema: OpenAPIV3.SchemaObject): void
6262
};
6363

6464
export const processAny = (schema: OpenAPIV3.SchemaObject): void => {
65+
processDeprecated(schema);
6566
stripBadDefault(schema);
6667
};
6768

packages/kbn-router-to-openapispec/src/oas_converter/kbn_config_schema/post_process_mutations/mutations/utils.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

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

1112
export const stripBadDefault = (schema: OpenAPIV3.SchemaObject): void => {
1213
if (schema.default?.special === 'deep') {
@@ -26,6 +27,13 @@ export const stripBadDefault = (schema: OpenAPIV3.SchemaObject): void => {
2627
}
2728
};
2829

30+
export const processDeprecated = (schema: OpenAPIV3.SchemaObject): void => {
31+
if (metaFields.META_FIELD_X_OAS_DEPRECATED in schema) {
32+
schema.deprecated = true;
33+
deleteField(schema, metaFields.META_FIELD_X_OAS_DEPRECATED);
34+
}
35+
};
36+
2937
/** Just for type convenience */
3038
export const deleteField = (schema: Record<any, unknown>, field: string): void => {
3139
delete schema[field];

0 commit comments

Comments
 (0)