Skip to content

Commit

Permalink
Merge pull request #47 from k1LoW/re-new-validator
Browse files Browse the repository at this point in the history
Add workaround for invalid validator instance after validation error
  • Loading branch information
k1LoW committed Apr 28, 2024
2 parents 79ff03e + 5a08be3 commit fad2074
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions openapi3.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strconv"
"strings"

validator "github.com/pb33f/libopenapi-validator"
verrors "github.com/pb33f/libopenapi-validator/errors"
"gopkg.in/yaml.v3"
)
Expand Down Expand Up @@ -65,6 +66,17 @@ func (rt *Router) setOpenApi3Vaildator() error {
if !rt.skipValidateRequest {
_, errs := v.ValidateHttpRequest(r)
if len(errs) > 0 {
{
// renew validator (workaround)
// ref: https://github.com/k1LoW/runn/issues/882
vv, errrs := validator.NewValidator(*rt.openAPI3Doc)
if len(errrs) > 0 {
rt.t.Errorf("failed to renew validator: %v", errors.Join(errrs...))
return
}
rt.openAPI3Validator = &vv
v = *rt.openAPI3Validator
}
var err error
for _, e := range errs {
// nullable type workaround.
Expand All @@ -82,6 +94,16 @@ func (rt *Router) setOpenApi3Vaildator() error {
if !rt.skipValidateResponse {
_, errs := v.ValidateHttpResponse(r, rec.toResponse())
if len(errs) > 0 {
{
// renew validator (workaround)
// ref: https://github.com/k1LoW/runn/issues/882
vv, errrs := validator.NewValidator(*rt.openAPI3Doc)
if len(errrs) > 0 {
rt.t.Errorf("failed to renew validator: %v", errors.Join(errrs...))
return
}
rt.openAPI3Validator = &vv
}
var err error
for _, e := range errs {
// nullable type workaround.
Expand Down

0 comments on commit fad2074

Please sign in to comment.