Skip to content

Crash when referenced item id does not exist. #223

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

Closed
Dragomir-Ivanov opened this issue Dec 31, 2018 · 2 comments
Closed

Crash when referenced item id does not exist. #223

Dragomir-Ivanov opened this issue Dec 31, 2018 · 2 comments

Comments

@Dragomir-Ivanov
Copy link
Contributor

Using the common example case:

PUT http://localhost:8080/api/users/bgl8mqh40fmepk0u53kg
POST http://localhost:8080/api/users/bgl8mqh40fmepk0u53kg/posts
DELETE http://localhost:8080/api/users/bgl8mqh40fmepk0u53kg
GET http://localhost:8080/api/posts?fields=user{id,name}

The last GET crashes rest-layer. I traced down to:

// MultiGet implements query.Resource interface.
func (r restResource) MultiGet(ctx context.Context, ids []interface{}) ([]map[string]interface{}, error) {
	items, err := r.Resource.MultiGet(ctx, ids)
	if err != nil {
		return nil, err
	}
	payloads := make([]map[string]interface{}, 0, len(items))
	for _, i := range items {
		payloads = append(payloads, i.Payload)
	}
	return payloads, nil
}

where i is nil.
Managed to somewhat workaround with:

		if i != nil {
			payloads = append(payloads, i.Payload)
		} else {
			payloads = append(payloads, nil)
		}

However, now user is returned as:

[{
  "_etag": "ecfdfa0ab4f3d63f0133c121ee7bf047",
  "user": {}
}]

Where ideally for me to be "user": null. What is your advice @smyrman @rs .Thanks.

@smyrman
Copy link
Collaborator

smyrman commented Jan 1, 2019

This is a side effect of there being no Cascade delete in MongoDB. Arguably, Cascade delete is not always what you want though.

For similar cases, we have a custom hook that remove dead references before returning some objects, also when not expanded, if I remember correctly.

Other then that, "user": null sounds reasonable to me.

@Dragomir-Ivanov
Copy link
Contributor Author

Thanks! Actually I don't care about cascading deletes, just want for rest-layer not to crash if such use case happens. Will try to fix "user": null issue.

Dragomir-Ivanov added a commit to Dragomir-Ivanov/rest-layer that referenced this issue Jan 2, 2019
Dragomir-Ivanov added a commit to Dragomir-Ivanov/rest-layer that referenced this issue Jan 2, 2019
Dragomir-Ivanov added a commit to Dragomir-Ivanov/rest-layer that referenced this issue Jan 3, 2019
@smyrman smyrman closed this as completed in 8bd1218 Jan 8, 2019
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

2 participants