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

Does JSONAPI support cyclic structures ? #161

Open
0xvoila opened this issue Oct 12, 2018 · 0 comments
Open

Does JSONAPI support cyclic structures ? #161

0xvoila opened this issue Oct 12, 2018 · 0 comments

Comments

@0xvoila
Copy link

0xvoila commented Oct 12, 2018

Hi,

I have the following models

type StudentProfileModel struct {
	Id        string        `bson:"_id,omitempty" jsonapi:"primary,studentprofiles"`
	StudentId bson.ObjectId `bson:"lead_id" jsonapi:"attr,student_id"`
	Student   *StudentModel `jsonapi:"relation,student"`
}
type StudentModel struct {
	ID             string               `bson:"_id,omitempty" jsonapi:"primary,students"`
	Email          string               `bson:"email" jsonapi:"attr,email"`
	Password       string               `bson:"password" jsonapi:"attr,password"`
	StudentProfile *StudentProfileModel `jsonapi:"relation,studentprofile"`
}

I face a problem when I am trying to fetch StudentProfile .

Here is the code

        studentProfileService := servicefactory.NewStudentProfileService()
	studentProfileModel := studentProfileService.GetStudentProfileById(studentProfileId)

	studentService := servicefactory.NewStudentService()
	studentModel := studentService.GetStudentById(studentProfileModel.StudentId.Hex())
   
        // Attaching student profile to studentModel 
	studentModel.StudentProfile = studentProfileModel

        // Attaching above fetched student model in student profile  
        studentProfileModel.Student = studentModel

	return studentProfileModel
  // Infinite loop here because of cyclic dependencies 
    jsonapi.MarshalPayload(buff, studentProfile)

How to resolve this issue ?

I have read an article on jsonapi.org where suggest we can fetch recursive data

JSONAPI Recommendation

GET /comments HTTP/1.1

{
  "data": [{
      "type": "comments",
      "id": "1",
      "attributes": {
          "text": "HATEOS are the thing!"
      },
      "links": {
          "self": "/comments/1"
      },
      "relationships": {
        "author": {
          "links": {
            "self": "/comments/1/relationships/author",
            "related": "/comments/1/author"
          }
        },
        "articles": {
          "links": {
            "self": "/comments/1/relationships/articles",
            "related": "/comments/1/articles"
          }
        }
      }
  }],
  "links": {
      "self": "/comments"
  }
}

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