Skip to content

fix: preserve JSON Schema 2020-12 keyword siblings on $ref schemas for OAS 3.1+ (#2896)#2905

Merged
baywet merged 1 commit into
support/v2from
fix/missing-ref-siblings-to-v2
Jun 26, 2026
Merged

fix: preserve JSON Schema 2020-12 keyword siblings on $ref schemas for OAS 3.1+ (#2896)#2905
baywet merged 1 commit into
support/v2from
fix/missing-ref-siblings-to-v2

Conversation

@baywet

@baywet baywet commented Jun 26, 2026

Copy link
Copy Markdown
Member

reflection of #2896 to v2

…r OAS 3.1+ (#2896)

* fix: preserve JSON Schema 2020-12 keyword siblings on $ref schemas for OAS 3.1+

OpenApiV31Deserializer.LoadSchema short-circuits on $ref before
ParseMap, so sibling keywords ($defs, $dynamicAnchor, $dynamicRef,
$id, $anchor, $vocabulary, $comment) were never parsed into the
object model. This made Pattern B (generic template + binding)
unimplementable for any tool built on Microsoft.OpenApi.

The fix mirrors the #2369 annotation-sibling pattern across four
coordinated changes:
- Parser extraction in SetAdditional31MetadataFromMapNode (scalars
  + $vocabulary) and LoadSchema ($defs, which needs LoadSchema for
  nested schema materialization)
- Storage: 7 new properties on JsonSchemaReference
- Accessor overrides on OpenApiSchemaReference (Reference.X ??
  Target?.X)
- Serialization in SerializeAdditionalV3XProperties

Version-safe by call-site separation: SetAdditional31MetadataFromMapNode
is only reachable from V31/V32 LoadSchema, never V3.

Ref: #2895

* fix: align scalar extraction pattern with existing Title convention

Switch from GetPropertyValueFromNode(...) ?? X to the
if (!string.IsNullOrEmpty(...)) pattern used by the existing
Title/annotation extraction, for reviewer consistency.

Also add test for the allOf-based binding variant where $defs
sits inside allOf[0] and the nested schema has $ref +
$dynamicAnchor (the pattern from the blocker analysis).

Ref: #2895

* fix: add $schema sibling preservation and fix $defs location tracking

Add $schema dialect URI as a sibling override on JsonSchemaReference,
matching the pattern used for the other JSON Schema 2020-12 keywords.

Also fix the $defs parsing loop in V31/V32 LoadSchema to push/pop
the parsing context location stack (context.StartObject/EndObject)
around each LoadSchema call, mirroring JsonNodeHelper.CreateMap.
Without this, nested schemas inside a reference's $defs get
incorrect nodeLocation values, breaking relative $ref resolution
and source-pointer diagnostics.

Adds a scalar round-trip test covering $id, $schema, $comment,
$anchor, $dynamicRef serialization.

Ref: #2895

* test: add V32 sibling preservation tests mirroring V31

Mirrors the 6 V31 sibling preservation tests in V32Tests, using
SerializeAsV32 for the round-trip tests. Parse tests are identical
since both versions share the same LoadSchema +
SetAdditional31MetadataFromMapNode path.

Ref: #2895

* fix: don't suppress target values when sibling collections are empty

An empty $defs: {} or $vocabulary: {} sibling would assign an empty
collection to the reference, blocking fallthrough to Target via the
?? coalescing getter. Only assign when the collection has entries.

Ref: #2895

* test: add empty-collection fallthrough, 3.0 version safety, and $vocabulary round-trip tests

- Empty $defs: {} / $vocabulary: {} must fall through to Target
  (guards the .Count > 0 fix in commit 4666f2c)
- 3.0 document with $ref + siblings must drop siblings per spec
  (guards the version-safety guarantee)
- $vocabulary round-trip (parse -> serialize -> parse)

Ref: #2895

* test: add CreateShallowCopy test to cover JsonSchemaReference copy constructor

Achieves 100% diff coverage on all changed files.

Ref: #2895

* fix: address PR review feedback

- Replace OpenApiSpecVersion branching with Action<IOpenApiWriter,
  IOpenApiSerializable> callback for future-proof serialization
- Add $defs context segment + try/catch error handling in V31/V32
  $defs parsing (mirrors ParseField error pattern)
- Convert all FluentAssertions calls to Assert.* per repo convention

Ref: #2896

* chore: refactor to avoid duplication iterating on schemas

Signed-off-by: Vincent Biret <vibiret@microsoft.com>

* tests: reduce the usage of bang operators

Signed-off-by: Vincent Biret <vibiret@microsoft.com>

* tests: use assertions instead of null forgiving to make investigations easier

Signed-off-by: Vincent Biret <vibiret@microsoft.com>

---------

Signed-off-by: Vincent Biret <vibiret@microsoft.com>
Co-authored-by: Vincent Biret <vibiret@microsoft.com>
@baywet baywet requested a review from a team as a code owner June 26, 2026 17:08
@baywet baywet self-assigned this Jun 26, 2026
@baywet baywet enabled auto-merge June 26, 2026 17:09
@github-code-quality

Copy link
Copy Markdown

Code Coverage Overview

Languages: C#

C# / code-coverage/dotnet

The overall coverage in the branch is 41%. The coverage in the branch is 88%.

Show a code coverage summary of the most impacted files.
File 26c2358 c79a83e +/-
/home/runner/wo...MapParameter.cs 100% 0% -100%
/home/runner/wo...Deserializer.cs 81% 0% -81%
/home/runner/wo...pyReferences.cs 63% 31% -32%
/home/runner/wo...piJsonReader.cs 100% 72% -28%
/home/runner/wo...Deserializer.cs 90% 62% -28%
/home/runner/wo...s/OpenApiTag.cs 100% 76% -24%
/home/runner/wo...Deserializer.cs 100% 81% -19%
/home/runner/wo...Deserializer.cs 96% 81% -15%
/home/runner/wo...Deserializer.cs 97% 89% -8%
/home/runner/wo...piComponents.cs 94% 87% -7%

Code Coverage is in Public Preview. Learn more and provide us with your feedback.

@sonarqubecloud

Copy link
Copy Markdown

@baywet baywet merged commit fb20d46 into support/v2 Jun 26, 2026
17 checks passed
@baywet baywet deleted the fix/missing-ref-siblings-to-v2 branch June 26, 2026 18:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants