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

Remove BodySerializer #2362

Merged
merged 1 commit into from
Dec 10, 2024
Merged

Remove BodySerializer #2362

merged 1 commit into from
Dec 10, 2024

Conversation

adamw
Copy link
Member

@adamw adamw commented Dec 9, 2024

Instead, bodies need to be serialized explicitly. E.g. instead of

import sttp.client4.jsoniter._

basicRequest.body(someInstance)

you now need to call the serialization method explicitly:

import sttp.client4.jsoniter._

basicRequest.body(asJson(someInstance))

@adamw adamw merged commit 3ce24c1 into master Dec 10, 2024
21 checks passed
@adamw adamw deleted the remove-body-serializer branch December 10, 2024 07:58
@Ravenow
Copy link

Ravenow commented Dec 21, 2024

With this change all code with body serialization should be touched. It would be interesting to know, whats the reason for removing BodySerializers? I mean - one import on top vs explicit functions calls everywhere

@adamw
Copy link
Member Author

adamw commented Dec 21, 2024

Yes, that's true. The reason is that implicit BodySerializers are ... well, implicit ;). They're non-obvious when you look at the code - to which form the code is serialized (if it's json or xml etc.). It's also hard to "discover", using auto-import for example.

So yes, the new approach is more explicit, requires more code, but is also predictable (when working with JSON, you always need asJson, both for request & response) and discoverable

@Ravenow
Copy link

Ravenow commented Dec 22, 2024

Is it possible to keep old way with implicits? In our code base we supply 'case classes' with circe encoder, and thats enough. Without importing of required serializer code will be 'red'.

Its not obvious why sttp force users to use single way as a general purpose library. Someone like explicit approach, someone not

just my 2 cents :)

@adamw
Copy link
Member Author

adamw commented Dec 23, 2024

Yes, some code will be read upon migration, I'm aware of that - but that's why this change happens across major versions (v3 -> v4).

I'd like to keep a single way to do things, if possible. You can always create your own sttp stack (by extending the SttpApi, SttpCirceApi traits), and adding an implicit conversion from T: Encoder to StringBody using asJson

Then you could have a single-sttp import with all the features that you use: import my.package.mysttp.*

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants