-
Notifications
You must be signed in to change notification settings - Fork 19
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
Use anyOf
when @allow-other = 'yes'
in JSON Schema
#360
Conversation
When there is a type and an enumeration in JSON Schema, using `allOf` means that a value must satisfy _both_ types. This is appropriate when `@allow-other = 'no'` because it effectively limits the permitted values to the enumeration; however, when `@allow-other = 'yes'`, that limitation means that you are not able to use 'locally defined' values. In this case, `anyOf` is preferable, this allows values in the enumeration and otherwise any value that matches the schema for the type. A check is added explicitly for `@allow-other = 'yes'` as `@allow-other = 'no'` is the default and matches the existing behavior.
Differences in the |
@wendellpiez , @aj-stein-nist , and @kylelaker Do we need a new unit test to check for this? |
I am looking into this more thoroughly but I am not sure what we had intended but I checked the following test (and intentionally mangled it locally to see the test generated correctly. Given this test Metaschema module below, the following JSON schema is desired for the I see how you can code around it with your PR, but this means in OSCAL something is being missed and the following code is not handling a certain condition the way it should. metaschema/toolchains/xslt-M4/schema-gen/make-json-schema-metamap.xsl Lines 329 to 338 in 7008554
In short, this is short-circuiting that code to avoid the bug. I guess this is an approach, but it calls into question why the test works in Metaschema but fails in the OSCAL case. I have investigated this locally on and off this evening, but I want to track down the root cause before approving this or recommending a different course of action (would also like Wendell's take on this). Apologies for the delays, but I will continue to look into 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.
@kylelaker, thanks for the PR. You can disregard my previous comments and we paired as team. It comes full circle, I now better understand why this change has to happen.
It seems we have been testing constraints on them being closed, loose, or totally open on fields, not on flags. You have fixed the part of the code we did not address in some previous work that missed this case after updating several enhancements in develop, so we really need to add some tests to the schema generation suite to address both flags and fields.
So in summation:
- Great work! 🎉
- You indicate some area of the test coverage I missed in previous work. 😢
- One of us needs to write tests: do you want to do it, I do it, or we work on it together? Let me know!
Thanks for the feedback. To get started with, I will work on integrating the specific feedback you provided in the next day or so. As far as writing tests, I am going to be 100% honest here and say that I am not really sure how to go about that... I neglected looking into that during the original PR. I'm happy to read and guess at it but if you want it done well/quickly it'd probably best to either have you do it or to at least collaborate. |
I am fine either way, if you want to collaborate on this, what is your week looking like? I would love to collaborate on community-authored tests (whether I drive or back-seat drive). And if that is too much for this week, totally understandable, I just PR recommended tests to your branch and we go through them? |
OK I guess I should get started on those tests then. :-) |
Co-authored-by: A.J. Stein <[email protected]>
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 am ok with the changes but will attempt to add relevant tests to ensure this is working in another PR.
Agree with @aj-stein-nist . The changes look fine in the view and may not be likely to break anything 'worse', but the only way to prevent further thrashing is to prove out the full range of cases (with and without |
We do actually test for that, but we don't test the different scenarios for allowed-values for both fields and flags, that was the but I missed. I'll start work on it (albeit outside this PR, in a separate one). |
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.
OK, so this is a fun teachable moment. @david-waltermire-nist, please revert the PR back as-is when Kyle originally submitted the PR (in commit 6fb8a74
) and I provided stylistic feedback. By doing that, I introduced a subtle bug and found it through testing #380. When testing some of the variations with the flag, I realized that we had inadvertently taken my recommendation to collapse the second if clause (or when
rather and that is distinct but different. With #380 I now have coverage and noticed some errors I couldn't resolve until going back to the make-json-schema-metamap.xsl
in the original.
I could find clever refactors or what not, but the real go of my previous review was to ensure I understood this submission by Kyle through tests and verify it, and in adding stuff I broke his original submission. So let's go back with that, and also add the tests with it or after. I leave that to your discretion. :-)
OK, I found another edge case while touching up my tests and I cannot easily recommend the change or commit directly to Kyle's PR. I am going to recommend we close this one out, I pull in his change, and also update the relevant code in a part Kyle didn't notice to address the edge case in both places. Please a revised #380 with updated comments shortly. |
Previously in usnistgov#360, @kylelaker reported a needed improvement to fix a regression for usnistgov/OSCAL#1773. While I had been completing testing in usnistgov#360 and properly expanding test coverage to address how we handle enumerations based on allow-other and target attributes for field and flag elements in Metaschema (both act similarly but subtly different with more scenarios for field), I observed my test data instances needed further correction in a way that cannot be address in the PR by Kyle. This merges the changes from the commit reffed by the URL below, adds my enhancement, tests, and all examples that helped me uncover missing code. usnistgov@6fb8a74 Co-authored-by: Kyle Laker <[email protected]>
* Correct field and flag enumerations in JSON schemas. Previously in #360, @kylelaker reported a needed improvement to fix a regression for usnistgov/OSCAL#1773. While I had been completing testing in #360 and properly expanding test coverage to address how we handle enumerations based on allow-other and target attributes for field and flag elements in Metaschema (both act similarly but subtly different with more scenarios for field), I observed my test data instances needed further correction in a way that cannot be address in the PR by Kyle. This merges the changes from the commit reffed by the URL below, adds my enhancement, tests, and all examples that helped me uncover missing code. 6fb8a74 * Surpress bottlecaps.de link fail More info below: https://github.com/usnistgov/metaschema/actions/runs/5228171226/jobs/9440341454 Co-authored-by: Kyle Laker <[email protected]>
Committer Notes
When there is a type and an enumeration in JSON Schema, using
allOf
means that a value must satisfy both types. This is appropriate when
@allow-other = 'no'
because it effectively limits the permitted valuesto the enumeration; however, when
@allow-other = 'yes'
, thatlimitation means that you are not able to use 'locally defined' values.
In this case,
anyOf
is preferable, this allows values in theenumeration and otherwise any value that matches the schema for the
type.
A check is added explicitly for
@allow-other = 'yes'
as@allow-other = 'no'
is the default and matches the existing behavior.This follows the pattern used in #253.
metaschema/toolchains/xslt-M4/schema-gen/make-json-schema-metamap.xsl
Lines 328 to 352 in 7008554
Closes: usnistgov/OSCAL#1773
All Submissions:
Changes to Core Features:
Have you written new tests for your core changes, as applicable?@aj-stein-nist will do this in a follow-on PRHave you included examples of how to use your new feature(s)?Have you updated all website](https://pages.nist.gov/metaschema) and readme documentation affected by the changes you made? Changes to the website can be made in the website/content directory of your branch.