-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Serialize instances of classes #2707
Conversation
@abernix is there anything I can do to get this merged? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not clear to me what this PR is for or why changing the expectations of the tests would be appropriate.
To be honest, I'm having a hard time understanding what the intention of this PR is, particularly since it's subject is suggesting that we should stringify something, but the changes here do nothing to stringify anything.
Before we can merge this, can you please put together a reproduction for the problem you're running into and update this PR with a link to that reproduction? Ultimately, that reproduction should be used to create a new test (rather than changing an old test to meet your new requirements), or you should provide evidence as to why the old test is wrong.
Thanks!
expect(fetch.mock.calls[0][0].body).not.toEqual('{}'); | ||
expect(fetch.mock.calls[0][0].headers.get('Content-Type')).not.toEqual( | ||
expect(fetch.mock.calls[0][0].body).toEqual('{}'); | ||
expect(fetch.mock.calls[0][0].headers.get('Content-Type')).toEqual( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are you reversing what these two expectations are testing for?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@abernix see my comment below.
@abernix Sorry for the lack of details. Here is the situation. The implementation of the Current implementation uses Consequently, the test I am reversing the expectations is also wrong, when passing an "instance of class", here a I see two possibilities here:
The correction in this pull request, is to use Hope this is clearer. Please tell me if you need any more information / change from my side. |
I appreciate the clear explanation; Thank you! I think that switching to Perhaps @uosl could provide their thoughts? |
This is a little bit sad, it would imply using using |
I agree with this PR. What @quentinus95 is saying is correct, and the test I made is semantically wrong. However, this commit will break code that depends on FormData not being serialized (which is definitely the case for our use of I will try to look into whether there is an accurate way of identifying form data without pulling in the A different way to solve this could be to require custom classes to implement a |
I don't think we should put an exception in here for The correct thing to leverage is the behavior that would be expected from |
Alright, so a simple solution would be to check for the presence of If you wanted the standard object stringification for class instances, you would simply define the method to return this.
|
I don't really like having to pollute dozen of classes with a toJSON method, with no interface implementation (due to JS limitations). Don't we have too much logic in this class? In my opinion, it would be clearer to ask for the user to set the proper Content-Type header to get FormData handled properly, otherwise it would default to application/json. For instance, this extract of the logic looks strange to me: options.body = JSON.stringify(options.body);
// If Content-Type header has not been previously set, set to application/json
if (!options.headers.get('Content-Type')) {
options.headers.set('Content-Type', 'application/json');
} We could have a json stringified body while not getting the header to be set properly. A condition on the headers in the first place would allow to determine if we should json-encode data.
|
I've found that using A safer equivalent alternative would be Related to apollographql/datasource-rest#68 |
The code to detect objects was malformed due to not detecting objects with direct constructors other than `Object`. It was implemented to prevent multipart data to be serialized. This should be prevented by detecting the multipart header and not the object type. This adds another safe guard to skip objects that might not be serializable in the first place. Refs: apollographql#1316 Refs: apollographql#2707 Fixes: https://github.com/apollographql/apollo-server/issues/1539
The code to detect objects was malformed due to not detecting objects with direct constructors other than `Object`. It was implemented to prevent multipart data to be serialized. This should be prevented by detecting the multipart header and not the object type. This adds another safe guard to skip objects that might not be serializable in the first place. Refs: apollographql#1316 Refs: apollographql#2707 Fixes: https://github.com/apollographql/apollo-server/issues/1539
In my app, I use TypeScript & instances of classes.