You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
Repository navigation
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
$resource.query turn strings into objects in response array #6314
@rodyhaddad there's no real use case, but it's besides the point, we shouldn't be trying to convert strings/numbers/booleans into objects --- or rather, we shouldn't be trying to copy properties of those primitives into a Resource object. Because, we lose the number/booleans, and we convert strings into garbage. This is wrong.
@deanshub what he's saying is that it's a weird use for ngResource, because resource is expected to be talking to someones definition of a RESTful web service, which will structure data according to someones definition of "appropriately".
The assumption is that the returned array is an array of individual resources (with their own unique identifiers, and their own content), so the contents of the array should always be objects. Otherwise, $http would work just as well for resources like this, without this issue.
So, whether it's a case of abuse of ngResource, or bad assumptions on our part, it doesn't really matter. But if you are running into this, you might consider just using $http instead, and then converting actual resources into Resource objects if you need to, while keeping the primitive/literals as they are. That is basically the work around until it lands, or in case it doesn't.
In our case, we just wanted to return a simple string, but with the existing implementation it was not possible. We had to first assign that string to the value of an object's key, and only then it would work. Otherwise something like "hello"would be transformed into {0: 'h', 1: 'e', 2: 'l', 3: 'l', 4: 'o'}, which is kinda lame (it's not even an array, so it can't be quickly converted back into a string).
@pensierinmusica, I believe this fix handles Literals inside a JSON. It does not work if the output string in a pure Literal (i.e. not wrapped in an array or an object).
@pensierinmusica. I am having the exact issue you mentioned right now. I am doing a get with parameters and just need a single string but its transforming like yours. Was this issue not fixed?
Edit: Nevermind, it was my bad. I was returning a string which is incorrect for this architecture. I should have returned an object.
This issue is closed and the original problem is supposed to have been fixed.
If you are still running into problems, please open a new issue explaining your usecase and the actual vs expected behavior.
Activity
tmeani commentedon Jun 7, 2014
+1
pensierinmusica commentedon Jun 7, 2014
+1
caitp commentedon Jun 7, 2014
A fix for this would be trivial, I'll submit a patch
caitp commentedon Jun 7, 2014
If you guys find bugs that you really want fixed, just ping me, I'm bored and I like fixing stuff :c
fix(ngResource): don't convert literal values into Resource objects w…
rodyhaddad commentedon Jun 9, 2014
Why would your REST server return an array in that form?
I'm failing to see a use case for this.
caitp commentedon Jun 9, 2014
@rodyhaddad there's no real use case, but it's besides the point, we shouldn't be trying to convert strings/numbers/booleans into objects --- or rather, we shouldn't be trying to copy properties of those primitives into a Resource object. Because, we lose the number/booleans, and we convert strings into garbage. This is wrong.
2 remaining items
caitp commentedon Jun 10, 2014
So this should be a pretty high-impact bug then, because thats basically the definition of most web services
deanshub commentedon Jun 12, 2014
Simpler use case is requesting:
http://localhost:8322/api/designers?distinct=name
(mongo + baucis = <3)
and i should be getting an array like this:
["Sean paul van dam",
"Charlie Biton",
"Donna Victoria",
"Igor Romanov",
"Madonna",
"Dean Shub"]
but instead i get garbage...
caitp commentedon Jun 12, 2014
@deanshub what he's saying is that it's a weird use for ngResource, because resource is expected to be talking to someones definition of a RESTful web service, which will structure data according to someones definition of "appropriately".
The assumption is that the returned array is an array of individual resources (with their own unique identifiers, and their own content), so the contents of the array should always be objects. Otherwise, $http would work just as well for resources like this, without this issue.
So, whether it's a case of abuse of ngResource, or bad assumptions on our part, it doesn't really matter. But if you are running into this, you might consider just using $http instead, and then converting actual resources into Resource objects if you need to, while keeping the primitive/literals as they are. That is basically the work around until it lands, or in case it doesn't.
deanshub commentedon Jun 13, 2014
Got it, hope you pull request will be accepted
because $http makes my code ugly,
thanks for the explanation your a good person @caitp
fix(ngResource): don't convert literal values into Resource objects w…
fix(ngResource): don't convert literal values into Resource objects w…
fix(ngResource): don't convert literal values into Resource objects w…
pensierinmusica commentedon Jun 29, 2014
In our case, we just wanted to return a simple string, but with the existing implementation it was not possible. We had to first assign that string to the value of an object's key, and only then it would work. Otherwise something like "hello"would be transformed into
{0: 'h', 1: 'e', 2: 'l', 3: 'l', 4: 'o'}, which is kinda lame (it's not even an array, so it can't be quickly converted back into a string).Thanks @caitp for fixing this :)
blacelle commentedon Aug 27, 2014
@pensierinmusica, I believe this fix handles Literals inside a JSON. It does not work if the output string in a pure Literal (i.e. not wrapped in an array or an object).
atentis commentedon Oct 16, 2014
@pensierinmusica. I am having the exact issue you mentioned right now. I am doing a get with parameters and just need a single string but its transforming like yours. Was this issue not fixed?
Edit: Nevermind, it was my bad. I was returning a string which is incorrect for this architecture. I should have returned an object.
DSpeichert commentedon Oct 17, 2014
Yup, still happens.
almothafar commentedon Mar 17, 2016
Version 1.5.1 and I still get happening.
gkalpak commentedon Mar 19, 2016
This issue is closed and the original problem is supposed to have been fixed.
If you are still running into problems, please open a new issue explaining your usecase and the actual vs expected behavior.
andreaslarssen commentedon Sep 21, 2017
Still there. Don't know what else to add to the issue.