Repository navigation
fix(index): treat $ref inside schema instance values as data - #708
Open
Tjmstudios wants to merge 1 commit into
Open
Tjmstudios wants to merge 1 commit into
Tjmstudios wants to merge 1 commit into
Conversation
The values of enum, const and default, and the elements of a schema's examples array, are instance data. An object there with a $ref key is a value, not a reference. The index extracted it anyway and the resolver tried to resolve it, so a valid document failed to build with errors such as "component `#/nope` does not exist in the specification". Skip those values in the index walk, in extractReferenceAt and in the resolver's relative walk. A keyword only counts where it is a field name: a property named "default", a response keyed "default" or an Example Object map called "examples" is still walked. The resolver also walked x- extensions when ExcludeExtensionRefs was set, so it reported references the index had deliberately skipped. It now skips extension keys too. A property named x-... is still a schema.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #708 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 315 315
Lines 39758 39788 +30
=========================================
+ Hits 39758 39788 +30
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Contributor
Author
|
#710 now overlaps with this one. It applies the same "data, not schema" rule to |
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.
Summary
A
$refkey inside a schema'senum,const,defaultorexamplesarray is instance data, not a reference. The index and resolver treated it as a reference, so documents that carry such values failed to build.Also, when
ExcludeExtensionRefsis set, the resolver now skipsx-extensions, as the index already does.Problem
On
main,BuildV3Modelfails with:None of those are references. In JSON Schema 2020-12,
enum,const,defaultandexampleshold instance values, and$refis only a keyword inside a schema. OpenAPI 3.2 uses the same dialect. The index walk does not depend on the version, soenumanddefaulthit the same errors in OpenAPI 3.0, whose Schema Object also treats them as values.With
ExcludeExtensionRefs: true, the index does not extract references underx-keys, but the resolver's relative walk still followed them and reported errors for them.Fix
underSchemaInstanceValuePathrecognizes a path inside one of those values. The index walk (walkChildExtractRefs,extractReferenceAt) and the resolver walk (extractRelativesWithState) stop there. This works the same way as the existing example-payload checks.isOpenAPIKeywordSegment, factored out ofisOpenAPIExampleKeywordSegment, rejects names the document chose. So a property nameddefault, thedefaultresponse, and an OpenAPIexamplesmap of Example Objects are still walked.examplescounts only when it holds an array.extractRelativesWithStateskipsx-keys whenExcludeExtensionRefsis set. A property namedx-nameis still a schema and is still walked.Tests
TestUnderSchemaInstanceValuePathandTestIsOpenAPIKeywordSegment_OutOfRange: path classification, including properties, responses and Example Object maps named like the keywords.TestExtractReferenceAt_IgnoresRefsInsideSchemaInstanceValuesandTestSpecIndex_ExtractRefs_SchemaInstanceValueRefsIgnored: index extraction.TestResolver_SchemaInstanceValueRefsNotResolvedandTestResolver_ExcludeExtensionRefs: resolver walk, with exclusion on and off.TestDocument_BuildV3Model_RefInSchemaInstanceValue: the document above builds, anddefaultandconstdecode to their literal$refobjects.Run against
main, the document, index and resolver tests above fail with the errors shown in Problem, and the two classification tests do not compile, since they cover new helpers.go test ./...passesgo test -race . ./index ./datamodel/... ./utilspasses.