Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

$resource.query turn strings into objects in response array #6314

Closed
ya-betmen opened this issue Feb 18, 2014 · 19 comments
Closed

$resource.query turn strings into objects in response array #6314

ya-betmen opened this issue Feb 18, 2014 · 19 comments

Comments

@ya-betmen
Copy link

http://plnkr.co/edit/6Up7bFGica8qOYTs3Gwk?p=preview

bug.html

<html ng-app="demo">
    <head>
        <title></title>
        <meta content="">
        <style></style>
        <script type="text/javascript" src="1.2.9/angular.js"></script>
        <script type="text/javascript" src="1.2.9/angular-resource.js"></script>
        <script type="text/javascript">
            angular.module('demo', ['ngResource'])
                    .controller('MainCtrl', function($scope, $resource) {
                        $resource('http://localhost/array.json', {}, {}).query(function(data) {
                            $scope.test = data;
                        });
                    });
        </script>
    </head>
    <body ng-controller="MainCtrl">
        {{test}}
    </body>
</html>

array.json

[{
        "@class": "class1",
        "@id": "31fde5d2-bad3-4dbf-892d-ae20323db310",
        "id": 1,
        "array": [
            {
                "@class": "class1",
                "@id": "8889b00c-b14d-47a8-9d82-ae3ca5d4b2d0",
                "id": 2

            }, {
                "@class": "class1",
                "@id": "100fe202-16b3-4e39-990e-a4f6c5005341",
                "id": 3
            }, {
                "@class": "class1",
                "@id": "fc2a83f4-27be-46b0-acd3-6a0430025af1",
                "id": 4
            }, {
                "@class": "class1",
                "@id": "c3d6630f-93b4-40b6-a37b-e0ce2b69dff3",
                "id": 5
            }
        ]
}, "8889b00c-b14d-47a8-9d82-ae3ca5d4b2d0", "100fe202-16b3-4e39-990e-a4f6c5005341", "fc2a83f4-27be-46b0-acd3-6a0430025af1", "c3d6630f-93b4-40b6-a37b-e0ce2b69dff3"]

result

[{"@class":"class1","@id":"31fde5d2-bad3-4dbf-892d-ae20323db310","id":1,"array":[{"@class":"class1","@id":"8889b00c-b14d-47a8-9d82-ae3ca5d4b2d0","id":2},{"@class":"class1","@id":"100fe202-16b3-4e39-990e-a4f6c5005341","id":3},{"@class":"class1","@id":"fc2a83f4-27be-46b0-acd3-6a0430025af1","id":4},{"@class":"class1","@id":"c3d6630f-93b4-40b6-a37b-e0ce2b69dff3","id":5}]},{"0":"8","1":"8","2":"8","3":"9","4":"b","5":"0","6":"0","7":"c","8":"-","9":"b","10":"1","11":"4","12":"d","13":"-","14":"4","15":"7","16":"a","17":"8","18":"-","19":"9","20":"d","21":"8","22":"2","23":"-","24":"a","25":"e","26":"3","27":"c","28":"a","29":"5","30":"d","31":"4","32":"b","33":"2","34":"d","35":"0"},{"0":"1","1":"0","2":"0","3":"f","4":"e","5":"2","6":"0","7":"2","8":"-","9":"1","10":"6","11":"b","12":"3","13":"-","14":"4","15":"e","16":"3","17":"9","18":"-","19":"9","20":"9","21":"0","22":"e","23":"-","24":"a","25":"4","26":"f","27":"6","28":"c","29":"5","30":"0","31":"0","32":"5","33":"3","34":"4","35":"1"},{"0":"f","1":"c","2":"2","3":"a","4":"8","5":"3","6":"f","7":"4","8":"-","9":"2","10":"7","11":"b","12":"e","13":"-","14":"4","15":"6","16":"b","17":"0","18":"-","19":"a","20":"c","21":"d","22":"3","23":"-","24":"6","25":"a","26":"0","27":"4","28":"3","29":"0","30":"0","31":"2","32":"5","33":"a","34":"f","35":"1"},{"0":"c","1":"3","2":"d","3":"6","4":"6","5":"3","6":"0","7":"f","8":"-","9":"9","10":"3","11":"b","12":"4","13":"-","14":"4","15":"0","16":"b","17":"6","18":"-","19":"a","20":"3","21":"7","22":"b","23":"-","24":"e","25":"0","26":"c","27":"e","28":"2","29":"b","30":"6","31":"9","32":"d","33":"f","34":"f","35":"3"}]
@tbosch tbosch self-assigned this Feb 18, 2014
@tbosch tbosch added this to the Backlog milestone Feb 18, 2014
@tbosch tbosch removed their assignment Feb 18, 2014
@tmeani
Copy link

tmeani commented Jun 7, 2014

+1

1 similar comment
@pensierinmusica
Copy link

+1

@caitp
Copy link
Contributor

caitp commented Jun 7, 2014

A fix for this would be trivial, I'll submit a patch

@caitp
Copy link
Contributor

caitp commented 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

caitp added a commit to caitp/angular.js that referenced this issue Jun 7, 2014
…hen isArray is true

Previously non-object literals would be thrown out of Resource responses with isArray===true, or
otherwise converted into Objects (in the case of string literals). The reason for this is because
shallowClearAndCopy iterates over keys, and copies keys into the destination. Iterating over String
keys results in integer keys, with a single-character value.

Not converting non-objects to Resources means that you lose the ability to perform Resource operations
on them. However, they become usable as strings, numbers, or booleans, which is important.

In the future, it would be useful to make these useful as Resources while still retaining their primitive
value usefulness.

Closes angular#6314
@rodyhaddad
Copy link
Contributor

Why would your REST server return an array in that form?
I'm failing to see a use case for this.

@caitp
Copy link
Contributor

caitp commented 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.

@rodyhaddad
Copy link
Contributor

@caitp okay, just wanted to make sure before commenting on your PR :-)

@ya-betmen
Copy link
Author

@caitp @rodyhaddad It's real use case if you have crazy data model. On server side json serializer replaces duplicates by UIDs and sometimes objects in main array already are in fields of first object. That’s why i have object and string in one array. Of course i used hypothetical example.

I tried fix it but I could not find one who breaks my response.

@caitp
Copy link
Contributor

caitp commented Jun 10, 2014

crazy data model

So this should be a pretty high-impact bug then, because thats basically the definition of most web services

@deanshub
Copy link

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
Copy link
Contributor

caitp commented 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
Copy link

Got it, hope you pull request will be accepted
because $http makes my code ugly,
thanks for the explanation your a good person @caitp

rodyhaddad pushed a commit to rodyhaddad/angular.js that referenced this issue Jun 13, 2014
…hen isArray is true

Previously non-object literals would be thrown out of Resource responses with isArray===true, or
otherwise converted into Objects (in the case of string literals). The reason for this is because
shallowClearAndCopy iterates over keys, and copies keys into the destination. Iterating over String
keys results in integer keys, with a single-character value.

Not converting non-objects to Resources means that you lose the ability to perform Resource operations
on them. However, they become usable as strings, numbers, or booleans, which is important.

In the future, it would be useful to make these useful as Resources while still retaining their primitive
value usefulness.

Closes angular#6314
Closes angular#7741
@caitp caitp closed this as completed in 16dfcb6 Jun 13, 2014
rodyhaddad pushed a commit to rodyhaddad/angular.js that referenced this issue Jun 13, 2014
…hen isArray is true

Previously non-object literals would be thrown out of Resource responses with isArray===true, or
otherwise converted into Objects (in the case of string literals). The reason for this is because
shallowClearAndCopy iterates over keys, and copies keys into the destination. Iterating over String
keys results in integer keys, with a single-character value.

Not converting non-objects to Resources means that you lose the ability to perform Resource operations
on them. However, they become usable as strings, numbers, or booleans, which is important.

In the future, it would be useful to make these useful as Resources while still retaining their primitive
value usefulness.

Closes angular#6314
Closes angular#7741
rodyhaddad pushed a commit to rodyhaddad/angular.js that referenced this issue Jun 13, 2014
…hen isArray is true

Previously non-object literals would be thrown out of Resource responses with isArray===true, or
otherwise converted into Objects (in the case of string literals). The reason for this is because
shallowClearAndCopy iterates over keys, and copies keys into the destination. Iterating over String
keys results in integer keys, with a single-character value.

Not converting non-objects to Resources means that you lose the ability to perform Resource operations
on them. However, they become usable as strings, numbers, or booleans, which is important.

In the future, it would be useful to make these useful as Resources while still retaining their primitive
value usefulness.

Closes angular#6314
Closes angular#7741
@pensierinmusica
Copy link

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
Copy link

@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
Copy link

atentis commented 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
Copy link

Yup, still happens.

@almothafar
Copy link

Version 1.5.1 and I still get happening.

@gkalpak
Copy link
Member

gkalpak commented 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
Copy link

Still there. Don't know what else to add to the issue.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet