forked from danielgtaylor/huma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
openapi.go
164 lines (139 loc) · 4.62 KB
/
openapi.go
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
package huma
import (
"fmt"
"reflect"
"github.com/danielgtaylor/huma/schema"
)
// oaContact describes contact information for this API.
type oaContact struct {
Name string `json:"name,omitempty"`
URL string `json:"url,omitempty"`
Email string `json:"email,omitempty"`
}
// oaServer describes an OpenAPI 3 API server location
type oaServer struct {
URL string `json:"url"`
Description string `json:"description,omitempty"`
}
// paramLocation describes where in the HTTP request the parameter comes from.
type paramLocation string
// Parameter locations supported by OpenAPI 3
const (
inPath paramLocation = "path"
inQuery paramLocation = "query"
inHeader paramLocation = "header"
)
// oaParam describes an OpenAPI 3 parameter
type oaParam struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
In paramLocation `json:"in"`
Required bool `json:"required,omitempty"`
Schema *schema.Schema `json:"schema,omitempty"`
Deprecated bool `json:"deprecated,omitempty"`
Explode *bool `json:"explode,omitempty"`
CLIName string `json:"x-cli-name,omitempty"`
// Internal params are excluded from the OpenAPI document and can set up
// params sent between a load balander / proxy and the service internally.
Internal bool `json:"-"`
typ reflect.Type
}
type oaComponents struct {
Schemas map[string]*schema.Schema `json:"schemas,omitempty"`
SecuritySchemes map[string]oaSecurityScheme `json:"securitySchemes,omitempty"`
}
// AddSchema creates and adds a new schema from a type.
func (c *oaComponents) AddSchema(t reflect.Type, mode schema.Mode, hint string) string {
return c.addSchema(t, mode, hint, true)
}
func (c *oaComponents) addSchema(t reflect.Type, mode schema.Mode, hint string, generateSchemaField bool) string {
// Try to determine the type's name.
name := t.Name()
if name == "" && t.Kind() == reflect.Ptr {
// Take the name of the pointed-to type.
name = t.Elem().Name()
}
if name == "" && t.Kind() == reflect.Slice {
// Take the name of the type in the array and append "List" to it.
tmp := t.Elem()
if tmp.Kind() == reflect.Ptr {
tmp = tmp.Elem()
}
name = tmp.Name()
if name != "" {
name += "List"
}
}
if name == "" {
// No luck, fall back to the passed-in hint. Better than nothing.
name = hint
}
var s *schema.Schema
if t.Kind() == reflect.Slice {
// We actually want to create two models: one for the container slice
// and one for the items within it.
ref := c.addSchema(t.Elem(), mode, name+"Item", false)
s = &schema.Schema{
Type: schema.TypeArray,
Items: &schema.Schema{
Ref: ref,
},
}
} else {
var err error
if s, err = schema.GenerateWithMode(t, mode, nil); err != nil {
panic(err)
}
}
return c.addExistingSchema(s, name, generateSchemaField)
}
// AddExistingSchema adds an existing schema instance under the given name.
func (c *oaComponents) AddExistingSchema(s *schema.Schema, name string) string {
return c.addExistingSchema(s, name, true)
}
func (c *oaComponents) addExistingSchema(s *schema.Schema, name string, generateSchemaField bool) string {
if generateSchemaField && s.Type == schema.TypeObject && s.Properties != nil {
if s.Properties["$schema"] == nil {
// Some editors allow you to place a $schema key which gives you rich
// validation and code completion support. Let's enable that by allowing
// a field here if it doesn't already exist in the model.
s.Properties["$schema"] = &schema.Schema{
Type: schema.TypeString,
Format: "uri",
Description: "An optional URL to a JSON Schema document describing this resource",
}
}
}
orig := name
num := 1
for {
if c.Schemas[name] == nil {
// No existing schema, we are the first!
break
}
if reflect.DeepEqual(c.Schemas[name], s) {
// Existing schema matches!
break
}
// If we are here, then an existing schema doesn't match and this is a new
// type. So we will rename it in a deterministic fashion.
num++
name = fmt.Sprintf("%s%d", orig, num)
}
c.Schemas[name] = s
return "#/components/schemas/" + name
}
type oaFlow struct {
AuthorizationURL string `json:"authorizationUrl,omitempty"`
TokenURL string `json:"tokenUrl,omitempty"`
Scopes map[string]string `json:"scopes,omitempty"`
}
type oaFlows struct {
ClientCredentials *oaFlow `json:"clientCredentials,omitempty"`
AuthorizationCode *oaFlow `json:"authorizationCode,omitempty"`
}
type oaSecurityScheme struct {
Type string `json:"type"`
Scheme string `json:"scheme,omitempty"`
Flows oaFlows `json:"flows,omitempty"`
}