Skip to content

Commit

Permalink
Replace deprecated io/ioutil with io (#840)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandear authored Sep 16, 2023
1 parent d14f7e0 commit fbee2c5
Show file tree
Hide file tree
Showing 13 changed files with 22 additions and 30 deletions.
4 changes: 2 additions & 2 deletions openapi3/loader_uri_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package openapi3
import (
"errors"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -64,7 +64,7 @@ func ReadFromHTTP(cl *http.Client) ReadFromURIFunc {
if resp.StatusCode > 399 {
return nil, fmt.Errorf("error loading %q: request returned status code %d", location.String(), resp.StatusCode)
}
return ioutil.ReadAll(resp.Body)
return io.ReadAll(resp.Body)
}
}

Expand Down
4 changes: 2 additions & 2 deletions openapi3filter/issue639_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package openapi3filter

import (
"encoding/json"
"io/ioutil"
"io"
"net/http"
"strings"
"testing"
Expand Down Expand Up @@ -87,7 +87,7 @@ func TestIssue639(t *testing.T) {
}
err = ValidateRequest(ctx, requestValidationInput)
require.NoError(t, err)
bodyAfterValidation, err := ioutil.ReadAll(httpReq.Body)
bodyAfterValidation, err := io.ReadAll(httpReq.Body)
require.NoError(t, err)

raw := map[string]interface{}{}
Expand Down
3 changes: 1 addition & 2 deletions openapi3filter/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package openapi3filter
import (
"bytes"
"io"
"io/ioutil"
"log"
"net/http"

Expand Down Expand Up @@ -149,7 +148,7 @@ func (v *Validator) Middleware(h http.Handler) http.Handler {
RequestValidationInput: requestValidationInput,
Status: wr.statusCode(),
Header: wr.Header(),
Body: ioutil.NopCloser(bytes.NewBuffer(wr.bodyContents())),
Body: io.NopCloser(bytes.NewBuffer(wr.bodyContents())),
Options: &v.options,
}); err != nil {
v.logFunc("invalid response", err)
Expand Down
5 changes: 2 additions & 3 deletions openapi3filter/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"path"
Expand Down Expand Up @@ -391,7 +390,7 @@ func TestValidator(t *testing.T) {
require.Equalf(t, test.response.statusCode, resp.StatusCode,
"response code expect %d got %d", test.response.statusCode, resp.StatusCode)

body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
require.NoError(t, err, "failed to read response body")
require.Equalf(t, test.response.body, string(body),
"response body expect %q got %q", test.response.body, string(body))
Expand Down Expand Up @@ -507,7 +506,7 @@ paths:
panic(err)
}
defer resp.Body.Close()
contents, err := ioutil.ReadAll(resp.Body)
contents, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
Expand Down
7 changes: 3 additions & 4 deletions openapi3filter/req_resp_decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"mime"
"mime/multipart"
"net/http"
Expand Down Expand Up @@ -1019,7 +1018,7 @@ func init() {
}

func plainBodyDecoder(body io.Reader, header http.Header, schema *openapi3.SchemaRef, encFn EncodingFn) (interface{}, error) {
data, err := ioutil.ReadAll(body)
data, err := io.ReadAll(body)
if err != nil {
return nil, &ParseError{Kind: KindInvalidFormat, Cause: err}
}
Expand Down Expand Up @@ -1064,7 +1063,7 @@ func urlencodedBodyDecoder(body io.Reader, header http.Header, schema *openapi3.
}

// Parse form.
b, err := ioutil.ReadAll(body)
b, err := io.ReadAll(body)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1216,7 +1215,7 @@ func multipartBodyDecoder(body io.Reader, header http.Header, schema *openapi3.S

// FileBodyDecoder is a body decoder that decodes a file body to a string.
func FileBodyDecoder(body io.Reader, header http.Header, schema *openapi3.SchemaRef, encFn EncodingFn) (interface{}, error) {
data, err := ioutil.ReadAll(body)
data, err := io.ReadAll(body)
if err != nil {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions openapi3filter/req_resp_decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"net/textproto"
Expand Down Expand Up @@ -1337,7 +1336,7 @@ func TestRegisterAndUnregisterBodyDecoder(t *testing.T) {
var decoder BodyDecoder
decoder = func(body io.Reader, h http.Header, schema *openapi3.SchemaRef, encFn EncodingFn) (decoded interface{}, err error) {
var data []byte
if data, err = ioutil.ReadAll(body); err != nil {
if data, err = io.ReadAll(body); err != nil {
return
}
return strings.Split(string(data), ","), nil
Expand Down
3 changes: 1 addition & 2 deletions openapi3filter/validate_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"sort"

Expand Down Expand Up @@ -208,7 +207,7 @@ func ValidateRequestBody(ctx context.Context, input *RequestValidationInput, req
if req.Body != http.NoBody && req.Body != nil {
defer req.Body.Close()
var err error
if data, err = ioutil.ReadAll(req.Body); err != nil {
if data, err = io.ReadAll(req.Body); err != nil {
return &RequestError{
Input: input,
RequestBody: requestBody,
Expand Down
4 changes: 2 additions & 2 deletions openapi3filter/validate_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"bytes"
"context"
"fmt"
"io/ioutil"
"io"
"net/http"
"sort"
"strings"
Expand Down Expand Up @@ -125,7 +125,7 @@ func ValidateResponse(ctx context.Context, input *ResponseValidationInput) error
defer body.Close()

// Read all
data, err := ioutil.ReadAll(body)
data, err := io.ReadAll(body)
if err != nil {
return &ResponseError{
Input: input,
Expand Down
3 changes: 1 addition & 2 deletions openapi3filter/validate_response_input.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package openapi3filter
import (
"bytes"
"io"
"io/ioutil"
"net/http"
)

Expand All @@ -16,7 +15,7 @@ type ResponseValidationInput struct {
}

func (input *ResponseValidationInput) SetBodyBytes(value []byte) *ResponseValidationInput {
input.Body = ioutil.NopCloser(bytes.NewReader(value))
input.Body = io.NopCloser(bytes.NewReader(value))
return input
}

Expand Down
4 changes: 2 additions & 2 deletions openapi3filter/validate_set_default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package openapi3filter
import (
"bytes"
"encoding/json"
"io/ioutil"
"io"
"net/http"
"net/url"
"testing"
Expand Down Expand Up @@ -795,7 +795,7 @@ func TestValidateRequestBodyAndSetDefault(t *testing.T) {
})
require.NoError(t, err)

validatedReqBody, err := ioutil.ReadAll(httpReq.Body)
validatedReqBody, err := io.ReadAll(httpReq.Body)
require.NoError(t, err)
tc.bodyAssertion(t, string(validatedReqBody))
})
Expand Down
5 changes: 2 additions & 3 deletions openapi3filter/validation_error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -657,7 +656,7 @@ func TestValidationHandler_ServeHTTP(t *testing.T) {
encoder := &ValidationErrorEncoder{Encoder: (ErrorEncoder)(DefaultErrorEncoder)}
resp := runTest_ServeHTTP(t, handler, encoder.Encode, r)

body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
require.Equal(t, http.StatusUnprocessableEntity, resp.StatusCode)
require.Equal(t, "[422][][] value must be an array [source pointer=/photoUrls]", string(body))
Expand Down Expand Up @@ -699,7 +698,7 @@ func TestValidationHandler_Middleware(t *testing.T) {
encoder := &ValidationErrorEncoder{Encoder: (ErrorEncoder)(DefaultErrorEncoder)}
resp := runTest_Middleware(t, handler, encoder.Encode, r)

body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
require.Equal(t, http.StatusUnprocessableEntity, resp.StatusCode)
require.Equal(t, "[422][][] value must be an array [source pointer=/photoUrls]", string(body))
Expand Down
3 changes: 1 addition & 2 deletions openapi3filter/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
Expand Down Expand Up @@ -365,7 +364,7 @@ func marshalReader(value interface{}) io.ReadCloser {
if err != nil {
panic(err)
}
return ioutil.NopCloser(bytes.NewReader(data))
return io.NopCloser(bytes.NewReader(data))
}

func TestValidateRequestBody(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions routers/issue356_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package routers_test

import (
"context"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"strings"
Expand Down Expand Up @@ -129,7 +129,7 @@ paths:
rep, err := http.DefaultClient.Do(req)
require.NoError(t, err)
defer rep.Body.Close()
body, err := ioutil.ReadAll(rep.Body)
body, err := io.ReadAll(rep.Body)
require.NoError(t, err)

if expectError {
Expand Down

0 comments on commit fbee2c5

Please sign in to comment.