From 8f41663aee1e3e0cc19265c2fc4cc784f970888b Mon Sep 17 00:00:00 2001 From: Jean-Louis Leysens Date: Tue, 16 May 2023 16:06:39 +0200 Subject: [PATCH] [HTTP] Fix how versioned route opts are mapped to IRouter registrar opts (#157875) ## Summary The versioned router internally uses the IRouter and currently incorrectly passes through options. TS was not complaining due to structural type checking seeing this as valid. Added a test to ensure all options are being passed through as expected. ### Checklist - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios --- .../core_versioned_route.test.ts | 36 ++++++++++++++++++- .../versioned_router/core_versioned_route.ts | 10 +++++- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/packages/core/http/core-http-router-server-internal/src/versioned_router/core_versioned_route.test.ts b/packages/core/http/core-http-router-server-internal/src/versioned_router/core_versioned_route.test.ts index b01b8b7647c87..032914981ceb4 100644 --- a/packages/core/http/core-http-router-server-internal/src/versioned_router/core_versioned_route.test.ts +++ b/packages/core/http/core-http-router-server-internal/src/versioned_router/core_versioned_route.test.ts @@ -9,12 +9,18 @@ import { hapiMocks } from '@kbn/hapi-mocks'; import { schema } from '@kbn/config-schema'; import type { ApiVersion } from '@kbn/core-http-common'; -import type { IRouter, KibanaResponseFactory, RequestHandler } from '@kbn/core-http-server'; +import type { + IRouter, + KibanaResponseFactory, + RequestHandler, + RouteConfig, +} from '@kbn/core-http-server'; import { ELASTIC_HTTP_VERSION_HEADER } from '@kbn/core-http-common'; import { createRouter } from './mocks'; import { CoreVersionedRouter } from '.'; import { passThroughValidation } from './core_versioned_route'; import { CoreKibanaRequest } from '../request'; +import { Method } from './types'; const createRequest = ( { @@ -204,4 +210,32 @@ describe('Versioned route', () => { expect(validatedQuery).toBe(true); expect(validatedOutputBody).toBe(true); }); + it('passes through the expected values to the IRouter registrar', () => { + const versionedRouter = CoreVersionedRouter.from({ router }); + const opts: Parameters[0] = { + path: '/test/{id}', + access: 'internal', + options: { + authRequired: true, + tags: ['access:test'], + timeout: { payload: 60_000, idleSocket: 10_000 }, + xsrfRequired: false, + }, + }; + + versionedRouter.post(opts); + expect(router.post).toHaveBeenCalledTimes(1); + const { access, options } = opts; + + const expectedRouteConfig: RouteConfig = { + path: opts.path, + options: { access, ...options }, + validate: passThroughValidation, + }; + + expect(router.post).toHaveBeenCalledWith( + expect.objectContaining(expectedRouteConfig), + expect.any(Function) + ); + }); }); diff --git a/packages/core/http/core-http-router-server-internal/src/versioned_router/core_versioned_route.ts b/packages/core/http/core-http-router-server-internal/src/versioned_router/core_versioned_route.ts index abf18d0ef7f97..9bef02cc46d9c 100644 --- a/packages/core/http/core-http-router-server-internal/src/versioned_router/core_versioned_route.ts +++ b/packages/core/http/core-http-router-server-internal/src/versioned_router/core_versioned_route.ts @@ -17,6 +17,7 @@ import type { VersionedRoute, VersionedRouteConfig, IKibanaResponse, + RouteConfigOptions, } from '@kbn/core-http-server'; import type { Mutable } from 'utility-types'; import type { Method } from './types'; @@ -72,12 +73,19 @@ export class CoreVersionedRoute implements VersionedRoute { { path: this.path, validate: passThroughValidation, - options: this.options, + options: this.getRouteConfigOptions(), }, this.requestHandler ); } + private getRouteConfigOptions(): RouteConfigOptions { + return { + access: this.options.access, + ...this.options.options, + }; + } + /** This method assumes that one or more versions handlers are registered */ private getDefaultVersion(): ApiVersion { return resolvers[this.router.defaultHandlerResolutionStrategy]([...this.handlers.keys()]);