-
Notifications
You must be signed in to change notification settings - Fork 0
/
spec.go
215 lines (191 loc) · 6.4 KB
/
spec.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
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
// Copyright (c) 2021 Bojan Zivanovic and contributors
// SPDX-License-Identifier: Apache-2.0
package broom
import (
"encoding/hex"
"errors"
"fmt"
"hash/adler32"
"net/http"
"os"
"slices"
"github.com/iancoleman/strcase"
"github.com/pb33f/libopenapi"
"github.com/pb33f/libopenapi/datamodel"
"github.com/pb33f/libopenapi/datamodel/high/base"
v3 "github.com/pb33f/libopenapi/datamodel/high/v3"
"github.com/pb33f/libopenapi/orderedmap"
)
// LoadOperations loads available operations from a specification on disk.
func LoadOperations(filename string) (Operations, error) {
spec, err := LoadSpec(filename)
if err != nil {
return Operations{}, fmt.Errorf("load spec: %w", err)
}
if spec.Paths == nil {
return Operations{}, nil
}
ops := Operations{}
for pair := orderedmap.First(spec.Paths.PathItems); pair != nil; pair = pair.Next() {
path := pair.Key()
pathItem := pair.Value()
if pathItem.Get != nil {
ops = append(ops, newOperationFromSpec(http.MethodGet, path, pathItem.Parameters, *pathItem.Get))
}
if pathItem.Post != nil {
ops = append(ops, newOperationFromSpec(http.MethodPost, path, pathItem.Parameters, *pathItem.Post))
}
if pathItem.Put != nil {
ops = append(ops, newOperationFromSpec(http.MethodPut, path, pathItem.Parameters, *pathItem.Put))
}
if pathItem.Patch != nil {
ops = append(ops, newOperationFromSpec(http.MethodPatch, path, pathItem.Parameters, *pathItem.Patch))
}
if pathItem.Delete != nil {
ops = append(ops, newOperationFromSpec(http.MethodDelete, path, pathItem.Parameters, *pathItem.Delete))
}
}
return ops, nil
}
// LoadSpec loads an OpenAPI 3.0/3.1 specification from disk.
func LoadSpec(filename string) (v3.Document, error) {
b, err := os.ReadFile(filename)
if err != nil {
return v3.Document{}, err
}
doc, err := libopenapi.NewDocumentWithConfiguration(b, &datamodel.DocumentConfiguration{
AllowRemoteReferences: true,
})
if err != nil {
return v3.Document{}, err
}
m, errs := doc.BuildV3Model()
if len(errs) > 0 {
return v3.Document{}, errors.Join(errs...)
}
return m.Model, nil
}
// newOperationFromSpec creates a new operation from the loaded specification.
func newOperationFromSpec(method string, path string, params []*v3.Parameter, specOp v3.Operation) Operation {
op := Operation{
ID: strcase.ToKebab(specOp.OperationId),
Summary: specOp.Summary,
Description: Sanitize(specOp.Description),
Method: method,
Path: path,
Deprecated: getBool(specOp.Deprecated),
}
// Make it possible to run operations without an ID.
if op.ID == "" {
// A hash like c5430c97 is better than nothing, though in the future we
// could try to generate a more user-friendly machine name from the path.
hash := adler32.New()
hash.Write([]byte(path))
op.ID = hex.EncodeToString(hash.Sum(nil))
}
if len(specOp.Tags) > 0 {
op.Tag = specOp.Tags[0]
}
// Parameters can be defined per-path or per-operation.
for _, param := range params {
op.Parameters.Add(newParameterFromSpec(*param))
}
for _, param := range specOp.Parameters {
op.Parameters.Add(newParameterFromSpec(*param))
}
if specOp.RequestBody != nil && specOp.RequestBody.Content != nil {
pair := orderedmap.First(specOp.RequestBody.Content)
format := pair.Key()
mediaType := pair.Value()
mediaTypeSchema := mediaType.Schema.Schema()
op.Parameters.Add(newBodyParameters("", mediaTypeSchema)...)
op.BodyFormat = format
}
return op
}
// newParameterFromSpec creates a new parameter from the loaded specification.
func newParameterFromSpec(specParam v3.Parameter) Parameter {
schema := specParam.Schema.Schema()
return Parameter{
In: specParam.In,
Name: specParam.Name,
Description: Sanitize(specParam.Description),
Style: specParam.Style,
Type: getSchemaType(schema),
Enum: getEnum(schema),
Example: getExample(schema),
Default: getDefaultValue(schema),
Deprecated: specParam.Deprecated,
Required: getBool(specParam.Required),
}
}
// newBodyParameters creates a slice of body parameters from the given schema.
func newBodyParameters(prefix string, schema *base.Schema) []Parameter {
parameters := make([]Parameter, 0, 10)
for pair := orderedmap.First(schema.Properties); pair != nil; pair = pair.Next() {
propertyName := pair.Key()
propertySchema := pair.Value().Schema()
propertySchemaType := getSchemaType(propertySchema)
if propertySchemaType == "object" {
// Nested parameters found, flatten them.
parameters = append(parameters, newBodyParameters(prefix+propertyName+".", propertySchema)...)
} else {
parameters = append(parameters, Parameter{
In: "body",
Name: prefix + propertyName,
Description: Sanitize(propertySchema.Description),
Type: propertySchemaType,
Enum: getEnum(propertySchema),
Example: getExample(propertySchema),
Default: getDefaultValue(propertySchema),
Deprecated: getBool(propertySchema.Deprecated),
Required: slices.Contains(schema.Required, propertyName),
})
}
}
return parameters
}
// getSchemaType retrieves the type of the given schema.
func getSchemaType(schema *base.Schema) string {
// schema.Type can contain multiple values in OpenAPI 3.1, e.g:
// [string, null] or [string, integer]. Broom needs a single type
// so that it can cast the value (see Parameter#CastString).
schemaType := schema.Type[0]
// Expand the array type into the underlying type (array -> []string).
if schemaType == "array" && schema.Items.IsA() {
schemaType = fmt.Sprintf("[]%v", schema.Items.A.Schema().Type[0])
}
return schemaType
}
// getEnum retrieves the enum values defined on the given schema.
func getEnum(schema *base.Schema) []string {
var enum []string
for _, v := range schema.Enum {
enum = append(enum, v.Value)
}
return enum
}
// getExample retrieves the examples defined on the given schema.
func getExample(schema *base.Schema) string {
var exampleValue string
if len(schema.Examples) > 0 {
exampleValue = schema.Examples[0].Value
} else if schema.Example != nil {
exampleValue = schema.Example.Value
}
return exampleValue
}
// getDefaultValue retrieves the default value defined on the given schema.
func getDefaultValue(schema *base.Schema) string {
if schema.Default == nil {
return ""
}
return schema.Default.Value
}
// getBool converts a boolean reference into a boolean, turning nil into false.
func getBool(v *bool) bool {
if v == nil {
return false
}
return *v
}