[R] Bug - Invalid code generated for POST with no request object#8067
[R] Bug - Invalid code generated for POST with no request object#8067wing328 merged 5 commits intoOpenAPITools:masterfrom
Conversation
|
👍 Thanks for opening this issue! The team will review the labels and make any necessary changes. |
|
Me attempting to copy the R maintainers (apologies if I misunderstood the request) @Ramanth and @saigiridhar21 . |
|
Pushed 2ff382a to update the samples so that the CI can verify the change. |
Can you please try the latest master or 5.0.0-beta3 to see if it's still an issue? |
|
Hi, I've confirmed with openapi-generator-cli-5.0.0-beta3.jar I still get the same error. The generated code for the relevant functions are: As you can see it has the same issue of setting body = body, without first assigning a local variable to body (resulting in body being passed in as the built-in function body). I get this error when running the code: |
|
Looks like the change breaks one of the tests: This line failed the tests: https://github.com/OpenAPITools/openapi-generator/blob/master/samples/client/petstore/R/tests/testthat/test_petstore.R#L13 |
|
Hi, thanks for the response. As I mentioned at the outset, I didn’t have the build system set up - I initially intended this to be just a bug report, but figured I might as well try provide a code pointer along with it ;) I’m not super familiar with R, but I can try to setup the build system properly, and see if I can fix whatever is going on. |
|
I think I've got the build system working such that I can run those R tests now. I'll report back when I've got something useful to add. |
|
I think that error was there before my change: I'm running: when I'm at: "634c4c09e46271c5baba28439f12424b562a5e34"
|
|
The CircleCI runs a local test server instead of using the public one (which fails pretty much all the test). I'll see what I can do later today to help you pinpoint the root cause of the issue. |
|
I think I found the issue with my code: This is the generated code.. My naive defaulting is nuking the case where body is a provided param. Now I've got it building locally, I'll see if I can make a valid fix. |
|
Okay, I believe that's it. I updated the samples. I also tried running the integ tests again, and now I get the same result as what I did in mainline. I updated it to only default body to NULL when the model had no "hasBodyParam". I generated code locally with my sample file, and confirmed the change would fix my issue. |
Example generated using my latest change, showing it would address my issue. |
|
I reviewed the sample generated code and noticed one other case - where hasBodyParam was false, but hasFormParams was true, would result in overriding body. Fixed that now. I don't see any other issues having reviewed the generated code (looks like body is only set to NULL when it's safe to do so now - which should fix my case, where hasBodyParam and hasFormParams are false for a POST). |
PR is just a suggestion of how I think you can fix it. I'm not setup to actually run the build system at the moment. i.e. I'm not claiming this is ready to merge as is, it's just what I expect will address the issue based on my observation. I.e. You can think of this more as a bug report with attached code pointer if you like.
Description
Our model has a POST that has no request modeled. i.e. no body. The code generated normally for a PUT with a request generates something akin to:
In the case where no request object is modeled, this initial code block (where body is set) isn't generated, so you just end up with something like:
This in turn means self$apiClient$CallApi is called with body set as the vale of the built in function base::body (as body isn't a local variable).
This results in a strange error indicating that body was unexpected:
where TokenExchangeStsPost is a POST with no request body modeled.
Swagger-codegen version
Build package: org.openapitools.codegen.languages.RClientCodegen
Swagger declaration file content or url
Note: I removed some of our model from what does the repro (to not expose certain info). The important bit is that the token_exchange_sts is a post, and has no request defined.
Command line used for generation
Steps to reproduce
You can see this code is invalid:
is "broken".
You can validate it by running the client like so:
api.instance$TokenExchangeStsPost() will break (Unknown type of
body: must be NULL, FALSE, character, raw or list").Suggest a fix/enhancement
I think you should either set
above in the case where no request is defined. I believe that will fix it.
Setting it here:
openapi-generator/modules/openapi-generator/src/main/resources/r/api.mustache
Line 180 in 634c4c0
would effectively default it to NULL, and then the rest of the code generation could override it.
Alternatively if you weren't using a variable name that maps to the base::body, then that might avoid it as well.
I made a local change to api_client.R
but that's clearly a hack.. it works because the body is assigned to the built in function in this special case, is otherwise not a function. I only made the change in this file, as it doesn't change as we update the model, unlike default_api.R, so I changed api_client.R and added it to the ignore file for the generator (so the fix sticks).
FYI : the same template works fine in Python without any changes/hacks.