-
Notifications
You must be signed in to change notification settings - Fork 6
Add anchor resolution + combinator resolution to fix Zowe 3.3 schema error #278
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ import Radio from '@mui/material/Radio'; | |
| import RadioGroup from '@mui/material/RadioGroup'; | ||
| import FormControlLabel from '@mui/material/FormControlLabel'; | ||
| import FormControl from '@mui/material/FormControl'; | ||
| import { sanitizeOldStyleAnchors } from './Utils'; | ||
|
|
||
| // Creates a basic input element in the UI schema | ||
| const createControl = (scope: string, label?: string) => ({ | ||
|
|
@@ -118,6 +119,7 @@ const findMatchingSchemaIndex = (formData: any, oneOfSchemas: any) => { | |
| }; | ||
|
|
||
| const makeUISchema = (schema: any, base: string, formData: any): any => { | ||
|
|
||
| if (!schema || !formData) { | ||
| return ""; | ||
| } | ||
|
|
@@ -185,6 +187,59 @@ const makeUISchema = (schema: any, base: string, formData: any): any => { | |
| return createVerticalLayout(elements); // Return whole structure | ||
| } | ||
|
|
||
| /* resolveCombinators takes a schema plus the current form data and collapses any oneOf/anyOf it finds. | ||
| For each combinator, it chooses the option that best matches the data (or index 0 as a fallback), | ||
| then replaces the whole combinator node with that chosen subschema and continues recursively | ||
| through properties/items/$defs, etc. | ||
|
|
||
| The result is schema without combinators, so JsonForms won’t render tabs as a result of multiple sources */ | ||
| const resolveCombinators = (schema: any, data: any): any => { | ||
| const resolvedSchema = JSON.parse(JSON.stringify(schema)); | ||
|
|
||
| const choose = (schemasArray: any[], formData: any) => { | ||
| // Find the matching schema for the given form data or default to 0 | ||
| return schemasArray[findMatchingSchemaIndex(formData ?? {}, schemasArray)] || schemasArray[0]; | ||
| }; | ||
|
|
||
| const traverse = (node: any, formData: any) => { | ||
| if (!node || typeof node !== 'object') return; | ||
|
|
||
| if (Array.isArray(node.oneOf)) { | ||
| const chosen = choose(node.oneOf, formData); | ||
| Object.keys(node).forEach(k => delete node[k]); | ||
| Object.assign(node, chosen); | ||
| } | ||
| if (Array.isArray(node.anyOf)) { | ||
| const chosen = choose(node.anyOf, formData); | ||
| Object.keys(node).forEach(k => delete node[k]); | ||
| Object.assign(node, chosen); | ||
| } | ||
|
|
||
| // descend down usual containerized tree | ||
| if (node.properties && typeof node.properties === 'object') { | ||
| for (const [k, v] of Object.entries(node.properties)) { | ||
| traverse(v, formData?.[k]); | ||
| } | ||
| } | ||
| for (const key of ['items','contains','if','then','else','not']) { | ||
| if (node[key]) { | ||
| traverse(node[key], formData); | ||
| } | ||
| } | ||
| for (const key of ['allOf','anyOf','oneOf','prefixItems']) { | ||
| if (Array.isArray(node[key])) { | ||
| node[key].forEach((x: any) => traverse(x, formData)); | ||
| } | ||
| } | ||
| if (node.$defs) { | ||
| Object.values(node.$defs).forEach((x: any) => traverse(x, undefined)); | ||
| } | ||
| }; | ||
|
|
||
| traverse(resolvedSchema, data); | ||
| return resolvedSchema; | ||
| } | ||
|
|
||
| export default function JsonForm(props: any) { | ||
| let {schema, onChange, formData} = props; | ||
|
|
||
|
|
@@ -209,6 +264,8 @@ export default function JsonForm(props: any) { | |
| onChange(formData, schemaIndex); | ||
| } | ||
|
|
||
| const sanitizedSchema = resolveCombinators(sanitizeOldStyleAnchors(requiredSchema), formData); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A few sentences summary of overall AJV/schema context for this issue woudl be good |
||
|
|
||
| return ( | ||
| <ThemeProvider theme={jsonFormTheme}> | ||
| { schema.oneOf && | ||
|
|
@@ -234,8 +291,8 @@ export default function JsonForm(props: any) { | |
|
|
||
| } | ||
| <JsonForms | ||
| schema={requiredSchema} | ||
| uischema={makeUISchema(requiredSchema, '/', formData)} | ||
| schema={sanitizedSchema} | ||
| uischema={makeUISchema(sanitizedSchema, '/', formData)} | ||
| data={formData} | ||
| renderers={materialRenderers} | ||
| cells={materialCells} | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Check that this method makes a decision based on different types of objects covered by combinator