-
-
Notifications
You must be signed in to change notification settings - Fork 197
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
Add support for $ref in header schemas #432
Conversation
@@ -167,6 +167,9 @@ function plainJsonObjectToOpenapi3 (container, jsonSchema, externalSchemas) { | |||
type: jsonSchemaElement.type | |||
} | |||
} | |||
|
|||
if (jsonSchemaElement.$ref) result.schema.$ref = jsonSchemaElement.$ref |
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'm wondering whether this should be
if (jsonSchemaElement.$ref) {
result.schema.$ref = jsonSchemaElement.$ref
delete result.schema.type
delete result.description
}
To avoid undefined
fields in fastify.swagger()
?
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.
As you pass a $ref
the property type
has no purpose to exist, on the other hand description
is still legit to be here. So technically you should only delete result.schema.type
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
Pull Request Test Coverage Report for Build 981880591
💛 - Coveralls |
After checking the generated json, I think this cannot be supported unless we add the support of object property {
"openapi": "3.0.3",
"info": {
"title": "Test swagger",
"description": "testing the fastify swagger api",
"version": "0.1.0"
},
"components": {
"securitySchemes": {
"apiKey": {
"type": "apiKey",
"name": "apiKey",
"in": "header"
}
},
"schemas": {}
},
"paths": {
"/": {
"get": {
"requestBody": {
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"hello"
],
"properties": {
"hello": {
"type": "string"
}
}
}
}
}
},
"parameters": [
{
"in": "header",
"name": "referencedHeader",
"required": true,
"schema": {
"$ref": "#/components/headers/someHeader"
}
}
],
"responses": {
"200": {
"description": "Default Response"
}
}
}
}
},
"servers": [
{
"url": "http://localhost"
}
],
"security": [
{
"apiKey": []
}
],
"tags": [
{
"name": "tag"
}
],
"externalDocs": {
"description": "Find more info here",
"url": "https://swagger.io"
}
} |
@climba03003 thanks for your checks. I have stumbled into this too. I was able to get it to work in a slightly hacky way, though. The solution was to use I also had those definitions added to fastify via It would appear that parsing Starting up the server you can toggle between registering @climba03003 , do you have any idea how we could estimate work on getting this feature right? |
The fastest way is update the code of resolver in https://github.com/Eomm/json-schema-resolver which fit the needs. |
FYI, I have created a drop-in replacement for I've published the code here: https://github.com/davidschmitt/fastify-schema-resolver.git I haven't published the package on EDIT: I just realized that while my new resolver fixes fastify-swagger $ref issues for body and response schemas, it does not correct the top level $ref issue for headers, params or querystring schemas. My apologies for posting on this thread |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Greetings!
First of all, thanks for all the work on the fastify ecosystem. It's a blast to utilise it in projects.
This PR includes a small adjustment to the way
header
s are treated inplainJsonObjectToOpenapi3
.The motivation is a scenario from my setup, where I attach some headers validation to most of my routes. They contain either
enum
s orexample
s that I'd like to be properly passed down to the resulting OpenAPI schema. One of the benefits of this is, in tandem with properly initialising refs, to have its presentation inswagger-ui
improved: select fields forenum
etc.I first experimented with injecting
example
andexamples
fields directly into the value returned fromtoOpenapiProp
, however I think the approach with$ref
is both more elegant and functional.Please let me know whether: my suggestion is too narrow or not, and is the added test sufficient enough.
I'm also wondering
$ref
within an additionalschema
property,type
field next to possible$ref
should now be optional or not - best case scenario I'd like to define the$ref
only; same goes fordescription
?PS: isn't the PR template outdated? I see no
benchmark
script inpackage.json
.Cheers!
Checklist
npm run test
andnpm run benchmark
and the Code of conduct