Bugfix/rheaders (#1965)

* debug headers in R client

* fixes to R client

* petstore samples

* missing space

* other space :)
This commit is contained in:
Joke Durnez
2019-01-26 01:27:24 -08:00
committed by William Cheng
parent 9ec594eec5
commit 1a07bd6573
15 changed files with 495 additions and 333 deletions

View File

@@ -74,37 +74,29 @@ public class RClientCodegen extends DefaultCodegen implements CodegenConfig {
)
);
defaultIncludes = new HashSet<String>(
Arrays.asList(
"map",
"array")
);
languageSpecificPrimitives = new HashSet<String>(
Arrays.asList(
"Integer",
"Numeric",
"Character")
);
instantiationTypes.clear();
languageSpecificPrimitives.clear();
languageSpecificPrimitives.add("integer");
languageSpecificPrimitives.add("numeric");
languageSpecificPrimitives.add("character");
languageSpecificPrimitives.add("data.frame");
languageSpecificPrimitives.add("object");
typeMapping.clear();
typeMapping.put("integer", "Integer");
typeMapping.put("long", "Integer");
typeMapping.put("number", "Numeric");
typeMapping.put("float", "Numeric");
typeMapping.put("double", "Numeric");
typeMapping.put("boolean", "Character");
typeMapping.put("string", "Character");
typeMapping.put("UUID", "Character");
typeMapping.put("date", "Character");
typeMapping.put("DateTime", "Character");
typeMapping.put("password", "Character");
typeMapping.put("integer", "integer");
typeMapping.put("long", "integer");
typeMapping.put("number", "numeric");
typeMapping.put("float", "numeric");
typeMapping.put("double", "numeric");
typeMapping.put("boolean", "character");
typeMapping.put("string", "character");
typeMapping.put("UUID", "character");
typeMapping.put("date", "character");
typeMapping.put("DateTime", "character");
typeMapping.put("password", "character");
typeMapping.put("file", "data.frame");
typeMapping.put("binary", "data.frame");
typeMapping.put("ByteArray", "Character");
typeMapping.put("object", "object");
typeMapping.put("ByteArray", "character");
typeMapping.put("map", "object");
cliOptions.clear();
cliOptions.add(new CliOption(CodegenConstants.PACKAGE_NAME, "R package name (convention: lowercase).")
@@ -370,8 +362,7 @@ public class RClientCodegen extends DefaultCodegen implements CodegenConfig {
@Override
protected boolean needToImport(String type) {
return !defaultIncludes.contains(type)
&& !languageSpecificPrimitives.contains(type);
return !languageSpecificPrimitives.contains(type);
}
public void setPackageName(String packageName) {

View File

@@ -92,15 +92,20 @@
headerParams = headerParams,
body = body,
...)
if (httr::status_code(resp) >= 200 && httr::status_code(resp) <= 299) {
{{#returnType}}
returnObject <- {{returnType}}$new()
result <- returnObject$fromJSON(httr::content(resp, "text", encoding = "UTF-8"))
Response$new(returnObject, resp)
{{/returnType}}
{{#isPrimitiveType}}
returnObject <- {{returnType}}$new()
result <- returnObject$fromJSON(httr::content(resp, "text", encoding = "UTF-8"))
Response$new(returnObject, resp)
{{/isPrimitiveType}}
{{^isPrimitiveType}}
jsonlite::fromJSON(httr::content(resp, "text", encoding = "UTF-8"))
{{/isPrimitiveType}}
{{/returnType}}
{{^returnType}}
# void response, no need to return anything
# void response, no need to return anything
{{/returnType}}
} else if (httr::status_code(resp) >= 400 && httr::status_code(resp) <= 499) {
Response$new("API client error", resp)

View File

@@ -36,19 +36,19 @@ ApiClient <- R6::R6Class(
self$`userAgent` <- '{{#httpUserAgent}}{{{.}}}{{/httpUserAgent}}{{^httpUserAgent}}OpenAPI-Generator/{{{packageVersion}}}/r{{/httpUserAgent}}'
},
callApi = function(url, method, queryParams, headerParams, body, ...){
headers <- httr::add_headers(headerParams)
headers <- httr::add_headers(c(headerParams, self$defaultHeaders))
if (method == "GET") {
httr::GET(url, queryParams, headers, ...)
}
else if (method == "POST") {
httr::POST(url, queryParams, headers, body = body, ...)
httr::POST(url, queryParams, headers, body = body, content_type("application/json"), ...)
}
else if (method == "PUT") {
httr::PUT(url, queryParams, headers, body = body, ...)
httr::PUT(url, queryParams, headers, body = body, content_type("application/json"), ...)
}
else if (method == "PATCH") {
httr::PATCH(url, queryParams, headers, body = body, ...)
httr::PATCH(url, queryParams, headers, body = body, content_type("application/json"), ...)
}
else if (method == "HEAD") {
httr::HEAD(url, queryParams, headers, ...)
@@ -61,4 +61,4 @@ ApiClient <- R6::R6Class(
}
}
)
)
)

View File

@@ -21,40 +21,40 @@
{{#vars}}
if (!missing(`{{baseName}}`)) {
{{^isListContainer}}
{{#isInteger}}
stopifnot(is.numeric(`{{baseName}}`), length(`{{baseName}}`) == 1)
{{/isInteger}}
{{#isLong}}
stopifnot(is.numeric(`{{baseName}}`), length(`{{baseName}}`) == 1)
{{/isLong}}
{{#isFloat}}
stopifnot(is.numeric(`{{baseName}}`), length(`{{baseName}}`) == 1)
{{/isFloat}}
{{#isDouble}}
stopifnot(is.numeric(`{{baseName}}`), length(`{{baseName}}`) == 1)
{{/isDouble}}
{{#isString}}
stopifnot(is.character(`{{baseName}}`), length(`{{baseName}}`) == 1)
{{/isString}}
{{#isDate}}
stopifnot(is.character(`{{baseName}}`), length(`{{baseName}}`) == 1)
{{/isDate}}
{{#isDateTime}}
stopifnot(is.character(`{{baseName}}`), length(`{{baseName}}`) == 1)
{{/isDateTime}}
{{^isPrimitiveType}}
stopifnot(R6::is.R6(`{{baseName}}`))
{{/isPrimitiveType}}
{{#isInteger}}
stopifnot(is.numeric(`{{baseName}}`), length(`{{baseName}}`) == 1)
{{/isInteger}}
{{#isLong}}
stopifnot(is.numeric(`{{baseName}}`), length(`{{baseName}}`) == 1)
{{/isLong}}
{{#isFloat}}
stopifnot(is.numeric(`{{baseName}}`), length(`{{baseName}}`) == 1)
{{/isFloat}}
{{#isDouble}}
stopifnot(is.numeric(`{{baseName}}`), length(`{{baseName}}`) == 1)
{{/isDouble}}
{{#isString}}
stopifnot(is.character(`{{baseName}}`), length(`{{baseName}}`) == 1)
{{/isString}}
{{#isDate}}
stopifnot(is.character(`{{baseName}}`), length(`{{baseName}}`) == 1)
{{/isDate}}
{{#isDateTime}}
stopifnot(is.character(`{{baseName}}`), length(`{{baseName}}`) == 1)
{{/isDateTime}}
{{^isPrimitiveType}}
stopifnot(R6::is.R6(`{{baseName}}`))
{{/isPrimitiveType}}
{{/isListContainer}}
{{#isListContainer}}
{{#isPrimitiveType}}
stopifnot(is.list(`{{baseName}}`), length(`{{baseName}}`) != 0)
lapply(`{{baseName}}`, function(x) stopifnot(is.character(x)))
{{/isPrimitiveType}}
{{^isPrimitiveType}}
stopifnot(is.list(`{{baseName}}`), length(`{{baseName}}`) != 0)
lapply(`{{baseName}}`, function(x) stopifnot(R6::is.R6(x)))
{{/isPrimitiveType}}
{{#isPrimitiveType}}
stopifnot(is.vector(`{{baseName}}`), length(`{{baseName}}`) != 0)
sapply(`{{baseName}}`, function(x) stopifnot(is.character(x)))
{{/isPrimitiveType}}
{{^isPrimitiveType}}
stopifnot(is.vector(`{{baseName}}`), length(`{{baseName}}`) != 0)
sapply(`{{baseName}}`, function(x) stopifnot(R6::is.R6(x)))
{{/isPrimitiveType}}
{{/isListContainer}}
self$`{{baseName}}` <- `{{baseName}}`
}
@@ -64,7 +64,23 @@
{{classname}}Object <- list()
{{#vars}}
if (!is.null(self$`{{baseName}}`)) {
{{classname}}Object[['{{baseName}}']] <- {{#isListContainer}}{{#isPrimitiveType}}self$`{{baseName}}`{{/isPrimitiveType}}{{^isPrimitiveType}}lapply(self$`{{baseName}}`, function(x) x$toJSON()){{/isPrimitiveType}}{{/isListContainer}}{{^isListContainer}}self$`{{baseName}}`{{^isPrimitiveType}}$toJSON(){{/isPrimitiveType}}{{/isListContainer}}
{{classname}}Object[['{{baseName}}']] <-
{{#isListContainer}}
{{#isPrimitiveType}}
self$`{{baseName}}`
{{/isPrimitiveType}}
{{^isPrimitiveType}}
sapply(self$`{{baseName}}`, function(x) x$toJSON())
{{/isPrimitiveType}}
{{/isListContainer}}
{{^isListContainer}}
{{#isPrimitiveType}}
self$`{{baseName}}`
{{/isPrimitiveType}}
{{^isPrimitiveType}}
self$`{{baseName}}`$toJSON()
{{/isPrimitiveType}}
{{/isListContainer}}
}
{{/vars}}
@@ -74,63 +90,94 @@
{{classname}}Object <- jsonlite::fromJSON({{classname}}Json)
{{#vars}}
if (!is.null({{classname}}Object$`{{baseName}}`)) {
{{#isPrimitiveType}}
self$`{{baseName}}` <- {{classname}}Object$`{{baseName}}`
{{/isPrimitiveType}}
{{^isPrimitiveType}}
{{#isListContainer}}
self$`{{baseName}}` <- lapply({{classname}}Object$`{{baseName}}`, function(x) {
{{baseName}}Object <- {{dataType}}$new()
{{baseName}}Object$fromJSON(jsonlite::toJSON(x, auto_unbox = TRUE))
{{baseName}}Object
})
{{#isPrimitiveType}}
self$`{{baseName}}` <- {{classname}}Object$`{{baseName}}`
{{/isPrimitiveType}}
{{^isPrimitiveType}}
self$`{{baseName}}` <- sapply({{classname}}Object$`{{baseName}}`, function(x) {
{{baseName}}Object <- {{dataType}}$new()
{{baseName}}Object$fromJSON(jsonlite::toJSON(x, auto_unbox = TRUE))
{{baseName}}Object
})
{{/isPrimitiveType}}
{{/isListContainer}}
{{^isListContainer}}
{{baseName}}Object <- {{dataType}}$new()
{{baseName}}Object$fromJSON(jsonlite::toJSON({{classname}}Object${{baseName}}, auto_unbox = TRUE))
self$`{{baseName}}` <- {{baseName}}Object
{{#isPrimitiveType}}
self$`{{baseName}}` <- {{classname}}Object$`{{baseName}}`
{{/isPrimitiveType}}
{{^isPrimitiveType}}
{{baseName}}Object <- {{dataType}}$new()
{{baseName}}Object$fromJSON(jsonlite::toJSON({{classname}}Object${{baseName}}, auto_unbox = TRUE))
self$`{{baseName}}` <- {{baseName}}Object
{{/isPrimitiveType}}
{{/isListContainer}}
{{/isPrimitiveType}}
}
{{/vars}}
},
toJSONString = function() {
sprintf(
outstring <- sprintf(
'{
{{#vars}}
"{{baseName}}": {{#isListContainer}}[{{/isListContainer}}{{#isPrimitiveType}}{{#isNumeric}}%d{{/isNumeric}}{{^isNumeric}}%s{{/isNumeric}}{{/isPrimitiveType}}{{^isPrimitiveType}}%s{{/isPrimitiveType}}{{#isListContainer}}]{{/isListContainer}}{{#hasMore}},{{/hasMore}}
"{{baseName}}":
{{#isListContainer}}
{{#isPrimitiveType}}
{{#isNumeric}}[%d]{{/isNumeric}}
{{^isNumeric}}["%s"]{{/isNumeric}}
{{/isPrimitiveType}}
{{^isPrimitiveType}}["%s"]{{/isPrimitiveType}}
{{/isListContainer}}
{{^isListContainer}}
{{#isPrimitiveType}}
{{#isNumeric}}%d{{/isNumeric}}
{{^isNumeric}}"%s"{{/isNumeric}}
{{/isPrimitiveType}}
{{^isPrimitiveType}}"%s"{{/isPrimitiveType}}
{{/isListContainer}}
{{#hasMore}},{{/hasMore}}
{{/vars}}
}',
{{#vars}}
{{#isListContainer}}
{{#isPrimitiveType}}
lapply(self$`{{baseName}}`, function(x) paste(paste0('"', x, '"'), sep=",")){{#hasMore}},{{/hasMore}}
{{/isPrimitiveType}}
{{^isPrimitiveType}}
lapply(self$`{{baseName}}`, function(x) paste(x$toJSON(), sep=",")){{#hasMore}},{{/hasMore}}
{{/isPrimitiveType}}
{{#isPrimitiveType}}
paste0(self$`{{baseName}}`, collapse='","'){{#hasMore}},{{/hasMore}}
{{/isPrimitiveType}}
{{^isPrimitiveType}}
paste0(sapply(self$`{{baseName}}`, function(x) x$toJSON()), collapse='","'){{#hasMore}},{{/hasMore}}
{{/isPrimitiveType}}
{{/isListContainer}}
{{^isListContainer}}
self$`{{baseName}}`{{^isPrimitiveType}}$toJSON(){{/isPrimitiveType}}{{#hasMore}},{{/hasMore}}
{{#isPrimitiveType}}
self$`{{baseName}}`{{#hasMore}},{{/hasMore}}
{{/isPrimitiveType}}
{{^isPrimitiveType}}
self$`{{baseName}}`$toJSON(){{#hasMore}},{{/hasMore}}
{{/isPrimitiveType}}
{{/isListContainer}}
{{/vars}}
)
gsub("[\r\n]| ", "", outstring)
},
fromJSONString = function({{classname}}Json) {
{{classname}}Object <- jsonlite::fromJSON({{classname}}Json)
{{#vars}}
{{#isPrimitiveType}}
self$`{{baseName}}` <- {{classname}}Object$`{{baseName}}`
{{/isPrimitiveType}}
{{^isPrimitiveType}}
{{#isListContainer}}
self$`{{baseName}}` <- lapply({{classname}}Object$`{{baseName}}`, function(x) {{dataType}}$new()$fromJSON(jsonlite::toJSON(x, auto_unbox = TRUE)))
{{#isPrimitiveType}}
self$`{{baseName}}` <- {{classname}}Object$`{{baseName}}`
{{/isPrimitiveType}}
{{^isPrimitiveType}}
self$`{{baseName}}` <- sapply({{classname}}Object$`{{baseName}}`, function(x) {{dataType}}$new()$fromJSON(jsonlite::toJSON(x, auto_unbox = TRUE)))
{{/isPrimitiveType}}
{{/isListContainer}}
{{^isListContainer}}
{{dataType}}Object <- {{dataType}}$new()
self$`{{baseName}}` <- {{dataType}}Object$fromJSON(jsonlite::toJSON({{classname}}Object${{baseName}}, auto_unbox = TRUE))
{{#isPrimitiveType}}
self$`{{baseName}}` <- {{classname}}Object$`{{baseName}}`
{{/isPrimitiveType}}
{{^isPrimitiveType}}
{{dataType}}Object <- {{dataType}}$new()
self$`{{baseName}}` <- {{dataType}}Object$fromJSON(jsonlite::toJSON({{classname}}Object${{baseName}}, auto_unbox = TRUE))
{{/isPrimitiveType}}
{{/isListContainer}}
{{/isPrimitiveType}}
{{/vars}}
}
)