-
-
Notifications
You must be signed in to change notification settings - Fork 50
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
Added noRequestBody function to check that HTTP GET and DELETE methods do not contain a requestBody #588
Open
lobocv
wants to merge
6
commits into
daveshanley:main
Choose a base branch
from
lobocv:no-request-body-oas-func
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Added noRequestBody function to check that HTTP GET and DELETE methods do not contain a requestBody #588
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bec0d85
Added noRequestBody function to check that HTTP GET and DELETE method…
b90fe58
fixed test
468c28d
Added oas function to check that HTTP GET and DELETE methods do not h…
0cda491
Added NoRequestBody to recommended ruleset
3b17d22
Merge remote-tracking branch 'origin/no-request-body-oas-func' into n…
4a9bb72
fixed more tests
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright 2022 Dave Shanley / Quobix | ||
// SPDX-License-Identifier: MIT | ||
|
||
package openapi | ||
|
||
import ( | ||
"fmt" | ||
"github.com/daveshanley/vacuum/model" | ||
vacuumUtils "github.com/daveshanley/vacuum/utils" | ||
"github.com/pb33f/doctor/model/high/base" | ||
"gopkg.in/yaml.v3" | ||
"strings" | ||
) | ||
|
||
// NoRequestBody is a rule that checks operations are using tags and they are not empty. | ||
type NoRequestBody struct { | ||
} | ||
|
||
// GetSchema returns a model.RuleFunctionSchema defining the schema of the NoRequestBody rule. | ||
func (r NoRequestBody) GetSchema() model.RuleFunctionSchema { | ||
return model.RuleFunctionSchema{ | ||
Name: "noRequestBody", | ||
} | ||
} | ||
|
||
// GetCategory returns the category of the TagDefined rule. | ||
func (r NoRequestBody) GetCategory() string { | ||
return model.FunctionCategoryOpenAPI | ||
} | ||
|
||
// RunRule will execute the NoRequestBody rule, based on supplied context and a supplied []*yaml.Node slice. | ||
func (r NoRequestBody) RunRule(nodes []*yaml.Node, context model.RuleFunctionContext) []model.RuleFunctionResult { | ||
|
||
var results []model.RuleFunctionResult | ||
|
||
if context.DrDocument == nil { | ||
return results | ||
} | ||
|
||
paths := context.DrDocument.V3Document.Paths | ||
if paths != nil { | ||
for pathItemPairs := paths.PathItems.First(); pathItemPairs != nil; pathItemPairs = pathItemPairs.Next() { | ||
path := pathItemPairs.Key() | ||
v := pathItemPairs.Value() | ||
|
||
for opPairs := v.GetOperations().First(); opPairs != nil; opPairs = opPairs.Next() { | ||
method := opPairs.Key() | ||
op := opPairs.Value() | ||
|
||
for _, checkedMethods := range []string{"GET", "DELETE"} { | ||
if strings.EqualFold(method, checkedMethods) { | ||
if op.RequestBody != nil { | ||
|
||
res := model.RuleFunctionResult{ | ||
Message: vacuumUtils.SuppliedOrDefault(context.Rule.Message, fmt.Sprintf("`%s` operation should not have a requestBody defined", | ||
strings.ToUpper(method))), | ||
StartNode: op.Value.GoLow().KeyNode, | ||
EndNode: vacuumUtils.BuildEndNode(op.Value.GoLow().KeyNode), | ||
Path: fmt.Sprintf("$.paths['%s'].%s", path, method), | ||
Rule: context.Rule, | ||
} | ||
results = append(results, res) | ||
op.AddRuleFunctionResult(base.ConvertRuleResult(&res)) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return results | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
package openapi | ||
|
||
import ( | ||
"fmt" | ||
"github.com/daveshanley/vacuum/model" | ||
drModel "github.com/pb33f/doctor/model" | ||
"github.com/pb33f/libopenapi" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestNoRequestBody_GetSchema(t *testing.T) { | ||
def := NoRequestBody{} | ||
assert.Equal(t, "noRequestBody", def.GetSchema().Name) | ||
} | ||
|
||
func TestNoRequestBody_RunRule(t *testing.T) { | ||
def := NoRequestBody{} | ||
res := def.RunRule(nil, model.RuleFunctionContext{}) | ||
assert.Len(t, res, 0) | ||
} | ||
|
||
func TestNoRequestBody_RunRule_Fail(t *testing.T) { | ||
|
||
yml := `openapi: 3.0.1 | ||
paths: | ||
/melody: | ||
post: | ||
requestBody: | ||
description: "the body of the request" | ||
content: | ||
application/json: | ||
schema: | ||
properties: | ||
id: | ||
type: string | ||
/maddox: | ||
get: | ||
requestBody: | ||
description: "the body of the request" | ||
content: | ||
application/json: | ||
schema: | ||
properties: | ||
id: | ||
type: string | ||
delete: | ||
requestBody: | ||
description: "the body of the request" | ||
content: | ||
application/json: | ||
schema: | ||
properties: | ||
id: | ||
type: string | ||
/ember: | ||
get: | ||
requestBody: | ||
description: "the body of the request" | ||
content: | ||
application/json: | ||
schema: | ||
properties: | ||
id: | ||
type: string | ||
` | ||
document, err := libopenapi.NewDocument([]byte(yml)) | ||
if err != nil { | ||
panic(fmt.Sprintf("cannot create new document: %e", err)) | ||
} | ||
|
||
m, _ := document.BuildV3Model() | ||
path := "$" | ||
|
||
drDocument := drModel.NewDrDocument(m) | ||
|
||
rule := buildOpenApiTestRuleAction(path, "responses", "", nil) | ||
ctx := buildOpenApiTestContext(model.CastToRuleAction(rule.Then), nil) | ||
|
||
ctx.Document = document | ||
ctx.DrDocument = drDocument | ||
ctx.Rule = &rule | ||
|
||
def := NoRequestBody{} | ||
res := def.RunRule(nil, ctx) | ||
assert.Len(t, res, 3) | ||
} | ||
|
||
func TestNoRequestBody_RunRule_Success(t *testing.T) { | ||
|
||
yml := `openapi: 3.0.1 | ||
paths: | ||
/melody: | ||
post: | ||
requestBody: | ||
description: "the body of the request" | ||
content: | ||
application/json: | ||
schema: | ||
properties: | ||
id: | ||
type: string | ||
/maddox: | ||
post: | ||
requestBody: | ||
description: "the body of the request" | ||
content: | ||
application/json: | ||
schema: | ||
properties: | ||
id: | ||
type: string | ||
/ember: | ||
patch: | ||
requestBody: | ||
description: "the body of the request" | ||
content: | ||
application/json: | ||
schema: | ||
properties: | ||
id: | ||
type: string | ||
` | ||
|
||
document, err := libopenapi.NewDocument([]byte(yml)) | ||
if err != nil { | ||
panic(fmt.Sprintf("cannot create new document: %e", err)) | ||
} | ||
|
||
m, _ := document.BuildV3Model() | ||
path := "$" | ||
|
||
drDocument := drModel.NewDrDocument(m) | ||
|
||
rule := buildOpenApiTestRuleAction(path, "responses", "", nil) | ||
ctx := buildOpenApiTestContext(model.CastToRuleAction(rule.Then), nil) | ||
|
||
ctx.Document = document | ||
ctx.DrDocument = drDocument | ||
ctx.Rule = &rule | ||
|
||
def := NoRequestBody{} | ||
res := def.RunRule(nil, ctx) | ||
|
||
assert.Len(t, res, 0) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@daveshanley I don't know what Resolved means. Should it be true or false here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Resolved means that the
$ref
values have all been stitched together by theresolver
inlibopenapi
This is used for rules that assume everything is resolved and inlined. It only makes a difference if the logic in your rule / function is recursive - if so, it means that any circular references could create an infinite loop.