Skip to content
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

Merged
merged 10 commits into from Dec 10, 2019
Merged

Conversation

YouCanKeepSilence
Copy link
Contributor

@YouCanKeepSilence YouCanKeepSilence commented Nov 12, 2019

Checklist

  • run npm run test and npm run benchmark
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message and code follows Code of conduct

lib/validation.js Outdated Show resolved Hide resolved
lib/validation.js Outdated Show resolved Hide resolved
lib/validation.js Outdated Show resolved Hide resolved
@YouCanKeepSilence
Copy link
Contributor Author

I'll try to add this as explicit param.

Copy link
Member

@jsumners jsumners left a 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.

lib/reply.js Outdated Show resolved Hide resolved
docs/Reply.md Outdated Show resolved Hide resolved
docs/Reply.md Outdated Show resolved Hide resolved
Co-Authored-By: James Sumners <james@sumners.email>
@@ -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 => {
Copy link
Member

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

Copy link
Contributor Author

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()?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

@YouCanKeepSilence
Copy link
Contributor Author

Don't know the reason why checks fails? Can't find the error log. At local machine 'test', 'test:ci', 'benchmark' scripts run correctly

@RafaelGSS
Copy link
Member

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.

@YouCanKeepSilence
Copy link
Contributor Author

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
Copy link
Member

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.

Copy link
Contributor Author

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?

Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@YouCanKeepSilence
Copy link
Contributor Author

So what about checks? What is incorrect?

@mcollina
Copy link
Member

It's just flaky CI

@YouCanKeepSilence
Copy link
Contributor Author

It's just flaky CI

So it'll be approved later? Or you just merge it with ignore this checks?

@mcollina
Copy link
Member

Let's wait another review (cc @jsumners), then it can land.

Copy link
Member

@jsumners jsumners left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Member

@RafaelGSS RafaelGSS left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@YouCanKeepSilence
Copy link
Contributor Author

What do you think?

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:)

@YouCanKeepSilence
Copy link
Contributor Author

So, what about merge this fix? Or you need changes?

@mcollina
Copy link
Member

@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.
This was added to fix #540 and #541 (a long time ago). None had the intention of implementing the behavior that this PR is fixing.
I think changing that serialization is skipped when the content-type is specified is what we intended.
Therefore, we propose to treat that as a bugfix and release it in v2.

@YouCanKeepSilence
Copy link
Contributor Author

@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.
This was added to fix #540 and #541 (a long time ago). None had the intention of implementing the behavior that this PR is fixing.
I think changing that serialization is skipped when the content-type is specified is what we intended.
Therefore, we propose to treat that as a bugfix and release it in v2.

So you'll make it by yourself and my PR is not needed?(

@delvedor
Copy link
Member

@YouCanKeepSilence you can update this pr and follow the suggestions I wrote in #1937 (comment).
Otherwise, you can open a new pr and reference this one :)

@Eomm Eomm added the bugfix Issue or PR that should land as semver patch label Nov 28, 2019
…it removed). Now if content-type is application/json and typeof payload is string it will not be serialize
@YouCanKeepSilence
Copy link
Contributor Author

I still don't know why checks fails, all test and benchs were ok.

Copy link
Member

@delvedor delvedor left a 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.

Copy link
Member

@delvedor delvedor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@delvedor delvedor requested a review from a team December 9, 2019 09:37
Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Eomm Eomm merged commit c5d2820 into fastify:master Dec 10, 2019
theliuk pushed a commit to theliuk/fastify that referenced this pull request Dec 13, 2019
@adrai
Copy link
Member

adrai commented Feb 19, 2020

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: 5d4a5dcf-f265-499d-94fe-644144c323dd
response with v2.11: "5d4a5dcf-f265-499d-94fe-644144c323dd"

in v2.11 it was a json and in v2.12 it is now a string

@jsumners
Copy link
Member

That definitely seems like an out of spec regression.

@mcollina
Copy link
Member

@jsumners it’s exacty what this PR does. We might consider reverting it in v2 and applying it only in v3.

@jsumners
Copy link
Member

@mcollina I thought the PR made it an option to skip serialization of a string.

Invalid JSON:

foo

Valid JSON:

"foo"

@adrai
Copy link
Member

adrai commented Feb 19, 2020

for me there is no problem... have a workaround...
imo this PR is ok for v2, but maybe there should also be a fix to handle a string correctly in case the content type is application/json

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

@github-actions
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 10, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bugfix Issue or PR that should land as semver patch
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants