-
Notifications
You must be signed in to change notification settings - Fork 27.4k
feat($httpParamSerializerJQLike): honor object.toJSON function if present #12330
Conversation
It sounds like a good addition, but it definitively need tests before it can be merged. @gabrielmaldi could you please add tests for this change? You can use the existing ones as inspiration. |
5ff4d10
to
919ea72
Compare
I added one test for |
Given the breaking chance nature, I am leaning on moving this to 1.5. @petebacondarwin WDYT? |
919ea72
to
7bc9cc4
Compare
I made an adjustment: The call to So now we call |
7bc9cc4
to
f393ad5
Compare
f393ad5
to
0b0a4f4
Compare
0b0a4f4
to
8cf99b6
Compare
Hey guys, I rebased my changes. Now that |
8cf99b6
to
b57bfae
Compare
b1aa8eb
to
21bbcb6
Compare
d4b331a
to
049b5f7
Compare
I found a problem when a
Since } else if (isFunction(toSerialize.toJSON)) {
parts.push(encodeUriQuery(prefix) + '=' + encodeUriQuery(toSerialize.toJSON())); And everything ends up with I didn't find a place where If } else if (isFunction(toSerialize.toJSON) && toSerialize.constructor.name !== "Resource") {
parts.push(encodeUriQuery(prefix) + '=' + encodeUriQuery(toSerialize.toJSON())); But this is very fragile because of the use of What do you guys think? Thanks! |
It is used implicitly by
It does conform to what WRT to this PR in general, I am not sure if it is a good idea to take If jQuery does that (which I don't think it should - but that is irrelevant), we should do it too in order to be consistent. But if jQuery doesn't do it, then neither should we. |
049b5f7
to
3dc5d0c
Compare
I had read that MDN article but I guess that since in their example they return a string, it wasn't immediatly obvious to me that
meant that you should return
Sorry about that and thanks for clarifying!
I ran some tests on jQuery and Angular with and without this PR: jQueryjQuery.param({
myObject: {
prop1: 'value1',
toJSON: function () {
return 'anotherValue';
}
}
});
jQuery.param({
myObject: {
prop1: 'value1',
func1: function () {
return { 'prop2': 'value2' };
}
}
});
Angular without this PRjqrSer({
myObject: {
prop1: 'value1',
toJSON: function () {
return 'anotherValue';
}
}
});
jqrSer({
myObject: {
prop1: 'value1',
func1: function () {
return { 'prop2': 'value2' };
}
}
});
Angular with this PRjqrSer({
myObject: {
prop1: 'value1',
toJSON: function () {
return 'anotherValue';
}
}
});
jqrSer({
myObject: {
prop1: 'value1',
func1: function () {
return { 'prop2': 'value2' };
}
}
});
The first thing that emerges (from the very first test) is that jQuery doesn't use However, it seems that jQuery also includes the values returned by any functions (it doesn't treat Angular is currently not consistent with this behaviour. I don't like jQuery's behaviour either, I'd ditch serializing functions altogether. But if jQuery does it this way, should we too? I don't know if consistency with jQuery should be the only deciding factor here, because what it is doing with funcions is IMHO nonsense (what we are doing is too). Also, jQuery doesn't sort parameters while Angular does (should we switch from If it were up to me, I wouldn't serialize functions and I'd take I updated the PR with these changes and added a couple of tests. This also solves the problem with Thanks for your time! |
3dc5d0c
to
df9dbfb
Compare
f8142d9
to
98d6691
Compare
d173960
to
73d82b7
Compare
73d82b7
to
8503a46
Compare
70049ae
to
b77e14b
Compare
Sorry for the late response. I still think we should follow jQuery as close as possible (especially since this paramSerializer has that exact purpose: replicate jQuery's serialization). |
8503a46
to
aa0de0d
Compare
Closing since I believe we should try to keep as close as possible to the jQuery behavior (whether we like it or not 😉). This is the purpose of Thx for investigating and working on this, @gabrielmaldi ❤️ |
$httpParamSerializerJQLike
gives dates special treatment (i.e. treats them as values instead of drilling down into their properties).This change extends this behavior by honoring the toJSON function, if present on the object being serialized.
While keeping the existing behavior for dates, it also adds support for serializing custom objects which should be treated as values, such as
Moment.js
moments.