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] Beautify generated operation ids #198132

Merged
merged 17 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
296 changes: 148 additions & 148 deletions oas_docs/bundle.json

Large diffs are not rendered by default.

286 changes: 143 additions & 143 deletions oas_docs/bundle.serverless.json

Large diffs are not rendered by default.

291 changes: 143 additions & 148 deletions oas_docs/output/kibana.serverless.yaml

Large diffs are not rendered by default.

301 changes: 148 additions & 153 deletions oas_docs/output/kibana.yaml

Large diffs are not rendered by default.

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 @@ -35,7 +35,7 @@ export const sharedOas = {
get: {
deprecated: true,
'x-discontinued': 'route discontinued version or date',
operationId: '%2Fbar#0',
operationId: 'get-bar',
parameters: [
{
description: 'The version of the API to use',
Expand Down Expand Up @@ -154,7 +154,7 @@ export const sharedOas = {
'/foo/{id}/{path*}': {
get: {
description: 'route description',
operationId: '%2Ffoo%2F%7Bid%7D%2F%7Bpath*%7D#0',
operationId: 'get-foo-id-path',
parameters: [
{
description: 'The version of the API to use',
Expand Down Expand Up @@ -278,7 +278,7 @@ export const sharedOas = {
},
post: {
description: 'route description',
operationId: '%2Ffoo%2F%7Bid%7D%2F%7Bpath*%7D#1',
operationId: 'post-foo-id-path',
parameters: [
{
description: 'The version of the API to use',
Expand Down
5 changes: 2 additions & 3 deletions packages/kbn-router-to-openapispec/src/generate_oas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
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';
import { processVersionedRouter } from './process_versioned_router';
import { buildGlobalTags } from './util';
import { buildGlobalTags, createOpIdGenerator } from './util';

export const openApiVersion = '3.0.0';

Expand All @@ -40,8 +39,8 @@ export const generateOpenApiDocument = (
): OpenAPIV3.Document => {
const { filters } = opts;
const converter = new OasConverter();
const getOpId = createOperationIdCounter();
const paths: OpenAPIV3.PathsObject = {};
const getOpId = createOpIdGenerator();
for (const router of appRouters.routers) {
const result = processRouter(router, converter, getOpId, filters);
Object.assign(paths, result.paths);
Expand Down

This file was deleted.

24 changes: 0 additions & 24 deletions packages/kbn-router-to-openapispec/src/operation_id_counter.ts

This file was deleted.

11 changes: 7 additions & 4 deletions packages/kbn-router-to-openapispec/src/process_router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
import { schema } from '@kbn/config-schema';
import { Router } from '@kbn/core-http-router-server-internal';
import { OasConverter } from './oas_converter';
import { createOperationIdCounter } from './operation_id_counter';
import { extractResponses, processRouter } from './process_router';
import { type InternalRouterRoute } from './type';
import { createOpIdGenerator } from './util';

describe('extractResponses', () => {
let oasConverter: OasConverter;
Expand Down Expand Up @@ -86,18 +86,21 @@ describe('processRouter', () => {
const testRouter = {
getRoutes: () => [
{
method: 'get',
path: '/foo',
options: { access: 'internal', deprecated: true, discontinued: 'discontinued router' },
handler: jest.fn(),
validationSchemas: { request: { body: schema.object({}) } },
},
{
method: 'get',
path: '/bar',
options: {},
handler: jest.fn(),
validationSchemas: { request: { body: schema.object({}) } },
},
{
method: 'get',
path: '/baz',
options: {},
handler: jest.fn(),
Expand Down Expand Up @@ -125,20 +128,20 @@ describe('processRouter', () => {
} as unknown as Router;

it('only provides routes for version 2023-10-31', () => {
const result1 = processRouter(testRouter, new OasConverter(), createOperationIdCounter(), {
const result1 = processRouter(testRouter, new OasConverter(), createOpIdGenerator(), {
version: '2023-10-31',
});

expect(Object.keys(result1.paths!)).toHaveLength(4);

const result2 = processRouter(testRouter, new OasConverter(), createOperationIdCounter(), {
const result2 = processRouter(testRouter, new OasConverter(), createOpIdGenerator(), {
version: '2024-10-31',
});
expect(Object.keys(result2.paths!)).toHaveLength(0);
});

it('updates description with privileges required', () => {
const result = processRouter(testRouter, new OasConverter(), createOperationIdCounter(), {
const result = processRouter(testRouter, new OasConverter(), createOpIdGenerator(), {
version: '2023-10-31',
});

Expand Down
6 changes: 3 additions & 3 deletions packages/kbn-router-to-openapispec/src/process_router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ import {
mergeResponseContent,
prepareRoutes,
setXState,
GetOpId,
} from './util';
import type { OperationIdCounter } from './operation_id_counter';
import type { GenerateOpenApiDocumentOptionsFilters } from './generate_oas';
import type { CustomOperationObject, InternalRouterRoute } from './type';
import { extractAuthzDescription } from './extract_authz_description';

export const processRouter = (
appRouter: Router,
converter: OasConverter,
getOpId: OperationIdCounter,
getOpId: GetOpId,
filters?: GenerateOpenApiDocumentOptionsFilters
) => {
const paths: OpenAPIV3.PathsObject = {};
Expand Down Expand Up @@ -91,7 +91,7 @@ export const processRouter = (
: undefined,
responses: extractResponses(route, converter),
parameters,
operationId: getOpId(route.path),
operationId: getOpId({ path: route.path, method: route.method }),
};

setXState(route.options.availability, operation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import { schema } from '@kbn/config-schema';
import type { CoreVersionedRouter } from '@kbn/core-http-router-server-internal';
import { get } from 'lodash';
import { OasConverter } from './oas_converter';
import { createOperationIdCounter } from './operation_id_counter';
import {
processVersionedRouter,
extractVersionedResponses,
extractVersionedRequestBodies,
} from './process_versioned_router';
import { VersionedRouterRoute } from '@kbn/core-http-server';
import { createOpIdGenerator } from './util';

let oasConverter: OasConverter;
beforeEach(() => {
Expand Down Expand Up @@ -125,7 +125,7 @@ describe('processVersionedRouter', () => {
const baseCase = processVersionedRouter(
{ getRoutes: () => [createTestRoute()] } as unknown as CoreVersionedRouter,
new OasConverter(),
createOperationIdCounter(),
createOpIdGenerator(),
{}
);

Expand All @@ -137,7 +137,7 @@ describe('processVersionedRouter', () => {
const filteredCase = processVersionedRouter(
{ getRoutes: () => [createTestRoute()] } as unknown as CoreVersionedRouter,
new OasConverter(),
createOperationIdCounter(),
createOpIdGenerator(),
{ version: '2023-10-31' }
);
expect(Object.keys(get(filteredCase, 'paths["/foo"].get.responses.200.content')!)).toEqual([
Expand All @@ -149,7 +149,7 @@ describe('processVersionedRouter', () => {
const results = processVersionedRouter(
{ getRoutes: () => [createTestRoute()] } as unknown as CoreVersionedRouter,
new OasConverter(),
createOperationIdCounter(),
createOpIdGenerator(),
{}
);
expect(results.paths['/foo']).toBeDefined();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import { extractAuthzDescription } from './extract_authz_description';
import type { GenerateOpenApiDocumentOptionsFilters } from './generate_oas';
import type { OasConverter } from './oas_converter';
import { isReferenceObject } from './oas_converter/common';
import type { OperationIdCounter } from './operation_id_counter';
import {
prepareRoutes,
getPathParameters,
Expand All @@ -30,12 +29,13 @@ import {
mergeResponseContent,
getXsrfHeaderForMethod,
setXState,
GetOpId,
} from './util';

export const processVersionedRouter = (
appRouter: CoreVersionedRouter,
converter: OasConverter,
getOpId: OperationIdCounter,
getOpId: GetOpId,
filters?: GenerateOpenApiDocumentOptionsFilters
) => {
const routes = prepareRoutes(appRouter.getRoutes(), filters);
Expand Down Expand Up @@ -121,7 +121,7 @@ export const processVersionedRouter = (
? extractVersionedResponse(handler, converter, contentType)
: extractVersionedResponses(route, converter, contentType),
parameters,
operationId: getOpId(route.path),
operationId: getOpId({ path: route.path, method: route.method }),
};

setXState(route.options.options?.availability, operation);
Expand Down
Loading