@fastify/swagger
Advanced tools
Comparing version 8.2.1 to 8.3.0
@@ -159,2 +159,3 @@ 'use strict' | ||
case 'cookie': | ||
case 'header': | ||
case 'query': | ||
@@ -188,2 +189,5 @@ toOpenapiProp = function (propertyName, jsonSchemaElement) { | ||
if (jsonSchema.explode != null) result.explode = jsonSchema.explode | ||
if (jsonSchema.allowReserved === true && container === 'query') { | ||
result.allowReserved = jsonSchema.allowReserved | ||
} | ||
return result | ||
@@ -208,26 +212,2 @@ } | ||
break | ||
case 'header': | ||
toOpenapiProp = function (propertyName, jsonSchemaElement) { | ||
const result = { | ||
in: 'header', | ||
name: propertyName, | ||
required: jsonSchemaElement.required, | ||
description: jsonSchemaElement.description, | ||
schema: { | ||
type: jsonSchemaElement.type | ||
} | ||
} | ||
const media = schemaToMedia(jsonSchemaElement) | ||
if (media.example) { | ||
result.example = media.example | ||
} | ||
if (media.examples) { | ||
result.examples = media.examples | ||
} | ||
return result | ||
} | ||
break | ||
} | ||
@@ -234,0 +214,0 @@ |
@@ -102,2 +102,3 @@ 'use strict' | ||
switch (container) { | ||
case 'header': | ||
case 'query': | ||
@@ -108,3 +109,3 @@ toSwaggerProp = function (propertyName, jsonSchemaElement) { | ||
throw new Error('Complex serialization is not supported by Swagger. ' + | ||
'Remove "' + xConsume + '" for "' + propertyName + '" querystring schema or ' + | ||
'Remove "' + xConsume + '" for "' + propertyName + '" querystring/header schema or ' + | ||
'change specification to OpenAPI') | ||
@@ -140,13 +141,2 @@ } | ||
break | ||
case 'header': | ||
toSwaggerProp = function (propertyName, jsonSchemaElement) { | ||
return { | ||
in: 'header', | ||
name: propertyName, | ||
required: jsonSchemaElement.required, | ||
description: jsonSchemaElement.description, | ||
type: jsonSchemaElement.type | ||
} | ||
} | ||
break | ||
} | ||
@@ -153,0 +143,0 @@ |
{ | ||
"name": "@fastify/swagger", | ||
"version": "8.2.1", | ||
"version": "8.3.0", | ||
"description": "Serve Swagger/OpenAPI documentation for Fastify, supporting dynamic generation", | ||
@@ -42,2 +42,3 @@ "main": "index.js", | ||
"@apidevtools/swagger-parser": "^10.1.0", | ||
"@fastify/cookie": "^8.3.0", | ||
"@fastify/pre-commit": "^2.0.2", | ||
@@ -44,0 +45,0 @@ "@types/node": "^18.0.0", |
@@ -285,1 +285,71 @@ 'use strict' | ||
}) | ||
test('renders $ref schema with enum in headers', async (t) => { | ||
const fastify = Fastify() | ||
await fastify.register(fastifySwagger, { openapi: {} }) | ||
fastify.register(async (instance) => { | ||
instance.addSchema({ $id: 'headerA', type: 'object', properties: { 'x-enum-header': { type: 'string', enum: ['OK', 'NOT_OK'] } } }) | ||
instance.get('/url1', { schema: { headers: { $ref: 'headerA#' }, response: { 200: { type: 'object' } } } }, async () => ({ result: 'OK' })) | ||
}) | ||
await fastify.ready() | ||
const openapiObject = fastify.swagger() | ||
await Swagger.validate(openapiObject) | ||
// the OpenAPI spec should show the enum | ||
t.match(openapiObject.paths['/url1'].get.parameters[0].schema, { type: 'string', enum: ['OK', 'NOT_OK'] }) | ||
}) | ||
test('renders $ref schema with additional keywords', async (t) => { | ||
const fastify = Fastify() | ||
await fastify.register(fastifySwagger, { openapi: {} }) | ||
await fastify.register(require('@fastify/cookie')) | ||
const cookie = { | ||
type: 'object', | ||
properties: { | ||
a: { type: 'string' }, | ||
b: { type: 'string' }, | ||
c: { type: 'string' } | ||
}, | ||
minProperties: 2 | ||
} | ||
fastify.register(async (instance) => { | ||
instance.addSchema({ | ||
$id: 'headerA', | ||
type: 'object', | ||
properties: { | ||
cookie | ||
} | ||
}) | ||
instance.get('/url1', { | ||
preValidation: async (request) => { | ||
request.headers.cookie = request.cookies | ||
}, | ||
schema: { | ||
headers: { | ||
$ref: 'headerA#' | ||
} | ||
} | ||
}, async (req) => (req.headers)) | ||
}) | ||
await fastify.ready() | ||
const openapiObject = fastify.swagger() | ||
await Swagger.validate(openapiObject) | ||
t.match(openapiObject.paths['/url1'].get.parameters[0].schema, cookie) | ||
let res = await fastify.inject({ method: 'GET', url: 'url1', cookies: { a: 'hi', b: 'asd' } }) | ||
t.match(res.statusCode, 200) | ||
res = await fastify.inject({ method: 'GET', url: 'url1', cookies: { a: 'hi' } }) | ||
t.match(res.statusCode, 400) | ||
t.match(openapiObject.paths['/url1'].get.parameters[0].schema, cookie) | ||
}) |
@@ -73,3 +73,2 @@ 'use strict' | ||
name: 'foo', | ||
description: undefined, | ||
schema: { | ||
@@ -823,2 +822,3 @@ type: 'string' | ||
type: 'object', | ||
allowReserved: true, | ||
properties: { | ||
@@ -839,2 +839,3 @@ obj: { | ||
ajv.addKeyword({ keyword: 'explode' }) | ||
ajv.addKeyword({ keyword: 'allowReserved' }) | ||
} | ||
@@ -854,2 +855,3 @@ ] | ||
t.equal(api.paths['/'].get.parameters[0].explode, false) | ||
t.equal(api.paths['/'].get.parameters[0].allowReserved, true) | ||
}) |
@@ -139,1 +139,18 @@ 'use strict' | ||
}) | ||
test('renders $ref schema with enum in headers', async (t) => { | ||
const fastify = Fastify() | ||
await fastify.register(fastifySwagger) | ||
fastify.register(async (instance) => { | ||
instance.addSchema({ $id: 'headerA', type: 'object', properties: { 'x-enum-header': { type: 'string', enum: ['OK', 'NOT_OK'] } } }) | ||
instance.get('/url1', { schema: { headers: { $ref: 'headerA#' }, response: { 200: { type: 'object' } } } }, async () => ({ result: 'OK' })) | ||
}) | ||
await fastify.ready() | ||
const swagger = fastify.swagger() | ||
await Swagger.validate(swagger) | ||
t.match(swagger.paths['/url1'].get.parameters[0], { type: 'string', enum: ['OK', 'NOT_OK'] }) | ||
}) |
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
238886
6812
12