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

Commit 517816d

Browse files
authored
[R] Add array support to path parameters (#13450)
* add array support to path parameters (r client) * update samples
1 parent d48209e commit 517816d

File tree

21 files changed

+668
-31
lines changed

21 files changed

+668
-31
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,14 @@
542542
{{#hasPathParams}}
543543
{{#pathParams}}
544544
if (!missing(`{{paramName}}`)) {
545-
local_var_url_path <- gsub(paste0("\\{", "{{baseName}}", "\\}"), URLencode(as.character(`{{paramName}}`), reserved = TRUE), local_var_url_path)
545+
{{=< >=}}
546+
<#isArray>
547+
local_var_url_path <- gsub("\\{<baseName>\\}", paste(URLencode(as.character(`<paramName>`), reserved = TRUE), collapse= ",", sep=""), local_var_url_path)
548+
</isArray>
549+
<^isArray>
550+
local_var_url_path <- gsub("\\{<baseName>\\}", URLencode(as.character(`<paramName>`), reserved = TRUE), local_var_url_path)
551+
</isArray>
552+
<={{ }}=>
546553
}
547554
548555
{{/pathParams}}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,25 @@ paths:
629629
description: User not found
630630
security:
631631
- api_key: []
632+
/fake/path_array/{path_array}/testing:
633+
get:
634+
tags:
635+
- fake
636+
summary: test array parameter in path
637+
description: ''
638+
operationId: fake_path_array
639+
parameters:
640+
- name: path_array
641+
in: path
642+
description: dummy path parameter
643+
required: true
644+
schema:
645+
type: array
646+
items:
647+
type: string
648+
responses:
649+
'200':
650+
description: successful operation
632651
/fake/regular_expression:
633652
get:
634653
tags:

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

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,23 @@
3232
#' }
3333
#' }
3434
#'
35+
#' \strong{ fake_path_array } \emph{ test array parameter in path }
36+
#'
37+
#'
38+
#' \itemize{
39+
#' \item \emph{ @param } path_array list( character )
40+
#'
41+
#' \item On encountering errors, an error of subclass ApiException will be thrown.
42+
#'
43+
#' \item status code : 200 | successful operation
44+
#'
45+
#'
46+
#' \item response headers :
47+
#'
48+
#' \tabular{ll}{
49+
#' }
50+
#' }
51+
#'
3552
#' \strong{ fake_regular_expression } \emph{ test regular expression to ensure no exception }
3653
#'
3754
#'
@@ -106,6 +123,31 @@
106123
#'
107124
#'
108125
#'
126+
#' #################### fake_path_array ####################
127+
#'
128+
#' library(petstore)
129+
#' var_path_array <- ["path_array_example"] # array[character] | dummy path parameter
130+
#'
131+
#' #test array parameter in path
132+
#' api_instance <- petstore_api$new()
133+
#'
134+
#' result <- tryCatch(
135+
#'
136+
#' api_instance$fake_api$fake_path_array(var_path_array),
137+
#' ApiException = function(ex) ex
138+
#' )
139+
#' # In case of error, print the error object
140+
#' if (!is.null(result$ApiException)) {
141+
#' print("Exception occurs when calling `fake_path_array`:")
142+
#' dput(result$ApiException$toString())
143+
#'
144+
#' # error object
145+
#' dput(result$ApiException$error_object$toJSONString())
146+
#'
147+
#' }#'
148+
#' # This endpoint doesn't return data
149+
#'
150+
#'
109151
#' #################### fake_regular_expression ####################
110152
#'
111153
#' library(petstore)
@@ -300,6 +342,108 @@ FakeApi <- R6::R6Class(
300342
ApiException = ApiException$new(http_response = local_var_resp))
301343
}
302344
},
345+
#' test array parameter in path
346+
#'
347+
#' @description
348+
#' test array parameter in path
349+
#'
350+
#' @param path_array dummy path parameter
351+
#' @param ... Other optional arguments
352+
#' @return void
353+
#' @export
354+
fake_path_array = function(path_array, ...) {
355+
local_var_response <- self$fake_path_array_with_http_info(path_array, ...)
356+
if (local_var_response$status_code >= 200 && local_var_response$status_code <= 299) {
357+
local_var_response$content
358+
} else if (local_var_response$status_code >= 300 && local_var_response$status_code <= 399) {
359+
local_var_response
360+
} else if (local_var_response$status_code >= 400 && local_var_response$status_code <= 499) {
361+
local_var_response
362+
} else if (local_var_response$status_code >= 500 && local_var_response$status_code <= 599) {
363+
local_var_response
364+
}
365+
},
366+
#' test array parameter in path
367+
#'
368+
#' @description
369+
#' test array parameter in path
370+
#'
371+
#' @param path_array dummy path parameter
372+
#' @param ... Other optional arguments
373+
#' @return API response (void) with additional information such as HTTP status code, headers
374+
#' @export
375+
fake_path_array_with_http_info = function(path_array, ...) {
376+
args <- list(...)
377+
query_params <- list()
378+
header_params <- c()
379+
form_params <- list()
380+
file_params <- list()
381+
local_var_body <- NULL
382+
oauth_scopes <- NULL
383+
is_oauth <- FALSE
384+
385+
if (missing(`path_array`)) {
386+
rlang::abort(message = "Missing required parameter `path_array`.",
387+
.subclass = "ApiException",
388+
ApiException = ApiException$new(status = 0,
389+
reason = "Missing required parameter `path_array`."))
390+
}
391+
392+
393+
local_var_url_path <- "/fake/path_array/{path_array}/testing"
394+
if (!missing(`path_array`)) {
395+
local_var_url_path <- gsub("\\{path_array\\}", paste(URLencode(as.character(`path_array`), reserved = TRUE), collapse= ",", sep=""), local_var_url_path)
396+
}
397+
398+
399+
# The Accept request HTTP header
400+
local_var_accepts <- list()
401+
402+
# The Content-Type representation header
403+
local_var_content_types <- list()
404+
405+
local_var_resp <- self$api_client$CallApi(url = paste0(self$api_client$base_path, local_var_url_path),
406+
method = "GET",
407+
query_params = query_params,
408+
header_params = header_params,
409+
form_params = form_params,
410+
file_params = file_params,
411+
accepts = local_var_accepts,
412+
content_types = local_var_content_types,
413+
body = local_var_body,
414+
is_oauth = is_oauth,
415+
oauth_scopes = oauth_scopes,
416+
...)
417+
418+
if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) {
419+
local_var_resp$content <- NULL
420+
local_var_resp
421+
} else if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) {
422+
local_var_error_msg <- local_var_resp$response
423+
if (local_var_error_msg == "") {
424+
local_var_error_msg <- paste("Server returned ", local_var_resp$status_code, " response status code.")
425+
}
426+
rlang::abort(message = local_var_error_msg,
427+
.subclass = "ApiException",
428+
ApiException = ApiException$new(http_response = local_var_resp))
429+
} else if (local_var_resp$status_code >= 400 && local_var_resp$status_code <= 499) {
430+
local_var_error_msg <- local_var_resp$response
431+
if (local_var_error_msg == "") {
432+
local_var_error_msg <- "Api client exception encountered."
433+
}
434+
rlang::abort(message = local_var_error_msg,
435+
.subclass = "ApiException",
436+
ApiException = ApiException$new(http_response = local_var_resp))
437+
} else if (local_var_resp$status_code >= 500 && local_var_resp$status_code <= 599) {
438+
local_var_error_msg <- local_var_resp$response
439+
if (local_var_error_msg == "") {
440+
local_var_error_msg <- "Api server exception encountered."
441+
}
442+
rlang::abort(message = error_msg,
443+
.subclass = "ApiException",
444+
ApiException = ApiException$new(http_response = local_var_resp))
445+
}
446+
},
303447
#' test regular expression to ensure no exception
304448
#'
305449
#' @description

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,7 @@ PetApi <- R6::R6Class(
836836

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

842842
# OAuth-related settings
@@ -1187,7 +1187,7 @@ PetApi <- R6::R6Class(
11871187

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

11931193
# Bearer token
@@ -1314,7 +1314,7 @@ PetApi <- R6::R6Class(
13141314

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

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

17041704

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

18161816
# OAuth-related settings

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ StoreApi <- R6::R6Class(
314314

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

320320

@@ -537,7 +537,7 @@ StoreApi <- R6::R6Class(
537537

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

543543

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -832,7 +832,7 @@ UserApi <- R6::R6Class(
832832

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

838838
# API key authentication
@@ -940,7 +940,7 @@ UserApi <- R6::R6Class(
940940

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

946946

@@ -1296,7 +1296,7 @@ UserApi <- R6::R6Class(
12961296

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

13021302
# API key authentication

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ All URIs are relative to *http://petstore.swagger.io/v2*
6262
Class | Method | HTTP request | Description
6363
------------ | ------------- | ------------- | -------------
6464
*FakeApi* | [**fake_data_file**](docs/FakeApi.md#fake_data_file) | **GET** /fake/data_file | test data_file to ensure it's escaped correctly
65+
*FakeApi* | [**fake_path_array**](docs/FakeApi.md#fake_path_array) | **GET** /fake/path_array/{path_array}/testing | test array parameter in path
6566
*FakeApi* | [**fake_regular_expression**](docs/FakeApi.md#fake_regular_expression) | **GET** /fake/regular_expression | test regular expression to ensure no exception
6667
*FakeApi* | [**fake_set_query**](docs/FakeApi.md#fake_set_query) | **GET** /fake/set_query_parameter | test set query parameter
6768
*PetApi* | [**add_pet**](docs/PetApi.md#add_pet) | **POST** /pet | Add a new pet to the store

samples/client/petstore/R-httr2-wrapper/docs/FakeApi.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ All URIs are relative to *http://petstore.swagger.io/v2*
55
Method | HTTP request | Description
66
------------- | ------------- | -------------
77
[**fake_data_file**](FakeApi.md#fake_data_file) | **GET** /fake/data_file | test data_file to ensure it&#39;s escaped correctly
8+
[**fake_path_array**](FakeApi.md#fake_path_array) | **GET** /fake/path_array/{path_array}/testing | test array parameter in path
89
[**fake_regular_expression**](FakeApi.md#fake_regular_expression) | **GET** /fake/regular_expression | test regular expression to ensure no exception
910
[**fake_set_query**](FakeApi.md#fake_set_query) | **GET** /fake/set_query_parameter | test set query parameter
1011

@@ -72,6 +73,61 @@ No authorization required
7273
|-------------|-------------|------------------|
7374
| **200** | successful operation | - |
7475

76+
# **fake_path_array**
77+
> fake_path_array(path_array)
78+
79+
test array parameter in path
80+
81+
82+
83+
### Example
84+
```R
85+
library(petstore)
86+
87+
# test array parameter in path
88+
#
89+
# prepare function argument(s)
90+
var_path_array <- list("inner_example") # array[character] | dummy path parameter
91+
92+
api_instance <- petstore_api$new()
93+
result <- tryCatch(
94+
api_instance$fake_api$fake_path_array(var_path_array),
95+
ApiException = function(ex) ex
96+
)
97+
# In case of error, print the error object
98+
if (!is.null(result$ApiException)) {
99+
print("Exception occurs when calling `fake_path_array`:")
100+
dput(result$ApiException$toString())
101+
# error object
102+
dput(result$ApiException$error_object$toJSONString())
103+
}
104+
# This endpoint doesn't return data
105+
```
106+
107+
### Parameters
108+
109+
Name | Type | Description | Notes
110+
------------- | ------------- | ------------- | -------------
111+
**path_array** | list( **character** )| dummy path parameter |
112+
113+
### Return type
114+
115+
void (empty response body)
116+
117+
### Authorization
118+
119+
No authorization required
120+
121+
### HTTP request headers
122+
123+
- **Content-Type**: Not defined
124+
- **Accept**: Not defined
125+
126+
### HTTP response details
127+
| Status code | Description | Response headers |
128+
|-------------|-------------|------------------|
129+
| **200** | successful operation | - |
130+
75131
# **fake_regular_expression**
76132
> fake_regular_expression(reg_exp_test)
77133

samples/client/petstore/R-httr2-wrapper/tests/testthat/test_petstore.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,14 @@ test_that("GetPetById with data_file", {
234234
expect_equal(response$name, "name_test")
235235
})
236236

237+
test_that("array test in path parameters", {
238+
fake_api <- FakeApi$new()
239+
# array input for path parameter
240+
#array_dummy <- list(1, 2, 2, 3)
241+
array_dummy <- list("hello world", "1&2")
242+
expect_error(fake_api$fake_path_array(array_dummy), "")
243+
})
244+
237245
test_that("set validation test", {
238246
fake_api <- FakeApi$new()
239247
# array input invalid (not unique)

0 commit comments

Comments
 (0)