Skip to content

Commit

Permalink
Ignore circular polymorphic ref.
Browse files Browse the repository at this point in the history
  • Loading branch information
eli-l authored and daveshanley committed Jan 26, 2024
1 parent 95d2349 commit 96f4433
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
7 changes: 6 additions & 1 deletion motor/rule_applicator.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ type RuleSetExecution struct {
Timeout time.Duration // The timeout for each rule to run, prevents run-away rules, default is five seconds.

// https://pb33f.io/libopenapi/circular-references/#circular-reference-results
IgnoreCircularArrayRef bool // Ignore array circular references
IgnoreCircularArrayRef bool // Ignore array circular references
IgnoreCircularPolymorphicRef bool // Ignore polymorphic circular references
}

// RuleSetExecutionResult returns the results of running the ruleset against the supplied spec.
Expand Down Expand Up @@ -118,6 +119,10 @@ func ApplyRulesToRuleSet(execution *RuleSetExecution) *RuleSetExecutionResult {
docConfig.IgnoreArrayCircularReferences = true
}

if execution.IgnoreCircularPolymorphicRef {
docConfig.IgnorePolymorphicCircularReferences = true
}

// add new pretty logger.
if execution.Logger == nil {
var logger *slog.Logger
Expand Down
51 changes: 51 additions & 0 deletions motor/rule_applicator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1930,6 +1930,57 @@ components:
assert.Len(t, results.Results, 0)
}

func TestRuleSet_InfiniteCircularLoop_AllowPolymorphicRecursion(t *testing.T) {
yml := `openapi: 3.1.0
paths:
/one:
get:
responses:
'200':
content:
application/json:
schema:
$ref: '#/components/schemas/One'
components:
schemas:
One:
properties:
thing:
oneOf:
- "$ref": "#/components/schemas/Two"
- "$ref": "#/components/schemas/Three"
required:
- thing
Two:
description: "test two"
properties:
testThing:
"$ref": "#/components/schemas/One"
Three:
description: "test three"
properties:
testThing:
"$ref": "#/components/schemas/One"`

rules := make(map[string]*model.Rule)
rules["openapi-tags-alphabetical"] = rulesets.GetOpenApiTagsAlphabeticalRule()

rs := &rulesets.RuleSet{
Rules: rules,
}

rse := &RuleSetExecution{
RuleSet: rs,
Spec: []byte(yml),
IgnoreCircularPolymorphicRef: true,
}
results := ApplyRulesToRuleSet(rse)
assert.Len(t, results.Errors, 0)

assert.NotNil(t, results)
assert.Len(t, results.Results, 0)
}

func TestApplyRules_TestRules_Custom_Document_Pattern(t *testing.T) {

yaml := `rules:
Expand Down

0 comments on commit 96f4433

Please sign in to comment.