Repository navigation
Expand file tree
/
Copy pathdocument_named_example_test.go
More file actions
75 lines (70 loc) · 2.47 KB
/
Copy pathdocument_named_example_test.go
File metadata and controls
75 lines (70 loc) · 2.47 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
package libopenapi
import (
"fmt"
"strings"
"testing"
"github.com/pb33f/libopenapi/datamodel"
"github.com/pb33f/testify/require"
)
func TestBuildV3Model_NamedExampleSchemaCycles(t *testing.T) {
for _, name := range []string{"example", "examples"} {
for _, container := range []string{"components/schemas", "definitions", "$defs", "dependentSchemas", "external"} {
t.Run(container+"/"+name, func(t *testing.T) {
prefix := "#/" + container + "/"
if container == "external" {
prefix = "#/"
}
schemas := fmt.Sprintf(`%s:
type: object
required: [child]
properties:
child: {$ref: '%sNode'}
Node:
type: object
required: [back]
properties:
back: {$ref: '%s%s'}
`, name, prefix, prefix, name)
header := "openapi: 3.1.1\ninfo: {title: Named schema cycles, version: '1'}\npaths: {}\n"
config := datamodel.NewDocumentConfiguration()
var source string
switch container {
case "external":
config = externalRegressionConfig(t, map[string]string{"schemas.yaml": schemas})
source = header + "components:\n schemas:\n Root: {$ref: './schemas.yaml#/" + name + "'}\n"
case "components/schemas":
source = header + "components:\n schemas:\n" + indentNamedSchema(schemas, 4)
default:
source = header + "components:\n schemas:\n Root: {$ref: '" + prefix + name + "'}\n" + container + ":\n" + indentNamedSchema(schemas, 2)
}
doc, err := NewDocumentWithConfiguration([]byte(source), config)
require.NoError(t, err)
_, err = doc.BuildV3Model()
require.ErrorContains(t, err, "infinite circular reference detected")
})
}
}
}
func indentNamedSchema(source string, spaces int) string {
indent := strings.Repeat(" ", spaces)
return indent + strings.ReplaceAll(strings.TrimSuffix(source, "\n"), "\n", "\n"+indent) + "\n"
}
func TestBuildV3Model_ExternalNamedExampleObject(t *testing.T) {
config := externalRegressionConfig(t, map[string]string{
"examples.yaml": "example:\n value:\n $ref: '#/payload-only'\n",
})
doc, err := NewDocumentWithConfiguration([]byte(`openapi: 3.1.1
info: {title: External example, version: '1'}
paths: {}
components:
examples:
External: {$ref: './examples.yaml#/example'}
`), config)
require.NoError(t, err)
model, err := doc.BuildV3Model()
require.NoError(t, err)
require.NotNil(t, model)
value := model.Model.Components.Examples.GetOrZero("External").Value
require.Equal(t, "$ref", value.Content[0].Value)
require.Equal(t, "#/payload-only", value.Content[1].Value)
}