Skip to content

Commit

Permalink
Merge pull request #164 from trojs/feature/no-example-value-in-response
Browse files Browse the repository at this point in the history
Add again option to use the example value
  • Loading branch information
w3nl authored Oct 4, 2024
2 parents 242e355 + c3bc29a commit 54ef44b
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 18 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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/"
Expand Down
4 changes: 3 additions & 1 deletion src/express-callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}`;

Expand Down
15 changes: 12 additions & 3 deletions src/params.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
63 changes: 56 additions & 7 deletions src/params.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
{
Expand All @@ -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: [
{
Expand All @@ -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' },
Expand Down Expand Up @@ -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
);
});
}
)
);
});
10 changes: 6 additions & 4 deletions src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>, openAPISpecification: object }}
*/
export const setupRouter = ({
Expand All @@ -37,6 +38,7 @@ export const setupRouter = ({
meta,
securityHandlers = [],
ajvOptions = {},
mock,
}) => {
const api = new OpenAPIBackend({
definition: openAPISpecification,
Expand Down Expand Up @@ -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) => {
Expand Down

0 comments on commit 54ef44b

Please sign in to comment.