forked from oapi-codegen/oapi-codegen-exp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenum_oneof.go
More file actions
66 lines (61 loc) · 1.76 KB
/
enum_oneof.go
File metadata and controls
66 lines (61 loc) · 1.76 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
package codegen
import (
"github.com/pb33f/libopenapi/datamodel/high/base"
)
// constOneOfItem is one branch of an OpenAPI 3.1 enum-via-oneOf schema.
// It captures the per-value name (from `title`), the raw enum value
// (stringified from `const`), and the doc comment (from `description`).
type constOneOfItem struct {
Title string
Value string
Doc string
}
// isConstOneOfEnum reports whether a schema matches the OpenAPI 3.1
// enum-via-oneOf idiom:
//
// type: integer|string
// oneOf:
// - { title: NAME, const: VALUE, description?: TEXT }
// - ...
//
// All members must carry both `title` and `const`, and no member may itself
// be a composition (oneOf/allOf/anyOf) or declare properties. The outer
// schema's primary type must be a scalar (string or integer).
//
// When the idiom matches, the per-branch values are returned in declaration
// order. Otherwise returns (nil, false).
func isConstOneOfEnum(schema *base.Schema) ([]constOneOfItem, bool) {
if schema == nil || len(schema.OneOf) == 0 {
return nil, false
}
primary := getPrimaryType(schema)
if primary != "string" && primary != "integer" {
return nil, false
}
items := make([]constOneOfItem, 0, len(schema.OneOf))
for _, proxy := range schema.OneOf {
if proxy == nil {
return nil, false
}
m := proxy.Schema()
if m == nil {
return nil, false
}
if m.Title == "" || m.Const == nil {
return nil, false
}
// Members must be simple scalar-const schemas, not nested composition.
if len(m.OneOf) > 0 || len(m.AllOf) > 0 || len(m.AnyOf) > 0 {
return nil, false
}
if m.Properties != nil && m.Properties.Len() > 0 {
return nil, false
}
items = append(items, constOneOfItem{
Title: m.Title,
Value: m.Const.Value,
Doc: m.Description,
})
}
return items, true
}