[R] support explode (true/false) in query array parameters (#13364)

* fix query parameters (explode or not) in r client

* add tests
This commit is contained in:
William Cheng 2022-09-07 12:21:13 +08:00 committed by GitHub
parent 4c19eca929
commit f98753aa8b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 114 additions and 54 deletions

View File

@ -382,12 +382,19 @@
{{/headerParams}}
{{#queryParams}}
{{#isArray}}
{{#isExplode}}
# explore
for (query_item in `{{{paramName}}}`) {
query_params[["{{{baseName}}}"]] <- c(query_params[["{{{baseName}}}"]], list(`{{{baseName}}}` = query_item))
}
{{/isExplode}}
{{^isExplode}}
# no explore
query_params[["{{{baseName}}}"]] <- I(paste(lapply(`{{{paramName}}}`, URLencode, reserved = TRUE), collapse = ","))
{{/isExplode}}
{{/isArray}}
{{^isArray}}
query_params["{{baseName}}"] <- `{{paramName}}`
query_params[["{{baseName}}"]] <- `{{paramName}}`
{{/isArray}}
{{/queryParams}}

View File

@ -234,8 +234,15 @@ ApiClient <- R6::R6Class(
}
## add query parameters
for (query_param in query_params) {
req <- req %>% req_url_query(!!!query_param)
for (query_param in names(query_params)) {
if (typeof(query_params[[query_param]]) == "list") {
# for explode, e.g. a=1,a=2,a=3
req <- req %>% req_url_query(!!!query_params[[query_param]])
} else { # for non-explode, e.g. a=1,2,3
tmp <- list()
tmp[[query_param]] <- query_params[[query_param]]
req <- req %>% req_url_query(!!!tmp)
}
}
# has file upload?

View File

@ -82,7 +82,7 @@ paths:
description: Status values that need to be considered for filter
required: true
style: form
explode: false
explode: true # change to true for testing purpose
deprecated: true
schema:
type: array
@ -148,9 +148,10 @@ paths:
$ref: '#/components/schemas/Pet'
'400':
description: Invalid tag value
security:
- petstore_auth:
- 'read:pets'
# comment out for testing purpose
#security:
# - petstore_auth:
# - 'read:pets'
deprecated: true
'/pet/{petId}':
get:

View File

@ -226,8 +226,15 @@ ApiClient <- R6::R6Class(
}
## add query parameters
for (query_param in query_params) {
req <- req %>% req_url_query(!!!query_param)
for (query_param in names(query_params)) {
if (typeof(query_params[[query_param]]) == "list") {
# for explode, e.g. a=1,a=2,a=3
req <- req %>% req_url_query(!!!query_params[[query_param]])
} else { # for non-explode, e.g. a=1,2,3
tmp <- list()
tmp[[query_param]] <- query_params[[query_param]]
req <- req %>% req_url_query(!!!tmp)
}
}
# has file upload?

View File

@ -395,9 +395,6 @@
#' #Finds Pets by tags
#' api_instance <- petstore_api$new()
#'
#' # Configure OAuth2 access token for authorization: petstore_auth
#' api_instance$api_client$access_token <- Sys.getenv("ACCESS_TOKEN")
#'
#' result <- tryCatch(
#'
#' # to save the result into a file, simply add the optional `data_file` parameter, e.g.
@ -944,6 +941,7 @@ PetApi <- R6::R6Class(
}
# explore
for (query_item in `status`) {
query_params[["status"]] <- c(query_params[["status"]], list(`status` = query_item))
}
@ -1064,14 +1062,10 @@ PetApi <- R6::R6Class(
}
for (query_item in `tags`) {
query_params[["tags"]] <- c(query_params[["tags"]], list(`tags` = query_item))
}
# no explore
query_params[["tags"]] <- I(paste(lapply(`tags`, URLencode, reserved = TRUE), collapse = ","))
local_var_url_path <- "/pet/findByTags"
# OAuth-related settings
is_oauth <- TRUE
oauth_scopes <- "read:pets"
# The Accept request HTTP header
local_var_accepts <- list("application/xml", "application/json")

View File

@ -1071,9 +1071,9 @@ UserApi <- R6::R6Class(
}
query_params["username"] <- `username`
query_params[["username"]] <- `username`
query_params["password"] <- `password`
query_params[["password"]] <- `password`
local_var_url_path <- "/user/login"

View File

@ -213,8 +213,6 @@ var_tags <- list("inner_example") # array[character] | Tags to filter by
# Finds Pets by tags
api_instance <- petstore_api$new()
# Configure OAuth2 access token for authorization: petstore_auth
api_instance$api_client$access_token <- Sys.getenv("ACCESS_TOKEN")
result <- tryCatch(
# to save the result into a file, simply add the optional `data_file` parameter, e.g.
# api_instance$pet_api$find_pets_by_tags(var_tags, data_file = "result.txt"),
@ -247,7 +245,7 @@ Name | Type | Description | Notes
### Authorization
[petstore_auth](../README.md#petstore_auth)
No authorization required
### HTTP request headers

View File

@ -3,7 +3,33 @@ install.packages("petstore_1.0.0.tar.gz",repos=NULL, type="source")
library(petstore)
library(jsonlite)
var_tags <- list("innerzzzzzzz", "second_example", "345") # array[character] | Tags to filter by
var_status <- list("something inside", "explode please", "123") # array[character] | Status values that need to be considered for filter
# Finds Pets by status
api_instance <- petstore_api$new()
# Configure OAuth2 access token for authorization: petstore_auth
#api_instance$api_client$access_token <- Sys.getenv("ACCESS_TOKEN")
result <- tryCatch(
# to save the result into a file, simply add the optional `data_file` parameter, e.g.
# api_instance$pet_api$find_pets_by_status(var_status, data_file = "result.txt"),
api_instance$pet_api$find_pets_by_status(var_status),
ApiException = function(ex) ex
)
# In case of error, print the error object
if (!is.null(result$ApiException)) {
print("Exception occurs when calling `find_pets_by_status`:")
dput(result$ApiException$toString())
# error object
dput(result$ApiException$error_object$toJSONString())
} else {
# deserialized response object
print("The response is ...")
dput(result)
}
var_tags <- c("innerzzzzzzz", "second,example", "345") # array[character] | Tags to filter by
# Finds Pets by tags
api_instance <- petstore_api$new()

View File

@ -234,6 +234,35 @@ test_that("GetPetById with data_file", {
expect_equal(response$name, "name_test")
})
test_that("find_pets_by_status", {
pet_tag_test <- Pet$new("name_test",
photoUrls = list("photo_test", "second test"),
category = Category$new(id = 4455, name = "test_cat"),
id = 4455,
tags = list(
Tag$new(id = 4455, name = "tag_test"), Tag$new(id = 488, name = "unknown 2")
),
status = "available"
)
result <- pet_api$add_pet(pet_tag_test)
# vector as input
var_tags <- c("unknown", "unknown 2") # array[character] | Tags to filter by
result <- pet_api$find_pets_by_tags(var_tags)
expect_true(!is.null(result))
expect_equal(result[[1]]$id, 123321)
expect_equal(result[[2]]$id, 123999)
expect_equal(result[[3]]$id, 4455)
# list as input
var_tags <- list("unknown", "unknown 2") # array[character] | Tags to filter by
result <- pet_api$find_pets_by_tags(var_tags)
expect_true(!is.null(result))
expect_equal(result[[1]]$id, 123321)
expect_equal(result[[2]]$id, 123999)
expect_equal(result[[3]]$id, 4455)
})
test_that("Tests allOf", {
# test allOf without discriminator
a1 <- AllofTagApiResponse$new(id = 450, name = "test_cat", code = 200, type = "test_type", message = "test_message")

View File

@ -226,8 +226,15 @@ ApiClient <- R6::R6Class(
}
## add query parameters
for (query_param in query_params) {
req <- req %>% req_url_query(!!!query_param)
for (query_param in names(query_params)) {
if (typeof(query_params[[query_param]]) == "list") {
# for explode, e.g. a=1,a=2,a=3
req <- req %>% req_url_query(!!!query_params[[query_param]])
} else { # for non-explode, e.g. a=1,2,3
tmp <- list()
tmp[[query_param]] <- query_params[[query_param]]
req <- req %>% req_url_query(!!!tmp)
}
}
# has file upload?

View File

@ -395,9 +395,6 @@
#' #Finds Pets by tags
#' api_instance <- PetApi$new()
#'
#' # Configure OAuth2 access token for authorization: petstore_auth
#' api_instance$api_client$access_token <- Sys.getenv("ACCESS_TOKEN")
#'
#' result <- tryCatch(
#'
#' # to save the result into a file, simply add the optional `data_file` parameter, e.g.
@ -944,6 +941,7 @@ PetApi <- R6::R6Class(
}
# explore
for (query_item in `status`) {
query_params[["status"]] <- c(query_params[["status"]], list(`status` = query_item))
}
@ -1064,14 +1062,10 @@ PetApi <- R6::R6Class(
}
for (query_item in `tags`) {
query_params[["tags"]] <- c(query_params[["tags"]], list(`tags` = query_item))
}
# no explore
query_params[["tags"]] <- I(paste(lapply(`tags`, URLencode, reserved = TRUE), collapse = ","))
local_var_url_path <- "/pet/findByTags"
# OAuth-related settings
is_oauth <- TRUE
oauth_scopes <- "read:pets"
# The Accept request HTTP header
local_var_accepts <- list("application/xml", "application/json")

View File

@ -1071,9 +1071,9 @@ UserApi <- R6::R6Class(
}
query_params["username"] <- `username`
query_params[["username"]] <- `username`
query_params["password"] <- `password`
query_params[["password"]] <- `password`
local_var_url_path <- "/user/login"

View File

@ -213,8 +213,6 @@ var_tags <- list("inner_example") # array[character] | Tags to filter by
# Finds Pets by tags
api_instance <- PetApi$new()
# Configure OAuth2 access token for authorization: petstore_auth
api_instance$api_client$access_token <- Sys.getenv("ACCESS_TOKEN")
result <- tryCatch(
# to save the result into a file, simply add the optional `data_file` parameter, e.g.
# api_instance$find_pets_by_tags(var_tags, data_file = "result.txt"),
@ -247,7 +245,7 @@ Name | Type | Description | Notes
### Authorization
[petstore_auth](../README.md#petstore_auth)
No authorization required
### HTTP request headers

View File

@ -395,9 +395,6 @@
#' #Finds Pets by tags
#' api_instance <- PetApi$new()
#'
#' # Configure OAuth2 access token for authorization: petstore_auth
#' api_instance$api_client$access_token <- Sys.getenv("ACCESS_TOKEN")
#'
#' result <- tryCatch(
#'
#' # to save the result into a file, simply add the optional `data_file` parameter, e.g.
@ -944,6 +941,7 @@ PetApi <- R6::R6Class(
}
# explore
for (query_item in `status`) {
query_params[["status"]] <- c(query_params[["status"]], list(`status` = query_item))
}
@ -1064,14 +1062,10 @@ PetApi <- R6::R6Class(
}
for (query_item in `tags`) {
query_params[["tags"]] <- c(query_params[["tags"]], list(`tags` = query_item))
}
# no explore
query_params[["tags"]] <- I(paste(lapply(`tags`, URLencode, reserved = TRUE), collapse = ","))
local_var_url_path <- "/pet/findByTags"
# OAuth-related settings
is_oauth <- TRUE
oauth_scopes <- "read:pets"
# The Accept request HTTP header
local_var_accepts <- list("application/xml", "application/json")

View File

@ -1071,9 +1071,9 @@ UserApi <- R6::R6Class(
}
query_params["username"] <- `username`
query_params[["username"]] <- `username`
query_params["password"] <- `password`
query_params[["password"]] <- `password`
local_var_url_path <- "/user/login"

View File

@ -213,8 +213,6 @@ var_tags <- list("inner_example") # array[character] | Tags to filter by
# Finds Pets by tags
api_instance <- PetApi$new()
# Configure OAuth2 access token for authorization: petstore_auth
api_instance$api_client$access_token <- Sys.getenv("ACCESS_TOKEN")
result <- tryCatch(
# to save the result into a file, simply add the optional `data_file` parameter, e.g.
# api_instance$FindPetsByTags(var_tags, data_file = "result.txt"),
@ -247,7 +245,7 @@ Name | Type | Description | Notes
### Authorization
[petstore_auth](../README.md#petstore_auth)
No authorization required
### HTTP request headers