fix: preserve JSON Schema 2020-12 keyword siblings on $ref schemas for OAS 3.1+#2896
Open
aqeelat wants to merge 7 commits into
Open
fix: preserve JSON Schema 2020-12 keyword siblings on $ref schemas for OAS 3.1+#2896aqeelat wants to merge 7 commits into
aqeelat wants to merge 7 commits into
Conversation
…r 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 microsoft#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: microsoft#2895
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: microsoft#2895
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: microsoft#2895
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: microsoft#2895
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: microsoft#2895
Author
|
@microsoft-github-policy-service agree |
…bulary 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: microsoft#2895
…nstructor Achieves 100% diff coverage on all changed files. Ref: microsoft#2895
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
OpenApiV31Deserializer.LoadSchemashort-circuits on$refbeforeParseMap, so JSON Schema 2020-12 keyword siblings ($defs,$dynamicAnchor,$dynamicRef,$id,$schema,$anchor,$vocabulary,$comment) were never parsed into the object model. This made Pattern B (generic template +$dynamicRefbinding via sibling$defs) unimplementable for any tool built onMicrosoft.OpenApi.Fixes #2895.
Solution
Mirrors the #2369 annotation-sibling pattern across four coordinated changes:
SetAdditional31MetadataFromMapNodeextracts scalar/collection siblings;$defsis parsed inLoadSchema(needsLoadSchemafor nested schema materialization).JsonSchemaReference($idstored asSchemaIdto avoid collision withBaseOpenApiReference.Id).OpenApiSchemaReferencegetters changed fromTarget?.XtoReference.X ?? Target?.X.SerializeAdditionalV3XPropertiesemits the new siblings alongside$ref.Version-safe by call-site separation:
SetAdditional31MetadataFromMapNodeis only reachable from V31/V32LoadSchema, never V3. All 1639 existing tests pass unchanged.Scope
Intentionally narrow — only JSON Schema 2020-12 structural keywords (
$-prefixed). Application keywords (type,properties,required, etc.) still delegate toTarget. Full sibling compliance is a separate concern with backward-compat implications.Empty
$defs: {}/$vocabulary: {}siblings do not suppress the target's values (collection assigned only when non-empty).Test coverage
12 new tests across V31 and V32:
$dynamicAnchor+$defs), scalar siblings ($id,$schema,$comment,$anchor,$dynamicRef),$vocabulary,allOf-binding variant