Skip to content

Commit

Permalink
Fix x-order not working with number value go-openapi#162
Browse files Browse the repository at this point in the history
Signed-off-by: ChandanChainani <[email protected]>
  • Loading branch information
ChandanChainani authored and casualjim committed Apr 22, 2023
1 parent 7ee5140 commit b123375
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
19 changes: 19 additions & 0 deletions info.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package spec

import (
"encoding/json"
"strconv"
"strings"

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

// GetInt gets a int value from the extensions
func (e Extensions) GetInt(key string) (int, bool) {
realKey := strings.ToLower(key)

if v, ok := e.GetString(realKey); ok {
if r, err := strconv.Atoi(v); err == nil {
return r, true
}
}

if v, ok := e[realKey]; ok {
if r, rOk := v.(float64); rOk {
return int(r), true
}
}
return -1, false
}

// GetBool gets a string value from the extensions
func (e Extensions) GetBool(key string) (bool, bool) {
if v, ok := e[strings.ToLower(key)]; ok {
Expand Down
6 changes: 3 additions & 3 deletions properties.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ func (items OrderSchemaItems) MarshalJSON() ([]byte, error) {
func (items OrderSchemaItems) Len() int { return len(items) }
func (items OrderSchemaItems) Swap(i, j int) { items[i], items[j] = items[j], items[i] }
func (items OrderSchemaItems) Less(i, j int) (ret bool) {
ii, oki := items[i].Extensions.GetString("x-order")
ij, okj := items[j].Extensions.GetString("x-order")
ii, oki := items[i].Extensions.GetInt("x-order")
ij, okj := items[j].Extensions.GetInt("x-order")
if oki {
if okj {
defer func() {
Expand All @@ -56,7 +56,7 @@ func (items OrderSchemaItems) Less(i, j int) (ret bool) {
ret = reflect.ValueOf(ii).String() < reflect.ValueOf(ij).String()
}
}()
return reflect.ValueOf(ii).Int() < reflect.ValueOf(ij).Int()
return ii < ij
}
return true
} else if okj {
Expand Down

0 comments on commit b123375

Please sign in to comment.