Skip to content

Commit ae5ca97

Browse files
authored
Merge pull request #149 from koron/validation-extremely-slow-146
use expanded Analyzer for some validators for speed
2 parents c5b9514 + cfc09bf commit ae5ca97

File tree

4 files changed

+14
-5
lines changed

4 files changed

+14
-5
lines changed

default_validator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func (d *defaultValidator) validateDefaultValueValidAgainstSchema() *Result {
9292
res := new(Result)
9393
s := d.SpecValidator
9494

95-
for method, pathItem := range s.analyzer.Operations() {
95+
for method, pathItem := range s.expandedAnalyzer().Operations() {
9696
for path, op := range pathItem {
9797
// parameters
9898
for _, param := range paramHelp.safeExpandedParamsFor(path, method, op.ID, res, s) {

example_validator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func (ex *exampleValidator) validateExampleValueValidAgainstSchema() *Result {
6868
res := new(Result)
6969
s := ex.SpecValidator
7070

71-
for method, pathItem := range s.analyzer.Operations() {
71+
for method, pathItem := range s.expandedAnalyzer().Operations() {
7272
for path, op := range pathItem {
7373
// parameters
7474
for _, param := range paramHelp.safeExpandedParamsFor(path, method, op.ID, res, s) {

helpers.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ type paramHelper struct {
210210
}
211211

212212
func (h *paramHelper) safeExpandedParamsFor(path, method, operationID string, res *Result, s *SpecValidator) (params []spec.Parameter) {
213-
operation, ok := s.analyzer.OperationFor(method, path)
213+
operation, ok := s.expandedAnalyzer().OperationFor(method, path)
214214
if ok {
215215
// expand parameters first if necessary
216216
resolvedParams := []spec.Parameter{}
@@ -224,7 +224,7 @@ func (h *paramHelper) safeExpandedParamsFor(path, method, operationID string, re
224224
// remove params with invalid expansion from Slice
225225
operation.Parameters = resolvedParams
226226

227-
for _, ppr := range s.analyzer.SafeParamsFor(method, path,
227+
for _, ppr := range s.expandedAnalyzer().SafeParamsFor(method, path,
228228
func(p spec.Parameter, err error) bool {
229229
// since params have already been expanded, there are few causes for error
230230
res.AddErrors(someParametersBrokenMsg(path, method, operationID))

spec.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ func (s *SpecValidator) validateParameters() *Result {
624624
// - path param must be required
625625
res := new(Result)
626626
rexGarbledPathSegment := mustCompileRegexp(`.*[{}\s]+.*`)
627-
for method, pi := range s.analyzer.Operations() {
627+
for method, pi := range s.expandedAnalyzer().Operations() {
628628
methodPaths := make(map[string]map[string]string)
629629
for path, op := range pi {
630630
pathToAdd := pathHelp.stripParametersInPath(path)
@@ -793,3 +793,12 @@ func (s *SpecValidator) checkUniqueParams(path, method string, op *spec.Operatio
793793
func (s *SpecValidator) SetContinueOnErrors(c bool) {
794794
s.Options.ContinueOnErrors = c
795795
}
796+
797+
// expandedAnalyzer returns expanded.Analyzer when it is available.
798+
// otherwise just analyzer.
799+
func (s *SpecValidator) expandedAnalyzer() *analysis.Spec {
800+
if s.expanded != nil && s.expanded.Analyzer != nil {
801+
return s.expanded.Analyzer
802+
}
803+
return s.analyzer
804+
}

0 commit comments

Comments
 (0)