From c3bc29a57ea9da3dc672f904436cb0c413fa479d Mon Sep 17 00:00:00 2001 From: Pieter Wigboldus Date: Fri, 4 Oct 2024 21:44:42 +0200 Subject: [PATCH] Add again option to use the example value --- package-lock.json | 4 +-- package.json | 2 +- src/express-callback.js | 4 ++- src/params.js | 15 ++++++++-- src/params.test.js | 63 ++++++++++++++++++++++++++++++++++++----- src/router.js | 10 ++++--- 6 files changed, 80 insertions(+), 18 deletions(-) diff --git a/package-lock.json b/package-lock.json index 2c40fa9..56e0d73 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@trojs/openapi-server", - "version": "1.5.0", + "version": "1.5.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@trojs/openapi-server", - "version": "1.5.0", + "version": "1.5.1", "license": "MIT", "dependencies": { "@sentry/node": "^7.112.1", diff --git a/package.json b/package.json index 4859d90..ab4b264 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@trojs/openapi-server", "description": "OpenAPI Server", - "version": "1.5.0", + "version": "1.5.1", "author": { "name": "Pieter Wigboldus", "url": "https://trojs.org/" diff --git a/src/express-callback.js b/src/express-callback.js index c70ae10..c2cf675 100644 --- a/src/express-callback.js +++ b/src/express-callback.js @@ -16,10 +16,11 @@ import { parseParams } from './params.js'; * @param {boolean=} params.errorDetails * @param {Logger=} params.logger * @param {object=} params.meta + * @param {boolean=} params.mock * @returns {Function} */ export const makeExpressCallback = - ({ controller, specification, errorDetails, logger, meta }) => + ({ controller, specification, errorDetails, logger, meta, mock }) => /** * Handle controller * @async @@ -37,6 +38,7 @@ export const makeExpressCallback = const parameters = parseParams({ query: allParameters, spec: context.operation.parameters, + mock, }); const url = `${request.protocol}://${request.get('Host')}${request.originalUrl}`; diff --git a/src/params.js b/src/params.js index 8d04286..c8c2a6d 100644 --- a/src/params.js +++ b/src/params.js @@ -5,20 +5,29 @@ import { types } from './types.js'; * @param {object} params * @param {object} params.query * @param {object} params.spec + * @param {boolean=} params.mock * @returns {object} */ -export const parseParams = ({ query, spec }) => +export const parseParams = ({ query, spec, mock = false }) => spec .map((parameter) => { const { name, schema } = parameter; - const { type, default: defaultValue } = schema; + const { + type, + default: defaultValue, + example: exampleValue, + } = schema; const Type = types[type]; const paramName = query?.[name]; - if (!paramName && defaultValue) { + if (!paramName && defaultValue !== undefined) { return { name, value: defaultValue }; } + if (!paramName && mock && exampleValue !== undefined) { + return { name, value: exampleValue }; + } + if (!paramName) { return undefined; } diff --git a/src/params.test.js b/src/params.test.js index 6083fb4..b9f3018 100644 --- a/src/params.test.js +++ b/src/params.test.js @@ -72,7 +72,7 @@ const TestCases = [ }, { description: - 'Get the example values from the schema if no query params are given', + 'Dont get the example values from the schema if no query params are given and mock is not enabled', query: {}, spec: [ { @@ -89,7 +89,29 @@ const TestCases = [ expectedResult: {}, }, { - description: 'It should not throw if no query params are given', + description: + 'Get the example values from the schema if no query params are given and mock is enabled', + query: {}, + spec: [ + { + name: 'page', + required: false, + in: 'query', + schema: { + type: 'integer', + minimum: 0, + example: 0, + }, + }, + ], + mock: true, + expectedResult: { + page: 0, + }, + }, + { + description: + 'It should not throw if no query params are given and mock is not enabled', query: undefined, spec: [ { @@ -106,6 +128,28 @@ const TestCases = [ ], expectedResult: {}, }, + { + description: + 'It should not throw if no query params are given and mock is enabled', + query: undefined, + spec: [ + { + name: 'size', + required: false, + in: 'query', + schema: { + type: 'integer', + minimum: 1, + maximum: 10000, + example: 10, + }, + }, + ], + mock: true, + expectedResult: { + size: 10, + }, + }, { description: 'Parse mixed params to the types defined in the spec', query: { name: 'Pieter', ok: 'true' }, @@ -157,10 +201,15 @@ const TestCases = [ test('Parse params', async (t) => { await Promise.all( - TestCases.map(async ({ description, query, spec, expectedResult }) => { - await t.test(description, () => { - assert.deepEqual(parseParams({ query, spec }), expectedResult); - }); - }) + TestCases.map( + async ({ description, query, spec, mock, expectedResult }) => { + await t.test(description, () => { + assert.deepEqual( + parseParams({ query, spec, mock }), + expectedResult + ); + }); + } + ) ); }); diff --git a/src/router.js b/src/router.js index 219ec77..1fdf843 100644 --- a/src/router.js +++ b/src/router.js @@ -25,6 +25,7 @@ import { unauthorized } from './handlers/unauthorized.js'; * @param {object=} params.meta * @param {SecurityHandler[]=} params.securityHandlers * @param {AjvOpts=} params.ajvOptions + * @param {boolean=} params.mock * @returns {{ api: OpenAPIBackend, openAPISpecification: object }} */ export const setupRouter = ({ @@ -37,6 +38,7 @@ export const setupRouter = ({ meta, securityHandlers = [], ajvOptions = {}, + mock, }) => { const api = new OpenAPIBackend({ definition: openAPISpecification, @@ -69,16 +71,16 @@ export const setupRouter = ({ errorDetails, logger, meta, + mock, }) ); } ); api.register('notImplemented', (context) => { - const { mock } = context.api.mockResponseForOperation( - context.operation.operationId - ); - return mock; + const { mock: mockImplementation } = + context.api.mockResponseForOperation(context.operation.operationId); + return mockImplementation; }); securityHandlers.forEach((securityHandler) => {