Skip to content

Commit

Permalink
feat: add extensions for params (#789)
Browse files Browse the repository at this point in the history
* feat: add extensions for params
  • Loading branch information
Nightapes authored Oct 2, 2021
1 parent f9645be commit 1a4da01
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,8 @@ Besides that, `swag` also accepts aliases for some MIME Types as follows:
// @Param int query int false "int valid" minimum(1) maximum(10)
// @Param default query string false "string default" default(A)
// @Param collection query []string false "string collection" collectionFormat(multi)
// @Param extensions query []string false "string collection" extensions(x-example=test,x-nullable)
```
It also works for the struct fields:
Expand All @@ -484,6 +486,7 @@ Field Name | Type | Description
<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` |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`.
<a name="parameterExtensions"></a>extensions | `string` | Add extension to parameters.
### Future
Expand Down
18 changes: 18 additions & 0 deletions operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,8 @@ var regexAttributes = map[string]*regexp.Regexp{
"maxlength": regexp.MustCompile(`(?i)\s+maxlength\(.*\)`),
// for format(email)
"format": regexp.MustCompile(`(?i)\s+format\(.*\)`),
// for extensions(x-example=test)
"extensions": regexp.MustCompile(`(?i)\s+extensions\(.*\)`),
// for collectionFormat(csv)
"collectionFormat": regexp.MustCompile(`(?i)\s+collectionFormat\(.*\)`),
}
Expand Down Expand Up @@ -395,13 +397,17 @@ func (operation *Operation) parseAndExtractionParamAttribute(commentLine, object
param.MinLength = &n
case "format":
param.Format = attr
case "extensions":
param.Extensions = map[string]interface{}{}
setExtensionParam(attr, param)
case "collectionFormat":
n, err := setCollectionFormatParam(attrKey, objectType, attr, commentLine)
if err != nil {
return err
}
param.CollectionFormat = n
}

}

return nil
Expand Down Expand Up @@ -465,6 +471,18 @@ func setEnumParam(attr, objectType, schemaType string, param *spec.Parameter) er
return nil
}

func setExtensionParam(attr string, param *spec.Parameter) error {
for _, val := range strings.Split(attr, ",") {
parts := strings.SplitN(val, "=", 2)
if len(parts) == 2 {
param.Extensions.Add(parts[0], parts[1])
} else {
param.Extensions.Add(parts[0], true)
}
}
return nil
}

func setCollectionFormatParam(name, schemaType, attr, commentLine string) (string, error) {
if schemaType == ARRAY {
return TransToValidCollectionFormat(attr), nil
Expand Down
24 changes: 24 additions & 0 deletions operation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1806,6 +1806,30 @@ func TestParseAndExtractionParamAttribute(t *testing.T) {
assert.NoError(t, err)
}

func TestParseParamCommentByExtensions(t *testing.T) {
comment := `@Param some_id path int true "Some ID" extensions(x-example=test,x-custom=Goopher,x-custom2)`
operation := NewOperation(nil)
err := operation.ParseComment(comment, nil)

assert.NoError(t, err)
b, _ := json.MarshalIndent(operation, "", " ")
expected := `{
"parameters": [
{
"type": "integer",
"x-custom": "Goopher",
"x-custom2": true,
"x-example": "test",
"description": "Some ID",
"name": "some_id",
"in": "path",
"required": true
}
]
}`
assert.Equal(t, expected, string(b))
}

func TestParseIdComment(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit 1a4da01

Please sign in to comment.