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

fix: set reply's default charset to utf8 #3789

Merged
merged 2 commits into from
Mar 22, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/Reference/Reply.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ Sets the content type for the response. This is a shortcut for
```js
reply.type('text/html')
```
When set `Content-Type` with json type, if you don't set `charset`, it will use `utf-8` by default.
Copy link
Member

@Fdawgs Fdawgs Mar 22, 2022

Choose a reason for hiding this comment

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

Suggested change
When set `Content-Type` with json type, if you don't set `charset`, it will use `utf-8` by default.
If the `Content-Type` has a JSON subtype, and the charset parameter is not set, `utf-8` will be used as the charset by default.

Copy link
Member

Choose a reason for hiding this comment

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

Ooops, I missed this. I'll amend on main.

Copy link
Member

Choose a reason for hiding this comment

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

Cheers @mcollina!


### .serializer(func)
<a id="serializer"></a>
Expand Down
20 changes: 6 additions & 14 deletions lib/reply.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,22 +158,14 @@ Reply.prototype.send = function (payload) {
if (hasContentType === false) {
this[kReplyHeaders]['content-type'] = CONTENT_TYPE.JSON
} else {
// If hasContentType === true, we have a JSON mimetype
// If user doesn't set charset, we will set charset to utf-8
if (contentType.indexOf('charset') === -1) {
// If we have simply application/json instead of a custom json mimetype
if (contentType.indexOf('/json') > -1) {
this[kReplyHeaders]['content-type'] = CONTENT_TYPE.JSON
const customContentType = contentType.trim()
if (customContentType.lastIndexOf(';') === customContentType.length - 1) {
// custom content-type is ended with ';'
this[kReplyHeaders]['content-type'] = `${customContentType} charset=utf-8`
} else {
const currContentType = this[kReplyHeaders]['content-type']
// We extract the custom mimetype part (e.g. 'hal+' from 'application/hal+json')
const customJsonType = currContentType.substring(
currContentType.indexOf('/'),
currContentType.indexOf('json') + 4
)

// We ensure we set the header to the proper JSON content-type if necessary
// (e.g. 'application/hal+json' instead of 'application/json')
this[kReplyHeaders]['content-type'] = CONTENT_TYPE.JSON.replace('/json', customJsonType)
this[kReplyHeaders]['content-type'] = `${customContentType}; charset=utf-8`
}
}
}
Expand Down
24 changes: 24 additions & 0 deletions test/internals/reply.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,30 @@ test('non-string with content type application/json SHOULD be serialized as json
})
})

test('non-string with custom json\'s content-type SHOULD be serialized as json', t => {
t.plan(4)

const fastify = require('../..')()

fastify.get('/', function (req, reply) {
reply.type('application/json; version=2; ').send({ key: 'hello world!' })
})

fastify.listen({ port: 0 }, err => {
t.error(err)
fastify.server.unref()
Copy link
Member

Choose a reason for hiding this comment

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

I know it is used in other tests, but could you use the t.teaddown function instead of server.unref?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

PTAL, but I want to know what is the difference between t.teardown & server.unref, more semantic?

Copy link
Member

Choose a reason for hiding this comment

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

t.teardown run when the test end and you can provide callback function to exit the environment cleanly.
server.unref just didn't block the program end when no code need to run inside the event loop. It is an unclean close.


sget({
method: 'GET',
url: 'http://localhost:' + fastify.server.address().port
}, (err, response, body) => {
t.error(err)
t.equal(response.headers['content-type'], 'application/json; version=2; charset=utf-8')
t.same(body.toString(), JSON.stringify({ key: 'hello world!' }))
})
})
})

test('non-string with custom json content type SHOULD be serialized as json', t => {
t.plan(16)

Expand Down