Skip to content

Commit e3e06af

Browse files
authored
[R] fix to-list and to-json functionality (#20132)
* [r client] fix to-list and to-json functionality * fix type of json string * wip * refactor pr * regenerate samples * update to-dataframe example to use non-superceded tidyverse functions * fix typo
1 parent 037cb12 commit e3e06af

File tree

111 files changed

+3072
-3381
lines changed

Some content is hidden

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

111 files changed

+3072
-3381
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@
456456
local_var_body <- `{{paramName}}`$toJSONString()
457457
{{/isArray}}
458458
} else {
459-
body <- NULL
459+
local_var_body <- NULL
460460
}
461461
462462
{{/bodyParams}}

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

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,27 +86,32 @@
8686
},
8787

8888
#' @description
89-
#' Serialize {{{classname}}} to JSON string.
89+
#' Convert to an R object. This method is deprecated. Use `toSimpleType()` instead.
90+
toJSON = function() {
91+
.Deprecated(new = "toSimpleType", msg = "Use the '$toSimpleType()' method instead since that is more clearly named. Use '$toJSONString()' to get a JSON string")
92+
return(self$toSimpleType())
93+
},
94+
95+
#' @description
96+
#' Convert {{{classname}}} to a base R type
9097
#'
91-
#' @return JSON string representation of the {{{classname}}}.
92-
toJSONString = function() {
98+
#' @return A base R type, e.g. a list or numeric/character array.
99+
toSimpleType = function() {
93100
if (!is.null(self$actual_instance)) {
94-
as.character(jsonlite::minify((self$actual_instance$toJSONString())))
101+
return(self$actual_instance$toSimpleType())
95102
} else {
96103
NULL
97104
}
98105
},
99106

100107
#' @description
101-
#' Serialize {{{classname}}} to JSON.
108+
#' Serialize {{{classname}}} to JSON string.
102109
#'
103-
#' @return JSON representation of the {{{classname}}}.
104-
toJSON = function() {
105-
if (!is.null(self$actual_instance)) {
106-
self$actual_instance$toJSON()
107-
} else {
108-
NULL
109-
}
110+
#' @param ... Parameters passed to `jsonlite::toJSON`
111+
#' @return JSON string representation of the {{{classname}}}.
112+
toJSONString = function(...) {
113+
json <- jsonlite::toJSON(self$toSimpleType(), auto_unbox = TRUE, ...)
114+
return(as.character(jsonlite::minify(json)))
110115
},
111116
112117
#' @description

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,18 @@
3838
},
3939

4040
#' @description
41-
#' To JSON String
42-
#'
43-
#' @return {{{classname}}} in JSON format
41+
#' Convert to an R object. This method is deprecated. Use `toSimpleType()` instead.
4442
toJSON = function() {
45-
jsonlite::toJSON(private$value, auto_unbox = TRUE)
43+
.Deprecated(new = "toSimpleType", msg = "Use the '$toSimpleType()' method instead since that is more clearly named. Use '$toJSONString()' to get a JSON string")
44+
return(self$toSimpleType())
45+
},
46+
47+
#' @description
48+
#' Convert {{{classname}}} to a base R type
49+
#'
50+
#' @return A base R type, e.g. a list or numeric/character array.
51+
toSimpleType = function() {
52+
return(private$value)
4653
},
4754

4855
#' @description
@@ -60,10 +67,11 @@
6067
#' @description
6168
#' To JSON String
6269
#'
70+
#' @param ... Parameters passed to `jsonlite::toJSON`
6371
#' @return {{{classname}}} in JSON format
64-
toJSONString = function() {
65-
as.character(jsonlite::toJSON(private$value,
66-
auto_unbox = TRUE))
72+
toJSONString = function(...) {
73+
json <- jsonlite::toJSON(self$toSimpleType(), auto_unbox = TRUE, ...)
74+
return(as.character(jsonlite::minify(json)))
6775
},
6876
6977
#' @description

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

Lines changed: 39 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,35 @@
203203
},
204204
205205
#' @description
206-
#' To JSON String
207-
#'
208-
#' @return {{{classname}}} in JSON format
206+
#' Convert to an R object. This method is deprecated. Use `toSimpleType()` instead.
209207
toJSON = function() {
208+
.Deprecated(new = "toSimpleType", msg = "Use the '$toSimpleType()' method instead since that is more clearly named. Use '$toJSONString()' to get a JSON string")
209+
return(self$toSimpleType())
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+
#' library(purrr)
222+
#' library(tibble)
223+
#' df <- x |> map(\(y)y$toList()) |> map(as_tibble) |> list_rbind()
224+
#' df
225+
#' }
226+
toList = function() {
227+
return(self$toSimpleType())
228+
},
229+
230+
#' @description
231+
#' Convert {{{classname}}} to a base R type
232+
#'
233+
#' @return A base R type, e.g. a list or numeric/character array.
234+
toSimpleType = function() {
210235
{{classname}}Object <- list()
211236
{{#vars}}
212237
if (!is.null(self$`{{name}}`)) {
@@ -217,15 +242,15 @@
217242
self$`{{name}}`
218243
{{/isPrimitiveType}}
219244
{{^isPrimitiveType}}
220-
lapply(self$`{{name}}`, function(x) x$toJSON())
245+
lapply(self$`{{name}}`, function(x) x$toSimpleType())
221246
{{/isPrimitiveType}}
222247
{{/isArray}}
223248
{{#isMap}}
224249
{{#isPrimitiveType}}
225250
self$`{{name}}`
226251
{{/isPrimitiveType}}
227252
{{^isPrimitiveType}}
228-
lapply(self$`{{name}}`, function(x) x$toJSON())
253+
lapply(self$`{{name}}`, function(x) x$toSimpleType())
229254
{{/isPrimitiveType}}
230255
{{/isMap}}
231256
{{/isContainer}}
@@ -234,7 +259,7 @@
234259
self$`{{name}}`
235260
{{/isPrimitiveType}}
236261
{{^isPrimitiveType}}
237-
self$`{{name}}`$toJSON()
262+
self$`{{name}}`$toSimpleType()
238263
{{/isPrimitiveType}}
239264
{{/isContainer}}
240265
}
@@ -245,7 +270,7 @@
245270
}
246271
247272
{{/isAdditionalPropertiesTrue}}
248-
{{classname}}Object
273+
return({{classname}}Object)
249274
},
250275
251276
#' @description
@@ -304,76 +329,18 @@
304329

305330
#' @description
306331
#' To JSON String
307-
#'
332+
#'
333+
#' @param ... Parameters passed to `jsonlite::toJSON`
308334
#' @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 = "")))
335+
toJSONString = function(...) {
336+
simple <- self$toSimpleType()
370337
{{#isAdditionalPropertiesTrue}}
371-
json_obj <- jsonlite::fromJSON(json_string)
372338
for (key in names(self$additional_properties)) {
373-
json_obj[[key]] <- self$additional_properties[[key]]
339+
simple[[key]] <- self$additional_properties[[key]]
374340
}
375-
json_string <- as.character(jsonlite::minify(jsonlite::toJSON(json_obj, auto_unbox = TRUE, digits = NA)))
376341
{{/isAdditionalPropertiesTrue}}
342+
json <- jsonlite::toJSON(simple, auto_unbox = TRUE, digits = NA, ...)
343+
return(as.character(jsonlite::minify(json)))
377344
},
378345
379346
#' @description

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

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -139,25 +139,35 @@
139139
140140
#' @description
141141
#' Serialize {{{classname}}} to JSON string.
142-
#'
142+
#'
143+
#' @param ... Parameters passed to `jsonlite::toJSON`
143144
#' @return JSON string representation of the {{{classname}}}.
144-
toJSONString = function() {
145+
toJSONString = function(...) {
146+
simple <- self$toSimpleType()
145147
if (!is.null(self$actual_instance)) {
146-
as.character(jsonlite::minify(self$actual_instance$toJSONString()))
148+
json <- jsonlite::toJSON(simple, auto_unbox = TRUE, ...)
149+
return(as.character(jsonlite::minify(json)))
147150
} else {
148-
NULL
151+
return(NULL)
149152
}
150153
},
151154
152155
#' @description
153-
#' Serialize {{{classname}}} to JSON.
154-
#'
155-
#' @return JSON representation of the {{{classname}}}.
156+
#' Convert to an R object. This method is deprecated. Use `toSimpleType()` instead.
156157
toJSON = function() {
158+
.Deprecated(new = "toSimpleType", msg = "Use the '$toSimpleType()' method instead since that is more clearly named. Use '$toJSONString()' to get a JSON string")
159+
return(self$toSimpleType())
160+
},
161+
162+
#' @description
163+
#' Convert {{{classname}}} to a base R type
164+
#'
165+
#' @return A base R type, e.g. a list or numeric/character array.
166+
toSimpleType = function() {
157167
if (!is.null(self$actual_instance)) {
158-
self$actual_instance$toJSON()
168+
return(self$actual_instance$toSimpleType())
159169
} else {
160-
NULL
170+
return(NULL)
161171
}
162172
},
163173

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

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

4242
#' @description
43-
#' To JSON String
44-
#'
45-
#' @return Bird in JSON format
43+
#' Convert to an R object. This method is deprecated. Use `toSimpleType()` instead.
4644
toJSON = function() {
45+
.Deprecated(new = "toSimpleType", msg = "Use the '$toSimpleType()' method instead since that is more clearly named. Use '$toJSONString()' to get a JSON string")
46+
return(self$toSimpleType())
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+
#' library(purrr)
59+
#' library(tibble)
60+
#' df <- x |> map(\(y)y$toList()) |> map(as_tibble) |> list_rbind()
61+
#' df
62+
#' }
63+
toList = function() {
64+
return(self$toSimpleType())
65+
},
66+
67+
#' @description
68+
#' Convert Bird to a base R type
69+
#'
70+
#' @return A base R type, e.g. a list or numeric/character array.
71+
toSimpleType = function() {
4772
BirdObject <- list()
4873
if (!is.null(self$`size`)) {
4974
BirdObject[["size"]] <-
@@ -53,7 +78,7 @@ Bird <- R6::R6Class(
5378
BirdObject[["color"]] <-
5479
self$`color`
5580
}
56-
BirdObject
81+
return(BirdObject)
5782
},
5883

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

75100
#' @description
76101
#' To JSON String
77-
#'
102+
#'
103+
#' @param ... Parameters passed to `jsonlite::toJSON`
78104
#' @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 = "")))
105+
toJSONString = function(...) {
106+
simple <- self$toSimpleType()
107+
json <- jsonlite::toJSON(simple, auto_unbox = TRUE, digits = NA, ...)
108+
return(as.character(jsonlite::minify(json)))
100109
},
101110

102111
#' @description

0 commit comments

Comments
 (0)