miragejs / ember-cli-mirage

An Ember Addon to easily add Mirage JS to your Ember app.
http://ember-cli-mirage.com
MIT License
864 stars 439 forks source link

Response coming as string instead of object #2577

Open gnanavinaymurari opened 4 months ago

gnanavinaymurari commented 4 months ago

Bug or question

mirage is giving string as response when we tried to return array in endpoint and it returns as object when we tried to return only one object from array:

Illustration: I have below endpoints:

  1. this.get('/users/:id',function(schema,request){ return schema.users.findBy({id:request.params.id}) }
  2. this.get('/users',function(schema,request){ return schema.users.all() }

    In test environment, both are working fine whereas in dev environment, second endpoint is giving response in string instead of an array.

gnanavinaymurari commented 4 months ago

When we debug an issue in depth, we encounter a model like this:

{
    "name": "John Doe",
    "age": 30,
    "hobbies": ["reading", "coding", "hiking"],
    "address": {
        "street": "123 Main St",
        "city": "Anytown",
        "country": "USA"
    }
}

However, when it is returned from Mirage, it appears as:

{
    "name": "John Doe",
    "age": 30,
    "hobbies": '["reading", "coding", "hiking"]',
    "address": {
        "street": "123 Main St",
        "city": "Anytown",
        "country": "USA"
    }
}

The hobbies field is an array but is being returned as a string instead of being converted back to an array.