-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocument_test.go
More file actions
95 lines (80 loc) · 3.49 KB
/
Copy pathdocument_test.go
File metadata and controls
95 lines (80 loc) · 3.49 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
package schemacompiler_test
import (
"context"
"strings"
"testing"
"github.com/stretchr/testify/require"
"github.com/ogen-go/schemacompiler"
"github.com/ogen-go/schemacompiler/plan"
)
func TestCompileRefDefinitions(t *testing.T) {
const schema = `{
"type": "object",
"properties": {"child": {"$ref": "#/$defs/Leaf"}},
"$defs": {"Leaf": {"type": "string"}}
}`
res, err := schemacompiler.Compile(context.Background(), []byte(schema), schemacompiler.Options{})
require.NoError(t, err)
// The document-level resolution graph must carry the referenced definition.
graph, ok := res.Plan.Resolution.(plan.StaticReferenceGraph)
require.True(t, ok, "expected StaticReferenceGraph, got %T", res.Plan.Resolution)
require.Len(t, graph.Definitions, 1, "one $ref target should become a definition")
for id, def := range graph.Definitions {
require.NotEmpty(t, id, "definition must have a SchemaID key")
prim, ok := def.Representation.(plan.PrimitiveRepresentation)
require.True(t, ok, "Leaf should be a primitive, got %T", def.Representation)
require.Equal(t, plan.KindString, prim.Kind)
require.Equal(t, plan.DirectGoType, def.Capability)
}
// The referencing field must point at the definition by name, not inline it.
obj, ok := res.Plan.Representation.(plan.ObjectRepresentation)
require.True(t, ok, "root should be an object, got %T", res.Plan.Representation)
child, ok := obj.Fields["child"]
require.True(t, ok, "root should have a child field")
ref, ok := child.Representation.(plan.ReferenceRepresentation)
require.True(t, ok, "child should be a reference, got %T", child.Representation)
_, inGraph := graph.Definitions[plan.SchemaID(ref.Name)]
require.True(t, inGraph, "child ref %q must resolve to a definition", ref.Name)
}
func TestCompileDanglingRef(t *testing.T) {
// A $ref to a missing target must not abort compilation: the rest of the document
// still yields a plan, and the dangling reference surfaces as an error diagnostic.
const schema = `{
"type": "object",
"properties": {"child": {"$ref": "#/$defs/Missing"}},
"$defs": {"Present": {"type": "string"}}
}`
res, err := schemacompiler.Compile(context.Background(), []byte(schema), schemacompiler.Options{})
require.NoError(t, err, "a dangling $ref must not fail Compile")
require.NotNil(t, res.Plan.Representation)
var found bool
for _, d := range res.Diagnostics {
if d.Severity == plan.SeverityError && strings.Contains(d.Message, "#/$defs/Missing") {
found = true
}
}
require.True(t, found, "expected an error diagnostic naming the dangling ref: %+v", res.Diagnostics)
}
func TestCompileGuardedRecursion(t *testing.T) {
// A linked list: guarded recursion (every cycle descends into a property), so it is
// representable and must not be flagged Unsupported.
const schema = `{
"$ref": "#/$defs/Node",
"$defs": {
"Node": {
"type": "object",
"properties": {"next": {"$ref": "#/$defs/Node"}}
}
}
}`
res, err := schemacompiler.Compile(context.Background(), []byte(schema), schemacompiler.Options{})
require.NoError(t, err)
graph, ok := res.Plan.Resolution.(plan.StaticReferenceGraph)
require.True(t, ok, "expected StaticReferenceGraph, got %T", res.Plan.Resolution)
require.NotEmpty(t, graph.Definitions, "recursive Node should be a definition")
for _, d := range res.Diagnostics {
require.NotEqual(t, plan.SeverityError, d.Severity,
"guarded recursion must not produce an error diagnostic: %s", d.Message)
}
require.NotEqual(t, plan.Unsupported, res.Capability, "guarded recursion is supported")
}