Repository navigation
Expand file tree
/
Copy pathdebug.go
More file actions
129 lines (104 loc) · 3.08 KB
/
Copy pathdebug.go
File metadata and controls
129 lines (104 loc) · 3.08 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
package ir
import "github.com/MarkRosemaker/openapi"
// narrowUnspecified makes every value doc leaves open an object of no members: the empty schema, an array's missing
// items and a free-form object's values. Each then decodes into struct{} instead of any, so that a client generated
// in debug mode fails on whatever the specification does not describe yet, and records it for enrich to fill in.
//
// A union's alternatives are left as they are: what is open there is the union, not the specification.
func narrowUnspecified(doc *openapi.Document) {
seen := map[*openapi.Schema]bool{}
var walk func(s *openapi.Schema)
// value narrows s and walks into it, for a schema that stands for a value of its own
value := func(s *openapi.Schema) {
if s == nil {
return
}
switch {
case s.Ref != nil:
case isOpen(s) || isOpenBut(s):
s.Type = openapi.TypeObject
case s.Type == openapi.TypeArray && s.Items == nil:
s.Items = &openapi.Schema{Type: openapi.TypeObject}
case len(s.Properties) == 0 && s.AdditionalProperties != nil &&
(s.AdditionalProperties.Schema == nil && s.AdditionalProperties.Allowed || isOpen(s.AdditionalProperties.Schema)):
s.AdditionalProperties = nil
}
walk(s)
}
walk = func(s *openapi.Schema) {
if s == nil || seen[s] {
return
}
seen[s] = true
if s.Ref != nil {
walk(s.Ref.Value)
return
}
for _, p := range s.Properties {
value(p)
}
value(s.Items)
for _, p := range s.PrefixItems {
value(p)
}
if s.AdditionalProperties != nil {
value(s.AdditionalProperties.Schema)
}
// X in "X or null" stands for the value
if v := nullableVariant(s); v != nil {
value(v)
}
for _, l := range []openapi.SchemaList{s.AllOf, s.OneOf, s.AnyOf} {
for _, e := range l {
walk(e)
}
}
}
content := func(c openapi.Content) {
for _, mt := range c {
if mt != nil {
value(mt.Schema)
}
}
}
for _, s := range doc.Components.Schemas {
value(s)
}
for _, r := range doc.Components.Responses {
if r != nil && r.Value != nil {
content(r.Value.Content)
}
}
for _, rb := range doc.Components.RequestBodies {
if rb != nil && rb.Value != nil {
content(rb.Value.Content)
}
}
for _, p := range doc.Paths {
for _, op := range p.Operations {
if op.RequestBody != nil && op.RequestBody.Value != nil {
content(op.RequestBody.Value.Content)
}
for _, r := range op.Responses {
if r != nil && r.Value != nil {
content(r.Value.Content)
}
}
}
}
}
// isOpen reports whether s is the empty schema, which any value matches.
func isOpen(s *openapi.Schema) bool {
return s != nil && s.Ref == nil && s.Type == "" && len(s.Properties) == 0 && s.AdditionalProperties == nil &&
len(s.AllOf) == 0 && len(s.OneOf) == 0 && len(s.AnyOf) == 0 && s.Not == nil && len(s.Enum) == 0 &&
len(s.Const) == 0 && s.Items == nil && len(s.PrefixItems) == 0
}
// isOpenBut reports whether s is the empty schema but for a not, such as "not": {}, which no value matches.
func isOpenBut(s *openapi.Schema) bool {
if s == nil || s.Not == nil {
return false
}
c := *s
c.Not = nil
return isOpen(&c)
}