From 9253c4ba8a37b97b10d8c21847fff7eb77bf111f Mon Sep 17 00:00:00 2001 From: FX Algrain Date: Mon, 13 Dec 2021 17:27:31 +0100 Subject: [PATCH] feat(openapi): support patternProperties --- lib/spec/openapi/utils.js | 6 ++++ test/spec/openapi/schema.js | 57 +++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/lib/spec/openapi/utils.js b/lib/spec/openapi/utils.js index be4ccac6..8ba7d30a 100644 --- a/lib/spec/openapi/utils.js +++ b/lib/spec/openapi/utils.js @@ -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') diff --git a/test/spec/openapi/schema.js b/test/spec/openapi/schema.js index 52e87cec..b1ecde63 100644 --- a/test/spec/openapi/schema.js +++ b/test/spec/openapi/schema.js @@ -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' } + } + } + } + } + } + }) +})