Skip to content

Commit

Permalink
[HTTP] Fix how versioned route opts are mapped to IRouter registrar o…
Browse files Browse the repository at this point in the history
…pts (#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
  • Loading branch information
jloleysens authored and jasonrhodes committed May 17, 2023
1 parent 46027a6 commit 8f41663
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
{
Expand Down Expand Up @@ -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<typeof versionedRouter.post>[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<unknown, unknown, unknown, Method> = {
path: opts.path,
options: { access, ...options },
validate: passThroughValidation,
};

expect(router.post).toHaveBeenCalledWith(
expect.objectContaining(expectedRouteConfig),
expect.any(Function)
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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<Method> {
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()]);
Expand Down

0 comments on commit 8f41663

Please sign in to comment.