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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃敟 Add support for application/problem+json #2704

Merged
merged 1 commit into from
Nov 13, 2023
Merged

馃敟 Add support for application/problem+json #2704

merged 1 commit into from
Nov 13, 2023

Conversation

rhburt
Copy link
Contributor

@rhburt rhburt commented Nov 5, 2023

Description

I have added support for RFC7807 application/problem+json alongside the existing implementation for application/json. According to the RFC, there are no mandatory fields for problem+json that distinguish it from regular json content; so, I have only added the ProblemJson methods that send requests/responses and associated tests that require the mime type.

This is my first PR to this project so please let me know if I need to change anything. Thanks.

EDIT:
I have changed the ProblemJSON methods to CustomJSON methods that take in the Content-Type header as a parameter. This allows us to support all JSON mime type extensions, such as "application/problem+json".

Fixes #2701

Type of change

  • New feature (non-breaking change which adds functionality)
  • This change requires a documentation update

Checklist:

  • For new functionalities I follow the inspiration of the express js framework and built them similar in usage
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation - /docs/ directory for https://docs.gofiber.io/
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • If new dependencies exist, I have checked that they are really necessary and agreed with the maintainers/community (we want to have as few dependencies as possible)
  • I tried to make my code as fast as possible with as few allocations as possible
  • For new code I have written benchmarks so that they can be analyzed and improved

Commit formatting:

Use emojis on commit messages so it provides an easy way of identifying the purpose or intention of a commit. Check out the emoji cheatsheet here: https://gitmoji.carloscuesta.me/

Copy link

welcome bot commented Nov 5, 2023

Thanks for opening this pull request! 馃帀 Please check out our contributing guidelines. If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

ctx.go Outdated Show resolved Hide resolved
Copy link
Member

@ReneWerner87 ReneWerner87 left a comment

Choose a reason for hiding this comment

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

thx for the effort
lets discuss some small points

client.go Outdated Show resolved Hide resolved
ctx.go Outdated Show resolved Hide resolved
ctx.go Outdated Show resolved Hide resolved
ctx_test.go Show resolved Hide resolved
client.go Outdated Show resolved Hide resolved
ctx.go Outdated Show resolved Hide resolved
@ReneWerner87 ReneWerner87 added this to the v2 Next Release milestone Nov 7, 2023
@rhburt
Copy link
Contributor Author

rhburt commented Nov 8, 2023

Benchmarks without variadic ctype parameter:

go test -run=^$ -bench=Benchmark_Ctx_CustomJSON_Ctype -benchmem -count=4

goos: linux
goarch: amd64
pkg: github.com/gofiber/fiber/v2
cpu: Intel(R) Core(TM) i7-10750H CPU @ 2.60GHz
Benchmark_Ctx_CustomJSON_Ctype-12        4666432               276.2 ns/op            56 B/op          2 allocs/op
Benchmark_Ctx_CustomJSON_Ctype-12        4483280               262.5 ns/op            56 B/op          2 allocs/op
Benchmark_Ctx_CustomJSON_Ctype-12        4582852               258.5 ns/op            56 B/op          2 allocs/op
Benchmark_Ctx_CustomJSON_Ctype-12        4697461               263.6 ns/op            56 B/op          2 allocs/op
PASS
ok      github.com/gofiber/fiber/v2     5.957s

Benchmarks with variadic ctype parameter:

go test -run=^$ -bench=Benchmark_Ctx_CustomJSON_Ctype -benchmem -count=4

goos: linux
goarch: amd64
pkg: github.com/gofiber/fiber/v2
cpu: Intel(R) Core(TM) i7-10750H CPU @ 2.60GHz
Benchmark_Ctx_CustomJSON_Ctype-12        4613229               269.6 ns/op            56 B/op         2 allocs/op
Benchmark_Ctx_CustomJSON_Ctype-12        4484907               256.6 ns/op            56 B/op         2 allocs/op
Benchmark_Ctx_CustomJSON_Ctype-12        4681248               244.3 ns/op            56 B/op         2 allocs/op
Benchmark_Ctx_CustomJSON_Ctype-12        4635668               229.8 ns/op            56 B/op         2 allocs/op
PASS
ok      github.com/gofiber/fiber/v2     5.673s

go test -run=^$ -bench=Benchmark_Ctx_CustomJSON_No_Ctype -benchmem -count=4

goos: linux
goarch: amd64
pkg: github.com/gofiber/fiber/v2
cpu: Intel(R) Core(TM) i7-10750H CPU @ 2.60GHz
Benchmark_Ctx_CustomJSON_No_Ctype-12             4810928               264.0 ns/op          56 B/op           2 allocs/op
Benchmark_Ctx_CustomJSON_No_Ctype-12             4475920               243.4 ns/op          56 B/op           2 allocs/op
Benchmark_Ctx_CustomJSON_No_Ctype-12             4942119               251.4 ns/op          56 B/op           2 allocs/op
Benchmark_Ctx_CustomJSON_No_Ctype-12             4984884               250.4 ns/op          56 B/op           2 allocs/op
PASS
ok      github.com/gofiber/fiber/v2     5.877s

@ReneWerner87
Copy link
Member

Benchmarks without variadic ctype parameter:

go test -run=^$ -bench=Benchmark_Ctx_CustomJSON_Ctype -benchmem -count=4

goos: linux
goarch: amd64
pkg: github.com/gofiber/fiber/v2
cpu: Intel(R) Core(TM) i7-10750H CPU @ 2.60GHz
Benchmark_Ctx_CustomJSON_Ctype-12        4666432               276.2 ns/op            56 B/op          2 allocs/op
Benchmark_Ctx_CustomJSON_Ctype-12        4483280               262.5 ns/op            56 B/op          2 allocs/op
Benchmark_Ctx_CustomJSON_Ctype-12        4582852               258.5 ns/op            56 B/op          2 allocs/op
Benchmark_Ctx_CustomJSON_Ctype-12        4697461               263.6 ns/op            56 B/op          2 allocs/op
PASS
ok      github.com/gofiber/fiber/v2     5.957s

Benchmarks with variadic ctype parameter:

go test -run=^$ -bench=Benchmark_Ctx_CustomJSON_Ctype -benchmem -count=4

goos: linux
goarch: amd64
pkg: github.com/gofiber/fiber/v2
cpu: Intel(R) Core(TM) i7-10750H CPU @ 2.60GHz
Benchmark_Ctx_CustomJSON_Ctype-12        4613229               269.6 ns/op            56 B/op         2 allocs/op
Benchmark_Ctx_CustomJSON_Ctype-12        4484907               256.6 ns/op            56 B/op         2 allocs/op
Benchmark_Ctx_CustomJSON_Ctype-12        4681248               244.3 ns/op            56 B/op         2 allocs/op
Benchmark_Ctx_CustomJSON_Ctype-12        4635668               229.8 ns/op            56 B/op         2 allocs/op
PASS
ok      github.com/gofiber/fiber/v2     5.673s

go test -run=^$ -bench=Benchmark_Ctx_CustomJSON_No_Ctype -benchmem -count=4

goos: linux
goarch: amd64
pkg: github.com/gofiber/fiber/v2
cpu: Intel(R) Core(TM) i7-10750H CPU @ 2.60GHz
Benchmark_Ctx_CustomJSON_No_Ctype-12             4810928               264.0 ns/op          56 B/op           2 allocs/op
Benchmark_Ctx_CustomJSON_No_Ctype-12             4475920               243.4 ns/op          56 B/op           2 allocs/op
Benchmark_Ctx_CustomJSON_No_Ctype-12             4942119               251.4 ns/op          56 B/op           2 allocs/op
Benchmark_Ctx_CustomJSON_No_Ctype-12             4984884               250.4 ns/op          56 B/op           2 allocs/op
PASS
ok      github.com/gofiber/fiber/v2     5.877s

ok then add this feature to the JSON function

Copy link
Member

@efectn efectn left a comment

Choose a reason for hiding this comment

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

Lgtm

@ReneWerner87
Copy link
Member

@rhburt pls solve the last part
image

@rhburt
Copy link
Contributor Author

rhburt commented Nov 13, 2023

Apologies for the delay @ReneWerner87, it should pass the linting checks now.

@ReneWerner87 ReneWerner87 merged commit 9f082af into gofiber:master Nov 13, 2023
20 checks passed
Copy link

welcome bot commented Nov 13, 2023

Congrats on merging your first pull request! 馃帀 We here at Fiber are proud of you! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

ReneWerner87 added a commit that referenced this pull request Nov 15, 2023
馃敟 Add support for application/problem+json #2704
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

馃殌 [Feature]: Support for application/problem+json
4 participants