Skip to content

Commit

Permalink
Fixed borked tests
Browse files Browse the repository at this point in the history
  • Loading branch information
daveshanley committed Jan 2, 2025
1 parent 2131152 commit f97b20e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
7 changes: 2 additions & 5 deletions functions/openapi/examples_missing.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ func (em ExamplesMissing) RunRule(_ []*yaml.Node, context model.RuleFunctionCont
if p.SchemaProxy != nil && p.SchemaProxy.Schema != nil && p.SchemaProxy.Schema.Value != nil && (p.SchemaProxy.Schema.Value.Examples != nil || p.SchemaProxy.Schema.Value.Example != nil) {
continue
}
if p.SchemaProxy != nil && isSchemaBoolean(p.SchemaProxy.Schema) ||
isSchemaEnum(p.SchemaProxy.Schema) || isSchemaNumber(p.SchemaProxy.Schema) || isSchemaString(p.SchemaProxy.Schema) {
if p.SchemaProxy != nil && (isSchemaBoolean(p.SchemaProxy.Schema) ||
isSchemaEnum(p.SchemaProxy.Schema) || isSchemaNumber(p.SchemaProxy.Schema) || isSchemaString(p.SchemaProxy.Schema)) {
continue
}

Expand Down Expand Up @@ -221,7 +221,6 @@ func (em ExamplesMissing) RunRule(_ []*yaml.Node, context model.RuleFunctionCont
}
}
path := mt.GenerateJSONPath()
fmt.Println(path)
results = append(results,
buildResult(vacuumUtils.SuppliedOrDefault(context.Rule.Message, "media type is missing `examples` or `example`"),
path,
Expand Down Expand Up @@ -269,7 +268,6 @@ func (em ExamplesMissing) RunRule(_ []*yaml.Node, context model.RuleFunctionCont
}

if isExampleNodeNull(s.Value.Examples) && isExampleNodeNull([]*yaml.Node{s.Value.Example}) {
println(s.GenerateJSONPath())
results = append(results,
buildResult(vacuumUtils.SuppliedOrDefault(context.Rule.Message, "schema is missing `examples` or `example`"),
s.GenerateJSONPath(),
Expand All @@ -293,7 +291,6 @@ func checkParent(s any, depth int) bool {
if sp.Parent != nil {

if pp, kk := sp.Parent.(*base.Schema); kk {
fmt.Println(pp.GenerateJSONPath())
if pp.Value != nil {
if pp.Value.Example != nil || pp.Value.Examples != nil {
return true
Expand Down
4 changes: 2 additions & 2 deletions functions/openapi/examples_missing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,9 @@ components:
def := ExamplesMissing{}
res := def.RunRule(nil, ctx)

assert.Len(t, res, 3)
assert.Len(t, res, 1)
assert.Equal(t, "schema is missing `examples` or `example`", res[0].Message)
assert.Contains(t, res[1].Path, "$.components.schemas['Pizza']")
assert.Contains(t, res[0].Path, "$.components.schemas['Pizza']")
}

func TestExamplesMissing_Header(t *testing.T) {
Expand Down
12 changes: 11 additions & 1 deletion functions/openapi/schema_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func (st SchemaTypeCheck) validateArray(schema *base.Schema, context *model.Rule
if schema.Value.MinItems != nil {
if *schema.Value.MinItems < 0 {
result := st.buildResult("`minItems` should be a non-negative number",
schema.GenerateJSONPath(), "minItem", -1,
schema.GenerateJSONPath(), "minItems", -1,
schema, schema.Value.GoLow().MinItems.KeyNode, context)
results = append(results, result)
}
Expand Down Expand Up @@ -231,13 +231,23 @@ func (st SchemaTypeCheck) buildResult(message, path, violationProperty string, s
if violationProperty != "" {
if segment >= 0 {
p = fmt.Sprintf("%s.%s[%d]", p, violationProperty, segment)
path = p
} else {
p = fmt.Sprintf("%s.%s", p, violationProperty)
path = p
}
allPaths = append(allPaths, p)
}
}
}
} else {
if violationProperty != "" {
if segment >= 0 {
path = fmt.Sprintf("%s.%s[%d]", path, violationProperty, segment)
} else {
path = fmt.Sprintf("%s.%s", path, violationProperty)
}
}
}
}
result := model.RuleFunctionResult{
Expand Down

0 comments on commit f97b20e

Please sign in to comment.