fix: preserve JSON Schema 2020-12 keyword siblings on $ref schemas for OAS 3.1+ (#2896)#2905
Merged
Merged
Conversation
…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>
Code Coverage OverviewLanguages: C# C# / code-coverage/dotnetThe overall coverage in the branch is 41%. The coverage in the branch is 88%. Show a code coverage summary of the most impacted files.
Code Coverage is in Public Preview. Learn more and provide us with your feedback. |
|
peombwa
approved these changes
Jun 26, 2026
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.



reflection of #2896 to v2