Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

fastify-swagger

Package Overview
Dependencies
Maintainers
8
Versions
100
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fastify-swagger - npm Package Compare versions

Comparing version 2.5.0 to 2.5.1

examples/collection-format.js

5

dynamic.js

@@ -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 @@

2

package.json
{
"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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc