Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
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

Closed

Description

@ya-betmen

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"}]

Activity

  1. self-assigned this
    on Feb 18, 2014
  2. added this to the milestone on Feb 18, 2014
  3. removed their assignment
    on Feb 18, 2014
  4. tmeani commented on Jun 7, 2014

    @tmeani

    +1

  5. pensierinmusica commented on Jun 7, 2014

    @pensierinmusica

    +1

  6. caitp commented on Jun 7, 2014

    @caitp
    Contributor

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

  7. caitp commented on Jun 7, 2014

    @caitp
    Contributor

    If you guys find bugs that you really want fixed, just ping me, I'm bored and I like fixing stuff :c

  8. added a commit that references this issue on Jun 7, 2014
    278c97b
  9. rodyhaddad commented on Jun 9, 2014

    @rodyhaddad
    Contributor

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

  10. caitp commented on Jun 9, 2014

    @caitp
    Contributor

    @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.

  11. 2 remaining items

  12. caitp commented on Jun 10, 2014

    @caitp
    Contributor

    crazy data model

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

  13. deanshub commented on Jun 12, 2014

    @deanshub

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

  14. caitp commented on Jun 12, 2014

    @caitp
    Contributor

    @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.

  15. deanshub commented on Jun 13, 2014

    @deanshub

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

  16. added a commit that references this issue on Jun 13, 2014
    19969fd
  17. added 2 commits that reference this issue on Jun 13, 2014
    1cae1d1
    f0904cf
  18. pensierinmusica commented on Jun 29, 2014

    @pensierinmusica

    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 :)

  19. blacelle commented on Aug 27, 2014

    @blacelle

    @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).

  20. atentis commented on Oct 16, 2014

    @atentis

    @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.

  21. DSpeichert commented on Oct 17, 2014

    @DSpeichert

    Yup, still happens.

  22. almothafar commented on Mar 17, 2016

    @almothafar

    Version 1.5.1 and I still get happening.

  23. gkalpak commented on Mar 19, 2016

    @gkalpak
    Member

    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.

  24. andreaslarssen commented on Sep 21, 2017

    @andreaslarssen

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions