-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathmerge_schemas.go
More file actions
275 lines (227 loc) · 8.17 KB
/
merge_schemas.go
File metadata and controls
275 lines (227 loc) · 8.17 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package codegen
import (
"errors"
"fmt"
"maps"
"strings"
"github.com/getkin/kin-openapi/openapi3"
)
// MergeSchemas merges all the fields in the schemas supplied into one giant schema.
// The idea is that we merge all fields together into one schema.
func MergeSchemas(allOf []*openapi3.SchemaRef, path []string) (Schema, error) {
// If someone asked for the old way, for backward compatibility, return the
// old style result.
if globalState.options.Compatibility.OldMergeSchemas {
return mergeSchemasV1(allOf, path)
}
return mergeSchemas(allOf, path)
}
func mergeSchemas(allOf []*openapi3.SchemaRef, path []string) (Schema, error) {
n := len(allOf)
if n == 1 {
return GenerateGoSchema(allOf[0], path)
}
schema, err := valueWithPropagatedRef(allOf[0])
if err != nil {
return Schema{}, err
}
// Seed allOf[0]'s ref so that if s1's own AllOf contains a back-reference
// to itself, the cycle is detected during recursive merging.
seenTopLevel := make(map[string]bool)
if allOf[0].Ref != "" {
seenTopLevel[allOf[0].Ref] = true
}
for i := 1; i < n; i++ {
var err error
oneOfSchema, err := valueWithPropagatedRef(allOf[i])
if err != nil {
return Schema{}, err
}
seenSchemaRef := make(map[string]bool)
for k := range seenTopLevel {
seenSchemaRef[k] = true
}
if allOf[i].Ref != "" {
seenSchemaRef[allOf[i].Ref] = true
seenTopLevel[allOf[i].Ref] = true
}
schema, err = mergeOpenapiSchemas(schema, oneOfSchema, true, seenSchemaRef)
if err != nil {
return Schema{}, fmt.Errorf("error merging schemas for AllOf: %w", err)
}
}
return GenerateGoSchema(openapi3.NewSchemaRef("", &schema), path)
}
// valueWithPropagatedRef returns a copy of ref schema with its Properties refs
// updated if ref itself is external. Otherwise, return ref.Value as-is.
func valueWithPropagatedRef(ref *openapi3.SchemaRef) (openapi3.Schema, error) {
if len(ref.Ref) == 0 || ref.Ref[0] == '#' {
return *ref.Value, nil
}
pathParts := strings.Split(ref.Ref, "#")
if len(pathParts) < 1 || len(pathParts) > 2 {
return openapi3.Schema{}, fmt.Errorf("unsupported reference: %s", ref.Ref)
}
remoteComponent := pathParts[0]
// remote ref
schema := *ref.Value
for _, value := range schema.Properties {
if len(value.Ref) > 0 && value.Ref[0] == '#' {
// local reference, should propagate remote
value.Ref = remoteComponent + value.Ref
}
}
return schema, nil
}
func mergeAllOf(allOf []*openapi3.SchemaRef, seenSchemaRef map[string]bool) (openapi3.Schema, error) {
var schema openapi3.Schema
for _, schemaRef := range allOf {
var err error
if schemaRef.Ref != "" && seenSchemaRef[schemaRef.Ref] {
continue
}
if schemaRef.Ref != "" {
seenSchemaRef[schemaRef.Ref] = true
}
schema, err = mergeOpenapiSchemas(schema, *schemaRef.Value, true, seenSchemaRef)
if err != nil {
return openapi3.Schema{}, fmt.Errorf("error merging schemas for AllOf: %w", err)
}
}
return schema, nil
}
// mergeOpenapiSchemas merges two openAPI schemas and returns the schema
// all of whose fields are composed.
func mergeOpenapiSchemas(s1, s2 openapi3.Schema, allOf bool, seenSchemaRef map[string]bool) (openapi3.Schema, error) {
var result openapi3.Schema
result.Extensions = make(map[string]any, len(s1.Extensions)+len(s2.Extensions))
maps.Copy(result.Extensions, s1.Extensions)
// TODO: Check for collisions
maps.Copy(result.Extensions, s2.Extensions)
result.OneOf = append(s1.OneOf, s2.OneOf...)
// We are going to make AllOf transitive, so that merging an AllOf that
// contains AllOf's will result in a flat object.
var err error
if s1.AllOf != nil {
var merged openapi3.Schema
merged, err = mergeAllOf(s1.AllOf, seenSchemaRef)
if err != nil {
return openapi3.Schema{}, fmt.Errorf("error transitive merging AllOf on schema 1")
}
s1 = merged
}
if s2.AllOf != nil {
var merged openapi3.Schema
merged, err = mergeAllOf(s2.AllOf, seenSchemaRef)
if err != nil {
return openapi3.Schema{}, fmt.Errorf("error transitive merging AllOf on schema 2")
}
s2 = merged
}
result.AllOf = append(s1.AllOf, s2.AllOf...)
if s1.Type.Slice() != nil && s2.Type.Slice() != nil && !equalTypes(s1.Type, s2.Type) {
return openapi3.Schema{}, fmt.Errorf("can not merge incompatible types: %v, %v", s1.Type.Slice(), s2.Type.Slice())
}
result.Type = s1.Type
if s1.Format != s2.Format {
return openapi3.Schema{}, errors.New("can not merge incompatible formats")
}
result.Format = s1.Format
// For Enums, do we union, or intersect? This is a bit vague. I choose
// to be more permissive and union.
result.Enum = append(s1.Enum, s2.Enum...)
// I don't know how to handle two different defaults.
if s1.Default != nil || s2.Default != nil {
return openapi3.Schema{}, errors.New("merging two sets of defaults is undefined")
}
if s1.Default != nil {
result.Default = s1.Default
}
if s2.Default != nil {
result.Default = s2.Default
}
// We skip Example
// We skip ExternalDocs
// If two schemas disagree on any of these flags, we error out.
if s1.UniqueItems != s2.UniqueItems {
return openapi3.Schema{}, errors.New("merging two schemas with different UniqueItems")
}
result.UniqueItems = s1.UniqueItems
if s1.ExclusiveMin != s2.ExclusiveMin {
return openapi3.Schema{}, errors.New("merging two schemas with different ExclusiveMin")
}
result.ExclusiveMin = s1.ExclusiveMin
if s1.ExclusiveMax != s2.ExclusiveMax {
return openapi3.Schema{}, errors.New("merging two schemas with different ExclusiveMax")
}
result.ExclusiveMax = s1.ExclusiveMax
if s1.Nullable != s2.Nullable {
return openapi3.Schema{}, errors.New("merging two schemas with different Nullable")
}
result.Nullable = s1.Nullable
if s1.ReadOnly != s2.ReadOnly {
return openapi3.Schema{}, errors.New("merging two schemas with different ReadOnly")
}
result.ReadOnly = s1.ReadOnly
if s1.WriteOnly != s2.WriteOnly {
return openapi3.Schema{}, errors.New("merging two schemas with different WriteOnly")
}
result.WriteOnly = s1.WriteOnly
if s1.AllowEmptyValue != s2.AllowEmptyValue {
return openapi3.Schema{}, errors.New("merging two schemas with different AllowEmptyValue")
}
result.AllowEmptyValue = s1.AllowEmptyValue
// Required. We merge these.
result.Required = append(s1.Required, s2.Required...)
// We merge all properties
result.Properties = make(map[string]*openapi3.SchemaRef, len(s1.Properties)+len(s2.Properties))
maps.Copy(result.Properties, s1.Properties)
// TODO: detect conflicts
maps.Copy(result.Properties, s2.Properties)
if isAdditionalPropertiesExplicitFalse(&s1) || isAdditionalPropertiesExplicitFalse(&s2) {
result.WithoutAdditionalProperties()
} else if s1.AdditionalProperties.Schema != nil {
if s2.AdditionalProperties.Schema != nil {
return openapi3.Schema{}, errors.New("merging two schemas with additional properties, this is unhandled")
} else {
result.AdditionalProperties.Schema = s1.AdditionalProperties.Schema
}
} else {
if s2.AdditionalProperties.Schema != nil {
result.AdditionalProperties.Schema = s2.AdditionalProperties.Schema
} else {
if s1.AdditionalProperties.Has != nil || s2.AdditionalProperties.Has != nil {
result.WithAnyAdditionalProperties()
}
}
}
// Allow discriminators for allOf merges, but disallow for one/anyOfs.
if !allOf && (s1.Discriminator != nil || s2.Discriminator != nil) {
return openapi3.Schema{}, errors.New("merging two schemas with discriminators is not supported")
}
// For allOf merges, propagate a discriminator if only one schema has it.
// Merging two different discriminators is not supported.
if s1.Discriminator != nil && s2.Discriminator != nil {
return openapi3.Schema{}, errors.New("merging two schemas with discriminators is not supported")
}
if s1.Discriminator != nil {
result.Discriminator = s1.Discriminator
} else if s2.Discriminator != nil {
result.Discriminator = s2.Discriminator
}
return result, nil
}
func equalTypes(t1 *openapi3.Types, t2 *openapi3.Types) bool {
s1 := t1.Slice()
s2 := t2.Slice()
if len(s1) != len(s2) {
return false
}
// NOTE that ideally we'd use `slices.Equal` but as we're currently supporting Go 1.20+, we can't use it (yet https://github.com/oapi-codegen/oapi-codegen/issues/1634)
for i := range s1 {
if s1[i] != s2[i] {
return false
}
}
return true
}