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

fix: nested examples in OpenAPI schemas for request body & response #772

Merged
merged 6 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1000,7 +1000,7 @@ Will generate this in the OpenAPI v3 schema's `path`:
"foo": { "type": "string" },
"bar": { "type": "string" }
},
"examples": [{ "foo": "bar", "bar": "baz" }]
"example": { "foo": "bar", "bar": "baz" }
}
}
},
Expand Down
32 changes: 26 additions & 6 deletions lib/spec/openapi/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ function plainJsonObjectToOpenapi3 (container, jsonSchema, externalSchemas, secu
required: jsonSchemaElement.required
}

const media = schemaToMedia(jsonSchemaElement)
const media = schemaToMedia(jsonSchemaElement, true)

// complex serialization in query or cookie, eg. JSON
// https://swagger.io/docs/specification/describing-parameters/#schema-vs-content
Expand Down Expand Up @@ -201,7 +201,7 @@ function plainJsonObjectToOpenapi3 (container, jsonSchema, externalSchemas, secu
break
case 'path':
toOpenapiProp = function (propertyName, jsonSchemaElement) {
const media = schemaToMedia(jsonSchemaElement)
const media = schemaToMedia(jsonSchemaElement, true)

const result = {
...media,
Expand Down Expand Up @@ -231,10 +231,10 @@ function plainJsonObjectToOpenapi3 (container, jsonSchema, externalSchemas, secu
})
}

function schemaToMedia (schema) {
function schemaToMedia (schema, allowsMultipleExamples) {
const media = { schema }

if (schema.examples?.length === 1) {
if (schema.examples && (!allowsMultipleExamples || schema.examples.length === 1)) {
media.example = schema.examples[0]
delete schema.examples
} else if (schema.examples?.length > 1) {
Expand All @@ -251,13 +251,33 @@ function schemaToMedia (schema) {
return media
}

function schemaToMediaRecursive (schema, allowsMultipleExamples) {
if (schema.type === 'object' && schema.properties) {
for (const property in schema.properties) {
const media = schemaToMediaRecursive(schema.properties[property], false)
schema.properties[property] = media.schema
if (typeof media.example !== 'undefined') {
schema.properties[property].example = media.example
}
}
} else if (schema.type === 'array' && schema.items) {
const media = schemaToMediaRecursive(schema.items, false)
schema.items = media.schema
if (typeof media.example !== 'undefined') {
schema.items.example = media.example
}
}
const media = schemaToMedia(schema, allowsMultipleExamples)
return media
}

function resolveBodyParams (body, schema, consumes, ref) {
const resolved = transformDefsToComponents(ref.resolve(schema))
if ((Array.isArray(consumes) && consumes.length === 0) || consumes === undefined) {
consumes = ['application/json']
}

const media = schemaToMedia(resolved)
const media = schemaToMediaRecursive(resolved, true)
consumes.forEach((consume) => {
body.content[consume] = media
})
Expand Down Expand Up @@ -346,7 +366,7 @@ function resolveResponse (fastifyResponseJson, produces, ref) {

delete resolved[xResponseDescription]

const media = schemaToMedia(resolved)
const media = schemaToMediaRecursive(resolved, true)

for (const produce of produces) {
content[produce] = media
Expand Down
Loading
Loading