From 0e71f2fcbd6f3ddb29a9b7922ff1d20138ed0168 Mon Sep 17 00:00:00 2001 From: quobix Date: Fri, 19 Jan 2024 19:52:10 -0500 Subject: [PATCH] Addresses #424 and #423 An off my one issue, I used the wrong comparitor and then did not build the correct tests required that would have caight it. This has now been fixed. Signed-off-by: quobix --- functions/openapi/schema_type.go | 8 +- functions/openapi/schema_type_test.go | 162 +++++++++++++++++++++++++- go.mod | 2 +- go.sum | 4 +- 4 files changed, 168 insertions(+), 8 deletions(-) diff --git a/functions/openapi/schema_type.go b/functions/openapi/schema_type.go index 95f286ca..ce8e54c3 100644 --- a/functions/openapi/schema_type.go +++ b/functions/openapi/schema_type.go @@ -83,7 +83,7 @@ func (st SchemaTypeCheck) validateNumber(schema *base.Schema, context *model.Rul } if schema.Value.Minimum != nil { - if *schema.Value.Minimum <= 0 { + if *schema.Value.Minimum < 0 { result := st.buildResult("`minimum` should be a number greater than or equal to `0`", fmt.Sprintf("%s.%s", schema.GenerateJSONPath(), "minimum"), schema, schema.Value.GoLow().Minimum.KeyNode, context) @@ -92,7 +92,7 @@ func (st SchemaTypeCheck) validateNumber(schema *base.Schema, context *model.Rul } if schema.Value.Maximum != nil { - if *schema.Value.Maximum <= 0 { + if *schema.Value.Maximum < 0 { result := st.buildResult("`maximum` should be a number greater than or equal to `0`", fmt.Sprintf("%s.%s", schema.GenerateJSONPath(), "maximum"), schema, schema.Value.GoLow().Maximum.KeyNode, context) @@ -109,7 +109,7 @@ func (st SchemaTypeCheck) validateNumber(schema *base.Schema, context *model.Rul } if schema.Value.ExclusiveMinimum != nil { - if schema.Value.ExclusiveMinimum.IsB() && schema.Value.ExclusiveMinimum.B <= 0 { + if schema.Value.ExclusiveMinimum.IsB() && schema.Value.ExclusiveMinimum.B < 0 { result := st.buildResult("`exclusiveMinimum` should be a number greater than or equal to `0`", fmt.Sprintf("%s.%s", schema.GenerateJSONPath(), "exclusiveMinimum"), schema, schema.Value.GoLow().ExclusiveMinimum.KeyNode, context) @@ -118,7 +118,7 @@ func (st SchemaTypeCheck) validateNumber(schema *base.Schema, context *model.Rul } if schema.Value.ExclusiveMaximum != nil { - if schema.Value.ExclusiveMaximum.IsB() && schema.Value.ExclusiveMaximum.B <= 0 { + if schema.Value.ExclusiveMaximum.IsB() && schema.Value.ExclusiveMaximum.B < 0 { result := st.buildResult("`exclusiveMaximum` should be a number greater than or equal to `0`", fmt.Sprintf("%s.%s", schema.GenerateJSONPath(), "exclusiveMaximum"), schema, schema.Value.GoLow().ExclusiveMaximum.KeyNode, context) diff --git a/functions/openapi/schema_type_test.go b/functions/openapi/schema_type_test.go index e32bb6fb..5c51167a 100644 --- a/functions/openapi/schema_type_test.go +++ b/functions/openapi/schema_type_test.go @@ -282,6 +282,70 @@ components: assert.Equal(t, "$.components.schemas['Gum'].minimum", res[0].Path) } +func TestSchemaType_Minimum_Zero(t *testing.T) { + + yml := `openapi: 3.1 +components: + schemas: + Gum: + type: number + minimum: 0` + + 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, "schema-type-check", "", nil) + ctx := buildOpenApiTestContext(model.CastToRuleAction(rule.Then), nil) + + ctx.Document = document + ctx.DrDocument = drDocument + ctx.Rule = &rule + + def := SchemaTypeCheck{} + res := def.RunRule(nil, ctx) + + assert.Len(t, res, 0) +} + +func TestSchemaType_Maximum_Zero(t *testing.T) { + + yml := `openapi: 3.1 +components: + schemas: + Gum: + type: number + maximum: 0` + + 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, "schema-type-check", "", nil) + ctx := buildOpenApiTestContext(model.CastToRuleAction(rule.Then), nil) + + ctx.Document = document + ctx.DrDocument = drDocument + ctx.Rule = &rule + + def := SchemaTypeCheck{} + res := def.RunRule(nil, ctx) + + assert.Len(t, res, 0) +} + func TestSchemaType_Maximum(t *testing.T) { yml := `openapi: 3.1 @@ -289,7 +353,39 @@ components: schemas: Gum: type: number - maximum: -2` + maximum: 0` + + 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, "schema-type-check", "", nil) + ctx := buildOpenApiTestContext(model.CastToRuleAction(rule.Then), nil) + + ctx.Document = document + ctx.DrDocument = drDocument + ctx.Rule = &rule + + def := SchemaTypeCheck{} + res := def.RunRule(nil, ctx) + + assert.Len(t, res, 0) +} + +func TestSchemaType_Maximum_Negative(t *testing.T) { + + yml := `openapi: 3.1 +components: + schemas: + Gum: + type: number + maximum: -50` document, err := libopenapi.NewDocument([]byte(yml)) if err != nil { @@ -385,6 +481,38 @@ components: assert.Equal(t, "$.components.schemas['Gum'].exclusiveMinimum", res[0].Path) } +func TestSchemaType_ExclusiveMinimum_Zero(t *testing.T) { + + yml := `openapi: 3.1 +components: + schemas: + Gum: + type: number + exclusiveMinimum: 0` + + 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, "schema-type-check", "", nil) + ctx := buildOpenApiTestContext(model.CastToRuleAction(rule.Then), nil) + + ctx.Document = document + ctx.DrDocument = drDocument + ctx.Rule = &rule + + def := SchemaTypeCheck{} + res := def.RunRule(nil, ctx) + assert.Len(t, res, 0) + +} + func TestSchemaType_ExclusiveMaximum(t *testing.T) { yml := `openapi: 3.1 @@ -419,6 +547,38 @@ components: assert.Equal(t, "$.components.schemas['Gum'].exclusiveMaximum", res[0].Path) } +func TestSchemaType_ExclusiveMaximum_Zero(t *testing.T) { + + yml := `openapi: 3.1 +components: + schemas: + Gum: + type: number + exclusiveMaximum: 0` + + 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, "schema-type-check", "", nil) + ctx := buildOpenApiTestContext(model.CastToRuleAction(rule.Then), nil) + + ctx.Document = document + ctx.DrDocument = drDocument + ctx.Rule = &rule + + def := SchemaTypeCheck{} + res := def.RunRule(nil, ctx) + + assert.Len(t, res, 0) +} + func TestSchemaType_ExclusiveMinMaximum(t *testing.T) { yml := `openapi: 3.1 diff --git a/go.mod b/go.mod index 742f9c91..6eee74f2 100644 --- a/go.mod +++ b/go.mod @@ -12,7 +12,7 @@ require ( github.com/json-iterator/go v1.1.12 github.com/mitchellh/mapstructure v1.5.0 github.com/pb33f/doctor v0.0.3 - github.com/pb33f/libopenapi v0.15.0 + github.com/pb33f/libopenapi v0.15.1 github.com/pb33f/libopenapi-validator v0.0.40 github.com/pterm/pterm v0.12.75 github.com/santhosh-tekuri/jsonschema/v5 v5.3.1 diff --git a/go.sum b/go.sum index 39416335..01f1aa4c 100644 --- a/go.sum +++ b/go.sum @@ -152,8 +152,8 @@ 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.3 h1:EeZhRssFoqIgRswVyGYAK0l+AQRQ3akrRCDJEa5z+Yg= github.com/pb33f/doctor v0.0.3/go.mod h1:yBs5hFHAoo/eeFvKN9sWwmHmgEPJ2SaotYOJc05GdMU= -github.com/pb33f/libopenapi v0.15.0 h1:AoBYIY3HXqDDF8O9kcudlqWaRFZZJmgtueE649oHzIw= -github.com/pb33f/libopenapi v0.15.0/go.mod h1:m+4Pwri31UvcnZjuP8M7TlbR906DXJmMvYsbis234xg= +github.com/pb33f/libopenapi v0.15.1 h1:DNWGhDDsvffPS8DhLkYErKZzbbtSvT7oZNBQSldWf+8= +github.com/pb33f/libopenapi v0.15.1/go.mod h1:m+4Pwri31UvcnZjuP8M7TlbR906DXJmMvYsbis234xg= github.com/pb33f/libopenapi-validator v0.0.40 h1:oS/kPLnzX0GgtjrQkwsaqN2m/xHiYONWqRQ57gQ2K5U= github.com/pb33f/libopenapi-validator v0.0.40/go.mod h1:VZ95obL+qvwoe6H2mhPW6NjJ3ip9q6BhP45sZupU7wM= github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4=