improve r regular expression handling (#13125)

This commit is contained in:
William Cheng 2022-08-08 00:56:43 +08:00 committed by GitHub
parent f287dde875
commit fb29284a99
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 445 additions and 6 deletions

View File

@ -939,4 +939,23 @@ public class RClientCodegen extends DefaultCodegen implements CodegenConfig {
public GeneratorLanguage generatorLanguage() {
return GeneratorLanguage.R;
}
@Override
public String toRegularExpression(String pattern) {
if (pattern == null) {
return null;
}
// remove leading '/'
if (pattern.charAt(0) == '/') {
pattern = pattern.substring(1);
}
// remove trailing '/'
if (pattern.charAt(pattern.length()-1) == '/') {
pattern = pattern.substring(0, pattern.length()-1);
}
return escapeText(pattern);
}
}

View File

@ -628,6 +628,24 @@ paths:
description: User not found
security:
- api_key: []
/fake/regular_expression:
get:
tags:
- fake
summary: test regular expression to ensure no exception
description: ''
operationId: fake_regular-expression
parameters:
- name: reg_exp_test
in: header
description: dummy required parameter
required: true
schema:
type: string
pattern: /^[A-Za-z0-9_]{1,15}$/
responses:
'200':
description: successful operation
/fake/data_file:
get:
tags:

View File

@ -33,6 +33,23 @@
#' }
#' }
#'
#' \strong{ fake_regular_expression } \emph{ test regular expression to ensure no exception }
#'
#'
#' \itemize{
#' \item \emph{ @param } reg_exp_test character
#'
#' \item On encountering errors, an error of subclass ApiException will be thrown.
#'
#' \item status code : 200 | successful operation
#'
#'
#' \item response headers :
#'
#' \tabular{ll}{
#' }
#' }
#'
#' }
#'
#'
@ -64,6 +81,29 @@
#' }
#'
#'
#' #################### fake_regular_expression ####################
#'
#' library(petstore)
#' var.reg_exp_test <- "reg_exp_test_example" # character | dummy required parameter
#'
#' #test regular expression to ensure no exception
#' api.instance <- FakeApi$new()
#'
#'result <- tryCatch(
#' api.instance$fake_regular_expression(var.reg_exp_test),
#' ApiException = function(ex) ex
#' )
#' # In case of error, print the error object
#' if(!is.null(result$ApiException)) {
#' cat(result$ApiException$toString())
#' } else {
#' # response headers
#' response.headers <- result$response$headers
#' # response status code
#' response.status.code <- result$response$status_code
#' }
#'
#'
#' }
#' @importFrom R6 R6Class
#' @importFrom base64enc base64encode
@ -206,6 +246,112 @@ FakeApi <- R6::R6Class(
.subclass = "ApiException",
ApiException = ApiException$new(http_response = local_var_resp))
}
},
#' test regular expression to ensure no exception
#'
#' @description
#' test regular expression to ensure no exception
#'
#' @param reg_exp_test dummy required parameter
#' @param ... Other optional arguments
#' @return void
#' @export
fake_regular_expression = function(reg_exp_test, ...) {
local_var_response <- self$fake_regular_expression_with_http_info(reg_exp_test, ...)
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 regular expression to ensure no exception
#'
#' @description
#' test regular expression to ensure no exception
#'
#' @param reg_exp_test dummy required parameter
#' @param ... Other optional arguments
#' @return API response (void) with additional information such as HTTP status code, headers
#' @export
fake_regular_expression_with_http_info = function(reg_exp_test, ...) {
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(`reg_exp_test`)) {
rlang::abort(message = "Missing required parameter `reg_exp_test`.",
.subclass = "ApiException",
ApiException = ApiException$new(status = 0,
reason = "Missing required parameter `reg_exp_test`."))
}
if (!str_detect(`reg_exp_test`, "^[A-Za-z0-9_]{1,15}$")) {
rlang::abort(message = "Invalid value for `reg_exp_test` when calling FakeApi$fake_regular_expression, must conform to the pattern ^[A-Za-z0-9_]{1,15}$.",
.subclass = "ApiException",
ApiException = ApiException$new(status = 0,
reason = "Invalid value for `reg_exp_test` when calling FakeApi$fake_regular_expression, must conform to the pattern ^[A-Za-z0-9_]{1,15}$."))
}
header_params["reg_exp_test"] <- `reg_exp_test`
local_var_url_path <- "/fake/regular_expression"
# 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))
}
}
)
)

View File

@ -1036,11 +1036,11 @@ UserApi <- R6::R6Class(
reason = "Missing required parameter `password`."))
}
if (!str_detect(`username`, "/^[a-zA-Z0-9]+[a-zA-Z0-9\\.\\-_]*[a-zA-Z0-9]+$/")) {
rlang::abort(message = "Invalid value for `username` when calling UserApi$login_user, must conform to the pattern /^[a-zA-Z0-9]+[a-zA-Z0-9\\.\\-_]*[a-zA-Z0-9]+$/.",
if (!str_detect(`username`, "^[a-zA-Z0-9]+[a-zA-Z0-9\\.\\-_]*[a-zA-Z0-9]+$")) {
rlang::abort(message = "Invalid value for `username` when calling UserApi$login_user, must conform to the pattern ^[a-zA-Z0-9]+[a-zA-Z0-9\\.\\-_]*[a-zA-Z0-9]+$.",
.subclass = "ApiException",
ApiException = ApiException$new(status = 0,
reason = "Invalid value for `username` when calling UserApi$login_user, must conform to the pattern /^[a-zA-Z0-9]+[a-zA-Z0-9\\.\\-_]*[a-zA-Z0-9]+$/."))
reason = "Invalid value for `username` when calling UserApi$login_user, must conform to the pattern ^[a-zA-Z0-9]+[a-zA-Z0-9\\.\\-_]*[a-zA-Z0-9]+$."))
}

View File

@ -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_regular_expression**](docs/FakeApi.md#fake_regular_expression) | **GET** /fake/regular_expression | test regular expression to ensure no exception
*PetApi* | [**add_pet**](docs/PetApi.md#add_pet) | **POST** /pet | Add a new pet to the store
*PetApi* | [**delete_pet**](docs/PetApi.md#delete_pet) | **DELETE** /pet/{petId} | Deletes a pet
*PetApi* | [**find_pets_by_status**](docs/PetApi.md#find_pets_by_status) | **GET** /pet/findByStatus | Finds Pets by status

View File

@ -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_regular_expression**](FakeApi.md#fake_regular_expression) | **GET** /fake/regular_expression | test regular expression to ensure no exception
# **fake_data_file**
@ -68,3 +69,56 @@ No authorization required
|-------------|-------------|------------------|
| **200** | successful operation | - |
# **fake_regular_expression**
> fake_regular_expression(reg_exp_test)
test regular expression to ensure no exception
### Example
```R
library(petstore)
var_reg_exp_test <- "reg_exp_test_example" # character | dummy required parameter
#test regular expression to ensure no exception
api_instance <- FakeApi$new()
result <- tryCatch(
api_instance$fake_regular_expression(var_reg_exp_test),
ApiException = function(ex) ex
)
# In case of error, print the error object
if (!is.null(result$ApiException)) {
print("Exception occurs when calling `fake_regular_expression`:")
dput(result$ApiException$toString())
# error object
dput(result$ApiException$error_object)
}
# This endpoint doesn't return data
```
### Parameters
Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**reg_exp_test** | **character**| dummy required 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 | - |

View File

@ -33,6 +33,23 @@
#' }
#' }
#'
#' \strong{ FakeRegularExpression } \emph{ test regular expression to ensure no exception }
#'
#'
#' \itemize{
#' \item \emph{ @param } reg_exp_test character
#'
#' \item On encountering errors, an error of subclass ApiException will be thrown.
#'
#' \item status code : 200 | successful operation
#'
#'
#' \item response headers :
#'
#' \tabular{ll}{
#' }
#' }
#'
#' }
#'
#'
@ -64,6 +81,29 @@
#' }
#'
#'
#' #################### FakeRegularExpression ####################
#'
#' library(petstore)
#' var.reg_exp_test <- "reg_exp_test_example" # character | dummy required parameter
#'
#' #test regular expression to ensure no exception
#' api.instance <- FakeApi$new()
#'
#'result <- tryCatch(
#' api.instance$FakeRegularExpression(var.reg_exp_test),
#' ApiException = function(ex) ex
#' )
#' # In case of error, print the error object
#' if(!is.null(result$ApiException)) {
#' cat(result$ApiException$toString())
#' } else {
#' # response headers
#' response.headers <- result$response$headers
#' # response status code
#' response.status.code <- result$response$status_code
#' }
#'
#'
#' }
#' @importFrom R6 R6Class
#' @importFrom base64enc base64encode
@ -206,6 +246,112 @@ FakeApi <- R6::R6Class(
.subclass = "ApiException",
ApiException = ApiException$new(http_response = local_var_resp))
}
},
#' test regular expression to ensure no exception
#'
#' @description
#' test regular expression to ensure no exception
#'
#' @param reg_exp_test dummy required parameter
#' @param ... Other optional arguments
#' @return void
#' @export
FakeRegularExpression = function(reg_exp_test, ...) {
local_var_response <- self$FakeRegularExpressionWithHttpInfo(reg_exp_test, ...)
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 regular expression to ensure no exception
#'
#' @description
#' test regular expression to ensure no exception
#'
#' @param reg_exp_test dummy required parameter
#' @param ... Other optional arguments
#' @return API response (void) with additional information such as HTTP status code, headers
#' @export
FakeRegularExpressionWithHttpInfo = function(reg_exp_test, ...) {
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(`reg_exp_test`)) {
rlang::abort(message = "Missing required parameter `reg_exp_test`.",
.subclass = "ApiException",
ApiException = ApiException$new(status = 0,
reason = "Missing required parameter `reg_exp_test`."))
}
if (!str_detect(`reg_exp_test`, "^[A-Za-z0-9_]{1,15}$")) {
rlang::abort(message = "Invalid value for `reg_exp_test` when calling FakeApi$FakeRegularExpression, must conform to the pattern ^[A-Za-z0-9_]{1,15}$.",
.subclass = "ApiException",
ApiException = ApiException$new(status = 0,
reason = "Invalid value for `reg_exp_test` when calling FakeApi$FakeRegularExpression, must conform to the pattern ^[A-Za-z0-9_]{1,15}$."))
}
header_params["reg_exp_test"] <- `reg_exp_test`
local_var_url_path <- "/fake/regular_expression"
# 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))
}
}
)
)

View File

@ -1036,11 +1036,11 @@ UserApi <- R6::R6Class(
reason = "Missing required parameter `password`."))
}
if (!str_detect(`username`, "/^[a-zA-Z0-9]+[a-zA-Z0-9\\.\\-_]*[a-zA-Z0-9]+$/")) {
rlang::abort(message = "Invalid value for `username` when calling UserApi$LoginUser, must conform to the pattern /^[a-zA-Z0-9]+[a-zA-Z0-9\\.\\-_]*[a-zA-Z0-9]+$/.",
if (!str_detect(`username`, "^[a-zA-Z0-9]+[a-zA-Z0-9\\.\\-_]*[a-zA-Z0-9]+$")) {
rlang::abort(message = "Invalid value for `username` when calling UserApi$LoginUser, must conform to the pattern ^[a-zA-Z0-9]+[a-zA-Z0-9\\.\\-_]*[a-zA-Z0-9]+$.",
.subclass = "ApiException",
ApiException = ApiException$new(status = 0,
reason = "Invalid value for `username` when calling UserApi$LoginUser, must conform to the pattern /^[a-zA-Z0-9]+[a-zA-Z0-9\\.\\-_]*[a-zA-Z0-9]+$/."))
reason = "Invalid value for `username` when calling UserApi$LoginUser, must conform to the pattern ^[a-zA-Z0-9]+[a-zA-Z0-9\\.\\-_]*[a-zA-Z0-9]+$."))
}

View File

@ -62,6 +62,7 @@ All URIs are relative to *http://petstore.swagger.io/v2*
Class | Method | HTTP request | Description
------------ | ------------- | ------------- | -------------
*FakeApi* | [**FakeDataFile**](docs/FakeApi.md#FakeDataFile) | **GET** /fake/data_file | test data_file to ensure it's escaped correctly
*FakeApi* | [**FakeRegularExpression**](docs/FakeApi.md#FakeRegularExpression) | **GET** /fake/regular_expression | test regular expression to ensure no exception
*PetApi* | [**AddPet**](docs/PetApi.md#AddPet) | **POST** /pet | Add a new pet to the store
*PetApi* | [**DeletePet**](docs/PetApi.md#DeletePet) | **DELETE** /pet/{petId} | Deletes a pet
*PetApi* | [**FindPetsByStatus**](docs/PetApi.md#FindPetsByStatus) | **GET** /pet/findByStatus | Finds Pets by status

View File

@ -5,6 +5,7 @@ All URIs are relative to *http://petstore.swagger.io/v2*
Method | HTTP request | Description
------------- | ------------- | -------------
[**FakeDataFile**](FakeApi.md#FakeDataFile) | **GET** /fake/data_file | test data_file to ensure it&#39;s escaped correctly
[**FakeRegularExpression**](FakeApi.md#FakeRegularExpression) | **GET** /fake/regular_expression | test regular expression to ensure no exception
# **FakeDataFile**
@ -68,3 +69,56 @@ No authorization required
|-------------|-------------|------------------|
| **200** | successful operation | - |
# **FakeRegularExpression**
> FakeRegularExpression(reg_exp_test)
test regular expression to ensure no exception
### Example
```R
library(petstore)
var_reg_exp_test <- "reg_exp_test_example" # character | dummy required parameter
#test regular expression to ensure no exception
api_instance <- FakeApi$new()
result <- tryCatch(
api_instance$FakeRegularExpression(var_reg_exp_test),
ApiException = function(ex) ex
)
# In case of error, print the error object
if (!is.null(result$ApiException)) {
print("Exception occurs when calling `FakeRegularExpression`:")
dput(result$ApiException$toString())
# error object
dput(result$ApiException$error_object)
}
# This endpoint doesn't return data
```
### Parameters
Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**reg_exp_test** | **character**| dummy required 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 | - |