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
Skip serialization for json string #1937
Conversation
I'll try to add this as explicit param. |
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.
This seems like a fair solution for people that know their output does not need to be serialized. However, the documentation will need to be updated.
Co-Authored-By: James Sumners <james@sumners.email>
test/internals/reply.test.js
Outdated
@@ -544,6 +544,30 @@ test('plain string with content type application/json should be serialized as js | |||
}) | |||
}) | |||
|
|||
test('string with content type application/json and skipSerialization() called should NOT be serialized as json', t => { |
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.
Could add another test to JSON.stringfy({ test: true })
? Just to prevent something
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.
What you mean? Simple send serialized via JSON.stringify data with skipSerialization()?
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.
Yes
Don't know the reason why checks fails? Can't find the error log. At local machine 'test', 'test:ci', 'benchmark' scripts run correctly |
I think that is a little bug inside our CI, timeout or something like it. Don't worry. |
So i should just wait? |
lib/reply.js
Outdated
@@ -195,6 +197,12 @@ Reply.prototype.headers = function (headers) { | |||
return this | |||
} | |||
|
|||
Reply.prototype.skipSerialization = function () { | |||
this[kReplyskipSerialization] = true | |||
this.context[kReplyskipSerialization] = true |
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.
Unfortunately this won't work. The context is shared between multiple execution of the handler and multiple reply objects. Can you remove it? The alternative is to move this to the Context itself, and set this option during the route creation.
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.
You told about this.context? What about just this? Will it work If i pass from this[kReplyskipSerialization] to serialize() method as optional params?
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.
I would actually change the serialize function to receive the Reply object instead.
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.
Changed it to pass this param via this
of reply object. Added test to check that skipSerialization
call doesn't affect next responses.
…am. Updated doc. Actualized tests
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.
LGTM
So what about checks? What is incorrect? |
It's just flaky CI |
So it'll be approved later? Or you just merge it with ignore this checks? |
Let's wait another review (cc @jsumners), then it can land. |
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.
LGTM
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.
LGTM
I agree with @Eomm, so as i think more acceptable solution now is add explicit flag, and later change behaviour of this functionality. So if we talking about explicit flag i made it in current PR:) |
So, what about merge this fix? Or you need changes? |
@delvedor and myself had a quick chat about this, and we came to the conclusion that this is a bug and that test is wrong. |
So you'll make it by yourself and my PR is not needed?( |
@YouCanKeepSilence you can update this pr and follow the suggestions I wrote in #1937 (comment). |
…it removed). Now if content-type is application/json and typeof payload is string it will not be serialize
I still don't know why checks fails, all test and benchs were ok. |
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.
@YouCanKeepSilence since the payload is already serialized, there is no need to call the preSerialization
hook.
You can follow what I wrote in #1937 (comment), and if we detect that the content-type
is configured, and the payload is a string already, we should call the onSend
hook directly, similar to what we are doing for the text-plain
handling.
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.
LGTM
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.
LGTM
Is it intended that with this fix also such a response now comes different with v2.12? example: reply.type('application/json')
reply.send('5d4a5dcf-f265-499d-94fe-644144c323dd') response with v2.12: in v2.11 it was a json and in v2.12 it is now a string |
That definitely seems like an out of spec regression. |
@jsumners it’s exacty what this PR does. We might consider reverting it in v2 and applying it only in v3. |
@mcollina I thought the PR made it an option to skip serialization of a string. Invalid JSON: foo Valid JSON: "foo" |
for me there is no problem... have a workaround... perhaps by testing the string with something like this: function isJSON(str) {
if (str === '') return false;
str = str.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@');
str = str.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']');
str = str.replace(/(?:^|:|,)(?:\s*\[)+/g, '');
return (/^[\],:{}\s]*$/).test(str);
} "copied" from: https://github.com/prototypejs/prototype/blob/dee2f7d8611248abce81287e1be4156011953c90/src/prototype/lang/string.js#L715 |
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Checklist
npm run test
andnpm run benchmark