MCPcopy Create free account
hub / github.com/getkin/kin-openapi / TestReferencesComponentInRootDocument

Function TestReferencesComponentInRootDocument

openapi3/helpers_test.go:12–83  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

10)
11
12func TestReferencesComponentInRootDocument(t *testing.T) {
13 loader := NewLoader()
14 loader.IsExternalRefsAllowed = true
15
16 runAssertions := func(doc *T) {
17 // The element type of ./records.yml references a document which is also in the root document.
18 v, ok := ReferencesComponentInRootDocument(doc, doc.Components.Schemas["BookRecords"].Value.Items)
19 assert.True(t, ok)
20 assert.Equal(t, "#/components/schemas/BookRecord", v)
21
22 // The array element type directly references the component in the root document.
23 v, ok = ReferencesComponentInRootDocument(doc, doc.Components.Schemas["CdRecords"].Value.Items)
24 assert.True(t, ok)
25 assert.Equal(t, "#/components/schemas/CdRecord", v)
26
27 // A component from the root document should
28 v, ok = ReferencesComponentInRootDocument(doc, doc.Components.Schemas["CdRecord"])
29 assert.True(t, ok)
30 assert.Equal(t, "#/components/schemas/CdRecord", v)
31
32 // The error response component is in the root doc.
33 v, ok = ReferencesComponentInRootDocument(doc, doc.Paths.Find("/records").Get.Responses.Value("500"))
34 assert.True(t, ok)
35 assert.Equal(t, "#/components/responses/ErrorResponse", v)
36
37 v, ok = ReferencesComponentInRootDocument(doc, doc.Paths.Find("/records").Get.Responses.Value("500").Value.Content.Get("application/json").Schema)
38 assert.False(t, ok)
39 assert.Empty(t, v)
40
41 // Ref path doesn't include a './'
42 v, ok = ReferencesComponentInRootDocument(doc, doc.Paths.Find("/record").Get.Parameters[0])
43 assert.True(t, ok)
44 assert.Equal(t, "#/components/parameters/BookIDParameter", v)
45
46 v, ok = ReferencesComponentInRootDocument(doc, doc.Paths.Find("/record").Get.Responses.Value("200").Value.Content.Get("application/json").Examples["first-example"])
47 assert.True(t, ok)
48 assert.Equal(t, "#/components/examples/RecordResponseExample", v)
49
50 // Matches equivalent paths where string is no equal.
51 v, ok = ReferencesComponentInRootDocument(doc, doc.Paths.Find("/record").Get.Responses.Value("200").Value.Headers["X-Custom-Header"])
52 assert.True(t, ok)
53 assert.Equal(t, "#/components/headers/CustomHeader", v)
54
55 // Same structure distinct definition of the same header
56 v, ok = ReferencesComponentInRootDocument(doc, doc.Paths.Find("/record").Get.Responses.Value("200").Value.Headers["X-Custom-Header2"])
57 assert.False(t, ok)
58 assert.Empty(t, v)
59 }
60
61 // Load from the file system
62 doc, err := loader.LoadFromFile("testdata/refsToRoot/openapi.yml")
63 require.NoError(t, err)
64
65 runAssertions(doc)
66
67 // Loading from a URL by mocking HTTP calls.
68 // Loads the data using the URI path from the testdata/ folder.
69 loader.ReadFromURIFunc = func(loader *Loader, url *url.URL) ([]byte, error) {

Callers

nothing calls this directly

Calls 8

LoadFromFileMethod · 0.95
LoadFromURIMethod · 0.95
NewLoaderFunction · 0.85
ReadFromFileFunction · 0.85
FindMethod · 0.80
ValueMethod · 0.45
GetMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…