Skip to content

Commit

Permalink
Sort query parameters generated from object
Browse files Browse the repository at this point in the history
  • Loading branch information
breezewish committed Apr 20, 2020
1 parent 93e5f15 commit 719cfd8
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
9 changes: 8 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 Down
39 changes: 38 additions & 1 deletion parser_test.go
Expand Up @@ -3056,6 +3056,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 @@ -3064,7 +3100,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

0 comments on commit 719cfd8

Please sign in to comment.