-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Allow schema $refs to not have to start with #/definitions
#954
Allow schema $refs to not have to start with #/definitions
#954
Conversation
This allows you to simply run `mocha test-file.js` without having to remember to add all the extra stuff
We're using this module to render forms for Swagger/OAS files. $refs in swagger are stored in `components/schemas` at the top level of the object. This change allows $ref strings to not always start with `#/definitions/` which is a much easier solution than us having to recursively modify $refs to prefix with this value. All existing tests are passing, with a new test for this functionality. The change was to make the definitions part of the regular expression an optional non-capturing group.
…chema-form's dereferencing instead This gets a little bit messy because you can do these 3 different types of refs for request bodies: - inline schema - $ref directly to a schema - $ref to a request body object, which contains the schema within a mime type object The first 2 work in react-jsonschema-form (after this has been merged rjsf-team/react-jsonschema-form#954) The 3rd one doesn't, and probably never will as it's far too specific to be merged into that module. So in the case where it is referencing a request body in components, I've switched to performing a single lookup there to fetch the actual schema $ref (or inline schema)
This is so that it can be separately styled easily via CSS. It is already done this way in https://github.com/mozilla-services/react-jsonschema-form/blob/aadbbe9701c4db9df761e86f0280e1ecafc509f8/src/components/fields/SchemaField.js#L59
Tests are passing locally on my machine. Anyone know why it would be failing the prettier checks for a file i have not modified? |
@domharrington we are also considering generating forms from OpenAPI. DO u have an example somewhere? |
Yes! We're doing it quite successfully over here: https://github.com/readmeio/api-explorer Demo site: https://preview.readme.io/ |
Dude, looks verrrry interesting. Gonna have a look then get back to you. Looks very close to what Im thinking about. |
If you're interested in a hosted solution for your docs, you should check out ReadMe: https://readme.io/. The API explorer that I linked to above is our open source project, but we have a whole host of other features built into the product including suggested edits for your docs, an API, user login with variables. I'm an engineer at ReadMe, please feel free to reach out to me dom[at]readme.io if you have any more questions. |
Hey, sorry for not responding to this at the time. You only change the regular expression not to start with To be honest, we handle
I'll admit ignorance here, but the reason is that rjsf is a library rather than an application, so constraining packages should happen in the calling code. [Edit: I guess this is what I get for replying to months-old comments. We have a package-lock.json file now.]
As you say, it's just to keep the repo clean. |
Hey! No worries. I made the This change just allowed the possibility for it to not have to start with Hope that makes it a little clearer for the reasoning for this change. I'm using a fork of this library currently but it would be great to get back onto the main repo. I've got one more PR too: #1001. Thanks in advance. |
I think no longer treating |
…til edited You can test this locally here: http://localhost:9966/?selected=swagger-files%2Ftypes.json Search for "default value" and see that it is already in the code sample. This issue luckily got fixed upstream: rjsf-team/react-jsonschema-form#1034 So to bring that in I needed to update my fork of the module. I would love to get off of my fork and get back onto the main release, but there's some outstanding work on my PR which means we can't right now: rjsf-team/react-jsonschema-form#954
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.
@domharrington , thanks for the PR; I think the approach @glasserc mentioned makes the most sense (i.e. doing #/components/abc
should reference the object with the path components.abc
in the schema, not definitions.components.abc
).
Would you be able to modify your PR to have that behavior -- and would that behavior suffice for your needs for your own project?
package.json
Outdated
@@ -16,8 +16,8 @@ | |||
"publish-to-npm": "npm run build:readme && npm run dist && npm publish", | |||
"preversion": "npm run build:playground && npm run dist && npm run build:readme && npm run cs-check && npm run lint", | |||
"start": "node devServer.js", | |||
"tdd": "cross-env NODE_ENV=test mocha --require babel-register --watch --require ./test/setup-jsdom.js test/**/*_test.js", |
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.
Thanks for the changes for mocha config, but can you please remove these changes from this PR and make a separate PR for it?
Hey @epicfaace yup i agree with everything that was said before. It's on my todo list to update this PR with the more generic solution, i've just not had the capacity to get round to it yet. |
This PR fixes #988 . |
I took a stab at finishing this off today in a fresh fork from master but kept hitting upon problems around this block here: https://github.com/mozilla-services/react-jsonschema-form/blob/bece2a5a847e216c2386d4f7f0ce9cf25b549562/src/utils.js#L402-L404 Since the $ref and the definitions are coming from the same object now, it appears to get stuck in an infinite loop during that while block. E.g. const schema = {
definitions: {
testdef: { type: "string" },
},
$ref: "#/definitions/testdef",
}; The code attempts to resolve the $ref before it gets to the point where it looks in the current object: https://github.com/mozilla-services/react-jsonschema-form/blob/bece2a5a847e216c2386d4f7f0ce9cf25b549562/src/utils.js#L405 Can anyone with more knowledge of this code point me in the right direction or suggest a way forward? |
Hey @epicfaace, any suggestions on a way forward for this? This PR as it is is backwards compatible and though it's not completely ideal, when I tried to refactor completely I came up against some issues. |
Anyone know what's the status for this one? Thanks |
I'm using a fork of this module, which only has this change. You can use it by running this command:
|
@domharrington Can you share the code for the approach you tried? I'm trying to understand what was your approach there. I'm thinking that a successful approach might need to make I'm not comfortable with the way it's implemented now, because having |
It's been over 2 months since I attempted it, so the code I put together at the time is probably lost to the ether or in a git stash somewhere, so I cannot remember what I tried. The fix I implemented makes having |
Any insight you can give into getting this merged would be amazing! I couldn't figure it out at the time. |
Hmm, not sure, will look into it and let you know |
@epicfaace @glasserc Is there anything else that needs to be done to this PR before it gets merged in? |
Closing this in favor of #1506 . |
Reasons for making this change
We're using this module to render forms for Swagger/OAS files. $refs
in swagger are stored in
components/schemas
at the top level of the object.This change allows $ref strings to not always start with
#/definitions/
which is a much easier solution than us having to recursively modify $refs
to prefix with this value.
All existing tests are passing, with a new test for this functionality.
The change was to make the definitions part of the regular expression an
optional non-capturing group.
Edit: any reason why you dont commit the package-lock.json file?
Edit 2: also, is there any main reason (apart from keeping the repo clean) why you don't commit the built source files (in lib/ and dist/?) This makes it tricky to
npm install
from a fork, aslib/index.js
doesn't exist in git.Checklist
npm run cs-format
on my branch to conform my code to prettier coding style