-
-
Notifications
You must be signed in to change notification settings - Fork 209
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
feat : add openapi 3 support #333
Conversation
- reduce the number of files in root folder
- hook - formatParamUrl - consumesFormOnly - plainJsonObjectToSwagger2 - localRefResolve
huge +1 for the refactor. |
Really nice to see a refactor and an upgrade in features for this package :D |
Basic Functionality for OpenAPI 3 is added. In order to make thing simple and not dealing with the transition between It should fix most of the issue with Maybe in the future, someone is willing to add a transition layer between |
TypeScript type will not updated for now. As #328 dealing with it. |
semver-major given the extent of the change? |
I think that's a good idea. |
This is missing some documentation update. |
I will complete it today and also the typing update. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should throw if both swagger
and openapi
options are both set at the same time.
I think someone will left the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Blocking because I need a couple of days to check the PR and I would like to review it 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this feature is ok as the first step to OAS3 support
There is some issues with this usage:
Click to expand!
'use strict'
const Fastify = require('fastify')
const fastifySwagger = require('../../index')
const fastify = Fastify({ logger: true })
fastify.register(fastifySwagger, {
openapi: {
info: {
title: 'Test swagger',
description: 'testing the fastify swagger api',
version: '0.1.0'
},
servers: [{
url: 'http://localhost'
}],
components: {
securitySchemes: {
apiKey: {
type: 'apiKey',
name: 'apiKey',
in: 'header'
}
}
}
},
exposeRoute: true
})
fastify.addSchema({
$id: 'Order',
type: 'object',
properties: {
id: {
type: 'integer'
// format: 'int64'
}
}
})
fastify.post('/', {
schema: {
body: {
$ref: 'Order#'
},
response: {
200: {
$ref: 'Order#'
}
}
}
}, () => {})
fastify.listen(8080, err => {
console.log(err)
})
That lead to:
Because one big difference between oas2 and oas3 is the components
over the old definitions
keyword. (I use this schema as reference)
So I would land this PR as a major preview release to collect other edges cases before a major bump.
What do you think? @fastify/plugins
I think it is the bug from Test Case for your reproduction test('collect addSchema on root instance', t => {
t.plan(2)
const fastify = Fastify()
fastify.register(fastifySwagger, swaggerInfo)
fastify.addSchema({ $id: 'Order', type: 'object', properties: { id: { type: 'integer' } } })
fastify.post('/', { schema: { body: { $ref: 'Order#' }, response: { 200: { $ref: 'Order#' } } } }, () => {})
fastify.ready(err => {
t.error(err)
const swaggerObject = fastify.swagger()
t.ok(swaggerObject.definitions['def-0'])
})
}) Reason behind fastify.addHook('onRoute', (routeOptions) => {
routes.push(routeOptions)
})
fastify.addHook('onRegister', async (instance) => {
instance.addHook('onReady', (done) => {
const allSchemas = instance.getSchemas() // we only collected the encapsulate schema
for (const schemaId of Object.keys(allSchemas)) {
if (!sharedSchemasMap.has(schemaId)) {
sharedSchemasMap.set(schemaId, allSchemas[schemaId])
}
}
done()
})
}) Another problem like you say, the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it should be exist even for swagger.
Nope, it is managed by the (ugly sigh) def-0
of the json-schema-resolver
and it could be fixed there:
https://github.com/Eomm/json-schema-resolver/blob/39191491f3ef2c4b5fac5e19ad067459ccccdded/ref-resolver.js#L24
but your hotfix works 👍
I do not think it can be solved like you say as the What I said it exist for fastify-swagger/test/openapi.js Lines 785 to 789 in 35a9be8
I need to wrap the addSchema in register in order to get the test work.It should be a bug needed to file, so I will not fix it now. |
For schemas there's slightly different approach that might be more elegant solution rather than resolve it. I've described it here: SkeLLLa/fastify-oas#30 (comment) but it require lot's of work, so it should be different PR. |
@@ -1,4 +1,5 @@ | |||
import { FastifyPlugin } from 'fastify'; | |||
import { FastifyPluginCallback } from 'fastify'; | |||
import { OpenAPI, OpenAPIV2, OpenAPIV3 } from 'openapi-types'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because of this openapi-types
needs to be a dependency and not dev-dependency.
See #340
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, it is landed from here #328
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But this pr was landed after #328 (if i see that correctly here). So on master index.d.ts has a dependency on openapi-types
, but it does not list it as dependency.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feedback are welcome while construction.
This PR used to add minimal support for OpenAPI 3. I do not assume complicated situation.
Issue related to OpenAPI 3
Resolve: #87, Resolve: #236, Resolve #265, Resolve #330
PR related to OpenAPI 3
Closes: #237
TODO List
dynamic.js
Checklist
npm run test
andnpm run benchmark
and the Code of conduct