Skip to content

Commit

Permalink
Merge pull request #2836 from cccRaim/master
Browse files Browse the repository at this point in the history
Support for extensions within swagger:parameters
  • Loading branch information
casualjim committed Sep 25, 2022
2 parents 0037e9f + 1fdd0a2 commit 91b12fd
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
11 changes: 10 additions & 1 deletion codescan/parameters.go
Expand Up @@ -285,6 +285,14 @@ func (p *parameterBuilder) buildFromField(fld *types.Var, tpe types.Type, typabl
}
}

func spExtensionsSetter(ps *spec.Parameter) func(*spec.Extensions) {
return func(exts *spec.Extensions) {
for name, value := range *exts {
addExtension(&ps.VendorExtensible, name, value)
}
}
}

func (p *parameterBuilder) buildFromStruct(decl *entityDecl, tpe *types.Struct, op *spec.Operation, seen map[string]spec.Parameter) error {
if tpe.NumFields() == 0 {
return nil
Expand Down Expand Up @@ -396,6 +404,7 @@ func (p *parameterBuilder) buildFromStruct(decl *entityDecl, tpe *types.Struct,
newSingleLineTagParser("default", &setDefault{&ps.SimpleSchema, paramValidations{&ps}, rxf(rxDefaultFmt, "")}),
newSingleLineTagParser("example", &setExample{&ps.SimpleSchema, paramValidations{&ps}, rxf(rxExampleFmt, "")}),
newSingleLineTagParser("required", &setRequiredParam{&ps}),
newMultiLineTagParser("Extensions", newSetExtensions(spExtensionsSetter(&ps)), true),
}

itemsTaggers := func(items *spec.Items, level int) []tagParser {
Expand Down Expand Up @@ -471,10 +480,10 @@ func (p *parameterBuilder) buildFromStruct(decl *entityDecl, tpe *types.Struct,
}

} else {

sp.taggers = []tagParser{
newSingleLineTagParser("in", &matchOnlyParam{&ps, rxIn}),
newSingleLineTagParser("required", &matchOnlyParam{&ps, rxRequired}),
newMultiLineTagParser("Extensions", newSetExtensions(spExtensionsSetter(&ps)), true),
}
}
if err := sp.Parse(afld.Doc); err != nil {
Expand Down
19 changes: 19 additions & 0 deletions docs/use/spec/params.md
Expand Up @@ -40,6 +40,7 @@ Annotation | Format
**Unique** | when set to true the slice can only contain unique items
**Required** | when set to true this value needs to be present in the request
**Example** | an example value, parsed as the field's type<br/>(objects and slices are parsed as JSON)
**Extensions** | a dictionary of custom [vendor extensions](https://swagger.io/docs/specification/2-0/swagger-extensions/); each key must start with `x-`

For slice properties there are also items to be defined. This might be a nested collection, for indicating nesting
level the value is a 0-based index, so items.minLength is the same as items.0.minLength
Expand Down Expand Up @@ -76,6 +77,12 @@ type BarSliceParam struct {
// collection format: pipe
// in: query
// example: [[["bar_000"]]]
// Extensions:
// x-example-flag: true
// x-some-list:
// - dog
// - cat
// - bird
BarSlice [][][]string `json:"bar_slice"`
}
```
Expand Down Expand Up @@ -111,6 +118,12 @@ operations:
minLength: 3
maxLength: 10
pattern: "\\w+"
extensions:
x-example-flag: true
x-some-list:
- dog
- cat
- bird
post:
operationId: addBars
parameters:
Expand All @@ -136,4 +149,10 @@ operations:
minLength: 3
maxLength: 10
pattern: "\\w+"
extensions:
x-example-flag: true
x-some-list:
- dog
- cat
- bird
```

0 comments on commit 91b12fd

Please sign in to comment.