Skip to content

Commit

Permalink
support fluent-json-schema for swagger and openapi
Browse files Browse the repository at this point in the history
  • Loading branch information
mcollina committed Sep 29, 2021
1 parent 146bfb1 commit 1747ecf
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/spec/openapi/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ function transformDefsToComponents (jsonSchema) {
} else if (key === 'examples' && Array.isArray(jsonSchema[key]) && (jsonSchema[key].length === 1)) {
jsonSchema.example = jsonSchema[key][0]
delete jsonSchema[key]
} else if (key === '$id') {
} else if (key === '$id' || key === '$schema') {
delete jsonSchema[key]
} else {
jsonSchema[key] = transformDefsToComponents(jsonSchema[key])
Expand Down
2 changes: 2 additions & 0 deletions lib/spec/swagger/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ function resolveResponse (fastifyResponseJson, ref) {
const rawJsonSchema = fastifyResponseJson[statusCode]
const resolved = ref.resolve(rawJsonSchema)

delete resolved.$schema

// 2xx is not supported by swagger
const deXXStatusCode = statusCode.toUpperCase().replace('XX', '00')
// conflict when we have both 2xx and 200
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"fastify": "^3.7.0",
"fastify-basic-auth": "^2.1.0",
"fastify-helmet": "^5.0.3",
"fluent-json-schema": "^3.0.1",
"fs-extra": "^10.0.0",
"joi": "^14.3.1",
"joi-to-json": "^2.0.0",
Expand Down
27 changes: 27 additions & 0 deletions test/spec/openapi/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const { test } = require('tap')
const Fastify = require('fastify')
const Swagger = require('swagger-parser')
const fastifySwagger = require('../../../index')
const S = require('fluent-json-schema')
const {
openapiOption,
schemaAllOf
Expand Down Expand Up @@ -389,3 +390,29 @@ test('support "default" parameter', async t => {
}
})
})

test('fluent-json-schema', async t => {
const opt = {
schema: {
response: {
200: S.object()
}
}
}

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, 'Default Response')
})
27 changes: 27 additions & 0 deletions test/spec/swagger/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const { test } = require('tap')
const Fastify = require('fastify')
const Swagger = require('swagger-parser')
const fastifySwagger = require('../../../index')
const S = require('fluent-json-schema')

test('support file in json schema', async t => {
const opts7 = {
Expand Down Expand Up @@ -388,3 +389,29 @@ test('support "default" parameter', async t => {
}
})
})

test('fluent-json-schema', async t => {
const opt = {
schema: {
response: {
200: S.object()
}
}
}

const fastify = Fastify()
fastify.register(fastifySwagger, {
swagger: 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, 'Default Response')
})

0 comments on commit 1747ecf

Please sign in to comment.