fastify-swagger
Advanced tools
Comparing version 4.8.0 to 4.8.1
@@ -86,2 +86,7 @@ 'use strict' | ||
jsonSchema[key] = jsonSchema[key].replace('definitions', 'components/schemas') | ||
} else if (key === 'examples' && Array.isArray(jsonSchema[key]) && (jsonSchema[key].length > 1)) { | ||
jsonSchema.examples = convertExamplesArrayToObject(jsonSchema.examples) | ||
} else if (key === 'examples' && Array.isArray(jsonSchema[key]) && (jsonSchema[key].length === 1)) { | ||
jsonSchema.example = jsonSchema[key][0] | ||
delete jsonSchema[key] | ||
} else { | ||
@@ -95,2 +100,14 @@ jsonSchema[key] = transformDefsToComponents(jsonSchema[key]) | ||
function convertExamplesArrayToObject (examples) { | ||
return examples.reduce((examplesObject, example, index) => { | ||
if (typeof example === 'object') { | ||
examplesObject['example' + (index + 1)] = { value: example } | ||
} else { | ||
examplesObject[example] = { value: example } | ||
} | ||
return examplesObject | ||
}, {}) | ||
} | ||
// For supported keys read: | ||
@@ -97,0 +114,0 @@ // https://swagger.io/docs/specification/describing-parameters/ |
{ | ||
"name": "fastify-swagger", | ||
"version": "4.8.0", | ||
"version": "4.8.1", | ||
"description": "Serve Swagger/OpenAPI documentation for Fastify, supporting dynamic generation", | ||
@@ -48,3 +48,3 @@ "main": "index.js", | ||
"swagger-parser": "^10.0.2", | ||
"swagger-ui-dist": "3.50.0", | ||
"swagger-ui-dist": "3.51.0", | ||
"tap": "^15.0.1", | ||
@@ -51,0 +51,0 @@ "tsd": "^0.17.0" |
@@ -347,2 +347,265 @@ 'use strict' | ||
test('transforms examples in example if single string example', t => { | ||
t.plan(4) | ||
const fastify = Fastify() | ||
fastify.register(fastifySwagger, openapiOption) | ||
const opts = { | ||
schema: { | ||
body: { | ||
type: 'object', | ||
required: ['hello'], | ||
properties: { | ||
hello: { | ||
type: 'string', | ||
examples: ['world'] | ||
} | ||
} | ||
} | ||
} | ||
} | ||
fastify.get('/', opts, () => {}) | ||
fastify.ready(err => { | ||
t.error(err) | ||
const openapiObject = fastify.swagger() | ||
const schema = openapiObject.paths['/'].get.requestBody.content['application/json'].schema | ||
t.ok(schema) | ||
t.notOk(schema.properties.hello.examples) | ||
t.equal(schema.properties.hello.example, 'world') | ||
}) | ||
}) | ||
test('transforms examples in example if single object example', t => { | ||
t.plan(4) | ||
const fastify = Fastify() | ||
fastify.register(fastifySwagger, openapiOption) | ||
const opts = { | ||
schema: { | ||
body: { | ||
type: 'object', | ||
required: ['hello'], | ||
properties: { | ||
hello: { | ||
type: 'object', | ||
properties: { | ||
lorem: { | ||
type: 'string' | ||
} | ||
}, | ||
examples: [{ lorem: 'ipsum' }] | ||
} | ||
} | ||
} | ||
} | ||
} | ||
fastify.get('/', opts, () => {}) | ||
fastify.ready(err => { | ||
t.error(err) | ||
const openapiObject = fastify.swagger() | ||
const schema = openapiObject.paths['/'].get.requestBody.content['application/json'].schema | ||
t.ok(schema) | ||
t.notOk(schema.properties.hello.examples) | ||
t.same(schema.properties.hello.example, { lorem: 'ipsum' }) | ||
}) | ||
}) | ||
test('uses examples if has multiple string examples', t => { | ||
t.plan(4) | ||
const fastify = Fastify() | ||
fastify.register(fastifySwagger, openapiOption) | ||
const opts = { | ||
schema: { | ||
body: { | ||
type: 'object', | ||
required: ['hello'], | ||
properties: { | ||
hello: { | ||
type: 'string', | ||
examples: ['hello', 'world'] | ||
} | ||
} | ||
} | ||
} | ||
} | ||
fastify.get('/', opts, () => {}) | ||
fastify.ready(err => { | ||
t.error(err) | ||
const openapiObject = fastify.swagger() | ||
const schema = openapiObject.paths['/'].get.requestBody.content['application/json'].schema | ||
t.ok(schema) | ||
t.ok(schema.properties.hello.examples) | ||
t.same(schema.properties.hello.examples, { | ||
hello: { | ||
value: 'hello' | ||
}, | ||
world: { | ||
value: 'world' | ||
} | ||
}) | ||
}) | ||
}) | ||
test('uses examples if has multiple numbers examples', t => { | ||
t.plan(4) | ||
const fastify = Fastify() | ||
fastify.register(fastifySwagger, openapiOption) | ||
const opts = { | ||
schema: { | ||
body: { | ||
type: 'object', | ||
required: ['hello'], | ||
properties: { | ||
hello: { | ||
type: 'number', | ||
examples: [1, 2] | ||
} | ||
} | ||
} | ||
} | ||
} | ||
fastify.get('/', opts, () => {}) | ||
fastify.ready(err => { | ||
t.error(err) | ||
const openapiObject = fastify.swagger() | ||
const schema = openapiObject.paths['/'].get.requestBody.content['application/json'].schema | ||
t.ok(schema) | ||
t.ok(schema.properties.hello.examples) | ||
t.same(schema.properties.hello.examples, { | ||
1: { | ||
value: 1 | ||
}, | ||
2: { | ||
value: 2 | ||
} | ||
}) | ||
}) | ||
}) | ||
test('uses examples if has multiple object examples', t => { | ||
t.plan(4) | ||
const fastify = Fastify() | ||
fastify.register(fastifySwagger, openapiOption) | ||
const opts = { | ||
schema: { | ||
body: { | ||
type: 'object', | ||
required: ['hello'], | ||
properties: { | ||
hello: { | ||
type: 'object', | ||
properties: { | ||
lorem: { | ||
type: 'string' | ||
} | ||
}, | ||
examples: [{ lorem: 'ipsum' }, { hello: 'world' }] | ||
} | ||
} | ||
} | ||
} | ||
} | ||
fastify.get('/', opts, () => {}) | ||
fastify.ready(err => { | ||
t.error(err) | ||
const openapiObject = fastify.swagger() | ||
const schema = openapiObject.paths['/'].get.requestBody.content['application/json'].schema | ||
t.ok(schema) | ||
t.ok(schema.properties.hello.examples) | ||
t.same(schema.properties.hello.examples, { | ||
example1: { | ||
value: { | ||
lorem: 'ipsum' | ||
} | ||
}, | ||
example2: { | ||
value: { | ||
hello: 'world' | ||
} | ||
} | ||
}) | ||
}) | ||
}) | ||
test('uses examples if has multiple array examples', t => { | ||
t.plan(4) | ||
const fastify = Fastify() | ||
fastify.register(fastifySwagger, openapiOption) | ||
const opts = { | ||
schema: { | ||
body: { | ||
type: 'object', | ||
required: ['hello'], | ||
properties: { | ||
hello: { | ||
type: 'array', | ||
items: { | ||
type: 'string' | ||
}, | ||
examples: [['a', 'b', 'c'], ['d', 'f', 'g']] | ||
} | ||
} | ||
} | ||
} | ||
} | ||
fastify.get('/', opts, () => {}) | ||
fastify.ready(err => { | ||
t.error(err) | ||
const openapiObject = fastify.swagger() | ||
const schema = openapiObject.paths['/'].get.requestBody.content['application/json'].schema | ||
t.ok(schema) | ||
t.ok(schema.properties.hello.examples) | ||
t.same(schema.properties.hello.examples, { | ||
example1: { | ||
value: [ | ||
'a', | ||
'b', | ||
'c' | ||
] | ||
}, | ||
example2: { | ||
value: [ | ||
'd', | ||
'f', | ||
'g' | ||
] | ||
} | ||
}) | ||
}) | ||
}) | ||
module.exports = { openapiOption } |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14323
10816986