Repository navigation
Expand file tree
/
Copy pathdocument_remote_origin_test.go
More file actions
133 lines (127 loc) · 4.53 KB
/
Copy pathdocument_remote_origin_test.go
File metadata and controls
133 lines (127 loc) · 4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright 2026 Dave Shanley / Quobix
// SPDX-License-Identifier: MIT
package libopenapi
import (
"fmt"
"io"
"net/http"
"net/url"
"strings"
"sync"
"testing"
"github.com/pb33f/libopenapi/datamodel"
"github.com/pb33f/testify/require"
)
func TestRemoteNestedReferencesHonorBaseURL(t *testing.T) {
spec := []byte(`openapi: 3.1.0
info:
title: Remote origin regression
version: 1.0.0
paths: {}
components:
schemas:
Item:
$ref: './schemas/item.yaml#/components/schemas/Item'
`)
sources := map[string]string{
"https://api.example/schemas/item.yaml": "components:\n schemas:\n Item:\n type: object\n properties:\n id:\n $ref: 'http://types.example/id.yaml#/components/schemas/Id'\n",
"https://api.example/id.yaml": "components:\n schemas:\n Id:\n type: integer\n",
}
var mu sync.Mutex
calls := map[string]int{}
base, _ := url.Parse("https://api.example/")
doc, err := NewDocumentWithConfiguration(spec, &datamodel.DocumentConfiguration{SpecFilePath: "https://retrieval.example/root.yaml?token=private", BaseURL: base, ResolveNestedRefsWithDocumentContext: true, AllowRemoteReferences: true, RemoteURLHandler: func(location string) (*http.Response, error) {
mu.Lock()
defer mu.Unlock()
calls[location]++
body, ok := sources[location]
if !ok {
return nil, fmt.Errorf("unexpected source: %s", location)
}
return &http.Response{StatusCode: 200, Header: make(http.Header), Body: io.NopCloser(strings.NewReader(body))}, nil
}})
require.NoError(t, err)
defer doc.Release()
model, err := doc.BuildV3Model()
require.NoError(t, err)
item := model.Model.Components.Schemas.GetOrZero("Item").Schema()
require.NotNil(t, item)
id := item.Properties.GetOrZero("id").Schema()
require.NotNil(t, id)
require.Equal(t, []string{"integer"}, id.Type)
found, _ := model.Index.SearchIndexForReference("./schemas/item.yaml#/components/schemas/Item")
require.NotNil(t, found, "raw relative lookups must use the same remote identity as extraction")
mu.Lock()
defer mu.Unlock()
require.Equal(t, map[string]int{"https://api.example/schemas/item.yaml": 1, "https://api.example/id.yaml": 1}, calls)
}
func TestRemoteOriginsWithSamePathStayDistinct(t *testing.T) {
spec := []byte(`openapi: 3.1.0
info:
title: Distinct remote origins
version: 1.0.0
paths: {}
components:
schemas:
Left:
$ref: 'https://left.example/type.yaml'
Right:
$ref: 'https://right.example/type.yaml'
`)
doc, err := NewDocumentWithConfiguration(spec, &datamodel.DocumentConfiguration{AllowRemoteReferences: true, RemoteURLHandler: func(location string) (*http.Response, error) {
kind := ""
switch location {
case "https://left.example/type.yaml":
kind = "integer"
case "https://right.example/type.yaml":
kind = "string"
default:
return nil, fmt.Errorf("unexpected origin: %s", location)
}
return &http.Response{StatusCode: 200, Header: make(http.Header), Body: io.NopCloser(strings.NewReader("type: " + kind))}, nil
}})
require.NoError(t, err)
defer doc.Release()
model, err := doc.BuildV3Model()
require.NoError(t, err)
require.Equal(t, []string{"integer"}, model.Model.Components.Schemas.GetOrZero("Left").Schema().Type)
require.Equal(t, []string{"string"}, model.Model.Components.Schemas.GetOrZero("Right").Schema().Type)
}
func TestRemoteOriginCycleBuildsOncePerFile(t *testing.T) {
spec := []byte(`openapi: 3.1.0
info:
title: Remote origin cycle
version: 1.0.0
paths: {}
components:
schemas:
Item:
$ref: 'https://a.example/type.yaml'
`)
var mu sync.Mutex
calls := map[string]int{}
doc, err := NewDocumentWithConfiguration(spec, &datamodel.DocumentConfiguration{AllowRemoteReferences: true, RemoteURLHandler: func(location string) (*http.Response, error) {
mu.Lock()
calls[location]++
mu.Unlock()
next := ""
switch location {
case "https://a.example/type.yaml":
next = "https://b.example/type.yaml"
case "https://b.example/type.yaml":
next = "https://a.example/type.yaml"
default:
return nil, fmt.Errorf("unexpected source: %s", location)
}
body := fmt.Sprintf("type: object\nproperties:\n child:\n $ref: '%s'\n", next)
return &http.Response{StatusCode: 200, Header: make(http.Header), Body: io.NopCloser(strings.NewReader(body))}, nil
}})
require.NoError(t, err)
defer doc.Release()
model, err := doc.BuildV3Model()
require.NoError(t, err)
require.NotNil(t, model.Model.Components.Schemas.GetOrZero("Item").Schema())
mu.Lock()
defer mu.Unlock()
require.Equal(t, map[string]int{"https://a.example/type.yaml": 1, "https://b.example/type.yaml": 1}, calls)
}