Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: upgrade to libopenapi with orderedmap support #38

Merged
merged 7 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions errors/request_errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@ package errors

import (
"fmt"
"github.com/pb33f/libopenapi-validator/helpers"
v3 "github.com/pb33f/libopenapi/datamodel/high/v3"
"net/http"
"strings"

"github.com/pb33f/libopenapi-validator/helpers"
v3 "github.com/pb33f/libopenapi/datamodel/high/v3"
"github.com/pb33f/libopenapi/orderedmap"
)

func RequestContentTypeNotFound(op *v3.Operation, request *http.Request) *ValidationError {
ct := request.Header.Get(helpers.ContentTypeHeader)
var ctypes []string
for k := range op.RequestBody.Content {
ctypes = append(ctypes, k)
for pair := orderedmap.First(op.RequestBody.Content); pair != nil; pair = pair.Next() {
ctypes = append(ctypes, pair.Key())
}
return &ValidationError{
ValidationType: helpers.RequestBodyValidation,
Expand All @@ -27,6 +29,6 @@ func RequestContentTypeNotFound(op *v3.Operation, request *http.Request) *Valida
SpecLine: op.RequestBody.GoLow().Content.KeyNode.Line,
SpecCol: op.RequestBody.GoLow().Content.KeyNode.Column,
Context: op,
HowToFix: fmt.Sprintf(HowToFixInvalidContentType, len(op.RequestBody.Content), strings.Join(ctypes, ", ")),
HowToFix: fmt.Sprintf(HowToFixInvalidContentType, orderedmap.Len(op.RequestBody.Content), strings.Join(ctypes, ", ")),
}
}
27 changes: 15 additions & 12 deletions errors/response_errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,37 @@ package errors

import (
"fmt"
"github.com/pb33f/libopenapi-validator/helpers"
"github.com/pb33f/libopenapi/datamodel/high/v3"
"net/http"
"strings"

"github.com/pb33f/libopenapi-validator/helpers"
v3 "github.com/pb33f/libopenapi/datamodel/high/v3"
"github.com/pb33f/libopenapi/orderedmap"
)

func ResponseContentTypeNotFound(op *v3.Operation,
request *http.Request,
response *http.Response,
code string,
isDefault bool) *ValidationError {
isDefault bool,
) *ValidationError {
ct := response.Header.Get(helpers.ContentTypeHeader)
mediaTypeString, _, _ := helpers.ExtractContentType(ct)
var ctypes []string
var specLine, specCol int
var contentMap map[string]*v3.MediaType
var contentMap *orderedmap.Map[string, *v3.MediaType]

// check for a default type (applies to all codes without a match)
if !isDefault {
for k := range op.Responses.Codes[code].Content {
ctypes = append(ctypes, k)
for pair := orderedmap.First(op.Responses.Codes.GetOrZero(code).Content); pair != nil; pair = pair.Next() {
ctypes = append(ctypes, pair.Key())
}
specLine = op.Responses.Codes[code].GoLow().Content.KeyNode.Line
specCol = op.Responses.Codes[code].GoLow().Content.KeyNode.Column
contentMap = op.Responses.Codes[code].Content
specLine = op.Responses.Codes.GetOrZero(code).GoLow().Content.KeyNode.Line
specCol = op.Responses.Codes.GetOrZero(code).GoLow().Content.KeyNode.Column
contentMap = op.Responses.Codes.GetOrZero(code).Content
} else {
for k := range op.Responses.Default.Content {
ctypes = append(ctypes, k)
for pair := orderedmap.First(op.Responses.Default.Content); pair != nil; pair = pair.Next() {
ctypes = append(ctypes, pair.Key())
}
specLine = op.Responses.Default.GoLow().Content.KeyNode.Line
specCol = op.Responses.Default.GoLow().Content.KeyNode.Column
Expand All @@ -49,7 +52,7 @@ func ResponseContentTypeNotFound(op *v3.Operation,
SpecCol: specCol,
Context: op,
HowToFix: fmt.Sprintf(HowToFixInvalidContentType,
len(contentMap), strings.Join(ctypes, ", ")),
orderedmap.Len(contentMap), strings.Join(ctypes, ", ")),
}
}

Expand Down
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,21 @@ module github.com/pb33f/libopenapi-validator
go 1.21

require (
github.com/pb33f/libopenapi v0.13.17
github.com/pb33f/libopenapi v0.14.0
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1
github.com/stretchr/testify v1.8.4
github.com/vmware-labs/yaml-jsonpath v0.3.2
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/bahlo/generic-list-go v0.2.0 // indirect
github.com/buger/jsonparser v1.1.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dprotaso/go-yit v0.0.0-20220510233725-9ba8df137936 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect
golang.org/x/exp v0.0.0-20230811145659-89c5cff77bcb // indirect
golang.org/x/sync v0.1.0 // indirect
)
13 changes: 11 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
github.com/bahlo/generic-list-go v0.2.0 h1:5sz/EEAK+ls5wF+NeqDpk5+iNdMDXrh3z3nPnH1Wvgk=
github.com/bahlo/generic-list-go v0.2.0/go.mod h1:2KvAjgMlE5NNynlg/5iLrrCCZ2+5xWbdbCW3pNTGyYg=
github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs=
github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
Expand Down Expand Up @@ -27,11 +31,14 @@ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
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 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
Expand All @@ -47,8 +54,8 @@ github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1y
github.com/onsi/gomega v1.17.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAlGdZY=
github.com/onsi/gomega v1.19.0 h1:4ieX6qQjPP/BfC3mpsAtIGGlxTWPeA3Inl/7DtXw1tw=
github.com/onsi/gomega v1.19.0/go.mod h1:LY+I3pBVzYsTBU1AnDwOSxaYi9WoWiqgwooUqq9yPro=
github.com/pb33f/libopenapi v0.13.17 h1:FbY5Nx3xmALZf7TtdITXRSyeNZhB7U3COYbkAisX2eY=
github.com/pb33f/libopenapi v0.13.17/go.mod h1:Lv2eEtsAtbRFlF8hjH82L8SIGoUNgemMVoKoB6A9THk=
github.com/pb33f/libopenapi v0.14.0 h1:sXcq6z5vG/QPbSLxYSjCMQAhszZD4+Yox60HBzcB2gI=
github.com/pb33f/libopenapi v0.14.0/go.mod h1:m+4Pwri31UvcnZjuP8M7TlbR906DXJmMvYsbis234xg=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1 h1:lZUw3E0/J3roVtGQ+SCrUrg3ON6NgVqpn3+iol9aGu4=
Expand All @@ -62,6 +69,8 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/vmware-labs/yaml-jsonpath v0.3.2 h1:/5QKeCBGdsInyDCyVNLbXyilb61MXGi9NP674f9Hobk=
github.com/vmware-labs/yaml-jsonpath v0.3.2/go.mod h1:U6whw1z03QyqgWdgXxvVnQ90zN1BWz5V+51Ewf8k+rQ=
github.com/wk8/go-ordered-map/v2 v2.1.8 h1:5h/BUHu93oj4gIdvHHHGsScSTMijfx5PeYkE/fJgbpc=
github.com/wk8/go-ordered-map/v2 v2.1.8/go.mod h1:5nJHM5DyteebpVlHnWMV0rPz6Zp7+xBAnxjb1X5vnTw=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
Expand Down
16 changes: 8 additions & 8 deletions parameters/header_parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ package parameters

import (
"fmt"
"net/http"
"strconv"
"strings"

"github.com/pb33f/libopenapi-validator/errors"
"github.com/pb33f/libopenapi-validator/helpers"
"github.com/pb33f/libopenapi-validator/paths"
"github.com/pb33f/libopenapi/datamodel/high/base"
"github.com/pb33f/libopenapi/datamodel/high/v3"
"net/http"
"strconv"
"strings"
v3 "github.com/pb33f/libopenapi/datamodel/high/v3"
)

func (v *paramValidator) ValidateHeaderParams(request *http.Request) (bool, []*errors.ValidationError) {

// find path
var pathItem *v3.PathItem
var errs []*errors.ValidationError
Expand All @@ -31,10 +31,10 @@ func (v *paramValidator) ValidateHeaderParams(request *http.Request) (bool, []*e
}

// extract params for the operation
var params = helpers.ExtractParamsForOperation(request, pathItem)
params := helpers.ExtractParamsForOperation(request, pathItem)

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

Expand Down Expand Up @@ -136,7 +136,7 @@ func (v *paramValidator) ValidateHeaderParams(request *http.Request) (bool, []*e
}
}
} else {
if p.Required {
if p.Required != nil && *p.Required {
validationErrors = append(validationErrors, errors.HeaderParameterMissing(p))
}
}
Expand Down
30 changes: 14 additions & 16 deletions parameters/query_parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,20 @@ package parameters
import (
"encoding/json"
"fmt"
"github.com/pb33f/libopenapi-validator/errors"
"github.com/pb33f/libopenapi-validator/helpers"
"github.com/pb33f/libopenapi-validator/paths"
"github.com/pb33f/libopenapi/datamodel/high/base"
"github.com/pb33f/libopenapi/datamodel/high/v3"
"net/http"
"regexp"
"strconv"
"strings"

"github.com/pb33f/libopenapi-validator/errors"
"github.com/pb33f/libopenapi-validator/helpers"
"github.com/pb33f/libopenapi-validator/paths"
"github.com/pb33f/libopenapi/datamodel/high/base"
v3 "github.com/pb33f/libopenapi/datamodel/high/v3"
"github.com/pb33f/libopenapi/orderedmap"
)

func (v *paramValidator) ValidateQueryParams(request *http.Request) (bool, []*errors.ValidationError) {

// find path
var pathItem *v3.PathItem
var errs []*errors.ValidationError
Expand All @@ -33,7 +34,7 @@ func (v *paramValidator) ValidateQueryParams(request *http.Request) (bool, []*er
}

// extract params for the operation
var params = helpers.ExtractParamsForOperation(request, pathItem)
params := helpers.ExtractParamsForOperation(request, pathItem)
queryParams := make(map[string][]*helpers.QueryParam)
var validationErrors []*errors.ValidationError

Expand Down Expand Up @@ -79,13 +80,11 @@ doneLooking:
sch = params[p].Schema.Schema()
} else {
// ok, no schema, check for a content type
if params[p].Content != nil {
for k, ct := range params[p].Content {
sch = ct.Schema.Schema()
contentWrapped = true
contentType = k
break
}
for pair := orderedmap.First(params[p].Content); pair != nil; pair = pair.Next() {
sch = pair.Value().Schema.Schema()
contentWrapped = true
contentType = pair.Key()
break
}
}
pType := sch.Type
Expand Down Expand Up @@ -210,7 +209,6 @@ doneLooking:
}
}
}

} else {
// if the param is not in the requests, so let's check if this param is an
// object, and if we should use default encoding and explode values.
Expand All @@ -234,7 +232,7 @@ doneLooking:
}
}
// if there is no match, check if the param is required or not.
if params[p].Required {
if params[p].Required != nil && *params[p].Required {
validationErrors = append(validationErrors, errors.QueryParameterMissing(params[p]))
}
}
Expand Down
Loading