Repository navigation
Conversation
…with a request body Retrofit rejects @Body, @FormUrlEncoded and @multipart on @delete, so a DELETE operation with a requestBody failed on its first call. Emit @http(method, path, hasBody = true) only when the method does not allow a body and the operation has body or form params. Other operations keep the short annotation, so existing output does not change. Fixes OpenAPITools#25454
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #25454
With
library = jvm-retrofit2, a DELETE operation with arequestBodywas generated as@DELETE("path")plus an@Bodyparameter. Retrofit rejects that on the first call with "Non-body HTTP method cannot contain @Body". A DELETE with a form body fails the same way: "FormUrlEncoded can only be specified on HTTP methods with request body".The template now emits
@HTTP(method = "DELETE", path = "...", hasBody = true)only when the HTTP method does not allow a body (isBodyAllowedis false) and the operation has body or form params (hasBodyOrFormParams). Every other operation keeps the short annotation. The line is shared by the Call, coroutines and Rx variants.I kept the scope narrow on purpose. #19056 changed every Java retrofit2 operation to
@HTTPand was reverted in #19100 because it broke multipart and PUT. With this change, POST, PUT, PATCH and body-less operations generate the same output as before.Changed files:
modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-retrofit2/api.mustachemodules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/KotlinClientCodegenApiTest.java(new test)modules/openapi-generator/src/test/resources/3_0/kotlin/issue25454-delete-with-body.yaml(the issue spec plus a form-body DELETE, a POST with a body and a DELETE without a body)Testing:
@HTTP(method = "DELETE", path = "items", hasBody = true)not found) and passes with the change.KotlinClientCodegenApiTestandKotlinClientCodegenModelTest: 101 tests, 0 failures.jvm-retrofit2. No sample file changed, so this PR has no sample updates.serializationLibrary=kotlinx_serializationanduseCoroutines=true, compiled it with Retrofit 3.0.0, and called each method against OkHttp MockWebServer. On master,deleteItemsanddeleteItemsFormthrowIllegalArgumentException. With the change, the server receivesDELETE /itemswith body{"ids":["a","b"]}andDELETE /items/formwith bodyid=x. The POST and the body-less DELETE behave as before.The Java retrofit2 template has the same problem. I left it out of this PR.
PR checklist
Commit all changed files.
This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
These must match the expectations made by your contribution.
You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example
./bin/generate-samples.sh bin/configs/java*.IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
cc @karismann @Zomzog @andrewemery @4brunu @yutaka0m @stefankoppier @e5l
Summary by cubic
Fixes DELETE operations with a request body in the Kotlin
jvm-retrofit2client, which previously generated@DELETEannotations that Retrofit rejects at runtime with "Non-body HTTP method cannot contain @Body" (or the equivalent error for form bodies).The template now emits
@HTTP(method = "DELETE", path = "...", hasBody = true)only when the HTTP method does not allow a body and the operation has body or form parameters. All other operations keep their short annotation, so POST, PUT, PATCH, and body-less operations generate the same output as before.Written for commit a3d4c8c. Summary will update on new commits.