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] Enum R6Class Support, closes #3367 #5728

Merged
merged 40 commits into from
Jul 1, 2020
Merged
Show file tree
Hide file tree
Changes from 34 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
ff87d33
wip: fix documentation tags
LiNk-NY Nov 19, 2019
34350d7
update model.mustache
LiNk-NY Nov 19, 2019
e9bfd30
Merge branch 'master' of github.com:LiNk-NY/openapi-generator
LiNk-NY Nov 19, 2019
0ec4e22
update template for documentation
LiNk-NY Nov 20, 2019
794306c
update mustache templates
LiNk-NY Nov 21, 2019
7270127
Merge branch 'master' of github.com:OpenApiTools/openapi-generator
LiNk-NY Nov 21, 2019
ab7daee
Merge branch 'master' of github.com:LiNk-NY/openapi-generator
LiNk-NY Nov 21, 2019
95e9a9f
update mustache templates
LiNk-NY Nov 21, 2019
456ac6a
run ./bin/r-petstore and update R pkg docs
LiNk-NY Nov 25, 2019
0cb7abd
use loadNamespace instead of package:pkgName string
LiNk-NY Nov 26, 2019
5958c01
update R package code
LiNk-NY Nov 26, 2019
d4d68ff
Merge branch 'master' of github.com:OpenApiTools/openapi-generator
LiNk-NY Dec 2, 2019
c43852f
Merge branch 'master' of github.com:OpenApiTools/openapi-generator
LiNk-NY Dec 5, 2019
92e470a
Merge branch 'master' of github.com:LiNk-NY/openapi-generator
LiNk-NY Dec 5, 2019
7528003
wip: enum
LiNk-NY Dec 5, 2019
5d7e410
update enum function
LiNk-NY Dec 7, 2019
231f03f
use httr::content to unwrap response
LiNk-NY Dec 7, 2019
011ab95
Merge branch 'master' of github.com:OpenAPItools/openapi-generator
LiNk-NY Dec 17, 2019
b575f07
Merge branch 'master' of github.com:LiNk-NY/openapi-generator
LiNk-NY Dec 17, 2019
e576586
Merge branch 'master' of github.com:LiNk-NY/openapi-generator
LiNk-NY Jan 2, 2020
d859042
Merge branch 'enum' of github.com:LiNk-NY/openapi-generator into enum
LiNk-NY Jan 2, 2020
8d78c2f
update enum table and functions
LiNk-NY Jan 3, 2020
9d178a1
include simplifyVector and auto_unbox arguments
LiNk-NY Jan 6, 2020
c5a445d
remove mapping and return strings
LiNk-NY Mar 23, 2020
4ff8abd
Merge remote-tracking branch 'origin/master' into enum
LiNk-NY Mar 23, 2020
c419f8c
use triple stash values and update enum class methods
LiNk-NY Mar 25, 2020
595b7ba
remove extra comma
LiNk-NY Mar 26, 2020
90c0dd5
minor: formatting
LiNk-NY Mar 26, 2020
06baee6
Merge remote-tracking branch 'origin/master' into enum
LiNk-NY Mar 26, 2020
de05d02
Merge remote-tracking branch 'origin/master' into enum
LiNk-NY Mar 27, 2020
7eec6f6
update and run ./bin/r-petstore.sh
LiNk-NY Mar 27, 2020
168baa7
Merge branch 'master' of https://github.com/openapitools/openapi-gene…
wing328 May 12, 2020
0580ea7
move helper for template
LiNk-NY May 12, 2020
b6314c8
update tests based on generator
LiNk-NY May 12, 2020
424a97b
Revert "update tests based on generator"
LiNk-NY May 18, 2020
c703965
Update tests with engine
LiNk-NY May 18, 2020
867eced
restore apiResponse
LiNk-NY May 19, 2020
dafb94f
restore simplifyVector to TRUE value
LiNk-NY May 19, 2020
8852f5b
update api.mustache templates
LiNk-NY May 19, 2020
5183ecf
fix and add tests to package
LiNk-NY May 19, 2020
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 @@ -163,7 +163,7 @@
apiResponse <- self${{{operationId}}}WithHttpInfo({{#allParams}}{{paramName}}, {{/allParams}}...)
resp <- apiResponse$response
if (httr::status_code(resp) >= 200 && httr::status_code(resp) <= 299) {
apiResponse$content
httr::content(resp)
LiNk-NY marked this conversation as resolved.
Show resolved Hide resolved
} else if (httr::status_code(resp) >= 300 && httr::status_code(resp) <= 399) {
apiResponse
} else if (httr::status_code(resp) >= 400 && httr::status_code(resp) <= 499) {
Expand Down
21 changes: 17 additions & 4 deletions modules/openapi-generator/src/main/resources/r/model.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,31 @@
{{>partial_header}}
#' @docType class
#' @title {{classname}}
#'
#' @description {{classname}} Class
#'
#' @format An \code{R6Class} generator object
#'
{{#vars}}
#' @field {{baseName}} {{title}} {{#isContainer}}{{#isListContainer}}list( {{/isListContainer}}{{#isMapContainer}}named list( {{/isMapContainer}}{{/isContainer}}{{^isPrimitiveType}}\link{{=<% %>=}}{<%/isPrimitiveType%><%={{ }}=%>{{#isContainer}}{{#items}}{{dataType}}{{/items}}{{/isContainer}}{{^isContainer}}{{dataType}}{{/isContainer}}{{=<% %>=}}<%^isPrimitiveType%>}<%={{ }}=%>{{/isPrimitiveType}}{{#isContainer}}{{#isListContainer}} ){{/isListContainer}}{{#isMapContainer}} ){{/isMapContainer}}{{/isContainer}} {{^required}}[optional]{{/required}}
#'
{{/vars}}
#'
#' @importFrom R6 R6Class
#' @importFrom jsonlite fromJSON toJSON
{{#isEnum}}
{{>modelEnum}}
{{/isEnum}}
{{^isEnum}}
#' @export
{{classname}} <- R6::R6Class(
'{{classname}}',
public = list(
{{#vars}}
`{{{baseName}}}` = NULL,
{{/vars}}
initialize = function({{#requiredVars}}`{{baseName}}`{{#hasMore}}, {{/hasMore}}{{/requiredVars}}{{#optionalVars}}{{#-first}}{{#requiredVars.0}}, {{/requiredVars.0}}{{/-first}}`{{baseName}}`={{#defaultValue}}{{{.}}}{{/defaultValue}}{{^defaultValue}}NULL{{/defaultValue}}{{^-last}}, {{/-last}}{{/optionalVars}}, ...){
initialize = function(
{{#requiredVars}}`{{baseName}}`, {{/requiredVars}}{{#optionalVars}}`{{baseName}}`={{#defaultValue}}{{{.}}}{{/defaultValue}}{{^defaultValue}}NULL{{/defaultValue}}, {{/optionalVars}}...
) {
local.optional.var <- list(...)
{{#requiredVars}}
if (!missing(`{{baseName}}`)) {
Expand Down Expand Up @@ -141,7 +149,8 @@
{{classname}}Object
},
fromJSON = function({{classname}}Json) {
{{classname}}Object <- jsonlite::fromJSON({{classname}}Json)
{{classname}}Object <- jsonlite::fromJSON({{classname}}Json,
simplifyVector = FALSE)
LiNk-NY marked this conversation as resolved.
Show resolved Hide resolved
{{#vars}}
if (!is.null({{classname}}Object$`{{baseName}}`)) {
{{#isContainer}}
Expand All @@ -159,6 +168,7 @@
{{/isContainer}}
}
{{/vars}}
self
},
toJSONString = function() {
jsoncontent <- c(
Expand Down Expand Up @@ -222,7 +232,8 @@
paste('{', jsoncontent, '}', sep = "")
},
fromJSONString = function({{classname}}Json) {
{{classname}}Object <- jsonlite::fromJSON({{classname}}Json)
{{classname}}Object <- jsonlite::fromJSON({{classname}}Json,
simplifyVector = FALSE)
{{#vars}}
{{! AAPI - added condition for handling container type of parameters, map and array}}
{{#isContainer}}
Expand All @@ -241,5 +252,7 @@
}
)
)
{{/isEnum}}
{{/model}}
{{/models}}

48 changes: 48 additions & 0 deletions modules/openapi-generator/src/main/resources/r/modelEnum.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#' @export
{{#allowableValues}}
{{classname}} <- R6::R6Class(
"{{classname}}",
public = list(
initialize = function(...) {
local.optional.var <- list(...)
val <- unlist(local.optional.var)
enumvec <- .parse_{{name}}()

stopifnot(length(val) == 1L)

if (!val %in% enumvec)
stop("Use one of the valid values: ",
paste0(enumvec, collapse = ", "))
private$value <- val
},
toJSON = function() {
jsonlite::toJSON(private$value, auto_unbox = TRUE)
},
fromJSON = function({{classname}}Json) {
private$value <- jsonlite::fromJSON({{classname}}Json,
simplifyVector = FALSE)
self
},
toJSONString = function() {
as.character(jsonlite::toJSON(private$value,
auto_unbox = TRUE))
},
fromJSONString = function({{classname}}Json) {
private$value <- jsonlite::fromJSON({{classname}}Json,
simplifyVector = FALSE)
self
}
),
private = list(
value = NULL
)
)

# add to utils.R
.parse_{{name}} <- function(vals) {
res <- gsub("^\\[|\\]$", "",
"{{{values}}}"
)
unlist(strsplit(res, ", "))
}
{{/allowableValues}}
16 changes: 12 additions & 4 deletions samples/client/petstore/R/R/category.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@

#' @docType class
#' @title Category
#'
#' @description Category Class
#'
#' @format An \code{R6Class} generator object
#'
#' @field id integer [optional]
#'
#' @field name character [optional]
#'
#'
#' @importFrom R6 R6Class
#' @importFrom jsonlite fromJSON toJSON
#' @export
Expand All @@ -23,7 +25,9 @@ Category <- R6::R6Class(
public = list(
`id` = NULL,
`name` = NULL,
initialize = function(`id`=NULL, `name`=NULL, ...){
initialize = function(
`id`=NULL, `name`=NULL, ...
) {
local.optional.var <- list(...)
if (!is.null(`id`)) {
stopifnot(is.numeric(`id`), length(`id`) == 1)
Expand All @@ -48,13 +52,15 @@ Category <- R6::R6Class(
CategoryObject
},
fromJSON = function(CategoryJson) {
CategoryObject <- jsonlite::fromJSON(CategoryJson)
CategoryObject <- jsonlite::fromJSON(CategoryJson,
simplifyVector = FALSE)
if (!is.null(CategoryObject$`id`)) {
self$`id` <- CategoryObject$`id`
}
if (!is.null(CategoryObject$`name`)) {
self$`name` <- CategoryObject$`name`
}
self
},
toJSONString = function() {
jsoncontent <- c(
Expand All @@ -77,10 +83,12 @@ Category <- R6::R6Class(
paste('{', jsoncontent, '}', sep = "")
},
fromJSONString = function(CategoryJson) {
CategoryObject <- jsonlite::fromJSON(CategoryJson)
CategoryObject <- jsonlite::fromJSON(CategoryJson,
simplifyVector = FALSE)
self$`id` <- CategoryObject$`id`
self$`name` <- CategoryObject$`name`
self
}
)
)

16 changes: 12 additions & 4 deletions samples/client/petstore/R/R/model_api_response.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@

#' @docType class
#' @title ModelApiResponse
#'
#' @description ModelApiResponse Class
#'
#' @format An \code{R6Class} generator object
#'
#' @field code integer [optional]
#'
#' @field type character [optional]
#'
#' @field message character [optional]
#'
#'
#' @importFrom R6 R6Class
#' @importFrom jsonlite fromJSON toJSON
#' @export
Expand All @@ -26,7 +28,9 @@ ModelApiResponse <- R6::R6Class(
`code` = NULL,
`type` = NULL,
`message` = NULL,
initialize = function(`code`=NULL, `type`=NULL, `message`=NULL, ...){
initialize = function(
`code`=NULL, `type`=NULL, `message`=NULL, ...
) {
local.optional.var <- list(...)
if (!is.null(`code`)) {
stopifnot(is.numeric(`code`), length(`code`) == 1)
Expand Down Expand Up @@ -59,7 +63,8 @@ ModelApiResponse <- R6::R6Class(
ModelApiResponseObject
},
fromJSON = function(ModelApiResponseJson) {
ModelApiResponseObject <- jsonlite::fromJSON(ModelApiResponseJson)
ModelApiResponseObject <- jsonlite::fromJSON(ModelApiResponseJson,
simplifyVector = FALSE)
if (!is.null(ModelApiResponseObject$`code`)) {
self$`code` <- ModelApiResponseObject$`code`
}
Expand All @@ -69,6 +74,7 @@ ModelApiResponse <- R6::R6Class(
if (!is.null(ModelApiResponseObject$`message`)) {
self$`message` <- ModelApiResponseObject$`message`
}
self
},
toJSONString = function() {
jsoncontent <- c(
Expand Down Expand Up @@ -98,11 +104,13 @@ ModelApiResponse <- R6::R6Class(
paste('{', jsoncontent, '}', sep = "")
},
fromJSONString = function(ModelApiResponseJson) {
ModelApiResponseObject <- jsonlite::fromJSON(ModelApiResponseJson)
ModelApiResponseObject <- jsonlite::fromJSON(ModelApiResponseJson,
simplifyVector = FALSE)
self$`code` <- ModelApiResponseObject$`code`
self$`type` <- ModelApiResponseObject$`type`
self$`message` <- ModelApiResponseObject$`message`
self
}
)
)

16 changes: 12 additions & 4 deletions samples/client/petstore/R/R/order.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@

#' @docType class
#' @title Order
#'
#' @description Order Class
#'
#' @format An \code{R6Class} generator object
#'
#' @field id integer [optional]
#'
#' @field petId integer [optional]
Expand All @@ -22,7 +25,6 @@
#'
#' @field complete character [optional]
#'
#'
#' @importFrom R6 R6Class
#' @importFrom jsonlite fromJSON toJSON
#' @export
Expand All @@ -35,7 +37,9 @@ Order <- R6::R6Class(
`shipDate` = NULL,
`status` = NULL,
`complete` = NULL,
initialize = function(`id`=NULL, `petId`=NULL, `quantity`=NULL, `shipDate`=NULL, `status`=NULL, `complete`=FALSE, ...){
initialize = function(
`id`=NULL, `petId`=NULL, `quantity`=NULL, `shipDate`=NULL, `status`=NULL, `complete`=FALSE, ...
) {
local.optional.var <- list(...)
if (!is.null(`id`)) {
stopifnot(is.numeric(`id`), length(`id`) == 1)
Expand Down Expand Up @@ -91,7 +95,8 @@ Order <- R6::R6Class(
OrderObject
},
fromJSON = function(OrderJson) {
OrderObject <- jsonlite::fromJSON(OrderJson)
OrderObject <- jsonlite::fromJSON(OrderJson,
simplifyVector = FALSE)
if (!is.null(OrderObject$`id`)) {
self$`id` <- OrderObject$`id`
}
Expand All @@ -110,6 +115,7 @@ Order <- R6::R6Class(
if (!is.null(OrderObject$`complete`)) {
self$`complete` <- OrderObject$`complete`
}
self
},
toJSONString = function() {
jsoncontent <- c(
Expand Down Expand Up @@ -160,7 +166,8 @@ Order <- R6::R6Class(
paste('{', jsoncontent, '}', sep = "")
},
fromJSONString = function(OrderJson) {
OrderObject <- jsonlite::fromJSON(OrderJson)
OrderObject <- jsonlite::fromJSON(OrderJson,
simplifyVector = FALSE)
self$`id` <- OrderObject$`id`
self$`petId` <- OrderObject$`petId`
self$`quantity` <- OrderObject$`quantity`
Expand All @@ -171,3 +178,4 @@ Order <- R6::R6Class(
}
)
)

16 changes: 12 additions & 4 deletions samples/client/petstore/R/R/pet.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@

#' @docType class
#' @title Pet
#'
#' @description Pet Class
#'
#' @format An \code{R6Class} generator object
#'
#' @field id integer [optional]
#'
#' @field category \link{Category} [optional]
Expand All @@ -22,7 +25,6 @@
#'
#' @field status character [optional]
#'
#'
#' @importFrom R6 R6Class
#' @importFrom jsonlite fromJSON toJSON
#' @export
Expand All @@ -35,7 +37,9 @@ Pet <- R6::R6Class(
`photoUrls` = NULL,
`tags` = NULL,
`status` = NULL,
initialize = function(`name`, `photoUrls`, `id`=NULL, `category`=NULL, `tags`=NULL, `status`=NULL, ...){
initialize = function(
`name`, `photoUrls`, `id`=NULL, `category`=NULL, `tags`=NULL, `status`=NULL, ...
) {
local.optional.var <- list(...)
if (!missing(`name`)) {
stopifnot(is.character(`name`), length(`name`) == 1)
Expand Down Expand Up @@ -94,7 +98,8 @@ Pet <- R6::R6Class(
PetObject
},
fromJSON = function(PetJson) {
PetObject <- jsonlite::fromJSON(PetJson)
PetObject <- jsonlite::fromJSON(PetJson,
simplifyVector = FALSE)
if (!is.null(PetObject$`id`)) {
self$`id` <- PetObject$`id`
}
Expand All @@ -115,6 +120,7 @@ Pet <- R6::R6Class(
if (!is.null(PetObject$`status`)) {
self$`status` <- PetObject$`status`
}
self
},
toJSONString = function() {
jsoncontent <- c(
Expand Down Expand Up @@ -165,7 +171,8 @@ Pet <- R6::R6Class(
paste('{', jsoncontent, '}', sep = "")
},
fromJSONString = function(PetJson) {
PetObject <- jsonlite::fromJSON(PetJson)
PetObject <- jsonlite::fromJSON(PetJson,
simplifyVector = FALSE)
self$`id` <- PetObject$`id`
self$`category` <- Category$new()$fromJSON(jsonlite::toJSON(PetObject$category, auto_unbox = TRUE, digits = NA))
self$`name` <- PetObject$`name`
Expand All @@ -176,3 +183,4 @@ Pet <- R6::R6Class(
}
)
)

Loading