Skip to content

Commit

Permalink
fix: handling of a nullable enum using the 3.1 spec type array
Browse files Browse the repository at this point in the history
  • Loading branch information
TristanSpeakEasy authored and daveshanley committed Dec 19, 2022
1 parent 454bd67 commit a8b6170
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 10 deletions.
18 changes: 13 additions & 5 deletions functions/openapi/typed_enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ package openapi

import (
"fmt"
"strconv"

"github.com/daveshanley/vacuum/model"
"github.com/pb33f/libopenapi/utils"
"gopkg.in/yaml.v3"
"strconv"
)

// TypedEnum will check enum values match the types provided
type TypedEnum struct {
}
type TypedEnum struct{}

// GetSchema returns a model.RuleFunctionSchema defining the schema of the TypedEnum rule.
func (te TypedEnum) GetSchema() model.RuleFunctionSchema {
Expand All @@ -24,7 +24,6 @@ func (te TypedEnum) GetSchema() model.RuleFunctionSchema {

// RunRule will execute the TypedEnum rule, based on supplied context and a supplied []*yaml.Node slice.
func (te TypedEnum) RunRule(nodes []*yaml.Node, context model.RuleFunctionContext) []model.RuleFunctionResult {

if len(nodes) <= 0 {
return nil
}
Expand All @@ -34,8 +33,17 @@ func (te TypedEnum) RunRule(nodes []*yaml.Node, context model.RuleFunctionContex
enums := context.Index.GetAllEnums()

for _, enum := range enums {
var enumType string
if enum.Type.Value != "" {
enumType = enum.Type.Value
} else if len(enum.Type.Content) == 2 {
for _, n := range enum.Type.Content {
if n.Value != "null" {
enumType = n.Value
}
}
}

enumType := enum.Type.Value
enumDataNode := enum.Node

// extract types into an array and have them checked against the spec.
Expand Down
40 changes: 35 additions & 5 deletions functions/openapi/typed_enum_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package openapi

import (
"testing"

"github.com/daveshanley/vacuum/model"
"github.com/pb33f/libopenapi/index"
"github.com/pb33f/libopenapi/utils"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
"testing"
)

func TestTypedEnum_GetSchema(t *testing.T) {
Expand All @@ -21,7 +22,6 @@ func TestTypedEnum_RunRule(t *testing.T) {
}

func TestTypedEnum_RunRule_SuccessCheck(t *testing.T) {

yml := `paths:
/pizza/:
parameters:
Expand Down Expand Up @@ -61,8 +61,35 @@ components:
assert.Len(t, res, 0)
}

func TestTypedEnums_RunRule_ThreeValue_WrongType(t *testing.T) {
func TestTypedEnum_RunRule_31NullableEnum_SuccessCheck(t *testing.T) {
yml := `paths:
/pizza/:
parameters:
- in: query
name: party
schema:
type: [string, null]
enum: [big, small, null]`

path := "$"

var rootNode yaml.Node
mErr := yaml.Unmarshal([]byte(yml), &rootNode)
assert.NoError(t, mErr)

nodes, _ := utils.FindNodes([]byte(yml), path)

rule := buildOpenApiTestRuleAction(path, "typed_enum", "", nil)
ctx := buildOpenApiTestContext(model.CastToRuleAction(rule.Then), nil)
ctx.Index = index.NewSpecIndex(&rootNode)

def := TypedEnum{}
res := def.RunRule(nodes, ctx)

assert.Len(t, res, 0)
}

func TestTypedEnums_RunRule_ThreeValue_WrongType(t *testing.T) {
yml := `paths:
/pizza/:
parameters:
Expand All @@ -82,7 +109,10 @@ components:
schemas:
YesNo:
type: string
enum: [yes, true]`
enum: [yes, true]
TooManyTypes:
type: [string, integer, null]
enum: [hi, 1, null]`

path := "$"

Expand All @@ -99,5 +129,5 @@ components:
def := TypedEnum{}
res := def.RunRule(nodes, ctx)

assert.Len(t, res, 3)
assert.Len(t, res, 5)
}

0 comments on commit a8b6170

Please sign in to comment.