Skip to content

Commit

Permalink
Added missing example check for parameters with content
Browse files Browse the repository at this point in the history
reported in discord channel, also bumped deps
  • Loading branch information
daveshanley committed May 9, 2024
1 parent fa35aa7 commit 99df10d
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 9 deletions.
17 changes: 17 additions & 0 deletions functions/openapi/examples_missing.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func (em ExamplesMissing) RunRule(_ []*yaml.Node, context model.RuleFunctionCont
seen := make(map[[32]byte]bool)

if context.DrDocument.Parameters != nil {
paramClear:
for i := range context.DrDocument.Parameters {
p := context.DrDocument.Parameters[i]
if p.SchemaProxy != nil && isSchemaBoolean(p.SchemaProxy.Schema) {
Expand All @@ -66,6 +67,22 @@ func (em ExamplesMissing) RunRule(_ []*yaml.Node, context model.RuleFunctionCont
if p.SchemaProxy != nil && (p.SchemaProxy.Schema.Value.Examples != nil || p.SchemaProxy.Schema.Value.Example != nil) {
continue
}

// check if the parameter has any content defined with examples
if p.Content.Len() > 0 {
for con := p.Content.First(); con != nil; con = con.Next() {
v := con.Value()
if v.Examples != nil && p.Examples.Len() >= 0 {
// add to seen elements, so when checking schemas we can mark them as good.
h := p.Value.GoLow().Hash()
if _, ok := seen[h]; !ok {
seen[h] = true
}
break paramClear
}
}
}

if p.Value.Examples.Len() <= 0 && isExampleNodeNull([]*yaml.Node{p.Value.Example}) {
n := p.Value.GoLow().RootNode
if p.Value.GoLow().KeyNode != nil {
Expand Down
47 changes: 47 additions & 0 deletions functions/openapi/examples_missing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,53 @@ paths:
assert.Contains(t, res[0].Path, "$.paths['/pizza'].get.requestBody.content['application/json']")
}

func TestExamples_ContentOK(t *testing.T) {

yml := `openapi: 3.1
components:
parameters:
ParameterA:
name: paramA
required: false
in: query
description: “some random text”
content:
application/json:
schema:
type: object
additionalProperties:
type: string
example: hey
example:
“a”: “5
examples:
ParameterA:
value:
Key: “value”`

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, "examples_missing", "", nil)
ctx := buildOpenApiTestContext(model.CastToRuleAction(rule.Then), nil)

ctx.Document = document
ctx.DrDocument = drDocument
ctx.Rule = &rule

def := ExamplesMissing{}
res := def.RunRule(nil, ctx)

assert.Len(t, res, 0)
}

func TestExamplesMissing_TrainTravel(t *testing.T) {

yml := `openapi: 3.1
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ require (
github.com/gizak/termui/v3 v3.1.0
github.com/json-iterator/go v1.1.12
github.com/mitchellh/mapstructure v1.5.0
github.com/pb33f/doctor v0.0.6
github.com/pb33f/libopenapi v0.16.1
github.com/pb33f/libopenapi-validator v0.0.49
github.com/pb33f/doctor v0.0.7
github.com/pb33f/libopenapi v0.16.5
github.com/pb33f/libopenapi-validator v0.0.53
github.com/pterm/pterm v0.12.79
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1
github.com/sourcegraph/conc v0.3.0
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,12 @@ github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1y
github.com/onsi/gomega v1.17.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAlGdZY=
github.com/onsi/gomega v1.19.0 h1:4ieX6qQjPP/BfC3mpsAtIGGlxTWPeA3Inl/7DtXw1tw=
github.com/onsi/gomega v1.19.0/go.mod h1:LY+I3pBVzYsTBU1AnDwOSxaYi9WoWiqgwooUqq9yPro=
github.com/pb33f/doctor v0.0.6 h1:su/mIujJy0rB8KsdpTl7cC4RAHZdTyB7V5SjXu+xVWE=
github.com/pb33f/doctor v0.0.6/go.mod h1:yBs5hFHAoo/eeFvKN9sWwmHmgEPJ2SaotYOJc05GdMU=
github.com/pb33f/libopenapi v0.16.1 h1:R2rmjiDiyX/cQKMKDbCiDFKOpt8kB0VYFtvH16tQHfA=
github.com/pb33f/libopenapi v0.16.1/go.mod h1:PEXNwvtT4KNdjrwudp5OYnD1ryqK6uJ68aMNyWvoMuc=
github.com/pb33f/libopenapi-validator v0.0.49 h1:oJ4Jukib2pCvW0ccdVVyIyK/s5LCNvAuNt1U+PnGgEs=
github.com/pb33f/libopenapi-validator v0.0.49/go.mod h1:kU1JYyXIRlpmsWx3NkL+drNNttLADMgdaNzJgXDhec0=
github.com/pb33f/doctor v0.0.7 h1:1HMomX3AqF7v51fsBi2iY2LBsPZEGAh1mUbBYezlFUc=
github.com/pb33f/doctor v0.0.7/go.mod h1:yBs5hFHAoo/eeFvKN9sWwmHmgEPJ2SaotYOJc05GdMU=
github.com/pb33f/libopenapi v0.16.5 h1:jqb/N5nc2zuSUSWDgCXi2vKGxMQfTsWHgtmPWSKQGqc=
github.com/pb33f/libopenapi v0.16.5/go.mod h1:PEXNwvtT4KNdjrwudp5OYnD1ryqK6uJ68aMNyWvoMuc=
github.com/pb33f/libopenapi-validator v0.0.53 h1:ZUZacrLYPPBL3rR6xiHH3IoehEsuWZDMXMlZESudEnA=
github.com/pb33f/libopenapi-validator v0.0.53/go.mod h1:nYi41uD7SfJWImyfkZr80WPb9LKYOb97PvOz6/azaFU=
github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4=
github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
github.com/petermattis/goid v0.0.0-20180202154549-b0b1615b78e5 h1:q2e307iGHPdTGp0hoxKjt1H5pDo6utceo3dQVK3I5XQ=
Expand Down

0 comments on commit 99df10d

Please sign in to comment.