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

use expanded Analyzer for some validators for speed #149

Merged
merged 1 commit into from May 20, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion default_validator.go
Expand Up @@ -92,7 +92,7 @@ func (d *defaultValidator) validateDefaultValueValidAgainstSchema() *Result {
res := new(Result)
s := d.SpecValidator

for method, pathItem := range s.analyzer.Operations() {
for method, pathItem := range s.expandedAnalyzer().Operations() {
for path, op := range pathItem {
// parameters
for _, param := range paramHelp.safeExpandedParamsFor(path, method, op.ID, res, s) {
Expand Down
2 changes: 1 addition & 1 deletion example_validator.go
Expand Up @@ -68,7 +68,7 @@ func (ex *exampleValidator) validateExampleValueValidAgainstSchema() *Result {
res := new(Result)
s := ex.SpecValidator

for method, pathItem := range s.analyzer.Operations() {
for method, pathItem := range s.expandedAnalyzer().Operations() {
for path, op := range pathItem {
// parameters
for _, param := range paramHelp.safeExpandedParamsFor(path, method, op.ID, res, s) {
Expand Down
4 changes: 2 additions & 2 deletions helpers.go
Expand Up @@ -210,7 +210,7 @@ type paramHelper struct {
}

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

for _, ppr := range s.analyzer.SafeParamsFor(method, path,
for _, ppr := range s.expandedAnalyzer().SafeParamsFor(method, path,
func(p spec.Parameter, err error) bool {
// since params have already been expanded, there are few causes for error
res.AddErrors(someParametersBrokenMsg(path, method, operationID))
Expand Down
11 changes: 10 additions & 1 deletion spec.go
Expand Up @@ -624,7 +624,7 @@ func (s *SpecValidator) validateParameters() *Result {
// - path param must be required
res := new(Result)
rexGarbledPathSegment := mustCompileRegexp(`.*[{}\s]+.*`)
for method, pi := range s.analyzer.Operations() {
for method, pi := range s.expandedAnalyzer().Operations() {
methodPaths := make(map[string]map[string]string)
for path, op := range pi {
pathToAdd := pathHelp.stripParametersInPath(path)
Expand Down Expand Up @@ -793,3 +793,12 @@ func (s *SpecValidator) checkUniqueParams(path, method string, op *spec.Operatio
func (s *SpecValidator) SetContinueOnErrors(c bool) {
s.Options.ContinueOnErrors = c
}

// expandedAnalyzer returns expanded.Analyzer when it is available.
// otherwise just analyzer.
func (s *SpecValidator) expandedAnalyzer() *analysis.Spec {
if s.expanded != nil && s.expanded.Analyzer != nil {
return s.expanded.Analyzer
}
return s.analyzer
}