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

Fix default values and matching schemas when oneOf / anyOf subschemas contain references #2272

Merged
merged 21 commits into from
Apr 13, 2021

Conversation

NixBiks
Copy link
Contributor

@NixBiks NixBiks commented Mar 12, 2021

Reasons for making this change

Atm. schemas are not able to match with the correct option based on the formData if the option schema have any references.

Fixes #2269
Branched out from PR #2270 since it is required

Checklist

  • I'm updating documentation
  • I'm adding or updating code
    • I've added and/or updated tests
    • I've updated docs if needed
  • I'm adding a new feature
    • I've updated the playground with an example use of the feature

@NixBiks
Copy link
Contributor Author

NixBiks commented Mar 14, 2021

@epicfaace thanks for this library first of all. I'm really digging it these days.

I don't like to poke you like this, but I just noticed that there are a lot of pull requests pending so I'm wondering if there is anything I can do to ease the acceptance of my PRs? This and #2270 since they'll bring a lot of value for my use case and anyone using FastAPI for their backend since it gives you auto/generated OpenAPI schema with references to each model schema (so references has to be supported).

@epicfaace
Copy link
Member

epicfaace commented Mar 14, 2021

I think it's really just maintainer bandwidth -- I'm the only person available who is able to regularly review PRs, and I can only work for so much on a volunteer basis (the problem with open source in general!) If you'd like to be a maintainer and help review PRs to clear out the queue, let me know 😄

@NixBiks
Copy link
Contributor Author

NixBiks commented Mar 15, 2021

I figured and totally understandable. I'm not sure what exactly it requires of me to be a maintainer. I'm quite new in the open-source world still but I might be able to help out (limited time for me as well).

On another note; I just noticed that this PR is highly related to #2245

@NixBiks NixBiks changed the title Infer matching schema from formData where refs are taking into account fix: default values and matching schema from formData when refs are part of the schema Mar 15, 2021
@NixBiks
Copy link
Contributor Author

NixBiks commented Mar 15, 2021

Integrated PR #2245 into this PR (copying some tests)

packages/core/test/anyOf_test.js Show resolved Hide resolved
packages/core/test/oneOf_test.js Show resolved Hide resolved
packages/core/test/oneOf_test.js Show resolved Hide resolved
packages/core/test/utils_test.js Show resolved Hide resolved
packages/core/src/utils.js Outdated Show resolved Hide resolved
@epicfaace epicfaace self-assigned this Mar 22, 2021
packages/core/src/validate.js Outdated Show resolved Hide resolved
packages/core/src/validate.js Outdated Show resolved Hide resolved
packages/core/src/validate.js Outdated Show resolved Hide resolved
* simplify withIdRefPrefix
* add comments
* improve error handling
@NixBiks
Copy link
Contributor Author

NixBiks commented Mar 24, 2021

I should've accounted for your directions now and merged the branch with the latest changes.

Should I squash the commits btw? It kinda blew up this progress

packages/core/src/validate.js Outdated Show resolved Hide resolved
packages/core/src/validate.js Outdated Show resolved Hide resolved
@epicfaace
Copy link
Member

Should I squash the commits btw? It kinda blew up this progress

Nope, doesn't matter -- we'll squash and merge anyway.

packages/core/src/validate.js Show resolved Hide resolved
packages/core/src/validate.js Outdated Show resolved Hide resolved
packages/core/src/validate.js Outdated Show resolved Hide resolved
packages/core/src/validate.js Outdated Show resolved Hide resolved
packages/core/src/validate.js Outdated Show resolved Hide resolved
packages/core/src/validate.js Show resolved Hide resolved
* Fixed bug with $ref being a property
* added tests for withIdRefPrefix
@NixBiks
Copy link
Contributor Author

NixBiks commented Mar 25, 2021

Might be ready now 😎

packages/core/src/validate.js Outdated Show resolved Hide resolved
// then we rewrite the schema ref's to point to the rootSchema using the id
// this accounts for the case where schema have references to models
// that lives in the rootSchema but not in the schema in question.
if (rootSchema) {
Copy link
Member

Choose a reason for hiding this comment

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

I think rootSchema is always given -- so maybe we can just remove this check.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right - except for some tests, but I've just updated those tests to add the schema as rootSchema as well

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Alternatively I could leave the tests as is and have this instead

return ajv
  .addSchema(rootSchema || schema, ROOT_SCHEMA_PREFIX)
  .validate(withIdRefPrefix(schema), data);

So if no rootSchema is provided then the schema itself will be used. Any preferences with one or the other?

Copy link
Member

Choose a reason for hiding this comment

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

I think it's better to just go ahead and update the tests rather than adding unnecessary functionality to this function.

* Check for # in ref
* update tests
@NixBiks
Copy link
Contributor Author

NixBiks commented Apr 3, 2021

Are we ready to merge this one?

@SimonKlausLudwig
Copy link

Would love to see this as well. Thanks for your work.

@epicfaace
Copy link
Member

Haven't taken a look yet -- will do soon

@gsstar9028
Copy link

please take a look soon

packages/core/test/validate_test.js Outdated Show resolved Hide resolved
packages/core/test/validate_test.js Outdated Show resolved Hide resolved
packages/core/test/anyOf_test.js Show resolved Hide resolved
@NixBiks
Copy link
Contributor Author

NixBiks commented Apr 13, 2021

Ready to go (fingers crossed)

Copy link
Member

@epicfaace epicfaace left a comment

Choose a reason for hiding this comment

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

Great, thanks for your work on this!

@epicfaace epicfaace changed the title fix: default values and matching schema from formData when refs are part of the schema Fix default values and matching schemas when oneOf / anyOf subschemas contain references Apr 13, 2021
@epicfaace epicfaace merged commit 86fbb03 into rjsf-team:master Apr 13, 2021
@NixBiks
Copy link
Contributor Author

NixBiks commented Apr 13, 2021

Wuhu thanks. My first actual code PR 🎊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Loading anyOf with initial data does not load the right option when using refs
4 participants