Skip to content

Commit 26ebde4

Browse files
authored
support set in r client parameters (#13430)
1 parent 03cf550 commit 26ebde4

File tree

12 files changed

+788
-0
lines changed

12 files changed

+788
-0
lines changed

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

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,65 @@
382382
{{/headerParams}}
383383
{{#queryParams}}
384384
{{#isArray}}
385+
{{#uniqueItems}}
386+
# check if items are unique
387+
if (!identical(`{{{paramName}}}`, unique(`{{{paramName}}}`))) {
388+
{{#useDefaultExceptionHandling}}
389+
stop("Invalid value for {{{paramName}}} when calling {{classname}}${{operationId}}. Items must be unqiue.")
390+
{{/useDefaultExceptionHandling}}
391+
{{#useRlangExceptionHandling}}
392+
rlang::abort(message = "Invalid value for `{{paramName}}` when calling {{classname}}${{operationId}}. Items must be unique.",
393+
.subclass = "ApiException",
394+
ApiException = ApiException$new(status = 0,
395+
reason = "Invalid value for `{{paramName}}` when calling {{classname}}${{operationId}}. Items must be unique."))
396+
{{/useRlangExceptionHandling}}
397+
}
398+
{{#isExplode}}
399+
# explore
400+
for (query_item in `{{{paramName}}}`) {
401+
{{#items}}
402+
{{#isEnum}}
403+
# validate enum values
404+
if (!(query_item %in% c({{#_enum}}"{{{.}}}"{{^-last}}, {{/-last}}{{/_enum}}))) {
405+
{{#useDefaultExceptionHandling}}
406+
stop("Invalid value for {{{paramName}}} when calling {{classname}}${{operationId}}. Must be {{_enum}}.")
407+
{{/useDefaultExceptionHandling}}
408+
{{#useRlangExceptionHandling}}
409+
rlang::abort(message = "Invalid value for `{{paramName}}` when calling {{classname}}${{operationId}}. Must be {{_enum}}.",
410+
.subclass = "ApiException",
411+
ApiException = ApiException$new(status = 0,
412+
reason = "Invalid value for `{{paramName}}` when calling {{classname}}${{operationId}}. Must be {{_enum}}."))
413+
{{/useRlangExceptionHandling}}
414+
}
415+
{{/isEnum}}
416+
{{/items}}
417+
query_params[["{{{baseName}}}"]] <- c(query_params[["{{{baseName}}}"]], list(`{{{baseName}}}` = query_item))
418+
}
419+
{{/isExplode}}
420+
{{^isExplode}}
421+
# no explore
422+
{{#items}}
423+
{{#isEnum}}
424+
# validate enum values
425+
for (query_item in `{{{paramName}}}`) {
426+
if (!(query_item %in% c({{#_enum}}"{{{.}}}"{{^-last}}, {{/-last}}{{/_enum}}))) {
427+
{{#useDefaultExceptionHandling}}
428+
stop("Invalid value for {{{paramName}}} when calling {{classname}}${{operationId}}. Must be {{_enum}}.")
429+
{{/useDefaultExceptionHandling}}
430+
{{#useRlangExceptionHandling}}
431+
rlang::abort(message = "Invalid value for `{{paramName}}` when calling {{classname}}${{operationId}}. Must be {{_enum}}.",
432+
.subclass = "ApiException",
433+
ApiException = ApiException$new(status = 0,
434+
reason = "Invalid value for `{{paramName}}` when calling {{classname}}${{operationId}}. Must be {{_enum}}."))
435+
{{/useRlangExceptionHandling}}
436+
}
437+
}
438+
{{/isEnum}}
439+
{{/items}}
440+
query_params[["{{{baseName}}}"]] <- I(paste(lapply(`{{{paramName}}}`, URLencode, reserved = TRUE), collapse = ","))
441+
{{/isExplode}}
442+
{{/uniqueItems}}
443+
{{^uniqueItems}}
385444
{{#isExplode}}
386445
# explore
387446
for (query_item in `{{{paramName}}}`) {
@@ -426,6 +485,7 @@
426485
{{/items}}
427486
query_params[["{{{baseName}}}"]] <- I(paste(lapply(`{{{paramName}}}`, URLencode, reserved = TRUE), collapse = ","))
428487
{{/isExplode}}
488+
{{/uniqueItems}}
429489
{{/isArray}}
430490
{{^isArray}}
431491
{{#isEnum}}

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,35 @@ paths:
677677
application/json:
678678
schema:
679679
$ref: '#/components/schemas/User'
680+
/fake/set_query_parameter:
681+
get:
682+
tags:
683+
- fake
684+
summary: test set query parameter
685+
description: ''
686+
operationId: fake_set_query
687+
parameters:
688+
- name: set_dummy
689+
in: query
690+
description: set query
691+
required: true
692+
schema:
693+
type: array
694+
uniqueItems: true
695+
items:
696+
type: string
697+
- name: array_dummy
698+
in: query
699+
description: array query
700+
required: true
701+
schema:
702+
type: array
703+
uniqueItems: false
704+
items:
705+
type: string
706+
responses:
707+
'200':
708+
description: successful operation
680709
externalDocs:
681710
description: Find out more about Swagger
682711
url: 'http://swagger.io'

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

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,24 @@
4949
#' }
5050
#' }
5151
#'
52+
#' \strong{ fake_set_query } \emph{ test set query parameter }
53+
#'
54+
#'
55+
#' \itemize{
56+
#' \item \emph{ @param } set_dummy list( character )
57+
#' \item \emph{ @param } array_dummy list( character )
58+
#'
59+
#' \item On encountering errors, an error of subclass ApiException will be thrown.
60+
#'
61+
#' \item status code : 200 | successful operation
62+
#'
63+
#'
64+
#' \item response headers :
65+
#'
66+
#' \tabular{ll}{
67+
#' }
68+
#' }
69+
#'
5270
#' }
5371
#'
5472
#'
@@ -113,6 +131,32 @@
113131
#' # This endpoint doesn't return data
114132
#'
115133
#'
134+
#' #################### fake_set_query ####################
135+
#'
136+
#' library(petstore)
137+
#' var_set_dummy <- ["set_dummy_example"] # set[character] | set query
138+
#' var_array_dummy <- ["array_dummy_example"] # array[character] | array query
139+
#'
140+
#' #test set query parameter
141+
#' api_instance <- petstore_api$new()
142+
#'
143+
#' result <- tryCatch(
144+
#'
145+
#' api_instance$fake_api$fake_set_query(var_set_dummy, var_array_dummy),
146+
#' ApiException = function(ex) ex
147+
#' )
148+
#' # In case of error, print the error object
149+
#' if (!is.null(result$ApiException)) {
150+
#' print("Exception occurs when calling `fake_set_query`:")
151+
#' dput(result$ApiException$toString())
152+
#'
153+
#' # error object
154+
#' dput(result$ApiException$error_object$toJSONString())
155+
#'
156+
#' }#'
157+
#' # This endpoint doesn't return data
158+
#'
159+
#'
116160
#' }
117161
#' @importFrom R6 R6Class
118162
#' @importFrom base64enc base64encode
@@ -320,6 +364,131 @@ FakeApi <- R6::R6Class(
320364
# The Content-Type representation header
321365
local_var_content_types <- list()
322366

367+
local_var_resp <- self$api_client$CallApi(url = paste0(self$api_client$base_path, local_var_url_path),
368+
method = "GET",
369+
query_params = query_params,
370+
header_params = header_params,
371+
form_params = form_params,
372+
file_params = file_params,
373+
accepts = local_var_accepts,
374+
content_types = local_var_content_types,
375+
body = local_var_body,
376+
is_oauth = is_oauth,
377+
oauth_scopes = oauth_scopes,
378+
...)
379+
380+
if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) {
381+
local_var_resp$content <- NULL
382+
local_var_resp
383+
} else if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) {
384+
local_var_error_msg <- local_var_resp$response
385+
if (local_var_error_msg == "") {
386+
local_var_error_msg <- paste("Server returned ", local_var_resp$status_code, " response status code.")
387+
}
388+
rlang::abort(message = local_var_error_msg,
389+
.subclass = "ApiException",
390+
ApiException = ApiException$new(http_response = local_var_resp))
391+
} else if (local_var_resp$status_code >= 400 && local_var_resp$status_code <= 499) {
392+
local_var_error_msg <- local_var_resp$response
393+
if (local_var_error_msg == "") {
394+
local_var_error_msg <- "Api client exception encountered."
395+
}
396+
rlang::abort(message = local_var_error_msg,
397+
.subclass = "ApiException",
398+
ApiException = ApiException$new(http_response = local_var_resp))
399+
} else if (local_var_resp$status_code >= 500 && local_var_resp$status_code <= 599) {
400+
local_var_error_msg <- local_var_resp$response
401+
if (local_var_error_msg == "") {
402+
local_var_error_msg <- "Api server exception encountered."
403+
}
404+
rlang::abort(message = error_msg,
405+
.subclass = "ApiException",
406+
ApiException = ApiException$new(http_response = local_var_resp))
407+
}
408+
},
409+
#' test set query parameter
410+
#'
411+
#' @description
412+
#' test set query parameter
413+
#'
414+
#' @param set_dummy set query
415+
#' @param array_dummy array query
416+
#' @param ... Other optional arguments
417+
#' @return void
418+
#' @export
419+
fake_set_query = function(set_dummy, array_dummy, ...) {
420+
local_var_response <- self$fake_set_query_with_http_info(set_dummy, array_dummy, ...)
421+
if (local_var_response$status_code >= 200 && local_var_response$status_code <= 299) {
422+
local_var_response$content
423+
} else if (local_var_response$status_code >= 300 && local_var_response$status_code <= 399) {
424+
local_var_response
425+
} else if (local_var_response$status_code >= 400 && local_var_response$status_code <= 499) {
426+
local_var_response
427+
} else if (local_var_response$status_code >= 500 && local_var_response$status_code <= 599) {
428+
local_var_response
429+
}
430+
},
431+
#' test set query parameter
432+
#'
433+
#' @description
434+
#' test set query parameter
435+
#'
436+
#' @param set_dummy set query
437+
#' @param array_dummy array query
438+
#' @param ... Other optional arguments
439+
#' @return API response (void) with additional information such as HTTP status code, headers
440+
#' @export
441+
fake_set_query_with_http_info = function(set_dummy, array_dummy, ...) {
442+
args <- list(...)
443+
query_params <- list()
444+
header_params <- c()
445+
form_params <- list()
446+
file_params <- list()
447+
local_var_body <- NULL
448+
oauth_scopes <- NULL
449+
is_oauth <- FALSE
450+
451+
if (missing(`set_dummy`)) {
452+
rlang::abort(message = "Missing required parameter `set_dummy`.",
453+
.subclass = "ApiException",
454+
ApiException = ApiException$new(status = 0,
455+
reason = "Missing required parameter `set_dummy`."))
456+
}
457+
458+
if (missing(`array_dummy`)) {
459+
rlang::abort(message = "Missing required parameter `array_dummy`.",
460+
.subclass = "ApiException",
461+
ApiException = ApiException$new(status = 0,
462+
reason = "Missing required parameter `array_dummy`."))
463+
}
464+
465+
466+
467+
# check if items are unique
468+
if (!identical(`set_dummy`, unique(`set_dummy`))) {
469+
rlang::abort(message = "Invalid value for `set_dummy` when calling FakeApi$fake_set_query. Items must be unique.",
470+
.subclass = "ApiException",
471+
ApiException = ApiException$new(status = 0,
472+
reason = "Invalid value for `set_dummy` when calling FakeApi$fake_set_query. Items must be unique."))
473+
}
474+
# explore
475+
for (query_item in `set_dummy`) {
476+
query_params[["set_dummy"]] <- c(query_params[["set_dummy"]], list(`set_dummy` = query_item))
477+
}
478+
479+
# explore
480+
for (query_item in `array_dummy`) {
481+
query_params[["array_dummy"]] <- c(query_params[["array_dummy"]], list(`array_dummy` = query_item))
482+
}
483+
484+
local_var_url_path <- "/fake/set_query_parameter"
485+
486+
# The Accept request HTTP header
487+
local_var_accepts <- list()
488+
489+
# The Content-Type representation header
490+
local_var_content_types <- list()
491+
323492
local_var_resp <- self$api_client$CallApi(url = paste0(self$api_client$base_path, local_var_url_path),
324493
method = "GET",
325494
query_params = query_params,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ 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
6565
*FakeApi* | [**fake_regular_expression**](docs/FakeApi.md#fake_regular_expression) | **GET** /fake/regular_expression | test regular expression to ensure no exception
66+
*FakeApi* | [**fake_set_query**](docs/FakeApi.md#fake_set_query) | **GET** /fake/set_query_parameter | test set query parameter
6667
*PetApi* | [**add_pet**](docs/PetApi.md#add_pet) | **POST** /pet | Add a new pet to the store
6768
*PetApi* | [**delete_pet**](docs/PetApi.md#delete_pet) | **DELETE** /pet/{petId} | Deletes a pet
6869
*PetApi* | [**find_pets_by_status**](docs/PetApi.md#find_pets_by_status) | **GET** /pet/findByStatus | Finds Pets by status

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ 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
88
[**fake_regular_expression**](FakeApi.md#fake_regular_expression) | **GET** /fake/regular_expression | test regular expression to ensure no exception
9+
[**fake_set_query**](FakeApi.md#fake_set_query) | **GET** /fake/set_query_parameter | test set query parameter
910

1011

1112
# **fake_data_file**
@@ -122,3 +123,58 @@ No authorization required
122123
|-------------|-------------|------------------|
123124
| **200** | successful operation | - |
124125

126+
# **fake_set_query**
127+
> fake_set_query(set_dummy, array_dummy)
128+
129+
test set query parameter
130+
131+
132+
133+
### Example
134+
```R
135+
library(petstore)
136+
137+
var_set_dummy <- list("inner_example") # set[character] | set query
138+
var_array_dummy <- list("inner_example") # array[character] | array query
139+
140+
# test set query parameter
141+
api_instance <- petstore_api$new()
142+
result <- tryCatch(
143+
api_instance$fake_api$fake_set_query(var_set_dummy, var_array_dummy),
144+
ApiException = function(ex) ex
145+
)
146+
# In case of error, print the error object
147+
if (!is.null(result$ApiException)) {
148+
print("Exception occurs when calling `fake_set_query`:")
149+
dput(result$ApiException$toString())
150+
# error object
151+
dput(result$ApiException$error_object$toJSONString())
152+
}
153+
# This endpoint doesn't return data
154+
```
155+
156+
### Parameters
157+
158+
Name | Type | Description | Notes
159+
------------- | ------------- | ------------- | -------------
160+
**set_dummy** | list( **character** )| set query |
161+
**array_dummy** | list( **character** )| array query |
162+
163+
### Return type
164+
165+
void (empty response body)
166+
167+
### Authorization
168+
169+
No authorization required
170+
171+
### HTTP request headers
172+
173+
- **Content-Type**: Not defined
174+
- **Accept**: Not defined
175+
176+
### HTTP response details
177+
| Status code | Description | Response headers |
178+
|-------------|-------------|------------------|
179+
| **200** | successful operation | - |
180+

0 commit comments

Comments
 (0)