Skip to content

Commit 316fdd6

Browse files
authored
Merge pull request #29 from padiazg/parameters-support
Parameters support
2 parents 8d92190 + 15de319 commit 316fdd6

File tree

5 files changed

+205
-0
lines changed

5 files changed

+205
-0
lines changed

build-map-keys.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,7 @@ const (
2323
keyFlows = "flows"
2424
keyAuthorizationURL = "authorizationUrl"
2525
keyScopes = "scopes"
26+
keyParameters = "parameters"
27+
keyRequired = "required"
28+
keySchema = "schema"
2629
)

build.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ func makeAllPathsMap(paths *Paths) pathsMap {
158158
pathMap[keySecurity] = makeSecurityMap(&path.Security)
159159
pathMap[keyRequestBody] = makeRequestBodyMap(&path.RequestBody)
160160
pathMap[keyResponses] = makeResponsesMap(&path.Responses)
161+
pathMap[keyParameters] = makeParametersMap(path.Parameters)
161162

162163
allPaths[path.Route][strings.ToLower(path.HTTPMethod)] = pathMap
163164
}
@@ -346,3 +347,43 @@ const emptyStr = ""
346347
func isStrEmpty(s string) bool {
347348
return s == emptyStr
348349
}
350+
351+
func makeParametersMap(parameters Parameters) []map[string]interface{} {
352+
parametersMap := []map[string]interface{}{}
353+
354+
for i := 0; i < len(parameters); i++ {
355+
var (
356+
param = &parameters[i]
357+
paramMap = make(map[string]interface{})
358+
)
359+
360+
paramMap[keyName] = param.Name
361+
paramMap[keyIn] = param.In
362+
paramMap[keyDescription] = param.Description
363+
paramMap[keyRequired] = param.Required
364+
paramMap[keySchema] = makeSchemaMap(&param.Schema)
365+
366+
parametersMap = append(parametersMap, paramMap)
367+
}
368+
369+
return parametersMap
370+
}
371+
372+
func makeSchemaMap(schema *Schema) map[string]interface{} {
373+
schemaMap := make(map[string]interface{})
374+
375+
if !isStrEmpty(schema.Ref) {
376+
schemaMap[keyRef] = schema.Ref
377+
} else {
378+
schemaMap[keyName] = schema.Name
379+
schemaMap[keyType] = schema.Type
380+
if len(schema.Properties) > 0 {
381+
schemaMap[keyProperties] = makePropertiesMap(&schema.Properties)
382+
}
383+
if schema.XML.Name != "" {
384+
schemaMap[keyXML] = map[string]interface{}{"name": schema.XML.Name}
385+
}
386+
}
387+
388+
return schemaMap
389+
}

build_test.go

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package docs
22

33
import (
44
"bytes"
5+
"reflect"
56
"testing"
67
)
78

@@ -229,3 +230,115 @@ func TestOAS_BuildStream(t *testing.T) {
229230
})
230231
}
231232
}
233+
234+
func Test_makeParametersMap(t *testing.T) {
235+
t.Parallel()
236+
237+
type args struct {
238+
parameters Parameters
239+
}
240+
241+
tests := []struct {
242+
name string
243+
args args
244+
want []map[string]interface{}
245+
}{
246+
{
247+
name: "success-minimal",
248+
args: args{
249+
parameters: Parameters{{
250+
Name: "id",
251+
In: "path",
252+
Description: "test",
253+
Required: true,
254+
Schema: Schema{Name: "id", Type: "integer"},
255+
}},
256+
},
257+
want: []map[string]interface{}{{
258+
"name": "id",
259+
"in": "path",
260+
"description": "test",
261+
"required": true,
262+
"schema": map[string]interface{}{"name": "id", "type": "integer"},
263+
}},
264+
},
265+
{
266+
name: "success-full",
267+
args: args{
268+
parameters: Parameters{{
269+
Name: "id",
270+
In: "path",
271+
Description: "test",
272+
Required: true,
273+
Schema: Schema{
274+
Name: "id",
275+
Type: "integer",
276+
Properties: SchemaProperties{{Name: "id", Type: "integer"}},
277+
},
278+
}},
279+
},
280+
want: []map[string]interface{}{{
281+
"name": "id",
282+
"in": "path",
283+
"description": "test",
284+
"required": true,
285+
"schema": map[string]interface{}{
286+
"name": "id",
287+
"type": "integer",
288+
"properties": map[string]interface{}{
289+
"id": map[string]interface{}{"type": "integer"},
290+
},
291+
},
292+
}},
293+
},
294+
{
295+
name: "success-ref",
296+
args: args{
297+
parameters: Parameters{{
298+
Name: "id",
299+
In: "path",
300+
Description: "test",
301+
Required: true,
302+
Schema: Schema{Ref: "$some-ref"},
303+
}},
304+
},
305+
want: []map[string]interface{}{{
306+
"name": "id",
307+
"in": "path",
308+
"description": "test",
309+
"required": true,
310+
"schema": map[string]interface{}{"$ref": "$some-ref"},
311+
}},
312+
},
313+
{
314+
name: "success-xml-entry",
315+
args: args{
316+
parameters: Parameters{{
317+
Name: "id",
318+
In: "path",
319+
Description: "test",
320+
Required: true,
321+
Schema: Schema{Name: "id", Type: "integer", XML: XMLEntry{Name: "id"}},
322+
}},
323+
},
324+
want: []map[string]interface{}{{
325+
"name": "id",
326+
"in": "path",
327+
"description": "test",
328+
"required": true,
329+
"schema": map[string]interface{}{"name": "id", "type": "integer", "xml": map[string]interface{}{"name": "id"}},
330+
}},
331+
},
332+
}
333+
for _, tt := range tests {
334+
trn := tt
335+
336+
t.Run(trn.name, func(t *testing.T) {
337+
t.Parallel()
338+
339+
if got := makeParametersMap(trn.args.parameters); !reflect.DeepEqual(got, trn.want) {
340+
t.Errorf("makeParametersMap() = %+v, want %+v", got, trn.want)
341+
}
342+
})
343+
}
344+
}

examples/stream_output/main.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,24 @@ func main() {
4747
Route: "/users",
4848
HTTPMethod: "GET",
4949
OperationID: "getUser",
50+
Summary: "Get Users list",
51+
Responses: docs.Responses{
52+
getResponseOK(),
53+
},
54+
// HandlerFuncName: "handleCreateUser",
55+
RequestBody: docs.RequestBody{
56+
Description: "Get Users list",
57+
Content: docs.ContentTypes{
58+
getContentApplicationJSON("#/components/schemas/User"),
59+
},
60+
Required: true,
61+
},
62+
})
63+
64+
apiDoc.AddRoute(&docs.Path{
65+
Route: "/users/{id}",
66+
HTTPMethod: "GET",
67+
OperationID: "getUser",
5068
Summary: "Get a User",
5169
Responses: docs.Responses{
5270
getResponseOK(),
@@ -59,6 +77,16 @@ func main() {
5977
},
6078
Required: true,
6179
},
80+
Parameters: docs.Parameters{{
81+
Name: "id",
82+
Description: "User ID",
83+
In: "path",
84+
Required: true,
85+
Schema: docs.Schema{
86+
Name: "id",
87+
Type: "string",
88+
},
89+
}},
6290
})
6391

6492
mux := http.NewServeMux()

models.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ type Path struct {
103103
RequestBody RequestBody `yaml:"requestBody"`
104104
Responses Responses `yaml:"responses"`
105105
Security SecurityEntities `yaml:"security,omitempty"`
106+
Parameters Parameters `yaml:"parameters,omitempty"`
106107
HandlerFuncName string `yaml:"-"`
107108
}
108109

@@ -210,6 +211,25 @@ type SecurityScope struct {
210211
Description string `yaml:"description,omitempty"`
211212
}
212213

214+
// Parameters is a slice of Parameter objects.
215+
type Parameters []Parameter
216+
217+
// Parameter represents OAS parameter object.
218+
type Parameter struct {
219+
// If in is "path", the name field MUST correspond to a template expression occurring within
220+
// the path field in the Paths Object. See Path Templating for further information.
221+
// If in is "header" and the name field is "Accept", "Content-Type" or "Authorization",
222+
// the parameter definition SHALL be ignored.
223+
// For all other cases, the name corresponds to the parameter name used by the in property.
224+
Name string `yaml:"name,omitempty"`
225+
In string `yaml:"in,omitempty"` // "query", "header", "path" or "cookie".
226+
Description string `yaml:"description,omitempty"`
227+
Required bool `yaml:"required,omitempty"`
228+
Deprecated bool `yaml:"deprecated,omitempty"`
229+
AllowEmptyValue bool `yaml:"allowEmptyValue,omitempty"`
230+
Schema Schema
231+
}
232+
213233
// isEmpty checks if *ExternalDocs struct is empty.
214234
func (ed *ExternalDocs) isEmpty() bool {
215235
if ed == nil {

0 commit comments

Comments
 (0)