Skip to content

Commit

Permalink
[R] Add array support to path parameters (#13450)
Browse files Browse the repository at this point in the history
* add array support to path parameters (r client)

* update samples
  • Loading branch information
wing328 committed Sep 17, 2022
1 parent d48209e commit 517816d
Show file tree
Hide file tree
Showing 21 changed files with 668 additions and 31 deletions.
9 changes: 8 additions & 1 deletion modules/openapi-generator/src/main/resources/r/api.mustache
Expand Up @@ -542,7 +542,14 @@
{{#hasPathParams}}
{{#pathParams}}
if (!missing(`{{paramName}}`)) {
local_var_url_path <- gsub(paste0("\\{", "{{baseName}}", "\\}"), URLencode(as.character(`{{paramName}}`), reserved = TRUE), local_var_url_path)
{{=< >=}}
<#isArray>
local_var_url_path <- gsub("\\{<baseName>\\}", paste(URLencode(as.character(`<paramName>`), reserved = TRUE), collapse= ",", sep=""), local_var_url_path)
</isArray>
<^isArray>
local_var_url_path <- gsub("\\{<baseName>\\}", URLencode(as.character(`<paramName>`), reserved = TRUE), local_var_url_path)
</isArray>
<={{ }}=>
}
{{/pathParams}}
Expand Down
19 changes: 19 additions & 0 deletions modules/openapi-generator/src/test/resources/3_0/r/petstore.yaml
Expand Up @@ -629,6 +629,25 @@ paths:
description: User not found
security:
- api_key: []
/fake/path_array/{path_array}/testing:
get:
tags:
- fake
summary: test array parameter in path
description: ''
operationId: fake_path_array
parameters:
- name: path_array
in: path
description: dummy path parameter
required: true
schema:
type: array
items:
type: string
responses:
'200':
description: successful operation
/fake/regular_expression:
get:
tags:
Expand Down
144 changes: 144 additions & 0 deletions samples/client/petstore/R-httr2-wrapper/R/fake_api.R
Expand Up @@ -32,6 +32,23 @@
#' }
#' }
#'
#' \strong{ fake_path_array } \emph{ test array parameter in path }
#'
#'
#' \itemize{
#' \item \emph{ @param } path_array list( character )
#'
#' \item On encountering errors, an error of subclass ApiException will be thrown.
#'
#' \item status code : 200 | successful operation
#'
#'
#' \item response headers :
#'
#' \tabular{ll}{
#' }
#' }
#'
#' \strong{ fake_regular_expression } \emph{ test regular expression to ensure no exception }
#'
#'
Expand Down Expand Up @@ -106,6 +123,31 @@
#'
#'
#'
#' #################### fake_path_array ####################
#'
#' library(petstore)
#' var_path_array <- ["path_array_example"] # array[character] | dummy path parameter
#'
#' #test array parameter in path
#' api_instance <- petstore_api$new()
#'
#' result <- tryCatch(
#'
#' api_instance$fake_api$fake_path_array(var_path_array),
#' ApiException = function(ex) ex
#' )
#' # In case of error, print the error object
#' if (!is.null(result$ApiException)) {
#' print("Exception occurs when calling `fake_path_array`:")
#' dput(result$ApiException$toString())
#'
#' # error object
#' dput(result$ApiException$error_object$toJSONString())
#'
#' }#'
#' # This endpoint doesn't return data
#'
#'
#' #################### fake_regular_expression ####################
#'
#' library(petstore)
Expand Down Expand Up @@ -300,6 +342,108 @@ FakeApi <- R6::R6Class(
ApiException = ApiException$new(http_response = local_var_resp))
}
},
#' test array parameter in path
#'
#' @description
#' test array parameter in path
#'
#' @param path_array dummy path parameter
#' @param ... Other optional arguments
#' @return void
#' @export
fake_path_array = function(path_array, ...) {
local_var_response <- self$fake_path_array_with_http_info(path_array, ...)
if (local_var_response$status_code >= 200 && local_var_response$status_code <= 299) {
local_var_response$content
} else if (local_var_response$status_code >= 300 && local_var_response$status_code <= 399) {
local_var_response
} else if (local_var_response$status_code >= 400 && local_var_response$status_code <= 499) {
local_var_response
} else if (local_var_response$status_code >= 500 && local_var_response$status_code <= 599) {
local_var_response
}
},
#' test array parameter in path
#'
#' @description
#' test array parameter in path
#'
#' @param path_array dummy path parameter
#' @param ... Other optional arguments
#' @return API response (void) with additional information such as HTTP status code, headers
#' @export
fake_path_array_with_http_info = function(path_array, ...) {
args <- list(...)
query_params <- list()
header_params <- c()
form_params <- list()
file_params <- list()
local_var_body <- NULL
oauth_scopes <- NULL
is_oauth <- FALSE

if (missing(`path_array`)) {
rlang::abort(message = "Missing required parameter `path_array`.",
.subclass = "ApiException",
ApiException = ApiException$new(status = 0,
reason = "Missing required parameter `path_array`."))
}


local_var_url_path <- "/fake/path_array/{path_array}/testing"
if (!missing(`path_array`)) {
local_var_url_path <- gsub("\\{path_array\\}", paste(URLencode(as.character(`path_array`), reserved = TRUE), collapse= ",", sep=""), local_var_url_path)
}


# The Accept request HTTP header
local_var_accepts <- list()

# The Content-Type representation header
local_var_content_types <- list()

local_var_resp <- self$api_client$CallApi(url = paste0(self$api_client$base_path, local_var_url_path),
method = "GET",
query_params = query_params,
header_params = header_params,
form_params = form_params,
file_params = file_params,
accepts = local_var_accepts,
content_types = local_var_content_types,
body = local_var_body,
is_oauth = is_oauth,
oauth_scopes = oauth_scopes,
...)

if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) {
local_var_resp$content <- NULL
local_var_resp
} else if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) {
local_var_error_msg <- local_var_resp$response
if (local_var_error_msg == "") {
local_var_error_msg <- paste("Server returned ", local_var_resp$status_code, " response status code.")
}
rlang::abort(message = local_var_error_msg,
.subclass = "ApiException",
ApiException = ApiException$new(http_response = local_var_resp))
} else if (local_var_resp$status_code >= 400 && local_var_resp$status_code <= 499) {
local_var_error_msg <- local_var_resp$response
if (local_var_error_msg == "") {
local_var_error_msg <- "Api client exception encountered."
}
rlang::abort(message = local_var_error_msg,
.subclass = "ApiException",
ApiException = ApiException$new(http_response = local_var_resp))
} else if (local_var_resp$status_code >= 500 && local_var_resp$status_code <= 599) {
local_var_error_msg <- local_var_resp$response
if (local_var_error_msg == "") {
local_var_error_msg <- "Api server exception encountered."
}
rlang::abort(message = error_msg,
.subclass = "ApiException",
ApiException = ApiException$new(http_response = local_var_resp))
}
},
#' test regular expression to ensure no exception
#'
#' @description
Expand Down
10 changes: 5 additions & 5 deletions samples/client/petstore/R-httr2-wrapper/R/pet_api.R
Expand Up @@ -836,7 +836,7 @@ PetApi <- R6::R6Class(

local_var_url_path <- "/pet/{petId}"
if (!missing(`pet_id`)) {
local_var_url_path <- gsub(paste0("\\{", "petId", "\\}"), URLencode(as.character(`pet_id`), reserved = TRUE), local_var_url_path)
local_var_url_path <- gsub("\\{petId\\}", URLencode(as.character(`pet_id`), reserved = TRUE), local_var_url_path)
}

# OAuth-related settings
Expand Down Expand Up @@ -1187,7 +1187,7 @@ PetApi <- R6::R6Class(

local_var_url_path <- "/pet/{petId}"
if (!missing(`pet_id`)) {
local_var_url_path <- gsub(paste0("\\{", "petId", "\\}"), URLencode(as.character(`pet_id`), reserved = TRUE), local_var_url_path)
local_var_url_path <- gsub("\\{petId\\}", URLencode(as.character(`pet_id`), reserved = TRUE), local_var_url_path)
}

# Bearer token
Expand Down Expand Up @@ -1314,7 +1314,7 @@ PetApi <- R6::R6Class(

local_var_url_path <- "/pet/{petId}?streaming"
if (!missing(`pet_id`)) {
local_var_url_path <- gsub(paste0("\\{", "petId", "\\}"), URLencode(as.character(`pet_id`), reserved = TRUE), local_var_url_path)
local_var_url_path <- gsub("\\{petId\\}", URLencode(as.character(`pet_id`), reserved = TRUE), local_var_url_path)
}

# API key authentication
Expand Down Expand Up @@ -1698,7 +1698,7 @@ PetApi <- R6::R6Class(
form_params["status"] <- `status`
local_var_url_path <- "/pet/{petId}"
if (!missing(`pet_id`)) {
local_var_url_path <- gsub(paste0("\\{", "petId", "\\}"), URLencode(as.character(`pet_id`), reserved = TRUE), local_var_url_path)
local_var_url_path <- gsub("\\{petId\\}", URLencode(as.character(`pet_id`), reserved = TRUE), local_var_url_path)
}


Expand Down Expand Up @@ -1810,7 +1810,7 @@ PetApi <- R6::R6Class(
file_params["file"] <- curl::form_file(`file`)
local_var_url_path <- "/pet/{petId}/uploadImage"
if (!missing(`pet_id`)) {
local_var_url_path <- gsub(paste0("\\{", "petId", "\\}"), URLencode(as.character(`pet_id`), reserved = TRUE), local_var_url_path)
local_var_url_path <- gsub("\\{petId\\}", URLencode(as.character(`pet_id`), reserved = TRUE), local_var_url_path)
}

# OAuth-related settings
Expand Down
4 changes: 2 additions & 2 deletions samples/client/petstore/R-httr2-wrapper/R/store_api.R
Expand Up @@ -314,7 +314,7 @@ StoreApi <- R6::R6Class(

local_var_url_path <- "/store/order/{orderId}"
if (!missing(`order_id`)) {
local_var_url_path <- gsub(paste0("\\{", "orderId", "\\}"), URLencode(as.character(`order_id`), reserved = TRUE), local_var_url_path)
local_var_url_path <- gsub("\\{orderId\\}", URLencode(as.character(`order_id`), reserved = TRUE), local_var_url_path)
}


Expand Down Expand Up @@ -537,7 +537,7 @@ StoreApi <- R6::R6Class(

local_var_url_path <- "/store/order/{orderId}"
if (!missing(`order_id`)) {
local_var_url_path <- gsub(paste0("\\{", "orderId", "\\}"), URLencode(as.character(`order_id`), reserved = TRUE), local_var_url_path)
local_var_url_path <- gsub("\\{orderId\\}", URLencode(as.character(`order_id`), reserved = TRUE), local_var_url_path)
}


Expand Down
6 changes: 3 additions & 3 deletions samples/client/petstore/R-httr2-wrapper/R/user_api.R
Expand Up @@ -832,7 +832,7 @@ UserApi <- R6::R6Class(

local_var_url_path <- "/user/{username}"
if (!missing(`username`)) {
local_var_url_path <- gsub(paste0("\\{", "username", "\\}"), URLencode(as.character(`username`), reserved = TRUE), local_var_url_path)
local_var_url_path <- gsub("\\{username\\}", URLencode(as.character(`username`), reserved = TRUE), local_var_url_path)
}

# API key authentication
Expand Down Expand Up @@ -940,7 +940,7 @@ UserApi <- R6::R6Class(

local_var_url_path <- "/user/{username}"
if (!missing(`username`)) {
local_var_url_path <- gsub(paste0("\\{", "username", "\\}"), URLencode(as.character(`username`), reserved = TRUE), local_var_url_path)
local_var_url_path <- gsub("\\{username\\}", URLencode(as.character(`username`), reserved = TRUE), local_var_url_path)
}


Expand Down Expand Up @@ -1296,7 +1296,7 @@ UserApi <- R6::R6Class(

local_var_url_path <- "/user/{username}"
if (!missing(`username`)) {
local_var_url_path <- gsub(paste0("\\{", "username", "\\}"), URLencode(as.character(`username`), reserved = TRUE), local_var_url_path)
local_var_url_path <- gsub("\\{username\\}", URLencode(as.character(`username`), reserved = TRUE), local_var_url_path)
}

# API key authentication
Expand Down
1 change: 1 addition & 0 deletions samples/client/petstore/R-httr2-wrapper/README.md
Expand Up @@ -62,6 +62,7 @@ All URIs are relative to *http://petstore.swagger.io/v2*
Class | Method | HTTP request | Description
------------ | ------------- | ------------- | -------------
*FakeApi* | [**fake_data_file**](docs/FakeApi.md#fake_data_file) | **GET** /fake/data_file | test data_file to ensure it's escaped correctly
*FakeApi* | [**fake_path_array**](docs/FakeApi.md#fake_path_array) | **GET** /fake/path_array/{path_array}/testing | test array parameter in path
*FakeApi* | [**fake_regular_expression**](docs/FakeApi.md#fake_regular_expression) | **GET** /fake/regular_expression | test regular expression to ensure no exception
*FakeApi* | [**fake_set_query**](docs/FakeApi.md#fake_set_query) | **GET** /fake/set_query_parameter | test set query parameter
*PetApi* | [**add_pet**](docs/PetApi.md#add_pet) | **POST** /pet | Add a new pet to the store
Expand Down
56 changes: 56 additions & 0 deletions samples/client/petstore/R-httr2-wrapper/docs/FakeApi.md
Expand Up @@ -5,6 +5,7 @@ All URIs are relative to *http://petstore.swagger.io/v2*
Method | HTTP request | Description
------------- | ------------- | -------------
[**fake_data_file**](FakeApi.md#fake_data_file) | **GET** /fake/data_file | test data_file to ensure it&#39;s escaped correctly
[**fake_path_array**](FakeApi.md#fake_path_array) | **GET** /fake/path_array/{path_array}/testing | test array parameter in path
[**fake_regular_expression**](FakeApi.md#fake_regular_expression) | **GET** /fake/regular_expression | test regular expression to ensure no exception
[**fake_set_query**](FakeApi.md#fake_set_query) | **GET** /fake/set_query_parameter | test set query parameter

Expand Down Expand Up @@ -72,6 +73,61 @@ No authorization required
|-------------|-------------|------------------|
| **200** | successful operation | - |

# **fake_path_array**
> fake_path_array(path_array)
test array parameter in path



### Example
```R
library(petstore)

# test array parameter in path
#
# prepare function argument(s)
var_path_array <- list("inner_example") # array[character] | dummy path parameter

api_instance <- petstore_api$new()
result <- tryCatch(
api_instance$fake_api$fake_path_array(var_path_array),
ApiException = function(ex) ex
)
# In case of error, print the error object
if (!is.null(result$ApiException)) {
print("Exception occurs when calling `fake_path_array`:")
dput(result$ApiException$toString())
# error object
dput(result$ApiException$error_object$toJSONString())
}
# This endpoint doesn't return data
```

### Parameters

Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**path_array** | list( **character** )| dummy path parameter |

### Return type

void (empty response body)

### Authorization

No authorization required

### HTTP request headers

- **Content-Type**: Not defined
- **Accept**: Not defined

### HTTP response details
| Status code | Description | Response headers |
|-------------|-------------|------------------|
| **200** | successful operation | - |

# **fake_regular_expression**
> fake_regular_expression(reg_exp_test)
Expand Down
Expand Up @@ -234,6 +234,14 @@ test_that("GetPetById with data_file", {
expect_equal(response$name, "name_test")
})

test_that("array test in path parameters", {
fake_api <- FakeApi$new()
# array input for path parameter
#array_dummy <- list(1, 2, 2, 3)
array_dummy <- list("hello world", "1&2")
expect_error(fake_api$fake_path_array(array_dummy), "")
})

test_that("set validation test", {
fake_api <- FakeApi$new()
# array input invalid (not unique)
Expand Down

0 comments on commit 517816d

Please sign in to comment.