feat: sort params in creation. In this manner, the params order is the ever same

This commit is contained in:
Davide Bianchi
2020-10-17 00:50:15 +02:00
parent 169ca8e4a3
commit 8dff4afe09
5 changed files with 26 additions and 15 deletions

1
go.mod
View File

@@ -6,5 +6,6 @@ require (
github.com/alecthomas/jsonschema v0.0.0-20200530073317-71f438968921
github.com/getkin/kin-openapi v0.22.1
github.com/gorilla/mux v1.8.0
github.com/iancoleman/orderedmap v0.1.0 // indirect
github.com/stretchr/testify v1.6.1
)

2
go.sum
View File

@@ -24,6 +24,8 @@ github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/iancoleman/orderedmap v0.0.0-20190318233801-ac98e3ecb4b0 h1:i462o439ZjprVSFSZLZxcsoAe592sZB1rci2Z8j4wdk=
github.com/iancoleman/orderedmap v0.0.0-20190318233801-ac98e3ecb4b0/go.mod h1:N0Wam8K1arqPXNWjMo21EXnBPOPp36vB07FNRdD2geA=
github.com/iancoleman/orderedmap v0.1.0 h1:2orAxZBJsvimgEBmMWfXaFlzSG2fbQil5qzP3F6cCkg=
github.com/iancoleman/orderedmap v0.1.0/go.mod h1:N0Wam8K1arqPXNWjMo21EXnBPOPp36vB07FNRdD2geA=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=

View File

@@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"net/http"
"sort"
"github.com/alecthomas/jsonschema"
"github.com/getkin/kin-openapi/openapi3"
@@ -218,17 +219,24 @@ func (r Router) resolveResponsesSchema(responses map[int]SchemaValue, operation
}
func (r Router) resolveParameterSchema(paramType string, paramConfig map[string]SchemaValue, operation *openapi3.Operation) error {
for k, v := range paramConfig {
var keys = make([]string, 0, len(paramConfig))
for k := range paramConfig {
keys = append(keys, k)
}
sort.Strings(keys)
for _, key := range keys {
v := paramConfig[key]
var param *openapi3.Parameter
switch paramType {
case "path":
param = openapi3.NewPathParameter(k)
param = openapi3.NewPathParameter(key)
case "query":
param = openapi3.NewQueryParameter(k)
param = openapi3.NewQueryParameter(key)
case "headers":
param = openapi3.NewHeaderParameter(k)
param = openapi3.NewHeaderParameter(key)
case "cookie":
param = openapi3.NewCookieParameter(k)
param = openapi3.NewCookieParameter(key)
}
if v.Content != nil {

14
testdata/cookies.json vendored
View File

@@ -9,6 +9,13 @@
"/projects": {
"get": {
"parameters": [
{
"in": "cookie",
"name": "csrftoken",
"schema": {
"type": "string"
}
},
{
"description": "boolean. Set 0 to disable and 1 to enable",
"in": "cookie",
@@ -16,13 +23,6 @@
"schema": {
"type": "integer"
}
},
{
"in": "cookie",
"name": "csrftoken",
"schema": {
"type": "string"
}
}
],
"responses": {

View File

@@ -10,16 +10,16 @@
"get": {
"parameters": [
{
"description": "foo description",
"in": "header",
"name": "foo",
"name": "bar",
"schema": {
"type": "string"
}
},
{
"description": "foo description",
"in": "header",
"name": "bar",
"name": "foo",
"schema": {
"type": "string"
}