Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for extensions within swagger:parameters #2836

Merged
merged 3 commits into from Sep 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
```