From 1c2d484bf691404a642f2b62c78b3f29c8fe24bd Mon Sep 17 00:00:00 2001 From: Adibla <32988879+Adibla@users.noreply.github.com> Date: Sat, 5 Feb 2022 10:26:59 +0100 Subject: [PATCH] feat(open-api): add required on body if necessary (#541) * feat(open-api): add required on body if necessary * fix: lint * fix: replace optional chaining * test: update required spec --- lib/spec/openapi/utils.js | 10 ++++++---- lib/spec/swagger/utils.js | 1 - test/spec/openapi/option.js | 38 +++++++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 5 deletions(-) diff --git a/lib/spec/openapi/utils.js b/lib/spec/openapi/utils.js index a78b5e8d..9a234f15 100644 --- a/lib/spec/openapi/utils.js +++ b/lib/spec/openapi/utils.js @@ -198,18 +198,20 @@ function plainJsonObjectToOpenapi3 (container, jsonSchema, externalSchemas, secu }) } -function resolveBodyParams (content, schema, consumes, ref) { +function resolveBodyParams (body, schema, consumes, ref) { const resolved = transformDefsToComponents(ref.resolve(schema)) - if ((Array.isArray(consumes) && consumes.length === 0) || typeof consumes === 'undefined') { consumes = ['application/json'] } consumes.forEach((consume) => { - content[consume] = { + body.content[consume] = { schema: resolved } }) + if (resolved && resolved.required && resolved.required.length) { + body.required = true + } } function resolveCommonParams (container, parameters, schema, ref, sharedSchemas, securityIgnores) { @@ -327,7 +329,7 @@ function prepareOpenapiMethod (schema, ref, openapiObject) { if (schema.querystring) resolveCommonParams('query', parameters, schema.querystring, ref, openapiObject.definitions, securityIgnores.query) if (schema.body) { openapiMethod.requestBody = { content: {} } - resolveBodyParams(openapiMethod.requestBody.content, schema.body, schema.consumes, ref) + resolveBodyParams(openapiMethod.requestBody, schema.body, schema.consumes, ref) } if (schema.params) resolveCommonParams('path', parameters, schema.params, ref, openapiObject.definitions) if (schema.headers) resolveCommonParams('header', parameters, schema.headers, ref, openapiObject.definitions, securityIgnores.header) diff --git a/lib/spec/swagger/utils.js b/lib/spec/swagger/utils.js index 3b403f15..b3590e07 100644 --- a/lib/spec/swagger/utils.js +++ b/lib/spec/swagger/utils.js @@ -190,7 +190,6 @@ function isConsumesFormOnly (schema) { function resolveBodyParams (parameters, schema, ref) { const resolved = ref.resolve(schema) - replaceUnsupported(resolved) parameters.push({ diff --git a/test/spec/openapi/option.js b/test/spec/openapi/option.js index 32bec212..015bf40e 100644 --- a/test/spec/openapi/option.js +++ b/test/spec/openapi/option.js @@ -607,4 +607,42 @@ test('uses examples if has multiple array examples', t => { }) }) +test('uses examples if has property required in body', t => { + t.plan(5) + const fastify = Fastify() + + fastify.register(fastifySwagger, openapiOption) + + const body = { + type: 'object', + required: ['hello'], + properties: { + hello: { + type: 'string' + } + } + } + + const opts = { + schema: { + body + } + } + + fastify.get('/', opts, () => {}) + + fastify.ready(err => { + t.error(err) + + const openapiObject = fastify.swagger() + const schema = openapiObject.paths['/'].get.requestBody.content['application/json'].schema + const requestBody = openapiObject.paths['/'].get.requestBody + + t.ok(schema) + t.ok(schema.properties) + t.same(body.required, ['hello']) + t.same(requestBody.required, true) + }) +}) + module.exports = { openapiOption }