Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[R] various improvements and bug fixes #2162

Merged
merged 4 commits into from
Feb 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public String modelFileFolder() {
}

@Override
public String toVarName(String name) {
public String toParamName(String name) {
// replace - with _ e.g. created-at => created_at
name = sanitizeName(name.replaceAll("-", "_"));

Expand All @@ -207,12 +207,13 @@ public String toVarName(String name) {
if (name.matches("^\\d.*"))
name = "Var" + name;

return name;
return name.replace("_", ".");
}

@Override
public String toParamName(String name) {
return toVarName(name).replace("_", ".");
public String toVarName(String name) {
// don't do anything as we'll put property name inside ` `, e.g. `date-time`
return name;
}

@Override
Expand Down Expand Up @@ -494,7 +495,7 @@ public void setParameterExampleValue(CodegenParameter p) {
example = "File.new('" + escapeText(example) + "')";
} else if (!languageSpecificPrimitives.contains(type)) {
// type is a model class, e.g. User
example = type + "$new";
example = type + "$new()";
}

if (example == null) {
Expand Down
32 changes: 15 additions & 17 deletions modules/openapi-generator/src/main/resources/r/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,25 @@
}
},
{{#operation}}
{{operationId}} = function({{#allParams}}{{paramName}}, {{/allParams}}...){
{{{operationId}}} = function({{#requiredParams}}{{paramName}}, {{/requiredParams}}{{#optionalParams}}{{paramName}}={{^defaultValue}}NULL{{/defaultValue}}{{#defaultValue}}{{{.}}}{{/defaultValue}}, {{/optionalParams}}...){
args <- list(...)
queryParams <- list()
headerParams <- character()
headerParams <- c()

{{#hasHeaderParams}}
{{#headerParams}}
if (!missing(`{{paramName}}`)) {
headerParams['{{baseName}}'] <- `{{paramName}}`
{{#requiredParams}}
if (missing(`{{paramName}}`)) {
stop("Missing required parameter `{{{paramName}}}`.")
}

{{/requiredParams}}
{{#headerParams}}
headerParams['{{baseName}}'] <- `{{paramName}}`

{{/headerParams}}
{{/hasHeaderParams}}
{{#hasQueryParams}}
{{#queryParams}}
if (!missing(`{{paramName}}`)) {
queryParams['{{baseName}}'] <- {{paramName}}
}
queryParams['{{baseName}}'] <- {{paramName}}

{{/queryParams}}
{{/hasQueryParams}}
{{#hasFormParams}}
body <- list(
{{#formParams}}
Expand Down Expand Up @@ -93,13 +91,13 @@
{{#isApiKey}}
wing328 marked this conversation as resolved.
Show resolved Hide resolved
# API key authentication
{{#isKeyInHeader}}
if ("{{{keyParamName}}}" %in% names(self$apiClient$apiKey) && nchar(self$apiClient$apiKey["{{{keyParamName}}}"]) > 0) {
headerParams['{{keyParamName}}'] <- self$apiClient$apiKey["{{keyParamName}}"]
if ("{{{keyParamName}}}" %in% names(self$apiClient$apiKeys) && nchar(self$apiClient$apiKeys["{{{keyParamName}}}"]) > 0) {
headerParams['{{keyParamName}}'] <- paste(unlist(self$apiClient$apiKeys["{{keyParamName}}"]), collapse='')
}
{{/isKeyInHeader}}
{{#isKeyInQuery}}
if ("{{{keyParamName}}}" %in% names(self$apiClient$apiKey) && nchar(self$apiClient$apiKey["{{{keyParamName}}}"]) > 0) {
queryParams['{{keyParamName}}'] <- self$apiClient$apiKey["{{keyParamName}}"]
if ("{{{keyParamName}}}" %in% names(self$apiClient$apiKeys) && nchar(self$apiClient$apiKeys["{{{keyParamName}}}"]) > 0) {
queryParams['{{keyParamName}}'] <- paste(unlist(self$apiClient$apiKeys["{{keyParamName}}"]), collapse='')
}
{{/isKeyInQuery}}
{{/isApiKey}}
Expand All @@ -109,7 +107,7 @@
{{/isOAuth}}
{{/authMethods}}

resp <- self$apiClient$callApi(url = paste0(self$apiClient$basePath, urlPath),
resp <- self$apiClient$CallApi(url = paste0(self$apiClient$basePath, urlPath),
method = "{{httpMethod}}",
queryParams = queryParams,
headerParams = headerParams,
Expand Down
17 changes: 13 additions & 4 deletions modules/openapi-generator/src/main/resources/r/api_client.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@
#' Ref: https://openapi-generator.tech
#' Do not edit the class manually.
#'
#' @importFrom httr content_type
#' @field basePath
#' @field userAgent
#' @field defaultHeaders
#' @field username
#' @field password
#' @field apiKeys
#' @field accessToken
#' @importFrom httr
#' @export
ApiClient <- R6::R6Class(
'ApiClient',
Expand All @@ -28,7 +35,7 @@ ApiClient <- R6::R6Class(
# password (HTTP basic authentication)
password = NULL,
# API keys
apiKeys = list(),
apiKeys = NULL,
# Access token
accessToken = NULL,
# constructor
Expand All @@ -55,13 +62,15 @@ ApiClient <- R6::R6Class(

if (!is.null(apiKeys)) {
self$apiKeys <- apiKeys
} else {
self$apiKeys <- list()
}

if (!is.null(userAgent)) {
self$`userAgent` <- '{{#httpUserAgent}}{{{.}}}{{/httpUserAgent}}{{^httpUserAgent}}OpenAPI-Generator/{{{packageVersion}}}/r{{/httpUserAgent}}'
self$`userAgent` <- userAgent
}
},
callApi = function(url, method, queryParams, headerParams, body, ...){
CallApi = function(url, method, queryParams, headerParams, body, ...){
headers <- httr::add_headers(c(headerParams, self$defaultHeaders))

if (method == "GET") {
Expand Down
26 changes: 23 additions & 3 deletions modules/openapi-generator/src/main/resources/r/api_doc.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,25 @@ var.{{{paramName}}} <- {{{example}}} # {{{dataType}}} | {{{description}}}
{{#summary}}
#{{{.}}}
{{/summary}}
{{#returnType}}result = {{/returnType}}{{{classname}}}${{{operationId}}}({{#requiredParams}}var.{{{paramName}}}{{^-last}}, {{/-last}}{{/requiredParams}}{{#optionalParams}}{{#-first}}{{#requiredParams.0}}, {{/requiredParams.0}}{{/-first}}{{{paramName}}}=var.{{{paramName}}}{{^-last}}, {{/-last}}{{/optionalParams}})
api.instance <- {{{classname}}}$new()
{{#hasAuthMethods}}
{{#authMethods}}
{{#isBasic}}
# Configure HTTP basic authorization: {{{name}}}
api.instance$apiClient$username <- 'TODO_YOUR_USERNAME';
api.instance$apiClient$password <- 'TODO_YOUR_PASSWORD';
{{/isBasic}}
{{#isApiKey}}
# Configure API key authorization: {{{name}}}
api.instance$apiClient$apiKeys['{{{keyParamName}}}'] <- 'TODO_YOUR_API_KEY';
{{/isApiKey}}
{{#isOAuth}}
# Configure OAuth2 access token for authorization: {{{name}}}
api.instance$apiClient$accessToken <- 'TODO_YOUR_ACCESS_TOKEN';
{{/isOAuth}}
{{/authMethods}}
{{/hasAuthMethods}}
{{#returnType}}result <- {{/returnType}}api.instance${{{operationId}}}({{#requiredParams}}var.{{{paramName}}}{{^-last}}, {{/-last}}{{/requiredParams}}{{#optionalParams}}{{#-first}}{{#requiredParams.0}}, {{/requiredParams.0}}{{/-first}}{{{paramName}}}=var.{{{paramName}}}{{^-last}}, {{/-last}}{{/optionalParams}})
{{#returnType}}
dput(result)
{{/returnType}}
Expand All @@ -38,8 +56,10 @@ dput(result)
{{^allParams}}This endpoint does not need any parameter.{{/allParams}}{{#allParams}}{{#-last}}
Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------{{/-last}}{{/allParams}}
{{#allParams}} **{{paramName}}** | {{#isPrimitiveType}}**{{dataType}}**{{/isPrimitiveType}}{{^isPrimitiveType}}{{#isFile}}**{{dataType}}**{{/isFile}}{{^isFile}}[**{{dataType}}**]({{baseType}}.md){{/isFile}}{{/isPrimitiveType}}| {{description}} | {{^required}}[optional] {{/required}}{{#defaultValue}}[default to {{defaultValue}}]{{/defaultValue}}
{{/allParams}}
{{#requiredParams}} **{{paramName}}** | {{#isPrimitiveType}}**{{dataType}}**{{/isPrimitiveType}}{{^isPrimitiveType}}{{#isFile}}**{{dataType}}**{{/isFile}}{{^isFile}}[**{{dataType}}**]({{baseType}}.md){{/isFile}}{{/isPrimitiveType}}| {{description}} | {{^required}}[optional] {{/required}}{{#defaultValue}}[default to {{defaultValue}}]{{/defaultValue}}
{{/requiredParams}}
{{#optionalParams}} **{{paramName}}** | {{#isPrimitiveType}}**{{dataType}}**{{/isPrimitiveType}}{{^isPrimitiveType}}{{#isFile}}**{{dataType}}**{{/isFile}}{{^isFile}}[**{{dataType}}**]({{baseType}}.md){{/isFile}}{{/isPrimitiveType}}| {{description}} | {{^required}}[optional] {{/required}}{{#defaultValue}}[default to {{defaultValue}}]{{/defaultValue}}
{{/optionalParams}}

### Return type

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@ api.instance <- {{{classname}}}$new()
{{#operation}}
test_that("{{{operationId}}}", {
# tests for {{operationId}}
# base path: {{{basePath}}}
{{#summary}}
# {{summary}}
{{/summary}}
{{#notes}}
# {{notes}}
{{/notes}}
{{#allParams}}{{#required}} # @param {{paramName}} {{description}}
{{/required}}{{/allParams}} # @param [Hash] opts the optional parameters
{{#allParams}}{{^required}} # @option opts [{{{dataType}}}] :{{paramName}} {{description}}
{{/required}}{{/allParams}} # @return [{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}]
{{#allParams}}
# @param {{{dataType}}} {{{paramName}}} {{{description}}} {{^required}} (optional){{/required}}
{{/allParams}}
# @return [{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}]

# uncomment below to test the operation
#expect_equal(result, "EXPECTED_RESULT")
Expand Down
17 changes: 13 additions & 4 deletions samples/client/petstore/R/R/api_client.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,14 @@
#' Ref: https://openapi-generator.tech
#' Do not edit the class manually.
#'
#' @importFrom httr content_type
#' @field basePath
#' @field userAgent
#' @field defaultHeaders
#' @field username
#' @field password
#' @field apiKeys
#' @field accessToken
#' @importFrom httr
#' @export
ApiClient <- R6::R6Class(
'ApiClient',
Expand All @@ -35,7 +42,7 @@ ApiClient <- R6::R6Class(
# password (HTTP basic authentication)
password = NULL,
# API keys
apiKeys = list(),
apiKeys = NULL,
# Access token
accessToken = NULL,
# constructor
Expand All @@ -62,13 +69,15 @@ ApiClient <- R6::R6Class(

if (!is.null(apiKeys)) {
self$apiKeys <- apiKeys
} else {
self$apiKeys <- list()
}

if (!is.null(userAgent)) {
self$`userAgent` <- 'OpenAPI-Generator/1.0.0/r'
self$`userAgent` <- userAgent
}
},
callApi = function(url, method, queryParams, headerParams, body, ...){
CallApi = function(url, method, queryParams, headerParams, body, ...){
headers <- httr::add_headers(c(headerParams, self$defaultHeaders))

if (method == "GET") {
Expand Down
Loading