← 输入框|github.com|刷新
Skip to content

Disable fetch delegate on $ref feature so external references are not allowed in Test-Json schema - #28214

Closed
Aditya Patwardhan (adityapatwardhan) wants to merge 1 commit into
masterfrom
testJson
Closed

Aditya Patwardhan (adityapatwardhan) wants to merge 1 commit into
masterfrom
testJson

Conversation

@adityapatwardhan

Copy link
Copy Markdown
Member

PR Summary

This pull request enhances the Test-Json PowerShell cmdlet by adding support for resolving external JSON schema references, improving error handling, and expanding test coverage. The main changes introduce a new parameter to control external reference fetching, ensure per-invocation isolation of this behavior, and provide clearer error messages and robust tests for various schema reference scenarios.

New functionality and parameterization:

  • Added a new -FetchExternalReference switch parameter to Test-Json, allowing users to opt-in to automatically resolve external (file or URI) references in JSON schemas. By default, external references are not resolved, improving security and predictability.
  • Refactored schema evaluation to use an instance-specific EvaluationOptions, ensuring that the external reference fetching behavior is isolated to each cmdlet invocation and does not affect subsequent calls. [1] [2] [3]

Error handling improvements:

  • Added explicit handling for unresolved external schema references, emitting a clear error message and a new error ID (UnresolvedJsonSchemaRef) when external references cannot be resolved and the new parameter is not set. [1] [2]

Testing and validation:

  • Significantly expanded Pester test coverage for Test-Json, including scenarios for schemas with local references, external file references, and external URI references, both with and without the new parameter. Tests also verify that setting -FetchExternalReference does not persist across invocations. [1] [2]
  • Added and updated schema asset files to support new and existing test cases, including valid and invalid schemas with various reference types. [1] [2] [3] [4] [5]

PR Context

PR Checklist

Copilot AI balanced review requested due to automatic review settings October 14, 2026 23:01
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

@adityapatwardhan Aditya Patwardhan (adityapatwardhan) added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Oct 14, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

HTTP tests need controlled endpoints, and reference validation needs negative-instance coverage.

2 open findings
What changed in this PR

Makes external schema fetching opt-in for Test-Json, while preserving internal references and isolating settings per invocation.

Changes:

  • Adds -FetchExternalReference and invocation-specific evaluation options.
  • Adds unresolved-reference diagnostics.
  • Expands reference tests and schema fixtures.
File Description
test/​powershell/​Modules/​Microsoft.PowerShell.Utility/​Test-Json.Tests.ps1 Tests reference handling and invocation isolation.
test/​powershell/​Modules/​Microsoft.PowerShell.Utility/​assets/​valid_schema.json Adds a reference-free schema.
test/​powershell/​Modules/​Microsoft.PowerShell.Utility/​assets/​valid_schema_uri_references_external.json Adds an HTTP reference fixture.
test/​powershell/​Modules/​Microsoft.PowerShell.Utility/​assets/​valid_schema_references_internal.json Adds internal reference coverage.
test/​powershell/​Modules/​Microsoft.PowerShell.Utility/​assets/​valid_schema_references_external.json Adds external file references.
test/​powershell/​Modules/​Microsoft.PowerShell.Utility/​assets/​valid_schema_definitions_external.json Supplies external schema definitions.
test/​powershell/​Modules/​Microsoft.PowerShell.Utility/​assets/​invalid_schema.json Makes schema invalidity independent of references.
src/​Microsoft.PowerShell.Commands.Utility/​resources/​TestJsonCmdletStrings.resx Adds unresolved-reference guidance.
src/​Microsoft.PowerShell.Commands.Utility/​commands/​utility/​TestJsonCommand.cs Implements opt-in fetching and error handling.

🧠 Review effort: Balanced


💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +273 to +275
It "Json is valid, with FetchExternalReference parameter, against a schema from file with external file reference" {
Test-Json -Json $validJson -SchemaFile $validSchemaWithExternalFileRefJsonPath -FetchExternalReference | Should -BeTrue
}
}

It "Json is valid but unresolvable, with FetchExternalReference parameter, against a schema with external URI reference" {
Test-Json -Json $validJson -SchemaFile $validSchemaWithExternalUriRefJsonPath -FetchExternalReference -ErrorVariable ErrorVar -ErrorAction SilentlyContinue | Should -BeFalse
@iSazonov

Copy link
Copy Markdown
Collaborator

Dup #28205

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

Labels

CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants