Skip to content

Commit

Permalink
increase code coverage (#267)
Browse files Browse the repository at this point in the history
  • Loading branch information
iandrc authored Jul 24, 2020
1 parent d9e400e commit adf0c95
Show file tree
Hide file tree
Showing 8 changed files with 499 additions and 72 deletions.
51 changes: 19 additions & 32 deletions dynamic.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,22 +106,20 @@ module.exports = function (fastify, opts, next) {
swaggerObject.externalDocs = externalDocs
}

if (!ref) {
const externalSchemas = Array.from(sharedSchemasMap.values())
const externalSchemas = Array.from(sharedSchemasMap.values())

ref = Ref({ clone: true, applicationUri: 'todo.com', externalSchemas })
swaggerObject.definitions = {
...swaggerObject.definitions,
...(ref.definitions().definitions)
}

// Swagger doesn't accept $id on /definitions schemas.
// The $ids are needed by Ref() to check the URI so we need
// to remove them at the end of the process
Object.values(swaggerObject.definitions)
.forEach(_ => { delete _.$id })
ref = Ref({ clone: true, applicationUri: 'todo.com', externalSchemas })
swaggerObject.definitions = {
...swaggerObject.definitions,
...(ref.definitions().definitions)
}

// Swagger doesn't accept $id on /definitions schemas.
// The $ids are needed by Ref() to check the URI so we need
// to remove them at the end of the process
Object.values(swaggerObject.definitions)
.forEach(_ => { delete _.$id })

swaggerObject.paths = {}
for (var route of routes) {
if (route.schema && route.schema.hide) {
Expand Down Expand Up @@ -218,9 +216,8 @@ module.exports = function (fastify, opts, next) {
}

swaggerMethod.responses = genResponse(schema ? schema.response : null)
if (swaggerRoute) {
swaggerObject.paths[url] = swaggerRoute
}

swaggerObject.paths[url] = swaggerRoute
}

if (opts && opts.yaml) {
Expand Down Expand Up @@ -281,16 +278,9 @@ module.exports = function (fastify, opts, next) {
const rawJsonSchema = fastifyResponseJson[key]
const resolved = ref.resolve(rawJsonSchema)

if (resolved.type || resolved.$ref) {
responsesContainer[key] = {
schema: resolved
}
} else {
responsesContainer[key] = resolved
}

if (!responsesContainer[key].description) {
responsesContainer[key].description = 'Default Response'
responsesContainer[key] = {
schema: resolved,
description: 'Default Response'
}
})

Expand Down Expand Up @@ -395,10 +385,7 @@ function localRefResolve (jsonSchema, externalSchemas) {
return propertiesMap
}

if (jsonSchema.$ref) {
// $ref is in the format: #/definitions/<resolved definition>/<optional fragment>
const localReference = jsonSchema.$ref.split('/')[2]
return localRefResolve(externalSchemas[localReference], externalSchemas)
}
return jsonSchema
// $ref is in the format: #/definitions/<resolved definition>/<optional fragment>
const localReference = jsonSchema.$ref.split('/')[2]
return localRefResolve(externalSchemas[localReference], externalSchemas)
}
Empty file.
47 changes: 47 additions & 0 deletions examples/example-static-specification.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"openapi": "3.0.0",
"info": {
"description": "Test swagger specification",
"version": "1.0.0",
"title": "Test swagger specification",
"contact": {
"email": "[email protected]"
}
},
"servers": {
"url": "http://localhost:3000/",
"description": "Localhost (uses test data)"
},
"paths": {
"/status": {
"get": {
"description": "Status route, so we can check if server is alive",
"tags": ["Status"]
},
"responses": {
"200": {
"description": "Server is alive",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"health": {
"type": "boolean"
},
"date": {
"type": "string"
}
},
"example": {
"health": true,
"date": "2018-02-19T15:36:46.758Z"
}
}
}
}
}
}
}
}
}
3 changes: 3 additions & 0 deletions examples/test-package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"version": "3.1.0"
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"scripts": {
"prepare": "node prepare-swagger-ui",
"test": "standard && tap test/*.js && npm run typescript",
"test": "standard && tap --100 test/*.js && npm run typescript",
"prepublishOnly": "npm run prepare",
"typescript": "tsd"
},
Expand Down
6 changes: 1 addition & 5 deletions routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,7 @@ function fastifySwagger (fastify, opts, next) {
schema: { hide: true },
handler: function (req, reply) {
const file = req.params['*']
if (file === '') {
reply.redirect(getRedirectPathForTheRootRoute(req.raw.url))
} else {
reply.sendFile(file)
}
reply.sendFile(file)
}
})

Expand Down
19 changes: 19 additions & 0 deletions test/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,24 @@ const opts5 = {
}
}

const opts6 = {
schema: {
params: {
type: 'object',
properties: {
id: {
type: 'string',
description: 'user id'
},
key: {
type: 'string',
description: 'just some random key'
}
}
}
}
}

test('/documentation/json route', t => {
t.plan(2)
const fastify = Fastify()
Expand Down Expand Up @@ -488,6 +506,7 @@ test('/documentation2/json route (overwrite)', t => {
fastify.post('/example', opts2, () => {})
fastify.get('/parameters/:id', opts3, () => {})
fastify.get('/example1', opts4, () => {})
fastify.get('/parameters/:id/:key', opts6, () => {})

fastify.inject({
method: 'GET',
Expand Down
Loading

0 comments on commit adf0c95

Please sign in to comment.