fastify-swagger
Advanced tools
Comparing version 2.5.0 to 2.5.1
@@ -99,5 +99,8 @@ 'use strict' | ||
: route.schema | ||
const path = route.url.startsWith(basePath) | ||
let path = route.url.startsWith(basePath) | ||
? route.url.replace(basePath, '') | ||
: route.url | ||
if (!path.startsWith('/')) { | ||
path = '/' + path | ||
} | ||
const url = formatParamUrl(path) | ||
@@ -104,0 +107,0 @@ |
{ | ||
"name": "fastify-swagger", | ||
"version": "2.5.0", | ||
"version": "2.5.1", | ||
"description": "Generate Swagger files automatically for Fastify.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -44,2 +44,15 @@ # fastify-swagger | ||
], | ||
definitions: { | ||
User: { | ||
$id: 'User', | ||
type: 'object', | ||
required: ['id', 'email'], | ||
properties: { | ||
id: { type: 'string', format: 'uuid' }, | ||
firstName: { type: 'string', nullable: true }, | ||
lastName: { type: 'string', nullable: true }, | ||
email: {type: 'string', format: 'email' } | ||
} | ||
} | ||
}, | ||
securityDefinitions: { | ||
@@ -52,3 +65,4 @@ apiKey: { | ||
} | ||
} | ||
}, | ||
exposeRoute: true | ||
}) | ||
@@ -218,2 +232,47 @@ | ||
### Open API (OA) Parameter Options | ||
Note: OA's terminology differs from Fastify's. OA uses the term "parameter" to refer to those parts of a request that in [Fastify's validation documentation](https://www.fastify.io/docs/latest/Validation-and-Serialization/#validation) are called "querystring", "params", "headers". | ||
OA provides some options beyond those provided by the JSON schema specification for specifying the shape of parameters. A prime example of this the option for specifying how to encode those parameters that should be handled as arrays of values. There is no single universally accepted method for encoding such parameters appearing as part of query strings. OA2 provides a `collectionFormat` option which allows specifying how an array parameter should be encoded. (We're giving an example in the OA2 specification, as this is the default specification version used by this plugin. The same principles apply to OA3.) Specifying this option is easy. You just need to add it to the other options for the field you are defining. Like in this example: | ||
``` | ||
fastify.route({ | ||
method: 'GET', | ||
url: '/', | ||
schema: { | ||
querystring: { | ||
type: 'object', | ||
required: ['fields'], | ||
additionalProperties: false, | ||
properties: { | ||
fields: { | ||
type: 'array', | ||
items: { | ||
type: 'string' | ||
}, | ||
minItems: 1, | ||
// | ||
// Note that this is an Open API version 2 configuration option. The | ||
// options changed in version 3. The plugin currently only supports | ||
// version 2 of Open API. | ||
// | ||
// Put `collectionFormat` on the same property which you are defining | ||
// as an array of values. (i.e. `collectionFormat` should be a sibling | ||
// of the `type: "array"` specification.) | ||
collectionFormat: 'multi' | ||
} | ||
} | ||
} | ||
}, | ||
handler (request, reply) { | ||
reply.send(request.query.fields) | ||
} | ||
}) | ||
``` | ||
There is a complete runnable example [here](examples/collection-format.js). | ||
**IMPORTANT CAVEAT** These encoding options you can set in your schema have no bearing on how, for instance, a query string parser parses the query string. They change how Swagger UI presents its documentation, and how it generates `curl` commands when you click the `Try it out` button. Depending on which options you set in your schema, you *may also need* to change the default query string parser used by Fastify so that it produces a JavaScript object that will conform to the schema. As far as arrays are concerned, the default query string parser conforms to the `collectionFormat: "multi"` specification. If you were to select `collectionFormat: "csv"`, you would have to replace the default query string parser with one that parses CSV parameter values into arrays. The same caveat applies to the other parts of a request that OA calls "parameters" (e.g. headers, path parameters) and which are not encoded as JSON in a request. | ||
<a name="hide"></a> | ||
@@ -220,0 +279,0 @@ ### Hide a route |
@@ -637,1 +637,44 @@ 'use strict' | ||
}) | ||
test('basePath ensure leading slash', t => { | ||
t.plan(3) | ||
const fastify = Fastify() | ||
fastify.register(fastifySwagger, { | ||
swagger: Object.assign({}, swaggerInfo.swagger, { | ||
basePath: '/' | ||
}) | ||
}) | ||
fastify.get('/endpoint', {}, () => {}) | ||
fastify.ready(err => { | ||
t.error(err) | ||
const swaggerObject = fastify.swagger() | ||
t.notOk(swaggerObject.paths.endpoint) | ||
t.ok(swaggerObject.paths['/endpoint']) | ||
}) | ||
}) | ||
test('basePath with prefix ensure leading slash', t => { | ||
t.plan(3) | ||
const fastify = Fastify() | ||
fastify.register(fastifySwagger, { | ||
prefix: '/', | ||
swagger: Object.assign({}, swaggerInfo.swagger, { | ||
basePath: '/' | ||
}) | ||
}) | ||
fastify.get('/endpoint', {}, () => {}) | ||
fastify.ready(err => { | ||
t.error(err) | ||
const swaggerObject = fastify.swagger() | ||
t.notOk(swaggerObject.paths.endpoint) | ||
t.ok(swaggerObject.paths['/endpoint']) | ||
}) | ||
}) |
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
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
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 2 instances in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
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
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
AI-detected potential code anomaly
Supply chain riskAI has identified unusual behaviors that may pose a security risk.
Found 1 instance in 1 package
High entropy strings
Supply chain riskContains high entropy strings. This could be a sign of encrypted data, leaked secrets or obfuscated code.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
9247416
37
10805
1
309
8
14