Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,29 @@ jobs:
matrix:
go: [ '1.21', '1.20', '1.19', '1.18']
steps:
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
- name: Checkout Code
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1

- uses: actions/setup-go@93397bea11091df50f3d7e59dc26a7711a8bcfbe # v4.1.0
- name: Setup Go
uses: actions/setup-go@93397bea11091df50f3d7e59dc26a7711a8bcfbe # v4.1.0
with:
go-version: ${{ matrix.go }}

- name: Build Go
run: go build ./...

- name: test
run: go test -race . -v
- name: Run golangci-lint
uses: golangci/golangci-lint-action@08e2f20817b15149a52b5b3ebe7de50aff2ba8c5

- name: run test and generate coverage report
run: go test -race ./... -v -coverprofile=coverage.out

- name: Upload coverage report
uses: actions/upload-artifact@65462800fd760344b1a7b4382951275a0abb4808
with:
path: coverage.out
name: Coverage-report-${{matrix.go}}

- name: Display coverage report
run: go tool cover -func=coverage.out

4 changes: 3 additions & 1 deletion errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ func TestMarshalErrorsWritesTheExpectedPayload(t *testing.T) {
var writer io.Writer = buffer

_ = MarshalErrors(writer, testRow.In)
json.Unmarshal(buffer.Bytes(), &output)
if err := json.Unmarshal(buffer.Bytes(), &output); err != nil {
t.Fatalf("failed to unmarshal: %v", err)
}

if !reflect.DeepEqual(output, testRow.Out) {
t.Fatalf("Expected: \n%#v \nto equal: \n%#v", output, testRow.Out)
Expand Down
10 changes: 7 additions & 3 deletions examples/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import (
func TestExampleHandler_post(t *testing.T) {
blog := fixtureBlogCreate(1)
requestBody := bytes.NewBuffer(nil)
jsonapi.MarshalOnePayloadEmbedded(requestBody, blog)
if err := jsonapi.MarshalOnePayloadEmbedded(requestBody, blog); err != nil {
t.Fatal(err)
}

r, err := http.NewRequest(http.MethodPost, "/blogs?id=1", requestBody)
if err != nil {
Expand All @@ -36,7 +38,9 @@ func TestExampleHandler_put(t *testing.T) {
fixtureBlogCreate(3),
}
requestBody := bytes.NewBuffer(nil)
jsonapi.MarshalPayload(requestBody, blogs)
if err := jsonapi.MarshalPayload(requestBody, blogs); err != nil {
t.Fatal(err)
}

r, err := http.NewRequest(http.MethodPut, "/blogs", requestBody)
if err != nil {
Expand Down Expand Up @@ -102,7 +106,7 @@ func TestHttpErrorWhenHeaderDoesNotMatch(t *testing.T) {
}

func TestHttpErrorWhenMethodDoesNotMatch(t *testing.T) {
r, err := http.NewRequest(http.MethodPatch, "/blogs", nil)
r, err := http.NewRequest(http.MethodOptions, "/blogs", nil)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is currently failing in the main branch. It is failing because previously there wasn't any handling for the method http.MethodPatch but around an year back code was added for this case as well.
I have changed the http method to an unhandled path, so that the test case runs as expected

if err != nil {
t.Fatal(err)
}
Expand Down
1 change: 0 additions & 1 deletion models_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,6 @@ type Video struct {

type OneOfMedia struct {
Image *Image
random int

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused field

Video *Video
RandomStuff *string
}
Expand Down
6 changes: 3 additions & 3 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,8 +453,8 @@ func unmarshalNode(data *Node, model reflect.Value, included *map[string]*Node)

buf := bytes.NewBuffer(nil)

json.NewEncoder(buf).Encode(data.Relationships[args[1]])
json.NewDecoder(buf).Decode(relationship)
json.NewEncoder(buf).Encode(data.Relationships[args[1]]) //nolint:errcheck
json.NewDecoder(buf).Decode(relationship) //nolint:errcheck

Comment on lines +456 to 458

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment: We are ignoring the lint error to preserve behaviour.

data := relationship.Data

Expand Down Expand Up @@ -483,7 +483,7 @@ func unmarshalNode(data *Node, model reflect.Value, included *map[string]*Node)

buf := bytes.NewBuffer(nil)
relDataStr := data.Relationships[args[1]]
json.NewEncoder(buf).Encode(relDataStr)
json.NewEncoder(buf).Encode(relDataStr) //nolint:errcheck

isExplicitNull := false
relationshipDecodeErr := json.NewDecoder(buf).Decode(relationship)
Expand Down
64 changes: 43 additions & 21 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,6 @@ func TestUnmarshalToStructWithPointerAttr_BadType_Struct(t *testing.T) {

func TestUnmarshalToStructWithPointerAttr_BadType_IntSlice(t *testing.T) {
out := new(WithPointer)
type FooStruct struct{ A, B int }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused

in := map[string]interface{}{
"name": []int{4, 5}, // This is the wrong type.
}
Expand Down Expand Up @@ -405,7 +404,9 @@ func TestUnmarshalNullableRelationshipsNonNullValue(t *testing.T) {
}

outBuf := bytes.NewBuffer(nil)
json.NewEncoder(outBuf).Encode(payload)
if err := json.NewEncoder(outBuf).Encode(payload); err != nil {
t.Fatal(err)
}

out := new(WithNullableAttrs)

Expand Down Expand Up @@ -442,7 +443,9 @@ func TestUnmarshalNullableRelationshipsExplicitNullValue(t *testing.T) {
}

outBuf := bytes.NewBuffer(nil)
json.NewEncoder(outBuf).Encode(payload)
if err := json.NewEncoder(outBuf).Encode(payload); err != nil {
t.Fatal(err)
}

out := new(WithNullableAttrs)

Expand All @@ -467,7 +470,9 @@ func TestUnmarshalNullableRelationshipsNonExistentValue(t *testing.T) {
}

outBuf := bytes.NewBuffer(nil)
json.NewEncoder(outBuf).Encode(payload)
if err := json.NewEncoder(outBuf).Encode(payload); err != nil {
t.Fatal(err)
}

out := new(WithNullableAttrs)

Expand All @@ -490,7 +495,9 @@ func TestUnmarshalNullableRelationshipsNoRelationships(t *testing.T) {
}

outBuf := bytes.NewBuffer(nil)
json.NewEncoder(outBuf).Encode(payload)
if err := json.NewEncoder(outBuf).Encode(payload); err != nil {
t.Fatal(err)
}

out := new(WithNullableAttrs)

Expand Down Expand Up @@ -1028,7 +1035,7 @@ func Test_choiceStructMapping(t *testing.T) {
t.Errorf("expected \"images\" to be the first field, but got %d", imageField.FieldNum)
}
videoField, ok := result["videos"]
if !ok || videoField.FieldNum != 2 {
if !ok || videoField.FieldNum != 1 {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An unused field was removed, thus the FieldNum for videoField decreased

t.Errorf("expected \"videos\" to be the third field, but got %d", videoField.FieldNum)
}
}
Expand Down Expand Up @@ -1120,7 +1127,10 @@ func TestUnmarshalNestedRelationships(t *testing.T) {
}

func TestUnmarshalRelationshipsSerializedEmbedded(t *testing.T) {
out := sampleSerializedEmbeddedTestModel()
out, err := sampleSerializedEmbeddedTestModel()
if err != nil {
t.Fatal(err)
}

if out.CurrentPost == nil {
t.Fatalf("Current post was not materialized")
Expand Down Expand Up @@ -1169,7 +1179,10 @@ func TestUnmarshalNestedRelationshipsEmbedded(t *testing.T) {
}

func TestUnmarshalRelationshipsSideloaded(t *testing.T) {
payload := samplePayloadWithSideloaded()
payload, err := samplePayloadWithSideloaded()
if err != nil {
t.Fatal(err)
}
out := new(Blog)

if err := UnmarshalPayload(payload, out); err != nil {
Expand All @@ -1190,7 +1203,10 @@ func TestUnmarshalRelationshipsSideloaded(t *testing.T) {
}

func TestUnmarshalNestedRelationshipsSideloaded(t *testing.T) {
payload := samplePayloadWithSideloaded()
payload, err := samplePayloadWithSideloaded()
if err != nil {
t.Fatal(err)
}
out := new(Blog)

if err := UnmarshalPayload(payload, out); err != nil {
Expand Down Expand Up @@ -1621,7 +1637,7 @@ func samplePayload() io.Reader {
}

out := bytes.NewBuffer(nil)
json.NewEncoder(out).Encode(payload)
json.NewEncoder(out).Encode(payload) //nolint:errcheck

return out
}
Expand All @@ -1639,7 +1655,7 @@ func samplePayloadWithID() io.Reader {
}

out := bytes.NewBuffer(nil)
json.NewEncoder(out).Encode(payload)
json.NewEncoder(out).Encode(payload) //nolint:errcheck

return out
}
Expand All @@ -1654,7 +1670,7 @@ func samplePayloadWithBadTypes(m map[string]interface{}) io.Reader {
}

out := bytes.NewBuffer(nil)
json.NewEncoder(out).Encode(payload)
json.NewEncoder(out).Encode(payload) //nolint:errcheck

return out
}
Expand All @@ -1669,7 +1685,7 @@ func sampleWithPointerPayload(m map[string]interface{}) io.Reader {
}

out := bytes.NewBuffer(nil)
json.NewEncoder(out).Encode(payload)
json.NewEncoder(out).Encode(payload) //nolint:errcheck

return out
}
Expand All @@ -1684,7 +1700,7 @@ func samplePayloadWithNullableAttrs(m map[string]interface{}) io.Reader {
}

out := bytes.NewBuffer(nil)
json.NewEncoder(out).Encode(payload)
json.NewEncoder(out).Encode(payload) //nolint:errcheck

return out
}
Expand Down Expand Up @@ -1757,23 +1773,29 @@ func testModel() *Blog {
}
}

func samplePayloadWithSideloaded() io.Reader {
func samplePayloadWithSideloaded() (io.Reader, error) {
testModel := testModel()

out := bytes.NewBuffer(nil)
MarshalPayload(out, testModel)
if err := MarshalPayload(out, testModel); err != nil {
return nil, err
}

return out
return out, nil
}

func sampleSerializedEmbeddedTestModel() *Blog {
func sampleSerializedEmbeddedTestModel() (*Blog, error) {
out := bytes.NewBuffer(nil)
MarshalOnePayloadEmbedded(out, testModel())
if err := MarshalOnePayloadEmbedded(out, testModel()); err != nil {
return nil, err
}

blog := new(Blog)
UnmarshalPayload(out, blog)
if err := UnmarshalPayload(out, blog); err != nil {
return nil, err
}

return blog
return blog, nil
}

func TestUnmarshalNestedStructPtr(t *testing.T) {
Expand Down
24 changes: 18 additions & 6 deletions response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ func TestMarshalPayload(t *testing.T) {

// One
out1 := bytes.NewBuffer(nil)
MarshalPayload(out1, book)
if err := MarshalPayload(out1, book); err != nil {
t.Fatal(err)
}

if err := json.Unmarshal(out1.Bytes(), &jsonData); err != nil {
t.Fatal(err)
Expand All @@ -29,7 +31,9 @@ func TestMarshalPayload(t *testing.T) {

// Many
out2 := bytes.NewBuffer(nil)
MarshalPayload(out2, books)
if err := MarshalPayload(out2, books); err != nil {
t.Fatal(err)
}

if err := json.Unmarshal(out2.Bytes(), &jsonData); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -936,7 +940,9 @@ func TestMarshal_Times(t *testing.T) {
}
// Use the standard JSON library to traverse the genereated JSON payload.
data := map[string]interface{}{}
json.Unmarshal(out.Bytes(), &data)
if err := json.Unmarshal(out.Bytes(), &data); err != nil {
t.Fatal(err)
}
if tc.verification != nil {
if err := tc.verification(data); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -1017,7 +1023,9 @@ func TestNullableRelationship(t *testing.T) {

// Use the standard JSON library to traverse the genereated JSON payload.
data := map[string]interface{}{}
json.Unmarshal(out.Bytes(), &data)
if err := json.Unmarshal(out.Bytes(), &data); err != nil {
t.Fatal(err)
}
if tc.verification != nil {
if err := tc.verification(data); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -1114,7 +1122,9 @@ func TestNullableAttr_Time(t *testing.T) {
}
// Use the standard JSON library to traverse the genereated JSON payload.
data := map[string]interface{}{}
json.Unmarshal(out.Bytes(), &data)
if err := json.Unmarshal(out.Bytes(), &data); err != nil {
t.Fatal(err)
}
if tc.verification != nil {
if err := tc.verification(data); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -1184,7 +1194,9 @@ func TestNullableAttr_Bool(t *testing.T) {
}
// Use the standard JSON library to traverse the genereated JSON payload.
data := map[string]interface{}{}
json.Unmarshal(out.Bytes(), &data)
if err := json.Unmarshal(out.Bytes(), &data); err != nil {
t.Fatal(err)
}
if tc.verification != nil {
if err := tc.verification(data); err != nil {
t.Fatal(err)
Expand Down
2 changes: 1 addition & 1 deletion runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (r *Runtime) UnmarshalPayload(reader io.Reader, model interface{}) error {

// UnmarshalManyPayload has docs in request.go for UnmarshalManyPayload.
func (r *Runtime) UnmarshalManyPayload(reader io.Reader, kind reflect.Type) (elems []interface{}, err error) {
r.instrumentCall(UnmarshalStart, UnmarshalStop, func() error {
r.instrumentCall(UnmarshalStart, UnmarshalStop, func() error { //nolint:errcheck
elems, err = UnmarshalManyPayload(reader, kind)
return err
})
Expand Down
Loading