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

Sort query parameters generated from object #681

Merged
merged 5 commits into from May 29, 2020
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
14 changes: 13 additions & 1 deletion operation.go
Expand Up @@ -9,6 +9,7 @@ import (
"net/http"
"os"
"regexp"
"sort"
"strconv"
"strings"

Expand Down Expand Up @@ -208,7 +209,13 @@ func (operation *Operation) ParseParamComment(commentLine string, astFile *ast.F
}
return false
}
for name, prop := range schema.Properties {
orderedNames := make([]string, 0, len(schema.Properties))
for k := range schema.Properties {
orderedNames = append(orderedNames, k)
}
sort.Strings(orderedNames)
for _, name := range orderedNames {
prop := schema.Properties[name]
if len(prop.Type) == 0 {
continue
}
Expand All @@ -232,6 +239,11 @@ func (operation *Operation) ParseParamComment(commentLine string, astFile *ast.F
Println(fmt.Sprintf("skip field [%s] in %s is not supported type for %s", name, refType, paramType))
continue
}
param.Nullable = prop.Nullable
param.Format = prop.Format
param.Default = prop.Default
param.Example = prop.Example
param.Extensions = prop.Extensions
param.CommonValidations.Maximum = prop.Maximum
param.CommonValidations.Minimum = prop.Minimum
param.CommonValidations.ExclusiveMaximum = prop.ExclusiveMaximum
Expand Down
39 changes: 38 additions & 1 deletion parser_test.go
Expand Up @@ -3028,6 +3028,42 @@ func Fun() {

}
`
expected := `{
"info": {
"contact": {},
"license": {}
},
"paths": {
"/test": {
"get": {
"parameters": [
{
"type": "integer",
"name": "age",
"in": "query"
},
{
"type": "string",
"name": "name",
"in": "query"
},
{
"type": "array",
"items": {
"type": "string"
},
"name": "teachers",
"in": "query"
}
],
"responses": {
"200": {}
}
}
}
}
}`

f, err := goparser.ParseFile(token.NewFileSet(), "", src, goparser.ParseComments)
assert.NoError(t, err)

Expand All @@ -3036,7 +3072,8 @@ func Fun() {
err = p.ParseRouterAPIInfo("", f)
assert.NoError(t, err)

assert.Equal(t, 3, len(p.swagger.Paths.Paths["/test"].Get.Parameters))
b, _ := json.MarshalIndent(p.swagger, "", " ")
assert.Equal(t, expected, string(b))
}

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