Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Marshal nested object does not work #207

Open
SkYNewZ opened this issue Apr 9, 2021 · 0 comments
Open

Marshal nested object does not work #207

SkYNewZ opened this issue Apr 9, 2021 · 0 comments

Comments

@SkYNewZ
Copy link

SkYNewZ commented Apr 9, 2021

In a third party library that uses this library, I have an error because marshaling a nested object does not work.

So I take the opportunity to post an issue here as well.
You can see an example test file which demonstrate this issue.

main.go
// main.go
package main

import (
	"bytes"

	"github.com/google/jsonapi"
)

// Nested describes a nested object
type Nested struct {
	Name *string `jsonapi:"attr,name"`
}

// Object is the main example object
type Object struct {
	Type         string  `jsonapi:"primary,object"`
	Color        *string `jsonapi:"attr,color"`
	Enable       *bool   `jsonapi:"attr,enable"`
	NestedObject *Nested `jsonapi:"attr,nested-object"`
}

func MarshalNestedObject() (string, error) {
	n := new(Nested)
	n.Name = toStringPtr("foo")

	var b bytes.Buffer
	err := jsonapi.MarshalPayload(&b, n)
	return b.String(), err
}

func MarshalObject() (string, error) {
	n := new(Nested)
	n.Name = toStringPtr("foo")

	o := &Object{
		Color:        toStringPtr("blue"),
		Enable:       toBoolPtr(true),
		NestedObject: n,
	}

	var b bytes.Buffer
	err := jsonapi.MarshalPayload(&b, o)
	return b.String(), err
}

func toStringPtr(in string) (out *string) {
	out = &in
	return
}

func toBoolPtr(in bool) (out *bool) {
	out = &in
	return
}
main_test.go
// main_test.go
package main

import (
	"reflect"
	"testing"
)

func TestMarshalNestedObject(t *testing.T) {
	tests := []struct {
		name    string
		want    string
		wantErr bool
	}{
		{
			name: "expected",
			want: `{"data":{"type":"","attributes":{"name":"foo"}}}
`,
			wantErr: false,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got, err := MarshalNestedObject()
			if (err != nil) != tt.wantErr {
				t.Errorf("MarshalNestedObject() error = %v, wantErr %v", err, tt.wantErr)
				return
			}
			if !reflect.DeepEqual(got, tt.want) {
				t.Errorf("MarshalNestedObject() got = %v, want %v", got, tt.want)
			}
		})
	}
}

func TestMarshalObject(t *testing.T) {
	tests := []struct {
		name    string
		want    string
		wantErr bool
	}{
		{
			name: "expected",
			want: `{"data":{"type":"object","attributes":{"color":"blue","enable":true,"nested-object":{"name":"foo"}}}}
`,
			wantErr: false,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got, err := MarshalObject()
			if (err != nil) != tt.wantErr {
				t.Errorf("MarshalObject() error = %v, wantErr %v", err, tt.wantErr)
				return
			}

			if !reflect.DeepEqual(got, tt.want) {
				t.Errorf("MarshalObject() got = %v, want %v", got, tt.want)
			}
		})
	}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant