Skip to content

[R] fix to-list and to-json functionality #20132

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

Merged
merged 7 commits into from
Nov 28, 2024
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 @@ -456,7 +456,7 @@
local_var_body <- `{{paramName}}`$toJSONString()
{{/isArray}}
} else {
body <- NULL
local_var_body <- NULL
}

{{/bodyParams}}
Expand Down
29 changes: 17 additions & 12 deletions modules/openapi-generator/src/main/resources/r/modelAnyOf.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -86,27 +86,32 @@
},

#' @description
#' Serialize {{{classname}}} to JSON string.
#' Convert to an R object. This method is deprecated. Use `toSimpleType()` instead.
toJSON = function() {
.Deprecated(new = "toSimpleType", msg = "Use the '$toSimpleType()' method instead since that is more clearly named. Use '$toJSONString()' to get a JSON string")
return(self$toSimpleType())
},

#' @description
#' Convert {{{classname}}} to a base R type
#'
#' @return JSON string representation of the {{{classname}}}.
toJSONString = function() {
#' @return A base R type, e.g. a list or numeric/character array.
toSimpleType = function() {
if (!is.null(self$actual_instance)) {
as.character(jsonlite::minify((self$actual_instance$toJSONString())))
return(self$actual_instance$toSimpleType())
} else {
NULL
}
},

#' @description
#' Serialize {{{classname}}} to JSON.
#' Serialize {{{classname}}} to JSON string.
#'
#' @return JSON representation of the {{{classname}}}.
toJSON = function() {
if (!is.null(self$actual_instance)) {
self$actual_instance$toJSON()
} else {
NULL
}
#' @param ... Parameters passed to `jsonlite::toJSON`
#' @return JSON string representation of the {{{classname}}}.
toJSONString = function(...) {
json <- jsonlite::toJSON(self$toSimpleType(), auto_unbox = TRUE, ...)
return(as.character(jsonlite::minify(json)))
},

#' @description
Expand Down
22 changes: 15 additions & 7 deletions modules/openapi-generator/src/main/resources/r/modelEnum.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,18 @@
},

#' @description
#' To JSON String
#'
#' @return {{{classname}}} in JSON format
#' Convert to an R object. This method is deprecated. Use `toSimpleType()` instead.
toJSON = function() {
jsonlite::toJSON(private$value, auto_unbox = TRUE)
.Deprecated(new = "toSimpleType", msg = "Use the '$toSimpleType()' method instead since that is more clearly named. Use '$toJSONString()' to get a JSON string")
return(self$toSimpleType())
},

#' @description
#' Convert {{{classname}}} to a base R type
#'
#' @return A base R type, e.g. a list or numeric/character array.
toSimpleType = function() {
return(private$value)
},

#' @description
Expand All @@ -60,10 +67,11 @@
#' @description
#' To JSON String
#'
#' @param ... Parameters passed to `jsonlite::toJSON`
#' @return {{{classname}}} in JSON format
toJSONString = function() {
as.character(jsonlite::toJSON(private$value,
auto_unbox = TRUE))
toJSONString = function(...) {
json <- jsonlite::toJSON(self$toSimpleType(), auto_unbox = TRUE, ...)
return(as.character(jsonlite::minify(json)))
},

#' @description
Expand Down
111 changes: 39 additions & 72 deletions modules/openapi-generator/src/main/resources/r/modelGeneric.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,35 @@
},

#' @description
#' To JSON String
#'
#' @return {{{classname}}} in JSON format
#' Convert to an R object. This method is deprecated. Use `toSimpleType()` instead.
toJSON = function() {
.Deprecated(new = "toSimpleType", msg = "Use the '$toSimpleType()' method instead since that is more clearly named. Use '$toJSONString()' to get a JSON string")
return(self$toSimpleType())
},

#' @description
#' Convert to a List
#'
#' Convert the R6 object to a list to work more easily with other tooling.
#'
#' @return {{{classname}}} as a base R list.
#' @examples
#' # convert array of {{{classname}}} (x) to a data frame
#' \dontrun{
#' library(purrr)
#' library(tibble)
#' df <- x |> map(\(y)y$toList()) |> map(as_tibble) |> list_rbind()
#' df
#' }
toList = function() {
return(self$toSimpleType())
},

#' @description
#' Convert {{{classname}}} to a base R type
#'
#' @return A base R type, e.g. a list or numeric/character array.
toSimpleType = function() {
{{classname}}Object <- list()
{{#vars}}
if (!is.null(self$`{{name}}`)) {
Expand All @@ -217,15 +242,15 @@
self$`{{name}}`
{{/isPrimitiveType}}
{{^isPrimitiveType}}
lapply(self$`{{name}}`, function(x) x$toJSON())
lapply(self$`{{name}}`, function(x) x$toSimpleType())
{{/isPrimitiveType}}
{{/isArray}}
{{#isMap}}
{{#isPrimitiveType}}
self$`{{name}}`
{{/isPrimitiveType}}
{{^isPrimitiveType}}
lapply(self$`{{name}}`, function(x) x$toJSON())
lapply(self$`{{name}}`, function(x) x$toSimpleType())
{{/isPrimitiveType}}
{{/isMap}}
{{/isContainer}}
Expand All @@ -234,7 +259,7 @@
self$`{{name}}`
{{/isPrimitiveType}}
{{^isPrimitiveType}}
self$`{{name}}`$toJSON()
self$`{{name}}`$toSimpleType()
{{/isPrimitiveType}}
{{/isContainer}}
}
Expand All @@ -245,7 +270,7 @@
}

{{/isAdditionalPropertiesTrue}}
{{classname}}Object
return({{classname}}Object)
},

#' @description
Expand Down Expand Up @@ -304,76 +329,18 @@

#' @description
#' To JSON String
#'
#'
#' @param ... Parameters passed to `jsonlite::toJSON`
#' @return {{{classname}}} in JSON format
toJSONString = function() {
jsoncontent <- c(
{{#vars}}
if (!is.null(self$`{{name}}`)) {
sprintf(
'"{{baseName}}":
{{#isContainer}}
{{#isArray}}
{{#isPrimitiveType}}
{{#isNumeric}}[%d]{{/isNumeric}}{{^isNumeric}}[%s]{{/isNumeric}}
{{/isPrimitiveType}}
{{^isPrimitiveType}}[%s]
{{/isPrimitiveType}}
{{/isArray}}
{{#isMap}}
{{#isPrimitiveType}}
{{#isNumeric}}%d{{/isNumeric}}{{^isNumeric}}{{^isBoolean}}{{/isBoolean}}%s{{^isBoolean}}{{/isBoolean}}{{/isNumeric}}
{{/isPrimitiveType}}
{{^isPrimitiveType}}%s
{{/isPrimitiveType}}
{{/isMap}}
{{/isContainer}}
{{^isContainer}}
{{#isPrimitiveType}}
{{#isNumeric}}%d{{/isNumeric}}{{^isNumeric}}{{^isBoolean}}"{{/isBoolean}}%s{{^isBoolean}}"{{/isBoolean}}{{/isNumeric}}
{{/isPrimitiveType}}
{{^isPrimitiveType}}%s
{{/isPrimitiveType}}
{{/isContainer}}',
{{#isContainer}}
{{#isArray}}
{{#isPrimitiveType}}
paste(unlist(lapply(self$`{{{name}}}`, function(x) paste0('"', x, '"'))), collapse = ",")
{{/isPrimitiveType}}
{{^isPrimitiveType}}
paste(sapply(self$`{{{name}}}`, function(x) jsonlite::toJSON(x$toJSON(), auto_unbox = TRUE, digits = NA)), collapse = ",")
{{/isPrimitiveType}}
{{/isArray}}
{{#isMap}}
{{#isPrimitiveType}}
jsonlite::toJSON(lapply(self$`{{{name}}}`, function(x){ x }), auto_unbox = TRUE, digits = NA)
{{/isPrimitiveType}}
{{^isPrimitiveType}}
jsonlite::toJSON(lapply(self$`{{{name}}}`, function(x){ x$toJSON() }), auto_unbox = TRUE, digits = NA)
{{/isPrimitiveType}}
{{/isMap}}
{{/isContainer}}
{{^isContainer}}
{{#isPrimitiveType}}
{{#isBoolean}}tolower({{/isBoolean}}self$`{{name}}`{{#isBoolean}}){{/isBoolean}}
{{/isPrimitiveType}}
{{^isPrimitiveType}}
jsonlite::toJSON(self$`{{name}}`$toJSON(), auto_unbox = TRUE, digits = NA)
{{/isPrimitiveType}}
{{/isContainer}}
)
}{{^-last}},{{/-last}}
{{/vars}}
)
jsoncontent <- paste(jsoncontent, collapse = ",")
json_string <- as.character(jsonlite::minify(paste("{", jsoncontent, "}", sep = "")))
toJSONString = function(...) {
simple <- self$toSimpleType()
{{#isAdditionalPropertiesTrue}}
json_obj <- jsonlite::fromJSON(json_string)
for (key in names(self$additional_properties)) {
json_obj[[key]] <- self$additional_properties[[key]]
simple[[key]] <- self$additional_properties[[key]]
}
json_string <- as.character(jsonlite::minify(jsonlite::toJSON(json_obj, auto_unbox = TRUE, digits = NA)))
{{/isAdditionalPropertiesTrue}}
json <- jsonlite::toJSON(simple, auto_unbox = TRUE, digits = NA, ...)
return(as.character(jsonlite::minify(json)))
},

#' @description
Expand Down
28 changes: 19 additions & 9 deletions modules/openapi-generator/src/main/resources/r/modelOneOf.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -139,25 +139,35 @@

#' @description
#' Serialize {{{classname}}} to JSON string.
#'
#'
#' @param ... Parameters passed to `jsonlite::toJSON`
#' @return JSON string representation of the {{{classname}}}.
toJSONString = function() {
toJSONString = function(...) {
simple <- self$toSimpleType()
if (!is.null(self$actual_instance)) {
as.character(jsonlite::minify(self$actual_instance$toJSONString()))
json <- jsonlite::toJSON(simple, auto_unbox = TRUE, ...)
return(as.character(jsonlite::minify(json)))
} else {
NULL
return(NULL)
}
},

#' @description
#' Serialize {{{classname}}} to JSON.
#'
#' @return JSON representation of the {{{classname}}}.
#' Convert to an R object. This method is deprecated. Use `toSimpleType()` instead.
toJSON = function() {
.Deprecated(new = "toSimpleType", msg = "Use the '$toSimpleType()' method instead since that is more clearly named. Use '$toJSONString()' to get a JSON string")
return(self$toSimpleType())
},

#' @description
#' Convert {{{classname}}} to a base R type
#'
#' @return A base R type, e.g. a list or numeric/character array.
toSimpleType = function() {
if (!is.null(self$actual_instance)) {
self$actual_instance$toJSON()
return(self$actual_instance$toSimpleType())
} else {
NULL
return(NULL)
}
},

Expand Down
61 changes: 35 additions & 26 deletions samples/client/echo_api/r/R/bird.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,35 @@ Bird <- R6::R6Class(
},

#' @description
#' To JSON String
#'
#' @return Bird in JSON format
#' Convert to an R object. This method is deprecated. Use `toSimpleType()` instead.
toJSON = function() {
.Deprecated(new = "toSimpleType", msg = "Use the '$toSimpleType()' method instead since that is more clearly named. Use '$toJSONString()' to get a JSON string")
return(self$toSimpleType())
},

#' @description
#' Convert to a List
#'
#' Convert the R6 object to a list to work more easily with other tooling.
#'
#' @return Bird as a base R list.
#' @examples
#' # convert array of Bird (x) to a data frame
#' \dontrun{
#' library(purrr)
#' library(tibble)
#' df <- x |> map(\(y)y$toList()) |> map(as_tibble) |> list_rbind()
#' df
#' }
toList = function() {
return(self$toSimpleType())
},

#' @description
#' Convert Bird to a base R type
#'
#' @return A base R type, e.g. a list or numeric/character array.
toSimpleType = function() {
BirdObject <- list()
if (!is.null(self$`size`)) {
BirdObject[["size"]] <-
Expand All @@ -53,7 +78,7 @@ Bird <- R6::R6Class(
BirdObject[["color"]] <-
self$`color`
}
BirdObject
return(BirdObject)
},

#' @description
Expand All @@ -74,29 +99,13 @@ Bird <- R6::R6Class(

#' @description
#' To JSON String
#'
#'
#' @param ... Parameters passed to `jsonlite::toJSON`
#' @return Bird in JSON format
toJSONString = function() {
jsoncontent <- c(
if (!is.null(self$`size`)) {
sprintf(
'"size":
"%s"
',
self$`size`
)
},
if (!is.null(self$`color`)) {
sprintf(
'"color":
"%s"
',
self$`color`
)
}
)
jsoncontent <- paste(jsoncontent, collapse = ",")
json_string <- as.character(jsonlite::minify(paste("{", jsoncontent, "}", sep = "")))
toJSONString = function(...) {
simple <- self$toSimpleType()
json <- jsonlite::toJSON(simple, auto_unbox = TRUE, digits = NA, ...)
return(as.character(jsonlite::minify(json)))
},

#' @description
Expand Down
Loading
Loading