Skip to content

Commit

Permalink
feat(openapi): support patternProperties
Browse files Browse the repository at this point in the history
  • Loading branch information
fxalgrain committed Dec 13, 2021
1 parent 9ebe8c8 commit 9253c4b
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/spec/openapi/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ function normalizeUrl (url, servers, stripBasePath) {

function transformDefsToComponents (jsonSchema) {
if (typeof jsonSchema === 'object' && jsonSchema !== null) {
// Handle patternProperties, that is not part of OpenAPI definitions
if (jsonSchema.patternProperties) {
jsonSchema.additionalProperties = { type: 'string' }
delete jsonSchema.patternProperties
}

Object.keys(jsonSchema).forEach(function (key) {
if (key === '$ref') {
jsonSchema[key] = jsonSchema[key].replace('definitions', 'components/schemas')
Expand Down
57 changes: 57 additions & 0 deletions test/spec/openapi/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -416,3 +416,60 @@ test('fluent-json-schema', async t => {
const definedPath = api.paths['/'].get
t.same(definedPath.responses['200'].description, 'Default Response')
})

test('support "patternProperties" parameter', async t => {
const opt = {
schema: {
response: {
200: {
description: 'Expected Response',
type: 'object',
properties: {
foo: {
type: 'object',
patternProperties: {
'^[a-z]{2,3}-[a-zA-Z]{2}$': {
type: 'string'
}
},
additionalProperties: false
}
}
}
}
}
}

const fastify = Fastify()
fastify.register(fastifySwagger, {
openapi: true,
routePrefix: '/docs',
exposeRoute: true
})
fastify.get('/', opt, () => {})

await fastify.ready()

const swaggerObject = fastify.swagger()
const api = await Swagger.validate(swaggerObject)

const definedPath = api.paths['/'].get

t.same(definedPath.responses[200], {
description: 'Expected Response',
content: {
'application/json': {
schema: {
description: 'Expected Response',
type: 'object',
properties: {
foo: {
type: 'object',
additionalProperties: { type: 'string' }
}
}
}
}
}
})
})

0 comments on commit 9253c4b

Please sign in to comment.