Skip to content

Commit 9c52be0

Browse files
committed
[r client] fix to-list and to-json functionality
1 parent 4c5a57f commit 9c52be0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+2343
-3118
lines changed

modules/openapi-generator/src/main/resources/r/modelGeneric.mustache

Lines changed: 34 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,25 @@
203203
},
204204
205205
#' @description
206-
#' To JSON String
207-
#'
208-
#' @return {{{classname}}} in JSON format
206+
#' Convert to a list. This method was misnamed, it actually returns a list. Use `toList()` instead.
209207
toJSON = function() {
208+
.Deprecated(new = "toList", msg = "Use the '$toList()' method instead since that is more learly named. Use '$toJSONstring()' to get a JSON string")
209+
return(self$toList())
210+
},
211+
212+
#' @description
213+
#' Convert to a List
214+
#'
215+
#' Convert the R6 object to a list to work more easily with other tooling.
216+
#'
217+
#' @return {{{classname}}} as a base R list.
218+
#' @examples
219+
#' # convert array of {{{classname}}} (x) to a data frame
220+
#' \dontrun{
221+
#' df <- x |> purrr::map_dfr(\(y)y$toList())
222+
#' df
223+
#' }
224+
toList = function() {
210225
{{classname}}Object <- list()
211226
{{#vars}}
212227
if (!is.null(self$`{{name}}`)) {
@@ -217,15 +232,15 @@
217232
self$`{{name}}`
218233
{{/isPrimitiveType}}
219234
{{^isPrimitiveType}}
220-
lapply(self$`{{name}}`, function(x) x$toJSON())
235+
lapply(self$`{{name}}`, function(x) x$toList())
221236
{{/isPrimitiveType}}
222237
{{/isArray}}
223238
{{#isMap}}
224239
{{#isPrimitiveType}}
225240
self$`{{name}}`
226241
{{/isPrimitiveType}}
227242
{{^isPrimitiveType}}
228-
lapply(self$`{{name}}`, function(x) x$toJSON())
243+
lapply(self$`{{name}}`, function(x) x$toList())
229244
{{/isPrimitiveType}}
230245
{{/isMap}}
231246
{{/isContainer}}
@@ -234,7 +249,7 @@
234249
self$`{{name}}`
235250
{{/isPrimitiveType}}
236251
{{^isPrimitiveType}}
237-
self$`{{name}}`$toJSON()
252+
self$`{{name}}`$toList()
238253
{{/isPrimitiveType}}
239254
{{/isContainer}}
240255
}
@@ -245,7 +260,7 @@
245260
}
246261
247262
{{/isAdditionalPropertiesTrue}}
248-
{{classname}}Object
263+
return({{classname}}Object)
249264
},
250265
251266
#' @description
@@ -304,76 +319,24 @@
304319

305320
#' @description
306321
#' To JSON String
307-
#'
322+
#'
323+
#' @param minify Logical. If `TRUE` remove all indentation and white space
324+
#' @param ... Parameters passed to `jsonlite::toJSON`
308325
#' @return {{{classname}}} in JSON format
309-
toJSONString = function() {
310-
jsoncontent <- c(
311-
{{#vars}}
312-
if (!is.null(self$`{{name}}`)) {
313-
sprintf(
314-
'"{{baseName}}":
315-
{{#isContainer}}
316-
{{#isArray}}
317-
{{#isPrimitiveType}}
318-
{{#isNumeric}}[%d]{{/isNumeric}}{{^isNumeric}}[%s]{{/isNumeric}}
319-
{{/isPrimitiveType}}
320-
{{^isPrimitiveType}}[%s]
321-
{{/isPrimitiveType}}
322-
{{/isArray}}
323-
{{#isMap}}
324-
{{#isPrimitiveType}}
325-
{{#isNumeric}}%d{{/isNumeric}}{{^isNumeric}}{{^isBoolean}}{{/isBoolean}}%s{{^isBoolean}}{{/isBoolean}}{{/isNumeric}}
326-
{{/isPrimitiveType}}
327-
{{^isPrimitiveType}}%s
328-
{{/isPrimitiveType}}
329-
{{/isMap}}
330-
{{/isContainer}}
331-
{{^isContainer}}
332-
{{#isPrimitiveType}}
333-
{{#isNumeric}}%d{{/isNumeric}}{{^isNumeric}}{{^isBoolean}}"{{/isBoolean}}%s{{^isBoolean}}"{{/isBoolean}}{{/isNumeric}}
334-
{{/isPrimitiveType}}
335-
{{^isPrimitiveType}}%s
336-
{{/isPrimitiveType}}
337-
{{/isContainer}}',
338-
{{#isContainer}}
339-
{{#isArray}}
340-
{{#isPrimitiveType}}
341-
paste(unlist(lapply(self$`{{{name}}}`, function(x) paste0('"', x, '"'))), collapse = ",")
342-
{{/isPrimitiveType}}
343-
{{^isPrimitiveType}}
344-
paste(sapply(self$`{{{name}}}`, function(x) jsonlite::toJSON(x$toJSON(), auto_unbox = TRUE, digits = NA)), collapse = ",")
345-
{{/isPrimitiveType}}
346-
{{/isArray}}
347-
{{#isMap}}
348-
{{#isPrimitiveType}}
349-
jsonlite::toJSON(lapply(self$`{{{name}}}`, function(x){ x }), auto_unbox = TRUE, digits = NA)
350-
{{/isPrimitiveType}}
351-
{{^isPrimitiveType}}
352-
jsonlite::toJSON(lapply(self$`{{{name}}}`, function(x){ x$toJSON() }), auto_unbox = TRUE, digits = NA)
353-
{{/isPrimitiveType}}
354-
{{/isMap}}
355-
{{/isContainer}}
356-
{{^isContainer}}
357-
{{#isPrimitiveType}}
358-
{{#isBoolean}}tolower({{/isBoolean}}self$`{{name}}`{{#isBoolean}}){{/isBoolean}}
359-
{{/isPrimitiveType}}
360-
{{^isPrimitiveType}}
361-
jsonlite::toJSON(self$`{{name}}`$toJSON(), auto_unbox = TRUE, digits = NA)
362-
{{/isPrimitiveType}}
363-
{{/isContainer}}
364-
)
365-
}{{^-last}},{{/-last}}
366-
{{/vars}}
367-
)
368-
jsoncontent <- paste(jsoncontent, collapse = ",")
369-
json_string <- as.character(jsonlite::minify(paste("{", jsoncontent, "}", sep = "")))
326+
toJSONString = function(minify = TRUE, ...) {
327+
json_obj <- self$toList()
328+
370329
{{#isAdditionalPropertiesTrue}}
371-
json_obj <- jsonlite::fromJSON(json_string)
372330
for (key in names(self$additional_properties)) {
373331
json_obj[[key]] <- self$additional_properties[[key]]
374332
}
375-
json_string <- as.character(jsonlite::minify(jsonlite::toJSON(json_obj, auto_unbox = TRUE, digits = NA)))
376333
{{/isAdditionalPropertiesTrue}}
334+
335+
json_string <- jsonlite::toJSON(json_obj, auto_unbox = TRUE, digits = NA, ...)
336+
if (minify) {
337+
return(jsonlite::minify(json_string))
338+
}
339+
return(json_string)
377340
},
378341

379342
#' @description

samples/client/echo_api/r/R/bird.R

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,25 @@ Bird <- R6::R6Class(
4040
},
4141

4242
#' @description
43-
#' To JSON String
44-
#'
45-
#' @return Bird in JSON format
43+
#' Convert to a list. This method was misnamed, it actually returns a list. Use `toList()` instead.
4644
toJSON = function() {
45+
.Deprecated(new = "toList", msg = "Use the '$toList()' method instead since that is more learly named. Use '$toJSONstring()' to get a JSON string")
46+
return(self$toList())
47+
},
48+
49+
#' @description
50+
#' Convert to a List
51+
#'
52+
#' Convert the R6 object to a list to work more easily with other tooling.
53+
#'
54+
#' @return Bird as a base R list.
55+
#' @examples
56+
#' # convert array of Bird (x) to a data frame
57+
#' \dontrun{
58+
#' df <- x |> purrr::map_dfr(\(y)y$toList())
59+
#' df
60+
#' }
61+
toList = function() {
4762
BirdObject <- list()
4863
if (!is.null(self$`size`)) {
4964
BirdObject[["size"]] <-
@@ -53,7 +68,7 @@ Bird <- R6::R6Class(
5368
BirdObject[["color"]] <-
5469
self$`color`
5570
}
56-
BirdObject
71+
return(BirdObject)
5772
},
5873

5974
#' @description
@@ -74,29 +89,19 @@ Bird <- R6::R6Class(
7489

7590
#' @description
7691
#' To JSON String
77-
#'
92+
#'
93+
#' @param minify Logical. If `TRUE` remove all indentation and white space
94+
#' @param ... Parameters passed to `jsonlite::toJSON`
7895
#' @return Bird in JSON format
79-
toJSONString = function() {
80-
jsoncontent <- c(
81-
if (!is.null(self$`size`)) {
82-
sprintf(
83-
'"size":
84-
"%s"
85-
',
86-
self$`size`
87-
)
88-
},
89-
if (!is.null(self$`color`)) {
90-
sprintf(
91-
'"color":
92-
"%s"
93-
',
94-
self$`color`
95-
)
96-
}
97-
)
98-
jsoncontent <- paste(jsoncontent, collapse = ",")
99-
json_string <- as.character(jsonlite::minify(paste("{", jsoncontent, "}", sep = "")))
96+
toJSONString = function(minify = TRUE, ...) {
97+
json_obj <- self$toList()
98+
99+
100+
json_string <- jsonlite::toJSON(json_obj, auto_unbox = TRUE, digits = NA, ...)
101+
if (minify) {
102+
return(jsonlite::minify(json_string))
103+
}
104+
return(json_string)
100105
},
101106

102107
#' @description

samples/client/echo_api/r/R/category.R

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,25 @@ Category <- R6::R6Class(
4040
},
4141

4242
#' @description
43-
#' To JSON String
44-
#'
45-
#' @return Category in JSON format
43+
#' Convert to a list. This method was misnamed, it actually returns a list. Use `toList()` instead.
4644
toJSON = function() {
45+
.Deprecated(new = "toList", msg = "Use the '$toList()' method instead since that is more learly named. Use '$toJSONstring()' to get a JSON string")
46+
return(self$toList())
47+
},
48+
49+
#' @description
50+
#' Convert to a List
51+
#'
52+
#' Convert the R6 object to a list to work more easily with other tooling.
53+
#'
54+
#' @return Category as a base R list.
55+
#' @examples
56+
#' # convert array of Category (x) to a data frame
57+
#' \dontrun{
58+
#' df <- x |> purrr::map_dfr(\(y)y$toList())
59+
#' df
60+
#' }
61+
toList = function() {
4762
CategoryObject <- list()
4863
if (!is.null(self$`id`)) {
4964
CategoryObject[["id"]] <-
@@ -53,7 +68,7 @@ Category <- R6::R6Class(
5368
CategoryObject[["name"]] <-
5469
self$`name`
5570
}
56-
CategoryObject
71+
return(CategoryObject)
5772
},
5873

5974
#' @description
@@ -74,29 +89,19 @@ Category <- R6::R6Class(
7489

7590
#' @description
7691
#' To JSON String
77-
#'
92+
#'
93+
#' @param minify Logical. If `TRUE` remove all indentation and white space
94+
#' @param ... Parameters passed to `jsonlite::toJSON`
7895
#' @return Category in JSON format
79-
toJSONString = function() {
80-
jsoncontent <- c(
81-
if (!is.null(self$`id`)) {
82-
sprintf(
83-
'"id":
84-
%d
85-
',
86-
self$`id`
87-
)
88-
},
89-
if (!is.null(self$`name`)) {
90-
sprintf(
91-
'"name":
92-
"%s"
93-
',
94-
self$`name`
95-
)
96-
}
97-
)
98-
jsoncontent <- paste(jsoncontent, collapse = ",")
99-
json_string <- as.character(jsonlite::minify(paste("{", jsoncontent, "}", sep = "")))
96+
toJSONString = function(minify = TRUE, ...) {
97+
json_obj <- self$toList()
98+
99+
100+
json_string <- jsonlite::toJSON(json_obj, auto_unbox = TRUE, digits = NA, ...)
101+
if (minify) {
102+
return(jsonlite::minify(json_string))
103+
}
104+
return(json_string)
100105
},
101106

102107
#' @description

0 commit comments

Comments
 (0)