Skip to content

Commit

Permalink
feat(open-api): add required on body if necessary (#541)
Browse files Browse the repository at this point in the history
* feat(open-api): add required on body if necessary

* fix: lint

* fix: replace optional chaining

* test: update required spec
  • Loading branch information
Adibla authored Feb 5, 2022
1 parent a81dcdb commit 1c2d484
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
10 changes: 6 additions & 4 deletions lib/spec/openapi/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion lib/spec/swagger/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,6 @@ function isConsumesFormOnly (schema) {

function resolveBodyParams (parameters, schema, ref) {
const resolved = ref.resolve(schema)

replaceUnsupported(resolved)

parameters.push({
Expand Down
38 changes: 38 additions & 0 deletions test/spec/openapi/option.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

0 comments on commit 1c2d484

Please sign in to comment.