-
Notifications
You must be signed in to change notification settings - Fork 38.4k
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
Add a requiredBody()
extension to RestClient.ResponseSpec
#32703
Add a requiredBody()
extension to RestClient.ResponseSpec
#32703
Conversation
Kotlin extensions try to stay as close as possible to the Java API, so I think I would not be in favor of a Kotlin specific API like the one proposed here. @poutsma Could you please let me know what you think of the idea of introducing a non-nullable return value variant of |
I am not sure such a method is necessary in Java, where the use of Instead, from a Java perspective, I think having a method that returns an Unless I am mistaken, such a optional-returning-method could then be used to implement the Kotlin extension described above, since I believe use of How does that sound, @sdeleuze? |
I think the use case here is "I know this endpoint is expected to return a body 99.99% of the time and I don't want any "noise" in my code like So I am leaning toward declining this PR and just recommend to use |
Thank you for your review, @sdeleuze @poutsma. As you mentioned, using Optional as a return type does not seem to be a suitable way to solve our current issue. Indeed, when calling the body method, it is typically presumed that a response body exists. Given this, I believe a method like bodyNotNull should be provided to handle responses without requiring additional From a practical perspective, many teams in the existing MVC ecosystem have been using For these reasons, please reconsider adding an interface like |
@Donghh0221 I propose that we use |
requiredBody()
extension to RestClient.ResponseSpec
da1e665
to
de9a9ac
Compare
de9a9ac
to
36a6e8d
Compare
@sdeleuze Of course! I've added a commit to respond to your review. |
Polished and merged, thanks for your contribution. |
Description
In WebClientExtensions.kt, developers have the flexibility to handle responses with methods that either guarantee a non-null return type or can return null. This design caters to various nullability requirements, enhancing code safety and expressiveness. However, RestClientExtensionKt currently offers only a nullable return type method, which may lead to unnecessary null checks and potentially less idiomatic Kotlin code.
Given Kotlin's emphasis on type safety and explicit nullability, extending RestClientExtensionKt to include a non-nullable return type method would align with these principles, offering a more robust and intuitive toolset for REST client development.
Proposal
I propose the addition of a bodyNonNull method to RestClientExtensionKt:
This method complements the existing nullable variant, providing a straightforward way to enforce non-null responses, thereby aligning with Kotlin's null safety features and reducing boilerplate code.
Impact and Use Cases
This enhancement introduces no breaking changes, enriching the API without affecting existing code. It particularly benefits scenarios where a non-null response is confidently expected, making error handling more explicit and idiomatic in Kotlin.
Seeking Feedback
I welcome feedback on this proposal, including alternative approaches, potential impacts on usability, and any concerns regarding implementation details.
Conclusion
By adopting this change, RestClientExtensionKt would not only align more closely with Kotlin's design principles but also offer developers a more expressive and safe way to handle REST responses. This proposal upholds the Kotlin ethos of safety and clarity, enhancing developer experience and code quality in REST client applications.