@@ -737,6 +737,31 @@ func TestUnderOpenAPIExamplePath(t *testing.T) {
737737 }
738738}
739739
740+ func TestUnderOpenAPIExamplePayloadPath (t * testing.T ) {
741+ tests := []struct {
742+ name string
743+ path []string
744+ want bool
745+ }{
746+ {"empty" , nil , false },
747+ {"example_payload" , []string {"paths" , "get" , "responses" , "200" , "content" , "application/json" , "schema" , "example" }, true },
748+ {"nested_under_example_payload" , []string {"components" , "schemas" , "Foo" , "example" , "nested" }, true },
749+ {"examples_collection" , []string {"components" , "examples" }, false },
750+ {"example_object_entry" , []string {"components" , "examples" , "ReusableExample" }, false },
751+ {"examples_value_payload" , []string {"content" , "application/json" , "examples" , "sample" , "value" }, true },
752+ {"examples_value_nested_payload" , []string {"content" , "application/json" , "examples" , "sample" , "value" , "nested" }, true },
753+ {"examples_data_value_payload" , []string {"components" , "examples" , "sample" , "dataValue" }, true },
754+ {"property_named_example" , []string {"components" , "schemas" , "Foo" , "properties" , "example" }, false },
755+ {"property_named_examples_value" , []string {"components" , "schemas" , "Foo" , "properties" , "examples" , "value" }, false },
756+ {"real_example_after_property_example" , []string {"components" , "schemas" , "Foo" , "properties" , "example" , "example" }, true },
757+ }
758+ for _ , tt := range tests {
759+ t .Run (tt .name , func (t * testing.T ) {
760+ assert .Equal (t , tt .want , underOpenAPIExamplePayloadPath (tt .path ))
761+ })
762+ }
763+ }
764+
740765func TestExtractRefs_InlineSchemaHelpers (t * testing.T ) {
741766 idx := NewTestSpecIndex ().Load ().(* SpecIndex )
742767 idx .specAbsolutePath = "test.yaml"
@@ -961,4 +986,87 @@ func TestExtractRefs_WalkHelpers(t *testing.T) {
961986 assert .False (t , shouldSkipMapSchemaCollection ([]string {"properties" , "example" }))
962987 assert .False (t , shouldSkipMapSchemaCollection ([]string {"patternProperties" , "example" }))
963988 assert .True (t , shouldSkipMapSchemaCollection ([]string {"x-test" }))
989+
990+ var noAppendNode yaml.Node
991+ _ = yaml .Unmarshal ([]byte ("summary: hello\n value: world" ), & noAppendNode )
992+ state .seenPath = []string {"components" , "examples" , "sample" }
993+ state .lastAppended = false
994+ idx .unwindExtractRefsPath (noAppendNode .Content [0 ], & state , 1 )
995+ assert .Equal (t , []string {"components" , "examples" , "sample" }, state .seenPath )
996+
997+ var appendNode yaml.Node
998+ _ = yaml .Unmarshal ([]byte ("value: hello\n next: world" ), & appendNode )
999+ state .lastAppended = true
1000+ idx .unwindExtractRefsPath (appendNode .Content [0 ], & state , 1 )
1001+ assert .Equal (t , []string {"components" , "examples" }, state .seenPath )
1002+ assert .False (t , state .lastAppended )
1003+ }
1004+
1005+ func TestExtractReferenceAt_IgnoresRefsInsideExamplePayloads (t * testing.T ) {
1006+ idx := NewTestSpecIndex ().Load ().(* SpecIndex )
1007+ idx .specAbsolutePath = "test.yaml"
1008+
1009+ var refNode yaml.Node
1010+ _ = yaml .Unmarshal ([]byte (`$ref: '#/components/schemas/Pet'` ), & refNode )
1011+
1012+ ref := idx .extractReferenceAt (refNode .Content [0 ], nil , 0 , []string {"components" , "examples" , "sample" , "value" }, nil , false , "" )
1013+ assert .Nil (t , ref )
1014+ assert .Empty (t , idx .GetAllReferences ())
1015+ assert .Empty (t , idx .GetAllSequencedReferences ())
1016+ }
1017+
1018+ func TestSpecIndex_ExtractRefs_ExampleObjectRefsIndexedButPayloadRefsIgnored (t * testing.T ) {
1019+ spec := `openapi: 3.2.0
1020+ info:
1021+ title: Example refs
1022+ version: 1.0.0
1023+ paths:
1024+ /widgets:
1025+ get:
1026+ responses:
1027+ "200":
1028+ description: ok
1029+ content:
1030+ application/json:
1031+ examples:
1032+ responseRef:
1033+ $ref: '#/components/examples/ReusableExample'
1034+ inlinePayload:
1035+ summary: payload example
1036+ value:
1037+ nested:
1038+ $ref: '#/components/schemas/ShouldNotIndex'
1039+ components:
1040+ examples:
1041+ ReusableExample:
1042+ $ref: '#/components/examples/LeafExample'
1043+ LeafExample:
1044+ summary: reusable
1045+ value:
1046+ ok: true
1047+ DataValueExample:
1048+ dataValue:
1049+ nested:
1050+ $ref: '#/components/schemas/ShouldNotIndexData'
1051+ schemas:
1052+ ShouldNotIndex:
1053+ type: object
1054+ ShouldNotIndexData:
1055+ type: object
1056+ `
1057+
1058+ var rootNode yaml.Node
1059+ _ = yaml .Unmarshal ([]byte (spec ), & rootNode )
1060+
1061+ idx := NewSpecIndexWithConfig (& rootNode , CreateOpenAPIIndexConfig ())
1062+
1063+ rawRefs := make (map [string ]bool )
1064+ for _ , ref := range idx .GetAllReferences () {
1065+ rawRefs [ref .RawRef ] = true
1066+ }
1067+
1068+ assert .True (t , rawRefs ["#/components/examples/ReusableExample" ])
1069+ assert .True (t , rawRefs ["#/components/examples/LeafExample" ])
1070+ assert .False (t , rawRefs ["#/components/schemas/ShouldNotIndex" ])
1071+ assert .False (t , rawRefs ["#/components/schemas/ShouldNotIndexData" ])
9641072}
0 commit comments