Skip to content

Commit b123375

Browse files
ChandanChainanicasualjim
authored andcommitted
Fix x-order not working with number value #162
Signed-off-by: ChandanChainani <chainanichan@gmail.com>
1 parent 7ee5140 commit b123375

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

info.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package spec
1616

1717
import (
1818
"encoding/json"
19+
"strconv"
1920
"strings"
2021

2122
"github.com/go-openapi/jsonpointer"
@@ -40,6 +41,24 @@ func (e Extensions) GetString(key string) (string, bool) {
4041
return "", false
4142
}
4243

44+
// GetInt gets a int value from the extensions
45+
func (e Extensions) GetInt(key string) (int, bool) {
46+
realKey := strings.ToLower(key)
47+
48+
if v, ok := e.GetString(realKey); ok {
49+
if r, err := strconv.Atoi(v); err == nil {
50+
return r, true
51+
}
52+
}
53+
54+
if v, ok := e[realKey]; ok {
55+
if r, rOk := v.(float64); rOk {
56+
return int(r), true
57+
}
58+
}
59+
return -1, false
60+
}
61+
4362
// GetBool gets a string value from the extensions
4463
func (e Extensions) GetBool(key string) (bool, bool) {
4564
if v, ok := e[strings.ToLower(key)]; ok {

properties.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ func (items OrderSchemaItems) MarshalJSON() ([]byte, error) {
4242
func (items OrderSchemaItems) Len() int { return len(items) }
4343
func (items OrderSchemaItems) Swap(i, j int) { items[i], items[j] = items[j], items[i] }
4444
func (items OrderSchemaItems) Less(i, j int) (ret bool) {
45-
ii, oki := items[i].Extensions.GetString("x-order")
46-
ij, okj := items[j].Extensions.GetString("x-order")
45+
ii, oki := items[i].Extensions.GetInt("x-order")
46+
ij, okj := items[j].Extensions.GetInt("x-order")
4747
if oki {
4848
if okj {
4949
defer func() {
@@ -56,7 +56,7 @@ func (items OrderSchemaItems) Less(i, j int) (ret bool) {
5656
ret = reflect.ValueOf(ii).String() < reflect.ValueOf(ij).String()
5757
}
5858
}()
59-
return reflect.ValueOf(ii).Int() < reflect.ValueOf(ij).Int()
59+
return ii < ij
6060
}
6161
return true
6262
} else if okj {

0 commit comments

Comments
 (0)