Skip to content

Commit

Permalink
feat(swagger): support patternProperties
Browse files Browse the repository at this point in the history
  • Loading branch information
fxalgrain committed Dec 15, 2021
1 parent 9253c4b commit e51730a
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
22 changes: 22 additions & 0 deletions lib/spec/swagger/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,25 @@ function plainJsonObjectToSwagger2 (container, jsonSchema, externalSchemas, secu
})
}

/*
* Map unsupported JSON schema definitions to Swagger definitions
*/
function replaceUnsupported (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) {
jsonSchema[key] = replaceUnsupported(jsonSchema[key])
})
}

return jsonSchema
}

function isConsumesFormOnly (schema) {
const consumes = schema.consumes
return (
Expand All @@ -168,6 +187,8 @@ function isConsumesFormOnly (schema) {
function resolveBodyParams (parameters, schema, ref) {
const resolved = ref.resolve(schema)

replaceUnsupported(resolved)

parameters.push({
name: 'body',
in: 'body',
Expand Down Expand Up @@ -224,6 +245,7 @@ function resolveResponse (fastifyResponseJson, ref) {
// add schema when type is not 'null'
if (rawJsonSchema.type !== 'null') {
const schema = { ...resolved }
replaceUnsupported(schema)
delete schema[xResponseDescription]
response.schema = schema
}
Expand Down
66 changes: 66 additions & 0 deletions test/spec/swagger/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -415,3 +415,69 @@ test('fluent-json-schema', async t => {
const definedPath = api.paths['/'].get
t.same(definedPath.responses['200'].description, 'Default Response')
})

test('support "patternProperties" in json schema', async t => {
const opt = {
schema: {
body: {
type: 'object',
patternProperties: {
'^[a-z]{2,3}-[a-zA-Z]{2}$': {
type: 'string'
}
}
},
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, {
swagger: 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.parameters[0].schema, {
type: 'object',
additionalProperties: { type: 'string' }
})

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

0 comments on commit e51730a

Please sign in to comment.