Skip to content

Commit

Permalink
Merge branch 'main' into 80-s-asset-entity-manager
Browse files Browse the repository at this point in the history
  • Loading branch information
klacabane committed Jun 25, 2024
2 parents 460f2d9 + 02096a6 commit 8b6132e
Show file tree
Hide file tree
Showing 4 changed files with 206 additions and 29 deletions.
29 changes: 2 additions & 27 deletions packages/kbn-router-to-openapispec/src/generate_oas.test.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,7 @@

import { schema } from '@kbn/config-schema';
import type { CoreVersionedRouter, Router } from '@kbn/core-http-router-server-internal';

/** Intended to cover a wide set of schema configurations */
export const testSchema = schema.object({
string: schema.string({ maxLength: 10, minLength: 1 }),
maybeNumber: schema.maybe(schema.number({ max: 1000, min: 1 })),
booleanDefault: schema.boolean({
defaultValue: true,
meta: {
description: 'defaults to to true',
},
}),
ipType: schema.ip({ versions: ['ipv4'] }),
literalType: schema.literal('literallythis'),
neverType: schema.never(),
map: schema.mapOf(schema.string(), schema.string()),
record: schema.recordOf(schema.string(), schema.string()),
union: schema.oneOf([
schema.string({ maxLength: 1, meta: { description: 'Union string' } }),
schema.number({ min: 0, meta: { description: 'Union number' } }),
]),
uri: schema.uri({
scheme: ['prototest'],
defaultValue: () => 'prototest://something',
}),
any: schema.any({ meta: { description: 'any type' } }),
});
import { createLargeSchema } from './oas_converter/kbn_config_schema/lib.test.util';

type RoutesMeta = ReturnType<Router['getRoutes']>[number];
type VersionedRoutesMeta = ReturnType<CoreVersionedRouter['getRoutes']>[number];
Expand Down Expand Up @@ -67,7 +42,7 @@ export const getRouterDefaults = () => ({
query: schema.object({
page: schema.number({ max: 999, min: 1, defaultValue: 1, meta: { description: 'page' } }),
}),
body: testSchema,
body: createLargeSchema(),
},
response: {
200: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,169 @@
*/

import { schema } from '@kbn/config-schema';
import { is, isNullableObjectType, getParamSchema } from './lib';
import {
is,
convert,
convertPathParameters,
convertQuery,
isNullableObjectType,
getParamSchema,
} from './lib';

import { createLargeSchema } from './lib.test.util';

describe('convert', () => {
test('base case', () => {
expect(convert(createLargeSchema())).toEqual({
schema: {
additionalProperties: false,
properties: {
any: {},
booleanDefault: {
default: true,
description: 'defaults to to true',
type: 'boolean',
},
ipType: {
format: 'ipv4',
type: 'string',
},
literalType: {
enum: ['literallythis'],
type: 'string',
},
map: {
additionalProperties: {
type: 'string',
},
type: 'object',
},
maybeNumber: {
maximum: 1000,
minimum: 1,
type: 'number',
},
record: {
additionalProperties: {
type: 'string',
},
type: 'object',
},
string: {
maxLength: 10,
minLength: 1,
type: 'string',
},
union: {
anyOf: [
{
description: 'Union string',
maxLength: 1,
type: 'string',
},
{
description: 'Union number',
minimum: 0,
type: 'number',
},
],
},
uri: {
default: 'prototest://something',
format: 'uri',
type: 'string',
},
},
required: ['string', 'ipType', 'literalType', 'map', 'record', 'union', 'any'],
type: 'object',
},
shared: {},
});
});

test('shared schemas', () => {
const idSchema = schema.object({ a: schema.string() }, { meta: { id: 'myId' } });
const otherSchema = schema.object({ id: idSchema });
expect(convert(otherSchema)).toEqual({
schema: {
additionalProperties: false,
properties: {
id: {
$ref: '#/components/schemas/myId',
},
},
required: ['id'],
type: 'object',
},
shared: {
myId: {
additionalProperties: false,
properties: {
a: {
type: 'string',
},
},
required: ['a'],
type: 'object',
},
},
});
});
});

describe('convertPathParameters', () => {
test('base conversion', () => {
expect(
convertPathParameters(schema.object({ a: schema.string() }), { a: { optional: false } })
).toEqual({
params: [
{
in: 'path',
name: 'a',
required: true,
schema: {
type: 'string',
},
},
],
shared: {},
});
});
test('conversion with refs is disallowed', () => {
const sharedSchema = schema.object({ a: schema.string() }, { meta: { id: 'myparams' } });
expect(() => convertPathParameters(sharedSchema, { a: { optional: false } })).toThrow(
/myparams.*not supported/
);
});
test('throws if known parameters not found', () => {
expect(() =>
convertPathParameters(schema.object({ b: schema.string() }), { a: { optional: false } })
).toThrow(/Unknown parameter: b/);
});
});

describe('convertQuery', () => {
test('base conversion', () => {
expect(convertQuery(schema.object({ a: schema.string() }))).toEqual({
query: [
{
in: 'query',
name: 'a',
required: true,
schema: {
type: 'string',
},
},
],
shared: {},
});
});

test('conversion with refs is disallowed', () => {
const sharedSchema = schema.object({ a: schema.string() }, { meta: { id: 'myparams' } });
expect(() => convertQuery(sharedSchema)).toThrow(/myparams.*not supported/);
});
});

describe('is', () => {
test.each([
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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 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 or the Server
* Side Public License, v 1.
*/

import { schema } from '@kbn/config-schema';

export function createLargeSchema() {
return schema.object({
string: schema.string({ maxLength: 10, minLength: 1 }),
maybeNumber: schema.maybe(schema.number({ max: 1000, min: 1 })),
booleanDefault: schema.boolean({
defaultValue: true,
meta: {
description: 'defaults to to true',
},
}),
ipType: schema.ip({ versions: ['ipv4'] }),
literalType: schema.literal('literallythis'),
neverType: schema.never(),
map: schema.mapOf(schema.string(), schema.string()),
record: schema.recordOf(schema.string(), schema.string()),
union: schema.oneOf([
schema.string({ maxLength: 1, meta: { description: 'Union string' } }),
schema.number({ min: 0, meta: { description: 'Union number' } }),
]),
uri: schema.uri({
scheme: ['prototest'],
defaultValue: () => 'prototest://something',
}),
any: schema.any({ meta: { description: 'any type' } }),
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ const convertObjectMembersToParameterObjects = (
const anyOf = (result as OpenAPIV3.SchemaObject).anyOf as OpenAPIV3.SchemaObject[];
properties = anyOf.find((s) => s.type === 'object')!.properties!;
} else if (isObjectType(schema)) {
const { result } = parse({ schema, ctx }) as { result: OpenAPIV3.SchemaObject };
const { result } = parse({ schema, ctx });
if ('$ref' in result)
throw new Error(
`Found a reference to "${result.$ref}". Runtime types with IDs are not supported in path or query parameters.`
);
properties = (result as OpenAPIV3.SchemaObject).properties!;
(result.required ?? []).forEach((key) => required.set(key, true));
} else if (isRecordType(schema)) {
Expand Down

0 comments on commit 8b6132e

Please sign in to comment.