Skip to content

Commit 93f1edf

Browse files
committed
migrate deprecated io/ioutil package to io and os packages
1 parent 4a1477f commit 93f1edf

10 files changed

Lines changed: 43 additions & 44 deletions

File tree

cmd/oapi-codegen/oapi-codegen.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package main
1616
import (
1717
"flag"
1818
"fmt"
19-
"io/ioutil"
2019
"os"
2120
"path"
2221
"runtime/debug"
@@ -182,7 +181,7 @@ func main() {
182181
if !*oldConfigStyle {
183182
// We simply read the configuration from disk.
184183
if flagConfigFile != "" {
185-
buf, err := ioutil.ReadFile(flagConfigFile)
184+
buf, err := os.ReadFile(flagConfigFile)
186185
if err != nil {
187186
errExit("error reading config file '%s': %v", flagConfigFile, err)
188187
}
@@ -199,7 +198,7 @@ func main() {
199198
} else {
200199
var oldConfig oldConfiguration
201200
if flagConfigFile != "" {
202-
buf, err := ioutil.ReadFile(flagConfigFile)
201+
buf, err := os.ReadFile(flagConfigFile)
203202
if err != nil {
204203
errExit("error reading config file '%s': %v", flagConfigFile, err)
205204
}
@@ -241,7 +240,7 @@ func main() {
241240
}
242241

243242
if opts.OutputFile != "" {
244-
err = ioutil.WriteFile(opts.OutputFile, []byte(code), 0644)
243+
err = os.WriteFile(opts.OutputFile, []byte(code), 0644)
245244
if err != nil {
246245
errExit("error writing generated code to file: %s", err)
247246
}
@@ -257,7 +256,7 @@ func loadTemplateOverrides(templatesDir string) (map[string]string, error) {
257256
return templates, nil
258257
}
259258

260-
files, err := ioutil.ReadDir(templatesDir)
259+
files, err := os.ReadDir(templatesDir)
261260
if err != nil {
262261
return nil, err
263262
}
@@ -276,7 +275,7 @@ func loadTemplateOverrides(templatesDir string) (map[string]string, error) {
276275
}
277276
continue
278277
}
279-
data, err := ioutil.ReadFile(path.Join(templatesDir, f.Name()))
278+
data, err := os.ReadFile(path.Join(templatesDir, f.Name()))
280279
if err != nil {
281280
return nil, err
282281
}

internal/test/issues/issue-grab_import_names/issue.gen.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/test/issues/issue-grab_import_names/spec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ paths:
1010
parameters:
1111
- name: Foo
1212
in: header
13-
description: base64. bytes. chi. context. echo. errors. fmt. gzip. http. io. ioutil. json. openapi3.
13+
description: base64. bytes. chi. context. echo. errors. fmt. gzip. http. io. json. openapi3.
1414
schema:
1515
type: string
1616
required: false
@@ -27,4 +27,4 @@ paths:
2727
content:
2828
application/json:
2929
schema:
30-
type: string
30+
type: string

internal/test/server/server_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package server
22

33
import (
44
"errors"
5-
"io/ioutil"
5+
"io"
66
"net/http"
77
"net/http/httptest"
88
"testing"
@@ -55,7 +55,7 @@ func TestErrorHandlerFuncBackwardsCompatible(t *testing.T) {
5555
defer s.Close()
5656

5757
req, err := http.DefaultClient.Get(s.URL + "/get-with-args")
58-
b, _ := ioutil.ReadAll(req.Body)
58+
b, _ := io.ReadAll(req.Body)
5959
assert.Nil(t, err)
6060
assert.Equal(t, "text/plain; charset=utf-8", req.Header.Get("Content-Type"))
6161
assert.Equal(t, "Query argument required_argument is required, but not found\n", string(b))

pkg/chi-middleware/oapi_validate_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"context"
55
_ "embed"
66
"errors"
7-
"io/ioutil"
7+
"io"
88
"net/http"
99
"net/http/httptest"
1010
"net/url"
@@ -96,7 +96,7 @@ func TestOapiRequestValidatorWithOptionsMultiError(t *testing.T) {
9696
{
9797
rec := doGet(t, r, "http://deepmap.ai/multiparamresource?id=50")
9898
assert.Equal(t, http.StatusBadRequest, rec.Code)
99-
body, err := ioutil.ReadAll(rec.Body)
99+
body, err := io.ReadAll(rec.Body)
100100
if assert.NoError(t, err) {
101101
assert.Contains(t, string(body), "parameter \"id2\"")
102102
assert.Contains(t, string(body), "value is required but missing")
@@ -110,7 +110,7 @@ func TestOapiRequestValidatorWithOptionsMultiError(t *testing.T) {
110110
{
111111
rec := doGet(t, r, "http://deepmap.ai/multiparamresource")
112112
assert.Equal(t, http.StatusBadRequest, rec.Code)
113-
body, err := ioutil.ReadAll(rec.Body)
113+
body, err := io.ReadAll(rec.Body)
114114
if assert.NoError(t, err) {
115115
assert.Contains(t, string(body), "parameter \"id\"")
116116
assert.Contains(t, string(body), "value is required but missing")
@@ -126,7 +126,7 @@ func TestOapiRequestValidatorWithOptionsMultiError(t *testing.T) {
126126
{
127127
rec := doGet(t, r, "http://deepmap.ai/multiparamresource?id=500")
128128
assert.Equal(t, http.StatusBadRequest, rec.Code)
129-
body, err := ioutil.ReadAll(rec.Body)
129+
body, err := io.ReadAll(rec.Body)
130130
if assert.NoError(t, err) {
131131
assert.Contains(t, string(body), "parameter \"id\"")
132132
assert.Contains(t, string(body), "number must be at most 100")
@@ -142,7 +142,7 @@ func TestOapiRequestValidatorWithOptionsMultiError(t *testing.T) {
142142
{
143143
rec := doGet(t, r, "http://deepmap.ai/multiparamresource?id=abc&id2=1")
144144
assert.Equal(t, http.StatusBadRequest, rec.Code)
145-
body, err := ioutil.ReadAll(rec.Body)
145+
body, err := io.ReadAll(rec.Body)
146146
if assert.NoError(t, err) {
147147
assert.Contains(t, string(body), "parameter \"id\"")
148148
assert.Contains(t, string(body), "parsing \"abc\": invalid syntax")
@@ -198,7 +198,7 @@ func TestOapiRequestValidatorWithOptionsMultiErrorAndCustomHandler(t *testing.T)
198198
{
199199
rec := doGet(t, r, "http://deepmap.ai/multiparamresource?id=50")
200200
assert.Equal(t, http.StatusTeapot, rec.Code)
201-
body, err := ioutil.ReadAll(rec.Body)
201+
body, err := io.ReadAll(rec.Body)
202202
if assert.NoError(t, err) {
203203
assert.Contains(t, string(body), "parameter \"id2\"")
204204
assert.Contains(t, string(body), "value is required but missing")
@@ -212,7 +212,7 @@ func TestOapiRequestValidatorWithOptionsMultiErrorAndCustomHandler(t *testing.T)
212212
{
213213
rec := doGet(t, r, "http://deepmap.ai/multiparamresource")
214214
assert.Equal(t, http.StatusTeapot, rec.Code)
215-
body, err := ioutil.ReadAll(rec.Body)
215+
body, err := io.ReadAll(rec.Body)
216216
if assert.NoError(t, err) {
217217
assert.Contains(t, string(body), "parameter \"id\"")
218218
assert.Contains(t, string(body), "value is required but missing")
@@ -228,7 +228,7 @@ func TestOapiRequestValidatorWithOptionsMultiErrorAndCustomHandler(t *testing.T)
228228
{
229229
rec := doGet(t, r, "http://deepmap.ai/multiparamresource?id=500")
230230
assert.Equal(t, http.StatusTeapot, rec.Code)
231-
body, err := ioutil.ReadAll(rec.Body)
231+
body, err := io.ReadAll(rec.Body)
232232
if assert.NoError(t, err) {
233233
assert.Contains(t, string(body), "parameter \"id\"")
234234
assert.Contains(t, string(body), "number must be at most 100")
@@ -244,7 +244,7 @@ func TestOapiRequestValidatorWithOptionsMultiErrorAndCustomHandler(t *testing.T)
244244
{
245245
rec := doGet(t, r, "http://deepmap.ai/multiparamresource?id=abc&id2=1")
246246
assert.Equal(t, http.StatusTeapot, rec.Code)
247-
body, err := ioutil.ReadAll(rec.Body)
247+
body, err := io.ReadAll(rec.Body)
248248
if assert.NoError(t, err) {
249249
assert.Contains(t, string(body), "parameter \"id\"")
250250
assert.Contains(t, string(body), "parsing \"abc\": invalid syntax")

pkg/codegen/codegen_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"bytes"
55
_ "embed"
66
"go/format"
7-
"io/ioutil"
7+
"io"
88
"net/http"
99
"testing"
1010

@@ -114,7 +114,7 @@ func TestExamplePetStoreParseFunction(t *testing.T) {
114114

115115
cannedResponse := &http.Response{
116116
StatusCode: 200,
117-
Body: ioutil.NopCloser(bytes.NewReader(bodyBytes)),
117+
Body: io.NopCloser(bytes.NewReader(bodyBytes)),
118118
Header: http.Header{},
119119
}
120120
cannedResponse.Header.Add("Content-type", "application/json")

pkg/gin-middleware/oapi_validate.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
"context"
1919
"errors"
2020
"fmt"
21-
"io/ioutil"
21+
"os"
2222
"net/http"
2323
"strings"
2424

@@ -36,7 +36,7 @@ const (
3636

3737
// Create validator middleware from a YAML file path
3838
func OapiValidatorFromYamlFile(path string) (gin.HandlerFunc, error) {
39-
data, err := ioutil.ReadFile(path)
39+
data, err := os.ReadFile(path)
4040
if err != nil {
4141
return nil, fmt.Errorf("error reading %s: %s", path, err)
4242
}

pkg/gin-middleware/oapi_validate_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
_ "embed"
2020
"errors"
2121
"fmt"
22-
"io/ioutil"
22+
"io"
2323
"net/http"
2424
"net/http/httptest"
2525
"net/url"
@@ -246,7 +246,7 @@ func TestOapiRequestValidatorWithOptionsMultiError(t *testing.T) {
246246
{
247247
rec := doGet(t, g, "http://deepmap.ai/multiparamresource?id=50")
248248
assert.Equal(t, http.StatusBadRequest, rec.Code)
249-
body, err := ioutil.ReadAll(rec.Body)
249+
body, err := io.ReadAll(rec.Body)
250250
if assert.NoError(t, err) {
251251
assert.Contains(t, string(body), "multiple errors encountered")
252252
assert.Contains(t, string(body), "parameter \\\"id2\\\"")
@@ -261,7 +261,7 @@ func TestOapiRequestValidatorWithOptionsMultiError(t *testing.T) {
261261
{
262262
rec := doGet(t, g, "http://deepmap.ai/multiparamresource")
263263
assert.Equal(t, http.StatusBadRequest, rec.Code)
264-
body, err := ioutil.ReadAll(rec.Body)
264+
body, err := io.ReadAll(rec.Body)
265265
if assert.NoError(t, err) {
266266
assert.Contains(t, string(body), "multiple errors encountered")
267267
assert.Contains(t, string(body), "parameter \\\"id\\\"")
@@ -278,7 +278,7 @@ func TestOapiRequestValidatorWithOptionsMultiError(t *testing.T) {
278278
{
279279
rec := doGet(t, g, "http://deepmap.ai/multiparamresource?id=500")
280280
assert.Equal(t, http.StatusBadRequest, rec.Code)
281-
body, err := ioutil.ReadAll(rec.Body)
281+
body, err := io.ReadAll(rec.Body)
282282
if assert.NoError(t, err) {
283283
assert.Contains(t, string(body), "multiple errors encountered")
284284
assert.Contains(t, string(body), "parameter \\\"id\\\"")
@@ -295,7 +295,7 @@ func TestOapiRequestValidatorWithOptionsMultiError(t *testing.T) {
295295
{
296296
rec := doGet(t, g, "http://deepmap.ai/multiparamresource?id=abc&id2=1")
297297
assert.Equal(t, http.StatusBadRequest, rec.Code)
298-
body, err := ioutil.ReadAll(rec.Body)
298+
body, err := io.ReadAll(rec.Body)
299299
if assert.NoError(t, err) {
300300
assert.Contains(t, string(body), "multiple errors encountered")
301301
assert.Contains(t, string(body), "parameter \\\"id\\\"")
@@ -352,7 +352,7 @@ func TestOapiRequestValidatorWithOptionsMultiErrorAndCustomHandler(t *testing.T)
352352
{
353353
rec := doGet(t, g, "http://deepmap.ai/multiparamresource?id=50")
354354
assert.Equal(t, http.StatusBadRequest, rec.Code)
355-
body, err := ioutil.ReadAll(rec.Body)
355+
body, err := io.ReadAll(rec.Body)
356356
if assert.NoError(t, err) {
357357
assert.Contains(t, string(body), "Bad stuff")
358358
assert.Contains(t, string(body), "parameter \\\"id2\\\"")
@@ -367,7 +367,7 @@ func TestOapiRequestValidatorWithOptionsMultiErrorAndCustomHandler(t *testing.T)
367367
{
368368
rec := doGet(t, g, "http://deepmap.ai/multiparamresource")
369369
assert.Equal(t, http.StatusBadRequest, rec.Code)
370-
body, err := ioutil.ReadAll(rec.Body)
370+
body, err := io.ReadAll(rec.Body)
371371
if assert.NoError(t, err) {
372372
assert.Contains(t, string(body), "Bad stuff")
373373
assert.Contains(t, string(body), "parameter \\\"id\\\"")
@@ -384,7 +384,7 @@ func TestOapiRequestValidatorWithOptionsMultiErrorAndCustomHandler(t *testing.T)
384384
{
385385
rec := doGet(t, g, "http://deepmap.ai/multiparamresource?id=500")
386386
assert.Equal(t, http.StatusBadRequest, rec.Code)
387-
body, err := ioutil.ReadAll(rec.Body)
387+
body, err := io.ReadAll(rec.Body)
388388
if assert.NoError(t, err) {
389389
assert.Contains(t, string(body), "Bad stuff")
390390
assert.Contains(t, string(body), "parameter \\\"id\\\"")
@@ -401,7 +401,7 @@ func TestOapiRequestValidatorWithOptionsMultiErrorAndCustomHandler(t *testing.T)
401401
{
402402
rec := doGet(t, g, "http://deepmap.ai/multiparamresource?id=abc&id2=1")
403403
assert.Equal(t, http.StatusBadRequest, rec.Code)
404-
body, err := ioutil.ReadAll(rec.Body)
404+
body, err := io.ReadAll(rec.Body)
405405
if assert.NoError(t, err) {
406406
assert.Contains(t, string(body), "Bad stuff")
407407
assert.Contains(t, string(body), "parameter \\\"id\\\"")

pkg/middleware/oapi_validate.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
"context"
1919
"errors"
2020
"fmt"
21-
"io/ioutil"
21+
"os"
2222
"net/http"
2323
"strings"
2424

@@ -41,7 +41,7 @@ const (
4141

4242
// Create validator middleware from a YAML file path
4343
func OapiValidatorFromYamlFile(path string) (echo.MiddlewareFunc, error) {
44-
data, err := ioutil.ReadFile(path)
44+
data, err := os.ReadFile(path)
4545
if err != nil {
4646
return nil, fmt.Errorf("error reading %s: %s", path, err)
4747
}

pkg/middleware/oapi_validate_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
"context"
1919
_ "embed"
2020
"errors"
21-
"io/ioutil"
21+
"io"
2222
"net/http"
2323
"net/http/httptest"
2424
"net/url"
@@ -250,7 +250,7 @@ func TestOapiRequestValidatorWithOptionsMultiError(t *testing.T) {
250250
{
251251
rec := doGet(t, e, "http://deepmap.ai/multiparamresource?id=50")
252252
assert.Equal(t, http.StatusBadRequest, rec.Code)
253-
body, err := ioutil.ReadAll(rec.Body)
253+
body, err := io.ReadAll(rec.Body)
254254
if assert.NoError(t, err) {
255255
assert.Contains(t, string(body), "parameter \\\"id2\\\"")
256256
assert.Contains(t, string(body), "value is required but missing")
@@ -264,7 +264,7 @@ func TestOapiRequestValidatorWithOptionsMultiError(t *testing.T) {
264264
{
265265
rec := doGet(t, e, "http://deepmap.ai/multiparamresource")
266266
assert.Equal(t, http.StatusBadRequest, rec.Code)
267-
body, err := ioutil.ReadAll(rec.Body)
267+
body, err := io.ReadAll(rec.Body)
268268
if assert.NoError(t, err) {
269269
assert.Contains(t, string(body), "parameter \\\"id\\\"")
270270
assert.Contains(t, string(body), "value is required but missing")
@@ -280,7 +280,7 @@ func TestOapiRequestValidatorWithOptionsMultiError(t *testing.T) {
280280
{
281281
rec := doGet(t, e, "http://deepmap.ai/multiparamresource?id=500")
282282
assert.Equal(t, http.StatusBadRequest, rec.Code)
283-
body, err := ioutil.ReadAll(rec.Body)
283+
body, err := io.ReadAll(rec.Body)
284284
if assert.NoError(t, err) {
285285
assert.Contains(t, string(body), "parameter \\\"id\\\"")
286286
assert.Contains(t, string(body), "number must be at most 100")
@@ -296,7 +296,7 @@ func TestOapiRequestValidatorWithOptionsMultiError(t *testing.T) {
296296
{
297297
rec := doGet(t, e, "http://deepmap.ai/multiparamresource?id=abc&id2=1")
298298
assert.Equal(t, http.StatusBadRequest, rec.Code)
299-
body, err := ioutil.ReadAll(rec.Body)
299+
body, err := io.ReadAll(rec.Body)
300300
if assert.NoError(t, err) {
301301
assert.Contains(t, string(body), "parameter \\\"id\\\"")
302302
assert.Contains(t, string(body), "parsing \\\"abc\\\": invalid syntax")
@@ -358,7 +358,7 @@ func TestOapiRequestValidatorWithOptionsMultiErrorAndCustomHandler(t *testing.T)
358358
{
359359
rec := doGet(t, e, "http://deepmap.ai/multiparamresource?id=50")
360360
assert.Equal(t, http.StatusTeapot, rec.Code)
361-
body, err := ioutil.ReadAll(rec.Body)
361+
body, err := io.ReadAll(rec.Body)
362362
if assert.NoError(t, err) {
363363
assert.Contains(t, string(body), "parameter \\\"id2\\\"")
364364
assert.Contains(t, string(body), "value is required but missing")
@@ -372,7 +372,7 @@ func TestOapiRequestValidatorWithOptionsMultiErrorAndCustomHandler(t *testing.T)
372372
{
373373
rec := doGet(t, e, "http://deepmap.ai/multiparamresource")
374374
assert.Equal(t, http.StatusTeapot, rec.Code)
375-
body, err := ioutil.ReadAll(rec.Body)
375+
body, err := io.ReadAll(rec.Body)
376376
if assert.NoError(t, err) {
377377
assert.Contains(t, string(body), "parameter \\\"id\\\"")
378378
assert.Contains(t, string(body), "value is required but missing")
@@ -388,7 +388,7 @@ func TestOapiRequestValidatorWithOptionsMultiErrorAndCustomHandler(t *testing.T)
388388
{
389389
rec := doGet(t, e, "http://deepmap.ai/multiparamresource?id=500")
390390
assert.Equal(t, http.StatusTeapot, rec.Code)
391-
body, err := ioutil.ReadAll(rec.Body)
391+
body, err := io.ReadAll(rec.Body)
392392
if assert.NoError(t, err) {
393393
assert.Contains(t, string(body), "parameter \\\"id\\\"")
394394
assert.Contains(t, string(body), "number must be at most 100")
@@ -404,7 +404,7 @@ func TestOapiRequestValidatorWithOptionsMultiErrorAndCustomHandler(t *testing.T)
404404
{
405405
rec := doGet(t, e, "http://deepmap.ai/multiparamresource?id=abc&id2=1")
406406
assert.Equal(t, http.StatusTeapot, rec.Code)
407-
body, err := ioutil.ReadAll(rec.Body)
407+
body, err := io.ReadAll(rec.Body)
408408
if assert.NoError(t, err) {
409409
assert.Contains(t, string(body), "parameter \\\"id\\\"")
410410
assert.Contains(t, string(body), "parsing \\\"abc\\\": invalid syntax")

0 commit comments

Comments
 (0)