🔥 feat: Add default UTF-8 charset#3583
Conversation
Co-authored-by: ReneWerner87 <7063188+ReneWerner87@users.noreply.github.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3583 +/- ##
==========================================
+ Coverage 91.10% 91.11% +0.01%
==========================================
Files 112 112
Lines 11262 11280 +18
==========================================
+ Hits 10260 10278 +18
Misses 748 748
Partials 254 254
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
⚠️ Performance Alert ⚠️
Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.50.
| Benchmark suite | Current: 67a21eb | Previous: 30dee26 | Ratio |
|---|---|---|---|
Benchmark_Ctx_Type |
87.27 ns/op 0 B/op 0 allocs/op |
38.05 ns/op 0 B/op 0 allocs/op |
2.29 |
Benchmark_Ctx_Type - ns/op |
87.27 ns/op |
38.05 ns/op |
2.29 |
This comment was automatically generated by workflow using github-action-benchmark.
|
@copilot is this extension of the config really needed ? the signature says func (c *DefaultCtx) JSON(data any, ctype ...string) error {its possible to give the content type as a second parameter |
|
should we change the default json content type ? |
While the Without the config, users would need to remember to call |
|
@copilot but why not making the uft8 json content type the default type ? |
Remove configuration complexity and directly set JSON content-type to include charset=utf-8 by default. Custom content-type parameter still allows override when needed. Co-authored-by: ReneWerner87 <7063188+ReneWerner87@users.noreply.github.com>
You're absolutely right! I've simplified the implementation to just change the default JSON content type to include Warning Firewall rules blocked me from connecting to one or more addressesI tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
|
Important Review skippedBot user detected. To trigger a single review, invoke the You can disable this status message by setting the 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Join our Discord community for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Pull Request Overview
This PR adds configurable charset support to Fiber with UTF-8 as the default for JSON responses, addressing character encoding issues where special characters were corrupted due to missing charset specification in the Content-Type header.
- Introduces a new
Charsetconfiguration field with UTF-8 as the default - Updates the
JSON()method to use charset-enabled MIME type when no custom content-type is provided - Maintains backward compatibility while improving JSON standard compliance
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| ctx.go | Updates JSON method implementation to use UTF-8 charset by default and updates documentation |
| ctx_test.go | Updates test expectations to reflect new default charset behavior in Content-Type headers |
gaby
left a comment
There was a problem hiding this comment.
The docs should be update to indicate that utf-8 charset is the default
…ture Co-authored-by: gaby <835733+gaby@users.noreply.github.com>
…set defaults Co-authored-by: gaby <835733+gaby@users.noreply.github.com>
Updated the documentation in docs/api/ctx.md to indicate that UTF-8 charset is the default for both JSON and XML methods. The docs now clearly show that JSON responses include "application/json; charset=utf-8" by default and XML responses include "application/xml; charset=utf-8". (commit dd81c9a) |
Co-authored-by: gaby <835733+gaby@users.noreply.github.com>
|
Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details. Warning Firewall rules blocked me from connecting to one or more addressesI tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
…cludeCharset function Co-authored-by: gaby <835733+gaby@users.noreply.github.com>
|
Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details. Warning Firewall rules blocked me from connecting to one or more addressesI tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
Summary
This PR adds configurable charset support to Fiber with UTF-8 as the default, resolving issues where special characters were corrupted in JSON responses due to missing charset specification.
Problem
Previously, Fiber's
JSON()method would set the Content-Type header toapplication/jsonwithout specifying a charset. This caused UTF-8 encoded characters like "Mädchen" to be displayed as "M�dchen" when stored in databases or processed by clients that didn't assume UTF-8 encoding.As noted in RFC 8259, JSON text exchanged between systems should be encoded using UTF-8, and implementations should specify the charset for better interoperability.
Solution
Added a new
Charsetconfiguration field that:charset=utf-8in JSON responsesCharset: "disabled"to use plainapplication/jsonUsage
Changes Made
Charsetfield toConfigstruct with UTF-8 defaultJSON()method to use charset-enabled MIME type when configuredTesting
This change makes Fiber more compliant with JSON standards and prevents character encoding issues out of the box, while maintaining full backward compatibility.
Fixes #2575.
Warning
Firewall rules blocked me from connecting to one or more addresses
I tried to connect to the following addresses, but was blocked by firewall rules:
exampleretry.com/tmp/go-build2262719892/b268/client.test -test.testlogfile=/tmp/go-build2262719892/b268/testlog.txt -test.paniconexit0 -test.v=true -test.timeout=2m0s(dns block)If you need me to access, download, or install something from one of these locations, you can either:
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.