Skip to content

Commit

Permalink
follow lint rules (#250)
Browse files Browse the repository at this point in the history
  • Loading branch information
fenollp authored Sep 6, 2020
1 parent ceae068 commit 5f67f43
Show file tree
Hide file tree
Showing 21 changed files with 398 additions and 499 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ import (

func main() {
router := openapi3filter.NewRouter().WithSwaggerFromFile("swagger.json")
ctx := context.TODO()
ctx := context.Background()
httpReq, _ := http.NewRequest(http.MethodGet, "/items", nil)

// Find route
Expand All @@ -105,9 +105,7 @@ func main() {
RequestValidationInput: requestValidationInput,
Status: respStatus,
Header: http.Header{
"Content-Type": []string{
respContentType,
},
"Content-Type": []string{respContentType},
},
}
if respBody != nil {
Expand Down
33 changes: 16 additions & 17 deletions jsoninfo/unmarshal_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package jsoninfo_test
package jsoninfo

import (
"errors"
"testing"

"github.com/getkin/kin-openapi/jsoninfo"
"github.com/stretchr/testify/assert"
)

Expand All @@ -16,7 +15,7 @@ func TestNewObjectDecoder(t *testing.T) {
}
`)
t.Run("test new object decoder", func(t *testing.T) {
decoder, err := jsoninfo.NewObjectDecoder(data)
decoder, err := NewObjectDecoder(data)
assert.Nil(t, err)
assert.NotNil(t, decoder)
assert.Equal(t, data, decoder.Data)
Expand All @@ -25,15 +24,15 @@ func TestNewObjectDecoder(t *testing.T) {
}

type mockStrictStruct struct {
EncodeWithFn func(encoder *jsoninfo.ObjectEncoder, value interface{}) error
DecodeWithFn func(decoder *jsoninfo.ObjectDecoder, value interface{}) error
EncodeWithFn func(encoder *ObjectEncoder, value interface{}) error
DecodeWithFn func(decoder *ObjectDecoder, value interface{}) error
}

func (m *mockStrictStruct) EncodeWith(encoder *jsoninfo.ObjectEncoder, value interface{}) error {
func (m *mockStrictStruct) EncodeWith(encoder *ObjectEncoder, value interface{}) error {
return m.EncodeWithFn(encoder, value)
}

func (m *mockStrictStruct) DecodeWith(decoder *jsoninfo.ObjectDecoder, value interface{}) error {
func (m *mockStrictStruct) DecodeWith(decoder *ObjectDecoder, value interface{}) error {
return m.DecodeWithFn(decoder, value)
}

Expand All @@ -48,31 +47,31 @@ func TestUnmarshalStrictStruct(t *testing.T) {
t.Run("test unmarshal with StrictStruct without err", func(t *testing.T) {
decodeWithFnCalled := 0
mockStruct := &mockStrictStruct{
EncodeWithFn: func(encoder *jsoninfo.ObjectEncoder, value interface{}) error {
EncodeWithFn: func(encoder *ObjectEncoder, value interface{}) error {
return nil
},
DecodeWithFn: func(decoder *jsoninfo.ObjectDecoder, value interface{}) error {
DecodeWithFn: func(decoder *ObjectDecoder, value interface{}) error {
decodeWithFnCalled++
return nil
},
}
err := jsoninfo.UnmarshalStrictStruct(data, mockStruct)
err := UnmarshalStrictStruct(data, mockStruct)
assert.Nil(t, err)
assert.Equal(t, 1, decodeWithFnCalled)
})

t.Run("test unmarshal with StrictStruct with err", func(t *testing.T) {
decodeWithFnCalled := 0
mockStruct := &mockStrictStruct{
EncodeWithFn: func(encoder *jsoninfo.ObjectEncoder, value interface{}) error {
EncodeWithFn: func(encoder *ObjectEncoder, value interface{}) error {
return nil
},
DecodeWithFn: func(decoder *jsoninfo.ObjectDecoder, value interface{}) error {
DecodeWithFn: func(decoder *ObjectDecoder, value interface{}) error {
decodeWithFnCalled++
return errors.New("unable to decode the value")
},
}
err := jsoninfo.UnmarshalStrictStruct(data, mockStruct)
err := UnmarshalStrictStruct(data, mockStruct)
assert.NotNil(t, err)
assert.Equal(t, 1, decodeWithFnCalled)
})
Expand All @@ -85,7 +84,7 @@ func TestDecodeStructFieldsAndExtensions(t *testing.T) {
"field2": "field2"
}
`)
decoder, err := jsoninfo.NewObjectDecoder(data)
decoder, err := NewObjectDecoder(data)
assert.Nil(t, err)
assert.NotNil(t, decoder)

Expand All @@ -111,7 +110,7 @@ func TestDecodeStructFieldsAndExtensions(t *testing.T) {
})

t.Run("successfully decoded with all fields", func(t *testing.T) {
d, err := jsoninfo.NewObjectDecoder(data)
d, err := NewObjectDecoder(data)
assert.Nil(t, err)
assert.NotNil(t, d)

Expand All @@ -127,7 +126,7 @@ func TestDecodeStructFieldsAndExtensions(t *testing.T) {
})

t.Run("successfully decoded with renaming field", func(t *testing.T) {
d, err := jsoninfo.NewObjectDecoder(data)
d, err := NewObjectDecoder(data)
assert.Nil(t, err)
assert.NotNil(t, d)

Expand All @@ -141,7 +140,7 @@ func TestDecodeStructFieldsAndExtensions(t *testing.T) {
})

t.Run("un-successfully decoded due to data mismatch", func(t *testing.T) {
d, err := jsoninfo.NewObjectDecoder(data)
d, err := NewObjectDecoder(data)
assert.Nil(t, err)
assert.NotNil(t, d)

Expand Down
5 changes: 2 additions & 3 deletions openapi3/discriminator_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package openapi3_test
package openapi3

import (
"testing"

"github.com/getkin/kin-openapi/openapi3"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -37,7 +36,7 @@ var jsonSpecWithDiscriminator = []byte(`
`)

func TestParsingDiscriminator(t *testing.T) {
loader, err := openapi3.NewSwaggerLoader().LoadSwaggerFromData(jsonSpecWithDiscriminator)
loader, err := NewSwaggerLoader().LoadSwaggerFromData(jsonSpecWithDiscriminator)
require.NoError(t, err)
require.Equal(t, 2, len(loader.Components.Schemas["MyResponseType"].Value.OneOf))
}
37 changes: 18 additions & 19 deletions openapi3/encoding_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package openapi3_test
package openapi3

import (
"context"
"encoding/json"
"reflect"
"testing"

"github.com/getkin/kin-openapi/openapi3"
"github.com/stretchr/testify/require"
)

Expand All @@ -17,13 +16,13 @@ func TestEncodingJSON(t *testing.T) {
require.NotEmpty(t, data)

t.Log("Unmarshal *openapi3.Encoding from JSON")
docA := &openapi3.Encoding{}
docA := &Encoding{}
err = json.Unmarshal(encodingJSON, &docA)
require.NoError(t, err)
require.NotEmpty(t, data)

t.Log("Validate *openapi3.Encoding")
err = docA.Validate(context.TODO())
err = docA.Validate(context.Background())
require.NoError(t, err)

t.Log("Ensure representations match")
Expand All @@ -45,13 +44,13 @@ var encodingJSON = []byte(`
}
`)

func encoding() *openapi3.Encoding {
func encoding() *Encoding {
explode := true
return &openapi3.Encoding{
return &Encoding{
ContentType: "application/json",
Headers: map[string]*openapi3.HeaderRef{
Headers: map[string]*HeaderRef{
"someHeader": {
Value: &openapi3.Header{},
Value: &Header{},
},
},
Style: "form",
Expand All @@ -64,32 +63,32 @@ func TestEncodingSerializationMethod(t *testing.T) {
boolPtr := func(b bool) *bool { return &b }
testCases := []struct {
name string
enc *openapi3.Encoding
want *openapi3.SerializationMethod
enc *Encoding
want *SerializationMethod
}{
{
name: "default",
want: &openapi3.SerializationMethod{Style: openapi3.SerializationForm, Explode: true},
want: &SerializationMethod{Style: SerializationForm, Explode: true},
},
{
name: "encoding with style",
enc: &openapi3.Encoding{Style: openapi3.SerializationSpaceDelimited},
want: &openapi3.SerializationMethod{Style: openapi3.SerializationSpaceDelimited, Explode: true},
enc: &Encoding{Style: SerializationSpaceDelimited},
want: &SerializationMethod{Style: SerializationSpaceDelimited, Explode: true},
},
{
name: "encoding with explode",
enc: &openapi3.Encoding{Explode: boolPtr(true)},
want: &openapi3.SerializationMethod{Style: openapi3.SerializationForm, Explode: true},
enc: &Encoding{Explode: boolPtr(true)},
want: &SerializationMethod{Style: SerializationForm, Explode: true},
},
{
name: "encoding with no explode",
enc: &openapi3.Encoding{Explode: boolPtr(false)},
want: &openapi3.SerializationMethod{Style: openapi3.SerializationForm, Explode: false},
enc: &Encoding{Explode: boolPtr(false)},
want: &SerializationMethod{Style: SerializationForm, Explode: false},
},
{
name: "encoding with style and explode ",
enc: &openapi3.Encoding{Style: openapi3.SerializationSpaceDelimited, Explode: boolPtr(false)},
want: &openapi3.SerializationMethod{Style: openapi3.SerializationSpaceDelimited, Explode: false},
enc: &Encoding{Style: SerializationSpaceDelimited, Explode: boolPtr(false)},
want: &SerializationMethod{Style: SerializationSpaceDelimited, Explode: false},
},
}
for _, tc := range testCases {
Expand Down
9 changes: 4 additions & 5 deletions openapi3/example_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package openapi3_test
package openapi3

import (
"encoding/json"
"testing"

"github.com/getkin/kin-openapi/openapi3"
"github.com/stretchr/testify/require"
)

Expand All @@ -15,7 +14,7 @@ func TestExampleJSON(t *testing.T) {
require.NotEmpty(t, data)

t.Log("Unmarshal *openapi3.Example from JSON")
docA := &openapi3.Example{}
docA := &Example{}
err = json.Unmarshal(exampleJSON, &docA)
require.NoError(t, err)
require.NotEmpty(t, data)
Expand All @@ -40,15 +39,15 @@ var exampleJSON = []byte(`
}
`)

func example() *openapi3.Example {
func example() *Example {
value := map[string]string{
"name": "Fluffy",
"petType": "Cat",
"color": "White",
"gender": "male",
"breed": "Persian",
}
return &openapi3.Example{
return &Example{
Summary: "An example of a cat",
Value: value,
}
Expand Down
14 changes: 7 additions & 7 deletions openapi3/extension_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package openapi3_test
package openapi3

import (
"testing"

"github.com/getkin/kin-openapi/jsoninfo"
"github.com/getkin/kin-openapi/openapi3"
"github.com/stretchr/testify/assert"
"testing"
)

func TestExtensionProps_EncodeWith(t *testing.T) {
t.Run("successfully encoded", func(t *testing.T) {
encoder := jsoninfo.NewObjectEncoder()
var extensionProps = openapi3.ExtensionProps{
var extensionProps = ExtensionProps{
Extensions: map[string]interface{}{
"field1": "value1",
},
Expand All @@ -36,7 +36,7 @@ func TestExtensionProps_DecodeWith(t *testing.T) {
t.Run("successfully decode all the fields", func(t *testing.T) {
decoder, err := jsoninfo.NewObjectDecoder(data)
assert.Nil(t, err)
var extensionProps = &openapi3.ExtensionProps{
var extensionProps = &ExtensionProps{
Extensions: map[string]interface{}{
"field1": "value1",
"field2": "value1",
Expand All @@ -58,7 +58,7 @@ func TestExtensionProps_DecodeWith(t *testing.T) {
t.Run("successfully decode some of the fields", func(t *testing.T) {
decoder, err := jsoninfo.NewObjectDecoder(data)
assert.Nil(t, err)
var extensionProps = &openapi3.ExtensionProps{
var extensionProps = &ExtensionProps{
Extensions: map[string]interface{}{
"field1": "value1",
"field2": "value2",
Expand All @@ -79,7 +79,7 @@ func TestExtensionProps_DecodeWith(t *testing.T) {
decoder, err := jsoninfo.NewObjectDecoder(data)
assert.Nil(t, err)

var extensionProps = &openapi3.ExtensionProps{
var extensionProps = &ExtensionProps{
Extensions: map[string]interface{}{
"field1": "value1",
"field2": "value2",
Expand Down
21 changes: 10 additions & 11 deletions openapi3/media_type_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package openapi3_test
package openapi3

import (
"context"
"encoding/json"
"testing"

"github.com/getkin/kin-openapi/openapi3"
"github.com/stretchr/testify/require"
)

Expand All @@ -16,13 +15,13 @@ func TestMediaTypeJSON(t *testing.T) {
require.NotEmpty(t, data)

t.Log("Unmarshal *openapi3.MediaType from JSON")
docA := &openapi3.MediaType{}
docA := &MediaType{}
err = json.Unmarshal(mediaTypeJSON, &docA)
require.NoError(t, err)
require.NotEmpty(t, data)

t.Log("Validate *openapi3.MediaType")
err = docA.Validate(context.TODO())
err = docA.Validate(context.Background())
require.NoError(t, err)

t.Log("Ensure representations match")
Expand Down Expand Up @@ -52,22 +51,22 @@ var mediaTypeJSON = []byte(`
}
`)

func mediaType() *openapi3.MediaType {
func mediaType() *MediaType {
example := map[string]string{"name": "Some example"}
return &openapi3.MediaType{
Schema: &openapi3.SchemaRef{
Value: &openapi3.Schema{
return &MediaType{
Schema: &SchemaRef{
Value: &Schema{
Description: "Some schema",
},
},
Encoding: map[string]*openapi3.Encoding{
Encoding: map[string]*Encoding{
"someEncoding": {
ContentType: "application/xml; charset=utf-8",
},
},
Examples: map[string]*openapi3.ExampleRef{
Examples: map[string]*ExampleRef{
"someExample": {
Value: openapi3.NewExample(example),
Value: NewExample(example),
},
},
}
Expand Down
Loading

0 comments on commit 5f67f43

Please sign in to comment.