Skip to content

Commit

Permalink
Change the implementation of collectionFormat in @param comment
Browse files Browse the repository at this point in the history
  • Loading branch information
sdghchj committed Mar 18, 2020
1 parent a91c0a6 commit bf19257
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 21 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@ Besides that, `swag` also accepts aliases for some MIME Types as follows:
// @Param string query string false "string valid" minlength(5) maxlength(10)
// @Param int query int false "int valid" mininum(1) maxinum(10)
// @Param default query string false "string default" default(A)
// @Param collection query []string false "string collection" collectionFormat(multi)
```

It also works for the struct fields:
Expand All @@ -461,6 +462,7 @@ Field Name | Type | Description
<a name="parameterMinLength"></a>minLength | `integer` | See https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.2.2.
<a name="parameterEnums"></a>enums | [\*] | See https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.5.1.
<a name="parameterFormat"></a>format | `string` | The extending format for the previously mentioned [`type`](#parameterType). See [Data Type Formats](https://swagger.io/specification/v2/#dataTypeFormat) for further details.
<a name="parameterCollectionFormat"></a>collectionFormat | `string` | Can only be used in @Param comment. Determines the format of the array if type array is used. Possible values are: <ul><li>`csv` - comma separated values `foo,bar`. <li>`ssv` - space separated values `foo bar`. <li>`tsv` - tab separated values `foo\tbar`. <li>`pipes` - pipe separated values <code>foo&#124;bar</code>. <li>`multi` - corresponds to multiple parameter instances instead of multiple values for a single instance `foo=bar&foo=baz`. This is valid only for parameters [`in`](#parameterIn) "query" or "formData". </ul> Default value is `csv`.

### Future

Expand All @@ -471,7 +473,6 @@ Field Name | Type | Description
<a name="parameterMaxItems"></a>maxItems | `integer` | See https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.3.2.
<a name="parameterMinItems"></a>minItems | `integer` | See https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.3.3.
<a name="parameterUniqueItems"></a>uniqueItems | `boolean` | See https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.3.4.
<a name="parameterCollectionFormat"></a>collectionFormat | `string` | Determines the format of the array if type array is used. Possible values are: <ul><li>`csv` - comma separated values `foo,bar`. <li>`ssv` - space separated values `foo bar`. <li>`tsv` - tab separated values `foo\tbar`. <li>`pipes` - pipe separated values <code>foo&#124;bar</code>. <li>`multi` - corresponds to multiple parameter instances instead of multiple values for a single instance `foo=bar&foo=baz`. This is valid only for parameters [`in`](#parameterIn) "query" or "formData". </ul> Default value is `csv`.

## Examples

Expand Down
49 changes: 30 additions & 19 deletions operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,18 +147,10 @@ func (operation *Operation) ParseParamComment(commentLine string, astFile *ast.F

// Detect refType
objectType := "object"
var collectionFormat string
if strings.HasPrefix(refType, "[]") {
objectType = "array"
refType = strings.TrimPrefix(refType, "[]")
refType = TransToValidSchemeType(refType)
if operation.parser != nil {
collectionFormat = TransToValidCollectionFormat(operation.parser.collectionFormatInQuery)
}
} else if pos := strings.IndexRune(refType, ']'); pos > 0 && strings.HasPrefix(refType, "[") { //for example: [csv]int
objectType = "array"
collectionFormat = TransToValidCollectionFormat(refType[1:pos])
refType = TransToValidSchemeType(refType[pos+1:])
} else if IsPrimitiveType(refType) ||
paramType == "formData" && refType == "file" {
objectType = "primitive"
Expand All @@ -183,7 +175,9 @@ func (operation *Operation) ParseParamComment(commentLine string, astFile *ast.F
return fmt.Errorf("%s is not supported array type for %s", refType, paramType)
}
param.SimpleSchema.Type = "array"
param.SimpleSchema.CollectionFormat = collectionFormat
if operation.parser != nil {
param.CollectionFormat = TransToValidCollectionFormat(operation.parser.collectionFormatInQuery)
}
param.SimpleSchema.Items = &spec.Items{
SimpleSchema: spec.SimpleSchema{
Type: refType,
Expand Down Expand Up @@ -224,6 +218,9 @@ func (operation *Operation) ParseParamComment(commentLine string, astFile *ast.F
IsSimplePrimitiveType(prop.Items.Schema.Type[0]) {
param = createParameter(paramType, prop.Description, name, prop.Type[0], find(schema.Required, name))
param.SimpleSchema.Type = prop.Type[0]
if operation.parser != nil && operation.parser.collectionFormatInQuery != "" && param.CollectionFormat == "" {
param.CollectionFormat = TransToValidCollectionFormat(operation.parser.collectionFormatInQuery)
}
param.SimpleSchema.Items = &spec.Items{
SimpleSchema: spec.SimpleSchema{
Type: prop.Items.Schema.Type[0],
Expand Down Expand Up @@ -288,7 +285,7 @@ func (operation *Operation) ParseParamComment(commentLine string, astFile *ast.F
return fmt.Errorf("%s is not supported paramType", paramType)
}

if err := operation.parseAndExtractionParamAttribute(commentLine, refType, &param); err != nil {
if err := operation.parseAndExtractionParamAttribute(commentLine, objectType, refType, &param); err != nil {
return err
}
operation.Operation.Parameters = append(operation.Operation.Parameters, param)
Expand Down Expand Up @@ -343,22 +340,24 @@ func (operation *Operation) registerSchemaType(schemaType string, astFile *ast.F

var regexAttributes = map[string]*regexp.Regexp{
// for Enums(A, B)
"enums": regexp.MustCompile(`(?i)enums\(.*\)`),
"enums": regexp.MustCompile(`(?i)\s+enums\(.*\)`),
// for Minimum(0)
"maxinum": regexp.MustCompile(`(?i)maxinum\(.*\)`),
"maxinum": regexp.MustCompile(`(?i)\s+maxinum\(.*\)`),
// for Maximum(0)
"mininum": regexp.MustCompile(`(?i)mininum\(.*\)`),
"mininum": regexp.MustCompile(`(?i)\s+mininum\(.*\)`),
// for Maximum(0)
"default": regexp.MustCompile(`(?i)default\(.*\)`),
"default": regexp.MustCompile(`(?i)\s+default\(.*\)`),
// for minlength(0)
"minlength": regexp.MustCompile(`(?i)minlength\(.*\)`),
"minlength": regexp.MustCompile(`(?i)\s+minlength\(.*\)`),
// for maxlength(0)
"maxlength": regexp.MustCompile(`(?i)maxlength\(.*\)`),
"maxlength": regexp.MustCompile(`(?i)\s+maxlength\(.*\)`),
// for format(email)
"format": regexp.MustCompile(`(?i)format\(.*\)`),
"format": regexp.MustCompile(`(?i)\s+format\(.*\)`),
// for collectionFormat(csv)
"collectionFormat": regexp.MustCompile(`(?i)\s+collectionFormat\(.*\)`),
}

func (operation *Operation) parseAndExtractionParamAttribute(commentLine, schemaType string, param *spec.Parameter) error {
func (operation *Operation) parseAndExtractionParamAttribute(commentLine, objectType, schemaType string, param *spec.Parameter) error {
schemaType = TransToValidSchemeType(schemaType)
for attrKey, re := range regexAttributes {
attr, err := findAttr(re, commentLine)
Expand Down Expand Up @@ -403,8 +402,13 @@ func (operation *Operation) parseAndExtractionParamAttribute(commentLine, schema
param.MinLength = &n
case "format":
param.Format = attr
case "collectionFormat":
n, err := setCollectionFormatParam(attrKey, objectType, attr, commentLine)
if err != nil {
return err
}
param.CollectionFormat = n
}

}
return nil
}
Expand Down Expand Up @@ -454,6 +458,13 @@ func setEnumParam(attr, schemaType string, param *spec.Parameter) error {
return nil
}

func setCollectionFormatParam(name, schemaType, attr, commentLine string) (string, error) {
if schemaType != "array" {
return "", fmt.Errorf("%s is attribute to set to an array. comment=%s got=%s", name, commentLine, schemaType)
}
return TransToValidCollectionFormat(attr), nil
}

// defineType enum value define the type (object and array unsupported)
func defineType(schemaType string, value string) (interface{}, error) {
schemaType = TransToValidSchemeType(schemaType)
Expand Down
2 changes: 1 addition & 1 deletion operation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ func TestParseParamCommentQueryArray(t *testing.T) {

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

Expand Down

0 comments on commit bf19257

Please sign in to comment.