try-json: Fix results to avoid false positives for constructs like OneOf or AnyOf by sotteson1 · Pull Request #24577 · PowerShell/PowerShell
PR Summary
Fixes #21471
When calling the API to get the results against the schema, ask for the results in hierarchical format. Then walk down the tree and skip any nodes and its children if IsValid is true.
PR Context
See #21471
When using a construct like OneOf, each json node that uses it will have results nodes like this:
OneOf - true
--Choice1 - false
--Choice2- true
OneOf - true
--Choice1 - true
--Choice2- false
The 'false' items aren't actually failures. They're just the result of checking each OneOf choice. But if you then add a node where the OneOf actually fails:
OneOf - false
--Choice1 - false
--Choice2- false
When you call try-json with the existing code, it includes all the "false" choices even if the parent was "true". This leads to a bunch of false positives and can make so much noise as to make try-json unsuable. The fix is to ask for the results in hierarchical format instead of list format. Then walk down the tree and skip any node and its children if IsValid is true.
Some real-world output:
Before the fix (the only invalid json node is /Devices/3):
Test-Json: The JSON is not valid with the schema: Expected 1 matching subschema but found 0 at '/Devices/3' Test-Json: The JSON is not valid with the schema: Required properties ["os"] are not present at '/Devices/0' Test-Json: The JSON is not valid with the schema: Required properties ["arch"] are not present at '/Devices/1' Test-Json: The JSON is not valid with the schema: Required properties ["os"] are not present at '/Devices/2' Test-Json: The JSON is not valid with the schema: Required properties ["arch"] are not present at '/Devices/3' Test-Json: The JSON is not valid with the schema: Expected "\u0022smartphone\u0022" at '/Devices/0/deviceType' Test-Json: The JSON is not valid with the schema: Expected "\u0022laptop\u0022" at '/Devices/1/deviceType' Test-Json: The JSON is not valid with the schema: Expected "\u0022smartphone\u0022" at '/Devices/2/deviceType' Test-Json: The JSON is not valid with the schema: Value should match one of the values specified by the enum at '/Devices/3/os' Test-Json: The JSON is not valid with the schema: Expected "\u0022laptop\u0022" at '/Devices/3/deviceType'
After the fix it correctly only shows errors from /Devices/3:
Test-Json: The JSON is not valid with the schema: Expected 1 matching subschema but found 0 at '/Devices/3' Test-Json: The JSON is not valid with the schema: Value should match one of the values specified by the enum at '/Devices/3/os' Test-Json: The JSON is not valid with the schema: Required properties ["arch"] are not present at '/Devices/3' Test-Json: The JSON is not valid with the schema: Expected "\u0022laptop\u0022" at '/Devices/3/deviceType'
PR Checklist
- PR has a meaningful title
- Use the present tense and imperative mood when describing your changes
- Summarized changes
- Make sure all
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright header - This PR is ready to merge and is not Work in Progress.
- If the PR is work in progress, please add the prefix
WIP:or[ WIP ]to the beginning of the title (theWIPbot will keep its status check atPendingwhile the prefix is present) and remove the prefix when the PR is ready.
- If the PR is work in progress, please add the prefix
- Breaking changes
- None
- OR
- Experimental feature(s) needed
- Experimental feature name(s):
- User-facing changes
- Not Applicable
- OR
- Documentation needed
- [ ] Issue filed:
- Testing - New and feature
- N/A or can only be tested interactively
- OR
- Make sure you've added a new test if existing tests do not effectively test the code changed
- Tooling
- I have considered the user experience from a tooling perspective and don't believe tooling will be impacted.
- OR
- I have considered the user experience from a tooling perspective and opened an issue in the relevant tool repository. This may include:
- Impact on PowerShell Editor Services which is used in the PowerShell extension for VSCode
(which runs in a different PS Host).- Issue filed:
- Impact on Completions (both in the console and in editors) - one of PowerShell's most powerful features.
- Issue filed:
- Impact on PSScriptAnalyzer (which provides linting & formatting in the editor extensions).
- Issue filed:
- Impact on EditorSyntax (which provides syntax highlighting with in VSCode, GitHub, and many other editors).
- Issue filed:
- Impact on PowerShell Editor Services which is used in the PowerShell extension for VSCode