Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(open-api): add required on body if necessary #541

Merged
merged 4 commits into from
Feb 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 }