Skip to content

Commit

Permalink
feat(parser): support slice of primitives for query and body, support…
Browse files Browse the repository at this point in the history
… primitives for body (#489)
  • Loading branch information
ubogdan authored and easonlin404 committed Aug 7, 2019
1 parent 8743381 commit e22b392
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 20 deletions.
72 changes: 58 additions & 14 deletions operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,34 +138,78 @@ func (operation *Operation) ParseParamComment(commentLine string, astFile *ast.F
}
name := matches[1]
paramType := matches[2]
refType := TransToValidSchemeType(matches[3])

schemaType := matches[3]
// Detect refType
objectType := "object"
if strings.HasPrefix(refType, "[]") == true {
objectType = "array"
refType = strings.TrimPrefix(refType, "[]")
} else if IsPrimitiveType(refType) ||
paramType == "formData" && refType == "file" {
objectType = "primitive"
}

requiredText := strings.ToLower(matches[4])
required := requiredText == "true" || requiredText == "required"
description := matches[5]

var param spec.Parameter
param := createParameter(paramType, description, name, refType, required)

//five possible parameter types.
switch paramType {
case "query", "path", "header":
param = createParameter(paramType, description, name, TransToValidSchemeType(schemaType), required)
case "body":
param = createParameter(paramType, description, name, "object", required) // TODO: if Parameter types can be objects, but also primitives and arrays
if err := operation.registerSchemaType(schemaType, astFile); err != nil {
return err
case "path", "header", "formData":
switch objectType {
case "array", "object":
return fmt.Errorf("%s is not supported type for %s", refType, paramType)
}
case "query":
switch objectType {
case "array":
if !IsPrimitiveType(refType) {
return fmt.Errorf("%s is not supported array type for %s", refType, paramType)
}
param.SimpleSchema.Type = "array"
param.SimpleSchema.Items = &spec.Items{
SimpleSchema: spec.SimpleSchema{
Type: refType,
},
}
case "object":
return fmt.Errorf("%s is not supported type for %s", refType, paramType)
}
param.Schema.Ref = spec.Ref{
Ref: jsonreference.MustCreateRef("#/definitions/" + schemaType),
case "body":
switch objectType {
case "primitive":
param.Schema.Type = spec.StringOrArray{refType}
case "array":
param.Schema.Items = &spec.SchemaOrArray{
Schema: &spec.Schema{
SchemaProps: spec.SchemaProps{},
},
}
// Arrau of Primitive or Object
if IsPrimitiveType(refType) {
param.Schema.Items.Schema.Type = spec.StringOrArray{refType}
} else {
if err := operation.registerSchemaType(refType, astFile); err != nil {
return err
}
param.Schema.Items.Schema.Ref = spec.Ref{Ref: jsonreference.MustCreateRef("#/definitions/" + refType)}
}
case "object":
if err := operation.registerSchemaType(refType, astFile); err != nil {
return err
}
param.Schema.Type = spec.StringOrArray{objectType}
param.Schema.Ref = spec.Ref{
Ref: jsonreference.MustCreateRef("#/definitions/" + refType),
}
}
case "formData":
param = createParameter(paramType, description, name, TransToValidSchemeType(schemaType), required)
default:
return fmt.Errorf("%s is not supported paramType", paramType)
}

if err := operation.parseAndExtractionParamAttribute(commentLine, schemaType, &param); err != nil {
if err := operation.parseAndExtractionParamAttribute(commentLine, refType, &param); err != nil {
return err
}
operation.Operation.Parameters = append(operation.Operation.Parameters, param)
Expand Down
59 changes: 53 additions & 6 deletions operation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ package swag

import (
"encoding/json"
"go/token"

"go/ast"
goparser "go/parser"
"go/token"
"testing"

"github.com/go-openapi/spec"
Expand Down Expand Up @@ -319,6 +318,58 @@ func TestParseParamCommentByPathType(t *testing.T) {
assert.Equal(t, expected, string(b))
}

// Test ParseParamComment Query Params
func TestParseParamCommentBodyArray(t *testing.T) {
comment := `@Param names body []string true "Users List"`
operation := NewOperation()
err := operation.ParseComment(comment, nil)

assert.NoError(t, err)
b, _ := json.MarshalIndent(operation, "", " ")
expected := `{
"parameters": [
{
"description": "Users List",
"name": "names",
"in": "body",
"required": true,
"schema": {
"type": "string",
"items": {
"type": "string"
}
}
}
]
}`
assert.Equal(t, expected, string(b))
}

// Test ParseParamComment Query Params
func TestParseParamCommentQueryArray(t *testing.T) {
comment := `@Param names query []string true "Users List"`
operation := NewOperation()
err := operation.ParseComment(comment, nil)

assert.NoError(t, err)
b, _ := json.MarshalIndent(operation, "", " ")
expected := `{
"parameters": [
{
"type": "array",
"items": {
"type": "string"
},
"description": "Users List",
"name": "names",
"in": "query",
"required": true
}
]
}`
assert.Equal(t, expected, string(b))
}

func TestParseParamCommentByID(t *testing.T) {
comment := `@Param unsafe_id[lte] query int true "Unsafe query param"`
operation := NewOperation()
Expand Down Expand Up @@ -707,10 +758,6 @@ func TestParseParamCommentByDefault(t *testing.T) {
]
}`
assert.Equal(t, expected, string(b))

comment = `@Param some_id query time.Duration true "Some ID" Default(10)`
operation = NewOperation()
assert.NoError(t, operation.ParseComment(comment, nil))
}

func TestParseIdComment(t *testing.T) {
Expand Down

0 comments on commit e22b392

Please sign in to comment.