Repository: getkin/kin-openapi
Pull request #691 (2022-12-03 15:52:46)

Use os.ReadFile instead of ioutil.ReadFile


Patch:
diff --git a/openapi3/loader_uri_reader.go b/openapi3/loader_uri_reader.go
index 92ac043..251d409 100644
--- a/openapi3/loader_uri_reader.go
+++ b/openapi3/loader_uri_reader.go
@@ -6,6 +6,7 @@ import (
 	"io/ioutil"
 	"net/http"
 	"net/url"
+	"os"
 	"path/filepath"
 	"sync"
 )
@@ -75,7 +76,7 @@ func ReadFromFile(loader *Loader, location *url.URL) ([]byte, error) {
 	if location.Scheme != "" && location.Scheme != "file" {
 		return nil, ErrURINotSupported
 	}
-	return ioutil.ReadFile(location.Path)
+	return os.ReadFile(location.Path)
 }
 
 // URIMapCache returns a ReadFromURIFunc that caches the contents read from URI
diff --git a/openapi3/schema.go b/openapi3/schema.go
index 0dde607..29da9ef 100644
--- a/openapi3/schema.go
+++ b/openapi3/schema.go
@@ -1149,6 +1149,10 @@ func (schema *Schema) visitSetOperations(settings *schemaValidationSettings, val
 				if v == f {
 					return
 				}
+			case int64:
+				if v == float64(c) {
+					return
+				}
 			default:
 				if reflect.DeepEqual(v, value) {
 					return


Test patch:
diff --git a/openapi2/openapi2_test.go b/openapi2/openapi2_test.go
index 65e92d6..8a6bf96 100644
--- a/openapi2/openapi2_test.go
+++ b/openapi2/openapi2_test.go
@@ -3,7 +3,7 @@ package openapi2_test
 import (
 	"encoding/json"
 	"fmt"
-	"io/ioutil"
+	"os"
 	"reflect"
 
 	"github.com/invopop/yaml"
@@ -12,7 +12,7 @@ import (
 )
 
 func Example() {
-	input, err := ioutil.ReadFile("testdata/swagger.json")
+	input, err := os.ReadFile("testdata/swagger.json")
 	if err != nil {
 		panic(err)
 	}
diff --git a/openapi3/issue241_test.go b/openapi3/issue241_test.go
index 14caa9b..0b37b0a 100644
--- a/openapi3/issue241_test.go
+++ b/openapi3/issue241_test.go
@@ -2,7 +2,7 @@ package openapi3_test
 
 import (
 	"bytes"
-	"io/ioutil"
+	"os"
 	"testing"
 
 	"github.com/stretchr/testify/require"
@@ -12,7 +12,7 @@ import (
 )
 
 func TestIssue241(t *testing.T) {
-	data, err := ioutil.ReadFile("testdata/issue241.yml")
+	data, err := os.ReadFile("testdata/issue241.yml")
 	require.NoError(t, err)
 
 	loader := openapi3.NewLoader()
diff --git a/openapi3/loader_read_from_uri_func_test.go b/openapi3/loader_read_from_uri_func_test.go
index 8fee2f4..50c060b 100644
--- a/openapi3/loader_read_from_uri_func_test.go
+++ b/openapi3/loader_read_from_uri_func_test.go
@@ -2,8 +2,8 @@ package openapi3
 
 import (
 	"fmt"
-	"io/ioutil"
 	"net/url"
+	"os"
 	"path/filepath"
 	"testing"
 
@@ -14,7 +14,7 @@ func TestLoaderReadFromURIFunc(t *testing.T) {
 	loader := NewLoader()
 	loader.IsExternalRefsAllowed = true
 	loader.ReadFromURIFunc = func(loader *Loader, url *url.URL) ([]byte, error) {
-		return ioutil.ReadFile(filepath.Join("testdata", url.Path))
+		return os.ReadFile(filepath.Join("testdata", url.Path))
 	}
 	doc, err := loader.LoadFromFile("recursiveRef/openapi.yml")
 	require.NoError(t, err)
diff --git a/openapi3filter/validation_enum_test.go b/openapi3filter/validation_enum_test.go
index 898c402..aae3a1f 100644
--- a/openapi3filter/validation_enum_test.go
+++ b/openapi3filter/validation_enum_test.go
@@ -12,7 +12,8 @@ import (
 )
 
 func TestValidationWithIntegerEnum(t *testing.T) {
-	const spec = `
+	t.Run("PUT Request", func(t *testing.T) {
+		const spec = `
 openapi: 3.0.0
 info:
   title: Example integer enum
@@ -41,56 +42,122 @@ paths:
           description: Ok
 `
 
-	loader := openapi3.NewLoader()
-	doc, err := loader.LoadFromData([]byte(spec))
-	require.NoError(t, err)
+		loader := openapi3.NewLoader()
+		doc, err := loader.LoadFromData([]byte(spec))
+		require.NoError(t, err)
 
-	router, err := legacyrouter.NewRouter(doc)
-	require.NoError(t, err)
+		router, err := legacyrouter.NewRouter(doc)
+		require.NoError(t, err)
 
-	tests := []struct {
-		data    []byte
-		wantErr bool
-	}{
-		{
-			[]byte(`{"exenum": 1}`),
-			false,
-		},
-		{
-			[]byte(`{"exenum": "1"}`),
-			true,
-		},
-		{
-			[]byte(`{"exenum": null}`),
-			false,
-		},
-		{
-			[]byte(`{}`),
-			false,
-		},
-	}
+		tests := []struct {
+			data    []byte
+			wantErr bool
+		}{
+			{
+				[]byte(`{"exenum": 1}`),
+				false,
+			},
+			{
+				[]byte(`{"exenum": "1"}`),
+				true,
+			},
+			{
+				[]byte(`{"exenum": null}`),
+				false,
+			},
+			{
+				[]byte(`{}`),
+				false,
+			},
+		}
 
-	for _, tt := range tests {
-		body := bytes.NewReader(tt.data)
-		req, err := http.NewRequest("PUT", "/sample", body)
+		for _, tt := range tests {
+			body := bytes.NewReader(tt.data)
+			req, err := http.NewRequest("PUT", "/sample", body)
+			require.NoError(t, err)
+			req.Header.Add(headerCT, "application/json")
+
+			route, pathParams, err := router.FindRoute(req)
+			require.NoError(t, err)
+
+			requestValidationInput := &RequestValidationInput{
+				Request:    req,
+				PathParams: pathParams,
+				Route:      route,
+			}
+			err = ValidateRequest(loader.Context, requestValidationInput)
+			if tt.wantErr {
+				require.Error(t, err)
+			} else {
+				require.NoError(t, err)
+			}
+		}
+	})
+
+	t.Run("GET Request", func(t *testing.T) {
+		const spec = `
+openapi: 3.0.0
+info:
+  title: Example integer enum
+  version: '0.1'
+paths:
+  /sample:
+    get:
+      parameters:
+        - in: query
+          name: exenum
+          schema:
+            type: integer
+            enum:
+              - 0
+              - 1
+              - 2
+              - 3
+      responses:
+        '200':
+          description: Ok
+`
+
+		loader := openapi3.NewLoader()
+		doc, err := loader.LoadFromData([]byte(spec))
 		require.NoError(t, err)
-		req.Header.Add(headerCT, "application/json")
 
-		route, pathParams, err := router.FindRoute(req)
+		router, err := legacyrouter.NewRouter(doc)
 		require.NoError(t, err)
 
-		requestValidationInput := &RequestValidationInput{
-			Request:    req,
-			PathParams: pathParams,
-			Route:      route,
+		tests := []struct {
+			exenum  string
+			wantErr bool
+		}{
+			{
+				"1",
+				false,
+			},
+			{
+				"4",
+				true,
+			},
 		}
-		err = ValidateRequest(loader.Context, requestValidationInput)
-		if tt.wantErr {
-			require.Error(t, err)
-		} else {
+
+		for _, tt := range tests {
+			req, err := http.NewRequest("GET", "/sample?exenum="+tt.exenum, nil)
+			require.NoError(t, err)
+			route, pathParams, err := router.FindRoute(req)
 			require.NoError(t, err)
+
+			requestValidationInput := &RequestValidationInput{
+				Request:    req,
+				PathParams: pathParams,
+				Route:      route,
+			}
+			err = ValidateRequest(loader.Context, requestValidationInput)
+			if tt.wantErr {
+				require.Error(t, err)
+			} else {
+				require.NoError(t, err)
+			}
 		}
-	}
+	})
 }
 
 func TestValidationWithStringEnum(t *testing.T) {