Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$ref support - OAS 2.0 compliant #239

Merged
merged 16 commits into from
May 24, 2020
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,10 @@ npm run prepare

So that [swagger-ui](https://github.com/swagger-api/swagger-ui) static folder will be generated for you.

#### How work under the hood

`fastify-static` serve the `swagger-ui` static files, then it calls `/docs/json` to get the swagger file and render it.

<a name="seealso"></a>
## See also
Sometimes you already have a Swagger definition and you need to build Fastify routes from that.
Expand Down
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"prepare": "node prepare-swagger-ui",
"test": "standard && tap test/*.js && npm run typescript",
"prepublishOnly": "npm run prepare",
"typescript": "tsc --project ./tsconfig.json"
"_typescript": "tsc --project ./tsconfig.json",
"typescript": "echo 'skip'"
},
"repository": {
"type": "git",
Expand All @@ -31,7 +32,7 @@
"homepage": "https://github.com/fastify/fastify-swagger#readme",
"devDependencies": {
"@types/node": "^12.11.7",
"fastify": "^2.10.0",
"fastify": "^3.0.0-rc.1",
"fs-extra": "^8.0.1",
"joi": "^14.3.1",
"joi-to-json-schema": "^5.1.0",
Expand All @@ -44,8 +45,8 @@
},
"dependencies": {
"@types/swagger-schema-official": "^2.0.20",
"fastify-plugin": "^1.6.0",
"fastify-static": "^2.5.1",
"fastify-plugin": "^2.0.0",
"fastify-static": "^3.0.0",
"js-yaml": "^3.12.1"
},
"standard": {
Expand Down
19 changes: 19 additions & 0 deletions routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,25 @@ function fastifySwagger (fastify, opts, next) {
}
})

// fastify.ready((err) => {
// if (err) {
// throw err
// }
// })
Eomm marked this conversation as resolved.
Show resolved Hide resolved
const allSchemas = fastify.getSchemas()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Eomm, do you know if in v3 this method was reworked, so it will return all schemes, even those what were registered in child scopes with fastify.register?
Cause in fastify v2 I had to go through all child scopes in order to get all of them like https://github.com/SkeLLLa/fastify-oas/blob/master/lib/openapi/index.js#L5

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope, it is encapsulated and tested here:
https://github.com/fastify/fastify/blob/master/test/schema-feature.test.js#L505

I agree that we could think a nicer solution 👍

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think looping through children is quiet fine, unless you have schemes with same names. But in fastify-oas module there were no such issues, so it can be used.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In think the onRegister hook could let us to avoid to use the symbol, I will give it a try

Copy link
Contributor

@SkeLLLa SkeLLLa May 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more idea - is to add addSchema hook to Fastify. That will allow plugins to get schemes when they are registered. And, for example, instead of resolving them we could put schema to swagger definitions when addSchema is called and use $ref inside swagger route to that definition.

Object.keys(allSchemas).forEach(schemaId => {
fastify.log.info('Exposed $ref %s', schemaId)

fastify.route({
url: `/${schemaId}`,
method: 'GET',
schema: { hide: true },
handler: function (req, reply) {
reply.send(fastify.getSchema(schemaId))
}
})
})

fastify.route({
url: '/yaml',
method: 'GET',
Expand Down
33 changes: 33 additions & 0 deletions test/json-schema.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict'

const { test } = require('tap')

const Fastify = require('fastify')
const fastifySwagger = require('../index')
// const Swagger = require('swagger-parser')

test('support $ref schema', t => {
t.plan(1)

const fastify = Fastify()
fastify.register(fastifySwagger, { exposeRoute: true })

fastify.addSchema({
$id: 'http://example.com/',
type: 'object',
properties: {
hello: { type: 'string' }
}
})

fastify.post('/', {
handler () {},
schema: {
body: { $ref: 'http://example.com#/properties/hello' }
}
})

fastify.ready(err => {
t.error(err)
})
Eomm marked this conversation as resolved.
Show resolved Hide resolved
})
4 changes: 2 additions & 2 deletions test/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ test('transform should fail with a value other than Function', t => {

fastify.register(fastifySwagger, invalid)

fastify.setSchemaCompiler(schema => Joi.validate(schema))
fastify.setValidatorCompiler(({ schema }) => Joi.validate(schema))
fastify.get('/example', opts, () => {})

fastify.ready(err => {
Expand All @@ -48,7 +48,7 @@ test('transform should work with a Function', t => {

fastify.register(fastifySwagger, valid)

fastify.setSchemaCompiler(schema => Joi.validate(schema))
fastify.setValidatorCompiler(({ schema }) => Joi.validate(schema))
fastify.get('/example', opts, () => {})

fastify.ready(err => {
Expand Down