Skip to content
This repository was archived by the owner on Dec 25, 2024. It is now read-only.

Commit 09d3b8f

Browse files
authored
fix optional body parameter in r (#13483)
1 parent 903f2e2 commit 09d3b8f

File tree

21 files changed

+841
-22
lines changed

21 files changed

+841
-22
lines changed

modules/openapi-generator/src/main/resources/r/api.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@
522522
{{/hasFormParams}}
523523
{{#hasBodyParam}}
524524
{{#bodyParams}}
525-
if (!missing(`{{paramName}}`)) {
525+
if (!is.null(`{{paramName}}`)) {
526526
{{#isArray}}
527527
body.items <- paste(unlist(lapply(`{{paramName}}`, function(param) {
528528
param$toJSONString()

modules/openapi-generator/src/test/resources/3_0/r/petstore.yaml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,29 @@ paths:
725725
responses:
726726
'200':
727727
description: successful operation
728+
/fake/test_optional_body_parameter:
729+
post:
730+
tags:
731+
- fake
732+
summary: Add a new pet to the store (optional body)
733+
description: ''
734+
operationId: addPetOptional
735+
responses:
736+
'200':
737+
description: successful operation
738+
content:
739+
application/xml:
740+
schema:
741+
$ref: '#/components/schemas/Pet'
742+
application/json:
743+
schema:
744+
$ref: '#/components/schemas/Pet'
745+
'405':
746+
description: Invalid input
747+
security:
748+
- http_auth: []
749+
requestBody:
750+
$ref: '#/components/requestBodies/OptionalPet'
728751
externalDocs:
729752
description: Find out more about Swagger
730753
url: 'http://swagger.io'
@@ -767,6 +790,34 @@ components:
767790
type: string
768791
description: Pet object that needs to be added to the store
769792
required: true
793+
OptionalPet:
794+
content:
795+
application/json:
796+
schema:
797+
$ref: '#/components/schemas/Pet'
798+
application/xml:
799+
schema:
800+
$ref: '#/components/schemas/Pet'
801+
multipart/related: # message with binary body part
802+
schema:
803+
type: object
804+
properties: # Request parts
805+
jsonData:
806+
$ref: '#/components/schemas/Pet'
807+
binaryDataN2Information:
808+
type: string
809+
format: binary
810+
encoding:
811+
jsonData:
812+
contentType: application/json
813+
binaryDataN2Information:
814+
contentType: application/vnd.3gpp.ngap
815+
headers:
816+
Content-Id:
817+
schema:
818+
type: string
819+
description: Pet object that needs to be added to the store
820+
required: false
770821
securitySchemes:
771822
http_auth:
772823
type : http

samples/client/petstore/R-httr2-wrapper/R/fake_api.R

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,31 @@
1313
#'
1414
#' @section Methods:
1515
#' \describe{
16+
#' \strong{ add_pet_optional } \emph{ Add a new pet to the store (optional body) }
17+
#'
18+
#'
19+
#' \itemize{
20+
#' \item \emph{ @param } pet \link{Pet}
21+
#' \item \emph{ @returnType } \link{Pet} \cr
22+
#'
23+
#' \item On encountering errors, an error of subclass ApiException will be thrown.
24+
#'
25+
#' \item status code : 200 | successful operation
26+
#'
27+
#' \item return type : Pet
28+
#' \item response headers :
29+
#'
30+
#' \tabular{ll}{
31+
#' }
32+
#' \item status code : 405 | Invalid input
33+
#'
34+
#'
35+
#' \item response headers :
36+
#'
37+
#' \tabular{ll}{
38+
#' }
39+
#' }
40+
#'
1641
#' \strong{ fake_data_file } \emph{ test data_file to ensure it&#39;s escaped correctly }
1742
#'
1843
#'
@@ -89,6 +114,43 @@
89114
#'
90115
#' @examples
91116
#' \dontrun{
117+
#' #################### add_pet_optional ####################
118+
#'
119+
#' library(petstore)
120+
#' var_pet <- Pet$new() # Pet | Pet object that needs to be added to the store
121+
#'
122+
#' #Add a new pet to the store (optional body)
123+
#' api_instance <- petstore_api$new()
124+
#'
125+
#' # Configure HTTP basic authorization: http_auth
126+
#' api_instance$api_client$username <- Sys.getenv("USERNAME")
127+
#' api_instance$api_client$password <- Sys.getenv("PASSWORD")
128+
#'
129+
#' result <- tryCatch(
130+
#'
131+
#' # to save the result into a file, simply add the optional `data_file` parameter, e.g.
132+
#' # api_instance$fake_api$add_pet_optional(pet = var_pet, data_file = "result.txt"),
133+
#'
134+
#'
135+
#' api_instance$fake_api$add_pet_optional(pet = var_pet),
136+
#' ApiException = function(ex) ex
137+
#' )
138+
#' # In case of error, print the error object
139+
#' if (!is.null(result$ApiException)) {
140+
#' print("Exception occurs when calling `add_pet_optional`:")
141+
#' dput(result$ApiException$toString())
142+
#'
143+
#' # error object
144+
#' dput(result$ApiException$error_object$toJSONString())
145+
#'
146+
#' } else {
147+
#' # deserialized response object
148+
#' print("The response is ...")
149+
#' dput(result$toString())
150+
#' }
151+
#'
152+
#'
153+
#'
92154
#' #################### fake_data_file ####################
93155
#'
94156
#' library(petstore)
@@ -222,6 +284,122 @@ FakeApi <- R6::R6Class(
222284
self$api_client <- ApiClient$new()
223285
}
224286
},
287+
#' Add a new pet to the store (optional body)
288+
#'
289+
#' @description
290+
#' Add a new pet to the store (optional body)
291+
#'
292+
#' @param pet (optional) Pet object that needs to be added to the store
293+
#' @param data_file (optional) name of the data file to save the result
294+
#' @param ... Other optional arguments
295+
#' @return Pet
296+
#' @export
297+
add_pet_optional = function(pet = NULL, data_file = NULL, ...) {
298+
local_var_response <- self$add_pet_optional_with_http_info(pet, data_file = data_file, ...)
299+
if (local_var_response$status_code >= 200 && local_var_response$status_code <= 299) {
300+
local_var_response$content
301+
} else if (local_var_response$status_code >= 300 && local_var_response$status_code <= 399) {
302+
local_var_response
303+
} else if (local_var_response$status_code >= 400 && local_var_response$status_code <= 499) {
304+
local_var_response
305+
} else if (local_var_response$status_code >= 500 && local_var_response$status_code <= 599) {
306+
local_var_response
307+
}
308+
},
309+
#' Add a new pet to the store (optional body)
310+
#'
311+
#' @description
312+
#' Add a new pet to the store (optional body)
313+
#'
314+
#' @param pet (optional) Pet object that needs to be added to the store
315+
#' @param data_file (optional) name of the data file to save the result
316+
#' @param ... Other optional arguments
317+
#' @return API response (Pet) with additional information such as HTTP status code, headers
318+
#' @export
319+
add_pet_optional_with_http_info = function(pet = NULL, data_file = NULL, ...) {
320+
args <- list(...)
321+
query_params <- list()
322+
header_params <- c()
323+
form_params <- list()
324+
file_params <- list()
325+
local_var_body <- NULL
326+
oauth_scopes <- NULL
327+
is_oauth <- FALSE
328+
329+
330+
if (!is.null(`pet`)) {
331+
local_var_body <- `pet`$toJSONString()
332+
} else {
333+
body <- NULL
334+
}
335+
336+
local_var_url_path <- "/fake/test_optional_body_parameter"
337+
# HTTP basic auth
338+
if (!is.null(self$api_client$username) || !is.null(self$api_client$password)) {
339+
header_params["Authorization"] <- paste("Basic", base64enc::base64encode(charToRaw(paste(self$api_client$username, self$api_client$password, sep = ":"))))
340+
}
341+
342+
# The Accept request HTTP header
343+
local_var_accepts <- list("application/xml", "application/json")
344+
345+
# The Content-Type representation header
346+
local_var_content_types <- list("application/json", "application/xml", "multipart/related")
347+
348+
local_var_resp <- self$api_client$CallApi(url = paste0(self$api_client$base_path, local_var_url_path),
349+
method = "POST",
350+
query_params = query_params,
351+
header_params = header_params,
352+
form_params = form_params,
353+
file_params = file_params,
354+
accepts = local_var_accepts,
355+
content_types = local_var_content_types,
356+
body = local_var_body,
357+
is_oauth = is_oauth,
358+
oauth_scopes = oauth_scopes,
359+
...)
360+
361+
if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) {
362+
# save response in a file
363+
if (!is.null(data_file)) {
364+
write(local_var_resp$response, data_file)
365+
}
366+
367+
deserialized_resp_obj <- tryCatch(
368+
self$api_client$deserialize(local_var_resp$response, "Pet", loadNamespace("petstore")),
369+
error = function(e) {
370+
rlang::abort(message = "Failed to deserialize response",
371+
.subclass = "ApiException",
372+
ApiException = ApiException$new(http_response = local_var_resp))
373+
}
374+
)
375+
local_var_resp$content <- deserialized_resp_obj
376+
local_var_resp
377+
} else if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) {
378+
local_var_error_msg <- local_var_resp$response
379+
if (local_var_error_msg == "") {
380+
local_var_error_msg <- paste("Server returned ", local_var_resp$status_code, " response status code.")
381+
}
382+
rlang::abort(message = local_var_error_msg,
383+
.subclass = "ApiException",
384+
ApiException = ApiException$new(http_response = local_var_resp))
385+
} else if (local_var_resp$status_code >= 400 && local_var_resp$status_code <= 499) {
386+
local_var_error_msg <- local_var_resp$response
387+
if (local_var_error_msg == "") {
388+
local_var_error_msg <- "Api client exception encountered."
389+
}
390+
rlang::abort(message = local_var_error_msg,
391+
.subclass = "ApiException",
392+
ApiException = ApiException$new(http_response = local_var_resp))
393+
} else if (local_var_resp$status_code >= 500 && local_var_resp$status_code <= 599) {
394+
local_var_error_msg <- local_var_resp$response
395+
if (local_var_error_msg == "") {
396+
local_var_error_msg <- "Api server exception encountered."
397+
}
398+
rlang::abort(message = error_msg,
399+
.subclass = "ApiException",
400+
ApiException = ApiException$new(http_response = local_var_resp))
401+
}
402+
},
225403
#' test data_file to ensure it's escaped correctly
226404
#'
227405
#' @description

samples/client/petstore/R-httr2-wrapper/R/pet_api.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,7 @@ PetApi <- R6::R6Class(
708708
}
709709

710710

711-
if (!missing(`pet`)) {
711+
if (!is.null(`pet`)) {
712712
local_var_body <- `pet`$toJSONString()
713713
} else {
714714
body <- NULL
@@ -1568,7 +1568,7 @@ PetApi <- R6::R6Class(
15681568
}
15691569

15701570

1571-
if (!missing(`pet`)) {
1571+
if (!is.null(`pet`)) {
15721572
local_var_body <- `pet`$toJSONString()
15731573
} else {
15741574
body <- NULL

samples/client/petstore/R-httr2-wrapper/R/store_api.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ StoreApi <- R6::R6Class(
652652
}
653653

654654

655-
if (!missing(`order`)) {
655+
if (!is.null(`order`)) {
656656
local_var_body <- `order`$toJSONString()
657657
} else {
658658
body <- NULL

samples/client/petstore/R-httr2-wrapper/R/user_api.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ UserApi <- R6::R6Class(
500500
}
501501

502502

503-
if (!missing(`user`)) {
503+
if (!is.null(`user`)) {
504504
local_var_body <- `user`$toJSONString()
505505
} else {
506506
body <- NULL
@@ -608,7 +608,7 @@ UserApi <- R6::R6Class(
608608
}
609609

610610

611-
if (!missing(`user`)) {
611+
if (!is.null(`user`)) {
612612
body.items <- paste(unlist(lapply(`user`, function(param) {
613613
param$toJSONString()
614614
})), collapse = ",")
@@ -719,7 +719,7 @@ UserApi <- R6::R6Class(
719719
}
720720

721721

722-
if (!missing(`user`)) {
722+
if (!is.null(`user`)) {
723723
body.items <- paste(unlist(lapply(`user`, function(param) {
724724
param$toJSONString()
725725
})), collapse = ",")
@@ -1288,7 +1288,7 @@ UserApi <- R6::R6Class(
12881288

12891289

12901290

1291-
if (!missing(`user`)) {
1291+
if (!is.null(`user`)) {
12921292
local_var_body <- `user`$toJSONString()
12931293
} else {
12941294
body <- NULL

samples/client/petstore/R-httr2-wrapper/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ All URIs are relative to *http://petstore.swagger.io/v2*
6161

6262
Class | Method | HTTP request | Description
6363
------------ | ------------- | ------------- | -------------
64+
*FakeApi* | [**add_pet_optional**](docs/FakeApi.md#add_pet_optional) | **POST** /fake/test_optional_body_parameter | Add a new pet to the store (optional body)
6465
*FakeApi* | [**fake_data_file**](docs/FakeApi.md#fake_data_file) | **GET** /fake/data_file | test data_file to ensure it's escaped correctly
6566
*FakeApi* | [**fake_path_array**](docs/FakeApi.md#fake_path_array) | **GET** /fake/path_array/{path_array}/testing | test array parameter in path
6667
*FakeApi* | [**fake_regular_expression**](docs/FakeApi.md#fake_regular_expression) | **GET** /fake/regular_expression | test regular expression to ensure no exception

0 commit comments

Comments
 (0)