angular-restmod icon indicating copy to clipboard operation
angular-restmod copied to clipboard

inline reference relations with same key as attribute name clobbers inline resource

Open jczerwinski opened this issue 10 years ago • 3 comments

Imagine we want:

    Person:
        restmod.model('/people').mix({
            parent: {
                belongsTo: 'Person',
                key: 'parent'
            }
        })

So we can support either:

    // GET /people/mom
    {
        id: "mom",
        parent: "grandma"
    }

or

    // GET /people/mom
    {
        id: "mom",
        parent: {
            id: "grandma"
        }
    }

Inline resources in responses like this like this seem to get clobbered. Am I the only one?

Maybe I should change my API -- should I? -- but it seems to me that this use case should work nevertheless.

Edit: Mongoose supports this kind of behavior by default.

jczerwinski avatar Feb 12 '15 00:02 jczerwinski

I have the same question!

geyang avatar Feb 12 '15 08:02 geyang

There seems to be a bug in restmod when using the same value for key as the association name, it tries to handle the key as inline data for the nested object and it breaks, I'll take a look into that.

I'm not sure if the behaviour you propose is possible though, restmod need to know in advance wether an attribute is a key or a nested object...

iobaixas avatar Feb 16 '15 20:02 iobaixas

Hi @iobaixas In mongoose we can test if a field is populated with isPopulated() function. Here we might be able to test so by looking for the $pk field, and if it doesn't exist then the item is unpopulated, and restmod can proceed to populate it.

geyang avatar Feb 21 '15 06:02 geyang