-
-
Notifications
You must be signed in to change notification settings - Fork 210
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: customize def-0 model name #459
Conversation
I think CI is failing due a temporary issue:
locally all is fine and 100% coverage reached |
Pull Request Test Coverage Report for Build 1182925217
💛 - Coveralls |
Pull Request Test Coverage Report for Build 1182925217
💛 - Coveralls |
It would be great to have some exported helper functions for use with
|
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 means that we keep the original $id
and move the resolution works to title
field? Or the reverse way?
No. We add a The 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.
lgtm
|
||
const swaggerObject = fastify.swagger() | ||
const api = await Swagger.validate(swaggerObject) | ||
t.match(api.components.schemas['def-0'], schema) |
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.
Why would this schema, which was added with an $id
, not longer show the id via the api
?
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 is explained in the first post of this pr
Hey guys, I kind of have a similar issue, but Im using TypeScript and it looks like that |
This PR:
$id
as model name (see screenshot at the bottom). Under the hood, thedef-0
renaming is still in place.$ref
's link into the inline schema. This gives to the user the chance to do what he/she wants and remove totally thedef-0
logic.I think this (too simple?) solution solves the case.
Feel free to give feedback about this PR
Why this change
To avoid a major bump, this PR fixes the view issue without breaking the
def-0
logic that has been developed to protect users actually 😄Let me explain.
This JSON Schema has a relative URI
$id
.This is wrong from the DRAFT 07 specification:
Moreover, referencing the
user
schema with the$ref: 'user#/definitions/id'
is wrong from the specification point of view: the reference is relative and it should be resolved using the local schema's root URI, not the external one (the$id: 'user'
one to be clear).The
fastify-swagger
knows that the$id
field is used as a simple key/value pair actually.For this reason, it ignores the user's
$id
*** and deference the schema to adef-0
: to be sure to avoid conflicts to let theswagger-ui
module resolve correctly the references. Otherwise, you may have different output based on the$id
s the developer set.The specification, cover a lot more use cases than I saw in the GitHub issues actually.
The last example, setting an absolute root URI tells to the
swagger-ui
module to fetch the schema from the link!This snippet causes the swagger-ui module to make an HTTP request to
http://example.com/user.json
!Now, forcing
fastify-swagger
to use the$id
can break the configuration of those devs that use the standard strictly.Using the
$id
in place ofdef-0
is correct only if the developer knows that there is no conflicts or nested schema's declarations.With this PR, the user may replace the
buildLocalReference
option to accomplish this. But this behaviour leads to a "convention over configuration" strategy.Fixes #337
Fixes #286
Fixes #225
Should help #432
*** when I say that this module "ignores" the
$id
s I mean that we know that all the$ref
can be resolved locally, accessing the data added to the fastify schema via theaddSchema
method. If the user set an absolute URI as$id
, fastify will never look up an external link.