Skip to content

Commit dd503a6

Browse files
committed
[R] avoid to-JSON issues when R6 classes contain lists of R6 classes
1 parent 39a3bfa commit dd503a6

75 files changed

Lines changed: 1743 additions & 18 deletions

Some content is hidden

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

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

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,15 +242,15 @@
242242
self$`{{name}}`
243243
{{/isPrimitiveType}}
244244
{{^isPrimitiveType}}
245-
lapply(self$`{{name}}`, function(x) x$toSimpleType())
245+
self$extractSimpleType(self$`{{name}}`)
246246
{{/isPrimitiveType}}
247247
{{/isArray}}
248248
{{#isMap}}
249249
{{#isPrimitiveType}}
250250
self$`{{name}}`
251251
{{/isPrimitiveType}}
252252
{{^isPrimitiveType}}
253-
lapply(self$`{{name}}`, function(x) x$toSimpleType())
253+
self$extractSimpleType(self$`{{name}}`)
254254
{{/isPrimitiveType}}
255255
{{/isMap}}
256256
{{/isContainer}}
@@ -259,7 +259,7 @@
259259
self$`{{name}}`
260260
{{/isPrimitiveType}}
261261
{{^isPrimitiveType}}
262-
self$`{{name}}`$toSimpleType()
262+
self$extractSimpleType(self$`{{name}}`)
263263
{{/isPrimitiveType}}
264264
{{/isContainer}}
265265
}
@@ -273,6 +273,29 @@
273273
return({{classname}}Object)
274274
},
275275
276+
extractSimpleType = function(x) {
277+
if (R6::is.R6(x)) {
278+
return(x$toSimpleType())
279+
} else if (!self$hasNestedR6(x)) {
280+
return(x)
281+
}
282+
lapply(x, self$extractSimpleType)
283+
},
284+
285+
hasNestedR6 = function(x) {
286+
if (R6::is.R6(x)) {
287+
return(TRUE)
288+
}
289+
if (is.list(x)) {
290+
for (item in x) {
291+
if (self$hasNestedR6(item)) {
292+
return(TRUE)
293+
}
294+
}
295+
}
296+
FALSE
297+
}
298+
276299
#' @description
277300
#' Deserialize JSON string into an instance of {{{classname}}}
278301
#'

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,29 @@ Bird <- R6::R6Class(
8181
return(BirdObject)
8282
},
8383

84+
extractSimpleType = function(x) {
85+
if (R6::is.R6(x)) {
86+
return(x$toSimpleType())
87+
} else if (!self$hasNestedR6(x)) {
88+
return(x)
89+
}
90+
lapply(x, self$extractSimpleType)
91+
},
92+
93+
hasNestedR6 = function(x) {
94+
if (R6::is.R6(x)) {
95+
return(TRUE)
96+
}
97+
if (is.list(x)) {
98+
for (item in x) {
99+
if (self$hasNestedR6(item)) {
100+
return(TRUE)
101+
}
102+
}
103+
}
104+
FALSE
105+
}
106+
84107
#' @description
85108
#' Deserialize JSON string into an instance of Bird
86109
#'

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,29 @@ Category <- R6::R6Class(
8181
return(CategoryObject)
8282
},
8383

84+
extractSimpleType = function(x) {
85+
if (R6::is.R6(x)) {
86+
return(x$toSimpleType())
87+
} else if (!self$hasNestedR6(x)) {
88+
return(x)
89+
}
90+
lapply(x, self$extractSimpleType)
91+
},
92+
93+
hasNestedR6 = function(x) {
94+
if (R6::is.R6(x)) {
95+
return(TRUE)
96+
}
97+
if (is.list(x)) {
98+
for (item in x) {
99+
if (self$hasNestedR6(item)) {
100+
return(TRUE)
101+
}
102+
}
103+
}
104+
FALSE
105+
}
106+
84107
#' @description
85108
#' Deserialize JSON string into an instance of Category
86109
#'

samples/client/echo_api/r/R/data_query.R

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,29 @@ DataQuery <- R6::R6Class(
120120
return(DataQueryObject)
121121
},
122122

123+
extractSimpleType = function(x) {
124+
if (R6::is.R6(x)) {
125+
return(x$toSimpleType())
126+
} else if (!self$hasNestedR6(x)) {
127+
return(x)
128+
}
129+
lapply(x, self$extractSimpleType)
130+
},
131+
132+
hasNestedR6 = function(x) {
133+
if (R6::is.R6(x)) {
134+
return(TRUE)
135+
}
136+
if (is.list(x)) {
137+
for (item in x) {
138+
if (self$hasNestedR6(item)) {
139+
return(TRUE)
140+
}
141+
}
142+
}
143+
FALSE
144+
}
145+
123146
#' @description
124147
#' Deserialize JSON string into an instance of DataQuery
125148
#'

samples/client/echo_api/r/R/default_value.R

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ DefaultValue <- R6::R6Class(
119119
DefaultValueObject <- list()
120120
if (!is.null(self$`array_string_enum_ref_default`)) {
121121
DefaultValueObject[["array_string_enum_ref_default"]] <-
122-
lapply(self$`array_string_enum_ref_default`, function(x) x$toSimpleType())
122+
self$extractSimpleType(self$`array_string_enum_ref_default`)
123123
}
124124
if (!is.null(self$`array_string_enum_default`)) {
125125
DefaultValueObject[["array_string_enum_default"]] <-
@@ -152,6 +152,29 @@ DefaultValue <- R6::R6Class(
152152
return(DefaultValueObject)
153153
},
154154

155+
extractSimpleType = function(x) {
156+
if (R6::is.R6(x)) {
157+
return(x$toSimpleType())
158+
} else if (!self$hasNestedR6(x)) {
159+
return(x)
160+
}
161+
lapply(x, self$extractSimpleType)
162+
},
163+
164+
hasNestedR6 = function(x) {
165+
if (R6::is.R6(x)) {
166+
return(TRUE)
167+
}
168+
if (is.list(x)) {
169+
for (item in x) {
170+
if (self$hasNestedR6(item)) {
171+
return(TRUE)
172+
}
173+
}
174+
}
175+
FALSE
176+
}
177+
155178
#' @description
156179
#' Deserialize JSON string into an instance of DefaultValue
157180
#'

samples/client/echo_api/r/R/number_properties_only.R

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,29 @@ NumberPropertiesOnly <- R6::R6Class(
9191
return(NumberPropertiesOnlyObject)
9292
},
9393

94+
extractSimpleType = function(x) {
95+
if (R6::is.R6(x)) {
96+
return(x$toSimpleType())
97+
} else if (!self$hasNestedR6(x)) {
98+
return(x)
99+
}
100+
lapply(x, self$extractSimpleType)
101+
},
102+
103+
hasNestedR6 = function(x) {
104+
if (R6::is.R6(x)) {
105+
return(TRUE)
106+
}
107+
if (is.list(x)) {
108+
for (item in x) {
109+
if (self$hasNestedR6(item)) {
110+
return(TRUE)
111+
}
112+
}
113+
}
114+
FALSE
115+
}
116+
94117
#' @description
95118
#' Deserialize JSON string into an instance of NumberPropertiesOnly
96119
#'

samples/client/echo_api/r/R/pet.R

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,15 @@ Pet <- R6::R6Class(
115115
}
116116
if (!is.null(self$`category`)) {
117117
PetObject[["category"]] <-
118-
self$`category`$toSimpleType()
118+
self$extractSimpleType(self$`category`)
119119
}
120120
if (!is.null(self$`photoUrls`)) {
121121
PetObject[["photoUrls"]] <-
122122
self$`photoUrls`
123123
}
124124
if (!is.null(self$`tags`)) {
125125
PetObject[["tags"]] <-
126-
lapply(self$`tags`, function(x) x$toSimpleType())
126+
self$extractSimpleType(self$`tags`)
127127
}
128128
if (!is.null(self$`status`)) {
129129
PetObject[["status"]] <-
@@ -132,6 +132,29 @@ Pet <- R6::R6Class(
132132
return(PetObject)
133133
},
134134

135+
extractSimpleType = function(x) {
136+
if (R6::is.R6(x)) {
137+
return(x$toSimpleType())
138+
} else if (!self$hasNestedR6(x)) {
139+
return(x)
140+
}
141+
lapply(x, self$extractSimpleType)
142+
},
143+
144+
hasNestedR6 = function(x) {
145+
if (R6::is.R6(x)) {
146+
return(TRUE)
147+
}
148+
if (is.list(x)) {
149+
for (item in x) {
150+
if (self$hasNestedR6(item)) {
151+
return(TRUE)
152+
}
153+
}
154+
}
155+
FALSE
156+
}
157+
135158
#' @description
136159
#' Deserialize JSON string into an instance of Pet
137160
#'

samples/client/echo_api/r/R/query.R

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,29 @@ Query <- R6::R6Class(
8080
return(QueryObject)
8181
},
8282

83+
extractSimpleType = function(x) {
84+
if (R6::is.R6(x)) {
85+
return(x$toSimpleType())
86+
} else if (!self$hasNestedR6(x)) {
87+
return(x)
88+
}
89+
lapply(x, self$extractSimpleType)
90+
},
91+
92+
hasNestedR6 = function(x) {
93+
if (R6::is.R6(x)) {
94+
return(TRUE)
95+
}
96+
if (is.list(x)) {
97+
for (item in x) {
98+
if (self$hasNestedR6(item)) {
99+
return(TRUE)
100+
}
101+
}
102+
}
103+
FALSE
104+
}
105+
83106
#' @description
84107
#' Deserialize JSON string into an instance of Query
85108
#'

samples/client/echo_api/r/R/tag.R

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,29 @@ Tag <- R6::R6Class(
8181
return(TagObject)
8282
},
8383

84+
extractSimpleType = function(x) {
85+
if (R6::is.R6(x)) {
86+
return(x$toSimpleType())
87+
} else if (!self$hasNestedR6(x)) {
88+
return(x)
89+
}
90+
lapply(x, self$extractSimpleType)
91+
},
92+
93+
hasNestedR6 = function(x) {
94+
if (R6::is.R6(x)) {
95+
return(TRUE)
96+
}
97+
if (is.list(x)) {
98+
for (item in x) {
99+
if (self$hasNestedR6(item)) {
100+
return(TRUE)
101+
}
102+
}
103+
}
104+
FALSE
105+
}
106+
84107
#' @description
85108
#' Deserialize JSON string into an instance of Tag
86109
#'

samples/client/echo_api/r/R/test_form_object_multipart_request_marker.R

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,29 @@ TestFormObjectMultipartRequestMarker <- R6::R6Class(
6868
return(TestFormObjectMultipartRequestMarkerObject)
6969
},
7070

71+
extractSimpleType = function(x) {
72+
if (R6::is.R6(x)) {
73+
return(x$toSimpleType())
74+
} else if (!self$hasNestedR6(x)) {
75+
return(x)
76+
}
77+
lapply(x, self$extractSimpleType)
78+
},
79+
80+
hasNestedR6 = function(x) {
81+
if (R6::is.R6(x)) {
82+
return(TRUE)
83+
}
84+
if (is.list(x)) {
85+
for (item in x) {
86+
if (self$hasNestedR6(item)) {
87+
return(TRUE)
88+
}
89+
}
90+
}
91+
FALSE
92+
}
93+
7194
#' @description
7295
#' Deserialize JSON string into an instance of TestFormObjectMultipartRequestMarker
7396
#'

0 commit comments

Comments
 (0)