Skip to content

Commit

Permalink
feat: support arrays in path and header params (#1039)
Browse files Browse the repository at this point in the history
Co-authored-by: Joe Zidell <jzidell@fearless.tech>
  • Loading branch information
josephzidell and Joe Zidell authored Oct 26, 2021
1 parent b5ca0bb commit 1780cd2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 23 deletions.
15 changes: 14 additions & 1 deletion operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,20 @@ func (operation *Operation) ParseParamComment(commentLine string, astFile *ast.F
switch paramType {
case "path", "header":
switch objectType {
case ARRAY, OBJECT:
case ARRAY:
if !IsPrimitiveType(refType) {
return fmt.Errorf("%s is not supported array type for %s", refType, paramType)
}
param.SimpleSchema.Type = objectType
if operation.parser != nil {
param.CollectionFormat = TransToValidCollectionFormat(operation.parser.collectionFormatInQuery)
}
param.SimpleSchema.Items = &spec.Items{
SimpleSchema: spec.SimpleSchema{
Type: refType,
},
}
case OBJECT:
return fmt.Errorf("%s is not supported type for %s", refType, paramType)
}
case "query", "formData":
Expand Down
39 changes: 17 additions & 22 deletions operation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1040,7 +1040,7 @@ func TestParseResponseCommentParamMissing(t *testing.T) {
}

// Test ParseParamComment
func TestParseParamCommentByPathType(t *testing.T) {
func TestParseParamCommentByParamType(t *testing.T) {
t.Parallel()

comment := `@Param some_id path int true "Some ID"`
Expand Down Expand Up @@ -1092,25 +1092,18 @@ func TestParseParamCommentBodyArray(t *testing.T) {
assert.Equal(t, expected, string(b))
}

// Test ParseParamComment header Params
func TestParseParamCommentHeaderArray(t *testing.T) {
t.Parallel()

comment := `@Param names header []string true "Users List"`
operation := NewOperation(nil)
assert.Error(t, operation.ParseComment(comment, nil))
}

// Test ParseParamComment Query Params
func TestParseParamCommentQueryArray(t *testing.T) {
t.Parallel()
// Test ParseParamComment Params
func TestParseParamCommentArray(t *testing.T) {
paramTypes := []string{"header", "path", "query"}

operation := NewOperation(nil)
err := operation.ParseComment(`@Param names query []string true "Users List"`, nil)
for _, paramType := range paramTypes {
t.Run(paramType, func(t *testing.T) {
operation := NewOperation(nil)
err := operation.ParseComment(`@Param names `+paramType+` []string true "Users List"`, nil)

assert.NoError(t, err)
b, _ := json.MarshalIndent(operation, "", " ")
expected := `{
assert.NoError(t, err)
b, _ := json.MarshalIndent(operation, "", " ")
expected := `{
"parameters": [
{
"type": "array",
Expand All @@ -1119,15 +1112,17 @@ func TestParseParamCommentQueryArray(t *testing.T) {
},
"description": "Users List",
"name": "names",
"in": "query",
"in": "` + paramType + `",
"required": true
}
]
}`
assert.Equal(t, expected, string(b))
assert.Equal(t, expected, string(b))

err = operation.ParseComment(`@Param names query []model.User true "Users List"`, nil)
assert.Error(t, err)
err = operation.ParseComment(`@Param names `+paramType+` []model.User true "Users List"`, nil)
assert.Error(t, err)
})
}
}

// Test TestParseParamCommentDefaultValue Query Params
Expand Down

0 comments on commit 1780cd2

Please sign in to comment.