Skip to content

Commit c1d5381

Browse files
TristanSpeakEasydaveshanley
authored andcommitted
feat: upgrade to libopenapi with orderedmap support
1 parent 2a03263 commit c1d5381

File tree

12 files changed

+117
-196
lines changed

12 files changed

+117
-196
lines changed

errors/request_errors.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,19 @@ package errors
55

66
import (
77
"fmt"
8-
"github.com/pb33f/libopenapi-validator/helpers"
9-
v3 "github.com/pb33f/libopenapi/datamodel/high/v3"
108
"net/http"
119
"strings"
10+
11+
"github.com/pb33f/libopenapi-validator/helpers"
12+
v3 "github.com/pb33f/libopenapi/datamodel/high/v3"
13+
"github.com/pb33f/libopenapi/orderedmap"
1214
)
1315

1416
func RequestContentTypeNotFound(op *v3.Operation, request *http.Request) *ValidationError {
1517
ct := request.Header.Get(helpers.ContentTypeHeader)
1618
var ctypes []string
17-
for k := range op.RequestBody.Content {
18-
ctypes = append(ctypes, k)
19+
for pair := orderedmap.First(op.RequestBody.Content); pair != nil; pair = pair.Next() {
20+
ctypes = append(ctypes, pair.Key())
1921
}
2022
return &ValidationError{
2123
ValidationType: helpers.RequestBodyValidation,
@@ -27,6 +29,6 @@ func RequestContentTypeNotFound(op *v3.Operation, request *http.Request) *Valida
2729
SpecLine: op.RequestBody.GoLow().Content.KeyNode.Line,
2830
SpecCol: op.RequestBody.GoLow().Content.KeyNode.Column,
2931
Context: op,
30-
HowToFix: fmt.Sprintf(HowToFixInvalidContentType, len(op.RequestBody.Content), strings.Join(ctypes, ", ")),
32+
HowToFix: fmt.Sprintf(HowToFixInvalidContentType, orderedmap.Len(op.RequestBody.Content), strings.Join(ctypes, ", ")),
3133
}
3234
}

errors/response_errors.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,37 @@ package errors
55

66
import (
77
"fmt"
8-
"github.com/pb33f/libopenapi-validator/helpers"
9-
"github.com/pb33f/libopenapi/datamodel/high/v3"
108
"net/http"
119
"strings"
10+
11+
"github.com/pb33f/libopenapi-validator/helpers"
12+
v3 "github.com/pb33f/libopenapi/datamodel/high/v3"
13+
"github.com/pb33f/libopenapi/orderedmap"
1214
)
1315

1416
func ResponseContentTypeNotFound(op *v3.Operation,
1517
request *http.Request,
1618
response *http.Response,
1719
code string,
18-
isDefault bool) *ValidationError {
20+
isDefault bool,
21+
) *ValidationError {
1922
ct := response.Header.Get(helpers.ContentTypeHeader)
2023
mediaTypeString, _, _ := helpers.ExtractContentType(ct)
2124
var ctypes []string
2225
var specLine, specCol int
23-
var contentMap map[string]*v3.MediaType
26+
var contentMap *orderedmap.Map[string, *v3.MediaType]
2427

2528
// check for a default type (applies to all codes without a match)
2629
if !isDefault {
27-
for k := range op.Responses.Codes[code].Content {
28-
ctypes = append(ctypes, k)
30+
for pair := orderedmap.First(op.Responses.Codes.GetOrZero(code).Content); pair != nil; pair = pair.Next() {
31+
ctypes = append(ctypes, pair.Key())
2932
}
30-
specLine = op.Responses.Codes[code].GoLow().Content.KeyNode.Line
31-
specCol = op.Responses.Codes[code].GoLow().Content.KeyNode.Column
32-
contentMap = op.Responses.Codes[code].Content
33+
specLine = op.Responses.Codes.GetOrZero(code).GoLow().Content.KeyNode.Line
34+
specCol = op.Responses.Codes.GetOrZero(code).GoLow().Content.KeyNode.Column
35+
contentMap = op.Responses.Codes.GetOrZero(code).Content
3336
} else {
34-
for k := range op.Responses.Default.Content {
35-
ctypes = append(ctypes, k)
37+
for pair := orderedmap.First(op.Responses.Default.Content); pair != nil; pair = pair.Next() {
38+
ctypes = append(ctypes, pair.Key())
3639
}
3740
specLine = op.Responses.Default.GoLow().Content.KeyNode.Line
3841
specCol = op.Responses.Default.GoLow().Content.KeyNode.Column
@@ -49,7 +52,7 @@ func ResponseContentTypeNotFound(op *v3.Operation,
4952
SpecCol: specCol,
5053
Context: op,
5154
HowToFix: fmt.Sprintf(HowToFixInvalidContentType,
52-
len(contentMap), strings.Join(ctypes, ", ")),
55+
orderedmap.Len(contentMap), strings.Join(ctypes, ", ")),
5356
}
5457
}
5558

go.mod

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,15 @@ require (
1111
)
1212

1313
require (
14+
github.com/bahlo/generic-list-go v0.2.0 // indirect
15+
github.com/buger/jsonparser v1.1.1 // indirect
1416
github.com/davecgh/go-spew v1.1.1 // indirect
1517
github.com/dprotaso/go-yit v0.0.0-20220510233725-9ba8df137936 // indirect
18+
github.com/mailru/easyjson v0.7.7 // indirect
1619
github.com/pmezard/go-difflib v1.0.0 // indirect
20+
github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect
1721
golang.org/x/exp v0.0.0-20230811145659-89c5cff77bcb // indirect
1822
golang.org/x/sync v0.1.0 // indirect
1923
)
24+
25+
replace github.com/pb33f/libopenapi => github.com/speakeasy-api/libopenapi v0.0.0-20231203222815-69ba5b875a1d

go.sum

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
github.com/bahlo/generic-list-go v0.2.0 h1:5sz/EEAK+ls5wF+NeqDpk5+iNdMDXrh3z3nPnH1Wvgk=
2+
github.com/bahlo/generic-list-go v0.2.0/go.mod h1:2KvAjgMlE5NNynlg/5iLrrCCZ2+5xWbdbCW3pNTGyYg=
3+
github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs=
4+
github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0=
15
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
26
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
37
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
@@ -27,11 +31,14 @@ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
2731
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
2832
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
2933
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
34+
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
3035
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
3136
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
3237
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
3338
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
3439
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
40+
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
41+
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
3542
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
3643
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
3744
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
@@ -47,21 +54,23 @@ github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1y
4754
github.com/onsi/gomega v1.17.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAlGdZY=
4855
github.com/onsi/gomega v1.19.0 h1:4ieX6qQjPP/BfC3mpsAtIGGlxTWPeA3Inl/7DtXw1tw=
4956
github.com/onsi/gomega v1.19.0/go.mod h1:LY+I3pBVzYsTBU1AnDwOSxaYi9WoWiqgwooUqq9yPro=
50-
github.com/pb33f/libopenapi v0.13.17 h1:FbY5Nx3xmALZf7TtdITXRSyeNZhB7U3COYbkAisX2eY=
51-
github.com/pb33f/libopenapi v0.13.17/go.mod h1:Lv2eEtsAtbRFlF8hjH82L8SIGoUNgemMVoKoB6A9THk=
5257
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
5358
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5459
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1 h1:lZUw3E0/J3roVtGQ+SCrUrg3ON6NgVqpn3+iol9aGu4=
5560
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1/go.mod h1:uToXkOrWAZ6/Oc07xWQrPOhJotwFIyu2bBVN41fcDUY=
5661
github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
5762
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
63+
github.com/speakeasy-api/libopenapi v0.0.0-20231203222815-69ba5b875a1d h1:rPCPao2+13LBlaGrXpI3r76i2qDK6tQL4mOG98yQ+gk=
64+
github.com/speakeasy-api/libopenapi v0.0.0-20231203222815-69ba5b875a1d/go.mod h1:m+4Pwri31UvcnZjuP8M7TlbR906DXJmMvYsbis234xg=
5865
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
5966
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
6067
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
6168
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
6269
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
6370
github.com/vmware-labs/yaml-jsonpath v0.3.2 h1:/5QKeCBGdsInyDCyVNLbXyilb61MXGi9NP674f9Hobk=
6471
github.com/vmware-labs/yaml-jsonpath v0.3.2/go.mod h1:U6whw1z03QyqgWdgXxvVnQ90zN1BWz5V+51Ewf8k+rQ=
72+
github.com/wk8/go-ordered-map/v2 v2.1.8 h1:5h/BUHu93oj4gIdvHHHGsScSTMijfx5PeYkE/fJgbpc=
73+
github.com/wk8/go-ordered-map/v2 v2.1.8/go.mod h1:5nJHM5DyteebpVlHnWMV0rPz6Zp7+xBAnxjb1X5vnTw=
6574
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
6675
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
6776
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=

parameters/header_parameters.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@ package parameters
55

66
import (
77
"fmt"
8+
"net/http"
9+
"strconv"
10+
"strings"
11+
812
"github.com/pb33f/libopenapi-validator/errors"
913
"github.com/pb33f/libopenapi-validator/helpers"
1014
"github.com/pb33f/libopenapi-validator/paths"
1115
"github.com/pb33f/libopenapi/datamodel/high/base"
12-
"github.com/pb33f/libopenapi/datamodel/high/v3"
13-
"net/http"
14-
"strconv"
15-
"strings"
16+
v3 "github.com/pb33f/libopenapi/datamodel/high/v3"
1617
)
1718

1819
func (v *paramValidator) ValidateHeaderParams(request *http.Request) (bool, []*errors.ValidationError) {
19-
2020
// find path
2121
var pathItem *v3.PathItem
2222
var errs []*errors.ValidationError
@@ -31,10 +31,10 @@ func (v *paramValidator) ValidateHeaderParams(request *http.Request) (bool, []*e
3131
}
3232

3333
// extract params for the operation
34-
var params = helpers.ExtractParamsForOperation(request, pathItem)
34+
params := helpers.ExtractParamsForOperation(request, pathItem)
3535

3636
var validationErrors []*errors.ValidationError
37-
var seenHeaders = make(map[string]bool)
37+
seenHeaders := make(map[string]bool)
3838
for _, p := range params {
3939
if p.In == helpers.Header {
4040

@@ -136,7 +136,7 @@ func (v *paramValidator) ValidateHeaderParams(request *http.Request) (bool, []*e
136136
}
137137
}
138138
} else {
139-
if p.Required {
139+
if p.Required != nil && *p.Required {
140140
validationErrors = append(validationErrors, errors.HeaderParameterMissing(p))
141141
}
142142
}

parameters/query_parameters.go

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,20 @@ package parameters
66
import (
77
"encoding/json"
88
"fmt"
9-
"github.com/pb33f/libopenapi-validator/errors"
10-
"github.com/pb33f/libopenapi-validator/helpers"
11-
"github.com/pb33f/libopenapi-validator/paths"
12-
"github.com/pb33f/libopenapi/datamodel/high/base"
13-
"github.com/pb33f/libopenapi/datamodel/high/v3"
149
"net/http"
1510
"regexp"
1611
"strconv"
1712
"strings"
13+
14+
"github.com/pb33f/libopenapi-validator/errors"
15+
"github.com/pb33f/libopenapi-validator/helpers"
16+
"github.com/pb33f/libopenapi-validator/paths"
17+
"github.com/pb33f/libopenapi/datamodel/high/base"
18+
v3 "github.com/pb33f/libopenapi/datamodel/high/v3"
19+
"github.com/pb33f/libopenapi/orderedmap"
1820
)
1921

2022
func (v *paramValidator) ValidateQueryParams(request *http.Request) (bool, []*errors.ValidationError) {
21-
2223
// find path
2324
var pathItem *v3.PathItem
2425
var errs []*errors.ValidationError
@@ -33,7 +34,7 @@ func (v *paramValidator) ValidateQueryParams(request *http.Request) (bool, []*er
3334
}
3435

3536
// extract params for the operation
36-
var params = helpers.ExtractParamsForOperation(request, pathItem)
37+
params := helpers.ExtractParamsForOperation(request, pathItem)
3738
queryParams := make(map[string][]*helpers.QueryParam)
3839
var validationErrors []*errors.ValidationError
3940

@@ -79,13 +80,11 @@ doneLooking:
7980
sch = params[p].Schema.Schema()
8081
} else {
8182
// ok, no schema, check for a content type
82-
if params[p].Content != nil {
83-
for k, ct := range params[p].Content {
84-
sch = ct.Schema.Schema()
85-
contentWrapped = true
86-
contentType = k
87-
break
88-
}
83+
for pair := orderedmap.First(params[p].Content); pair != nil; pair = pair.Next() {
84+
sch = pair.Value().Schema.Schema()
85+
contentWrapped = true
86+
contentType = pair.Key()
87+
break
8988
}
9089
}
9190
pType := sch.Type
@@ -210,7 +209,6 @@ doneLooking:
210209
}
211210
}
212211
}
213-
214212
} else {
215213
// if the param is not in the requests, so let's check if this param is an
216214
// object, and if we should use default encoding and explode values.
@@ -234,7 +232,7 @@ doneLooking:
234232
}
235233
}
236234
// if there is no match, check if the param is required or not.
237-
if params[p].Required {
235+
if params[p].Required != nil && *params[p].Required {
238236
validationErrors = append(validationErrors, errors.QueryParameterMissing(params[p]))
239237
}
240238
}

0 commit comments

Comments
 (0)