Repository: getkin/kin-openapi
Pull request #510 (2022-03-13 12:49:15)

Add nilness check to CI
Fixes https://github.com/getkin/kin-openapi/issues/507


Patch:
diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml
index fc17de2..c652acf 100644
--- a/.github/workflows/go.yml
+++ b/.github/workflows/go.yml
@@ -82,6 +82,10 @@ jobs:
       run: |
         ! git grep -InE 'json:"' | grep -v _test.go | grep -v yaml:
 
+    - if: runner.os == 'Linux' && matrix.go != '1.16'
+      name: nilness
+      run: go run golang.org/x/tools/go/analysis/passes/nilness/cmd/nilness@latest ./...
+
     - if: runner.os == 'Linux'
       name: Missing specification object link to definition
       run: |
diff --git a/openapi3/schema.go b/openapi3/schema.go
index 73506f7..1b500b7 100644
--- a/openapi3/schema.go
+++ b/openapi3/schema.go
@@ -795,8 +795,6 @@ func (schema *Schema) visitJSON(settings *schemaValidationSettings, value interf
 	}
 
 	switch value := value.(type) {
-	case nil:
-		return schema.visitJSONNull(settings)
 	case bool:
 		return schema.visitJSONBoolean(settings, value)
 	case float64:
@@ -871,7 +869,12 @@ func (schema *Schema) visitSetOperations(settings *schemaValidationSettings, val
 					return errors.New("input does not contain the discriminator property")
 				}
 
-				if discriminatorRef, okcheck = schema.Discriminator.Mapping[discriminatorVal.(string)]; len(schema.Discriminator.Mapping) > 0 && !okcheck {
+				discriminatorValString, okcheck := discriminatorVal.(string)
+				if !okcheck {
+					return errors.New("descriminator value is not a string")
+				}
+
+				if discriminatorRef, okcheck = schema.Discriminator.Mapping[discriminatorValString]; len(schema.Discriminator.Mapping) > 0 && !okcheck {
 					return errors.New("input does not contain a valid discriminator value")
 				}
 			}
@@ -1416,7 +1419,7 @@ func (schema *Schema) visitJSONObject(settings *schemaValidationSettings, value
 			}
 		}
 		allowed := schema.AdditionalPropertiesAllowed
-		if additionalProperties != nil || allowed == nil || (allowed != nil && *allowed) {
+		if additionalProperties != nil || allowed == nil || *allowed {
 			if additionalProperties != nil {
 				if err := additionalProperties.visitJSON(settings, v); err != nil {
 					if settings.failfast {


Test patch:
diff --git a/openapi3/schema_oneOf_test.go b/openapi3/schema_oneOf_test.go
index 03fb670..7faf268 100644
--- a/openapi3/schema_oneOf_test.go
+++ b/openapi3/schema_oneOf_test.go
@@ -116,3 +116,21 @@ func TestVisitJSON_OneOf_NoDiscriptor_MissingField(t *testing.T) {
 	})
 	require.EqualError(t, err, "doesn't match schema due to: Error at \"/scratches\": property \"scratches\" is missing\nSchema:\n  {\n    \"properties\": {\n      \"name\": {\n        \"type\": \"string\"\n      },\n      \"scratches\": {\n        \"type\": \"boolean\"\n      }\n    },\n    \"required\": [\n      \"name\",\n      \"scratches\"\n    ],\n    \"type\": \"object\"\n  }\n\nValue:\n  {\n    \"name\": \"snoopy\"\n  }\n Or Error at \"/barks\": property \"barks\" is missing\nSchema:\n  {\n    \"properties\": {\n      \"barks\": {\n        \"type\": \"boolean\"\n      },\n      \"name\": {\n        \"type\": \"string\"\n      }\n    },\n    \"required\": [\n      \"name\",\n      \"barks\"\n    ],\n    \"type\": \"object\"\n  }\n\nValue:\n  {\n    \"name\": \"snoopy\"\n  }\n")
 }
+
+func TestVisitJSON_OneOf_BadDescriminatorType(t *testing.T) {
+	s, err := NewLoader().LoadFromData(oneofSpec)
+	require.NoError(t, err)
+	err = s.Components.Schemas["Animal"].Value.VisitJSON(map[string]interface{}{
+		"name":      "snoopy",
+		"scratches": true,
+		"$type":     1,
+	})
+	require.EqualError(t, err, "descriminator value is not a string")
+
+	err = s.Components.Schemas["Animal"].Value.VisitJSON(map[string]interface{}{
+		"name":  "snoopy",
+		"barks": true,
+		"$type": nil,
+	})
+	require.EqualError(t, err, "descriminator value is not a string")
+}