Skip to content

Commit

Permalink
Add an example showing how to decode some extension props (#304)
Browse files Browse the repository at this point in the history
  • Loading branch information
fenollp authored Feb 23, 2021
1 parent 33fc721 commit 66fba45
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 74 deletions.
62 changes: 31 additions & 31 deletions jsoninfo/unmarshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"errors"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestNewObjectDecoder(t *testing.T) {
Expand All @@ -16,10 +16,10 @@ func TestNewObjectDecoder(t *testing.T) {
`)
t.Run("test new object decoder", func(t *testing.T) {
decoder, err := NewObjectDecoder(data)
assert.Nil(t, err)
assert.NotNil(t, decoder)
assert.Equal(t, data, decoder.Data)
assert.Equal(t, 2, len(decoder.DecodeExtensionMap()))
require.NoError(t, err)
require.NotNil(t, decoder)
require.Equal(t, data, decoder.Data)
require.Equal(t, 2, len(decoder.DecodeExtensionMap()))
})
}

Expand Down Expand Up @@ -56,8 +56,8 @@ func TestUnmarshalStrictStruct(t *testing.T) {
},
}
err := UnmarshalStrictStruct(data, mockStruct)
assert.Nil(t, err)
assert.Equal(t, 1, decodeWithFnCalled)
require.NoError(t, err)
require.Equal(t, 1, decodeWithFnCalled)
})

t.Run("test unmarshal with StrictStruct with err", func(t *testing.T) {
Expand All @@ -72,8 +72,8 @@ func TestUnmarshalStrictStruct(t *testing.T) {
},
}
err := UnmarshalStrictStruct(data, mockStruct)
assert.NotNil(t, err)
assert.Equal(t, 1, decodeWithFnCalled)
require.Error(t, err)
require.Equal(t, 1, decodeWithFnCalled)
})
}

Expand All @@ -85,72 +85,72 @@ func TestDecodeStructFieldsAndExtensions(t *testing.T) {
}
`)
decoder, err := NewObjectDecoder(data)
assert.Nil(t, err)
assert.NotNil(t, decoder)
require.NoError(t, err)
require.NotNil(t, decoder)

t.Run("value is not pointer", func(t *testing.T) {
var value interface{}
assert.Panics(t, func() {
require.Panics(t, func() {
_ = decoder.DecodeStructFieldsAndExtensions(value)
}, "value is not a pointer")
})

t.Run("value is nil", func(t *testing.T) {
var value *string = nil
assert.Panics(t, func() {
require.Panics(t, func() {
_ = decoder.DecodeStructFieldsAndExtensions(value)
}, "value is nil")
})

t.Run("value is not struct", func(t *testing.T) {
var value = "simple string"
assert.Panics(t, func() {
require.Panics(t, func() {
_ = decoder.DecodeStructFieldsAndExtensions(&value)
}, "value is not struct")
})

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

var value = struct {
Field1 string `json:"field1"`
Field2 string `json:"field2"`
}{}
err = d.DecodeStructFieldsAndExtensions(&value)
assert.Nil(t, err)
assert.Equal(t, "field1", value.Field1)
assert.Equal(t, "field2", value.Field2)
assert.Equal(t, 0, len(d.DecodeExtensionMap()))
require.NoError(t, err)
require.Equal(t, "field1", value.Field1)
require.Equal(t, "field2", value.Field2)
require.Equal(t, 0, len(d.DecodeExtensionMap()))
})

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

var value = struct {
Field1 string `json:"field1"`
}{}
err = d.DecodeStructFieldsAndExtensions(&value)
assert.Nil(t, err)
assert.Equal(t, "field1", value.Field1)
assert.Equal(t, 1, len(d.DecodeExtensionMap()))
require.NoError(t, err)
require.Equal(t, "field1", value.Field1)
require.Equal(t, 1, len(d.DecodeExtensionMap()))
})

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

var value = struct {
Field1 int `json:"field1"`
}{}
err = d.DecodeStructFieldsAndExtensions(&value)
assert.NotNil(t, err)
assert.EqualError(t, err, "Error while unmarshalling property 'field1' (*int): json: cannot unmarshal string into Go value of type int")
assert.Equal(t, 0, value.Field1)
assert.Equal(t, 2, len(d.DecodeExtensionMap()))
require.Error(t, err)
require.EqualError(t, err, "Error while unmarshalling property 'field1' (*int): json: cannot unmarshal string into Go value of type int")
require.Equal(t, 0, value.Field1)
require.Equal(t, 2, len(d.DecodeExtensionMap()))
})
}
3 changes: 1 addition & 2 deletions openapi2/openapi2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"io/ioutil"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand All @@ -21,5 +20,5 @@ func TestReadingSwagger(t *testing.T) {
output, err := json.Marshal(swagger)
require.NoError(t, err)

assert.JSONEq(t, string(input), string(output))
require.JSONEq(t, string(input), string(output))
}
56 changes: 40 additions & 16 deletions openapi3/extension_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,36 @@
package openapi3

import (
"encoding/json"
"fmt"
"testing"

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

func ExampleExtensionProps_DecodeWith() {
loader := NewSwaggerLoader()
loader.IsExternalRefsAllowed = true
spec, err := loader.LoadSwaggerFromFile("testdata/testref.openapi.json")
if err != nil {
panic(err)
}

dec, err := jsoninfo.NewObjectDecoder(spec.Info.Extensions["x-my-extension"].(json.RawMessage))
if err != nil {
panic(err)
}
var value struct {
Key int `json:"k"`
}
if err = spec.Info.DecodeWith(dec, &value); err != nil {
panic(err)
}
fmt.Println(value.Key)
// Output: 42
}

func TestExtensionProps_EncodeWith(t *testing.T) {
t.Run("successfully encoded", func(t *testing.T) {
encoder := jsoninfo.NewObjectEncoder()
Expand All @@ -22,7 +46,7 @@ func TestExtensionProps_EncodeWith(t *testing.T) {
}{}

err := extensionProps.EncodeWith(encoder, &value)
assert.Nil(t, err)
require.NoError(t, err)
})
}

Expand All @@ -35,7 +59,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)
require.NoError(t, err)
var extensionProps = &ExtensionProps{
Extensions: map[string]interface{}{
"field1": "value1",
Expand All @@ -49,15 +73,15 @@ func TestExtensionProps_DecodeWith(t *testing.T) {
}{}

err = extensionProps.DecodeWith(decoder, &value)
assert.Nil(t, err)
assert.Equal(t, 0, len(extensionProps.Extensions))
assert.Equal(t, "value1", value.Field1)
assert.Equal(t, "value2", value.Field2)
require.NoError(t, err)
require.Equal(t, 0, len(extensionProps.Extensions))
require.Equal(t, "value1", value.Field1)
require.Equal(t, "value2", value.Field2)
})

t.Run("successfully decode some of the fields", func(t *testing.T) {
decoder, err := jsoninfo.NewObjectDecoder(data)
assert.Nil(t, err)
require.NoError(t, err)
var extensionProps = &ExtensionProps{
Extensions: map[string]interface{}{
"field1": "value1",
Expand All @@ -70,14 +94,14 @@ func TestExtensionProps_DecodeWith(t *testing.T) {
}{}

err = extensionProps.DecodeWith(decoder, value)
assert.Nil(t, err)
assert.Equal(t, 1, len(extensionProps.Extensions))
assert.Equal(t, "value1", value.Field1)
require.NoError(t, err)
require.Equal(t, 1, len(extensionProps.Extensions))
require.Equal(t, "value1", value.Field1)
})

t.Run("successfully decode none of the fields", func(t *testing.T) {
decoder, err := jsoninfo.NewObjectDecoder(data)
assert.Nil(t, err)
require.NoError(t, err)

var extensionProps = &ExtensionProps{
Extensions: map[string]interface{}{
Expand All @@ -92,9 +116,9 @@ func TestExtensionProps_DecodeWith(t *testing.T) {
}{}

err = extensionProps.DecodeWith(decoder, &value)
assert.Nil(t, err)
assert.Equal(t, 2, len(extensionProps.Extensions))
assert.Empty(t, value.Field3)
assert.Empty(t, value.Field4)
require.NoError(t, err)
require.Equal(t, 2, len(extensionProps.Extensions))
require.Empty(t, value.Field3)
require.Empty(t, value.Field4)
})
}
43 changes: 21 additions & 22 deletions openapi3/refs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"testing"

"github.com/go-openapi/jsonpointer"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -220,57 +219,57 @@ components:
ptr, err := jsonpointer.New("/paths/~1pet/put/responses/200/content")
require.NoError(t, err)
v, kind, err := ptr.Get(root)
assert.NoError(t, err)
assert.Equal(t, reflect.TypeOf(Content{}).Kind(), kind)
assert.IsType(t, Content{}, v)
require.NoError(t, err)
require.Equal(t, reflect.TypeOf(Content{}).Kind(), kind)
require.IsType(t, Content{}, v)

ptr, err = jsonpointer.New("/paths/~1pet/put/responses/200/content/application~1json/schema")
require.NoError(t, err)
v, kind, err = ptr.Get(root)
assert.NoError(t, err)
assert.Equal(t, reflect.Ptr, kind)
assert.IsType(t, &Ref{}, v)
assert.Equal(t, "#/components/schemas/Pet", v.(*Ref).Ref)
require.NoError(t, err)
require.Equal(t, reflect.Ptr, kind)
require.IsType(t, &Ref{}, v)
require.Equal(t, "#/components/schemas/Pet", v.(*Ref).Ref)

ptr, err = jsonpointer.New("/components/schemas/Pets/items")
require.NoError(t, err)
v, kind, err = ptr.Get(root)
assert.NoError(t, err)
assert.Equal(t, reflect.Ptr, kind)
require.NoError(t, err)
require.Equal(t, reflect.Ptr, kind)
require.IsType(t, &Ref{}, v)
assert.Equal(t, "#/components/schemas/Pet", v.(*Ref).Ref)
require.Equal(t, "#/components/schemas/Pet", v.(*Ref).Ref)

ptr, err = jsonpointer.New("/components/schemas/Error/properties/code")
require.NoError(t, err)
v, kind, err = ptr.Get(root)
assert.NoError(t, err)
assert.Equal(t, reflect.Ptr, kind)
require.NoError(t, err)
require.Equal(t, reflect.Ptr, kind)
require.IsType(t, &Schema{}, v)
assert.Equal(t, "integer", v.(*Schema).Type)
require.Equal(t, "integer", v.(*Schema).Type)

ptr, err = jsonpointer.New("/components/schemas/OneOfTest/oneOf/0")
require.NoError(t, err)
v, kind, err = ptr.Get(root)
assert.NoError(t, err)
assert.Equal(t, reflect.Ptr, kind)
require.NoError(t, err)
require.Equal(t, reflect.Ptr, kind)
require.IsType(t, &Schema{}, v)
assert.Equal(t, "string", v.(*Schema).Type)
require.Equal(t, "string", v.(*Schema).Type)

ptr, err = jsonpointer.New("/components/schemas/OneOfTest/oneOf/1")
require.NoError(t, err)
v, kind, err = ptr.Get(root)
assert.NoError(t, err)
assert.Equal(t, reflect.Ptr, kind)
require.NoError(t, err)
require.Equal(t, reflect.Ptr, kind)
require.IsType(t, &Schema{}, v)
assert.Equal(t, "integer", v.(*Schema).Type)
require.Equal(t, "integer", v.(*Schema).Type)

ptr, err = jsonpointer.New("/components/schemas/OneOfTest/oneOf/5")
require.NoError(t, err)
_, _, err = ptr.Get(root)
assert.Error(t, err)
require.Error(t, err)

ptr, err = jsonpointer.New("/components/schemas/OneOfTest/oneOf/-1")
require.NoError(t, err)
_, _, err = ptr.Get(root)
assert.Error(t, err)
require.Error(t, err)
}
5 changes: 2 additions & 3 deletions openapi3/swagger_loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,13 @@ paths:
}

func ExampleSwaggerLoader() {
source := `{"info":{"description":"An API"}}`
const source = `{"info":{"description":"An API"}}`
swagger, err := NewSwaggerLoader().LoadSwaggerFromData([]byte(source))
if err != nil {
panic(err)
}
fmt.Print(swagger.Info.Description)
// Output:
// An API
// Output: An API
}

func TestResolveSchemaRef(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions openapi3/testdata/testref.openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"openapi": "3.0.0",
"info": {
"title": "",
"x-my-extension": {"k": 42},
"version": "1"
},
"paths": {},
Expand Down

0 comments on commit 66fba45

Please sign in to comment.