Skip to content

Commit a599ae9

Browse files
authored
fix deserialization for r model with special item name (#12658)
1 parent f2cc3b8 commit a599ae9

3 files changed

Lines changed: 37 additions & 14 deletions

File tree

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,17 +158,17 @@
158158
fromJSON = function({{classname}}Json) {
159159
{{classname}}Object <- jsonlite::fromJSON({{classname}}Json)
160160
{{#vars}}
161-
if (!is.null({{classname}}Object$`{{name}}`)) {
161+
if (!is.null({{classname}}Object$`{{baseName}}`)) {
162162
{{#isContainer}}
163-
self$`{{name}}` <- ApiClient$new()$deserializeObj({{classname}}Object$`{{name}}`, "{{dataType}}", loadNamespace("{{packageName}}"))
163+
self$`{{name}}` <- ApiClient$new()$deserializeObj({{classname}}Object$`{{baseName}}`, "{{dataType}}", loadNamespace("{{packageName}}"))
164164
{{/isContainer}}
165165
{{^isContainer}}
166166
{{#isPrimitiveType}}
167-
self$`{{name}}` <- {{classname}}Object$`{{name}}`
167+
self$`{{name}}` <- {{classname}}Object$`{{baseName}}`
168168
{{/isPrimitiveType}}
169169
{{^isPrimitiveType}}
170170
{{name}}Object <- {{dataType}}$new()
171-
{{name}}Object$fromJSON(jsonlite::toJSON({{classname}}Object${{name}}, auto_unbox = TRUE, digits = NA))
171+
{{name}}Object$fromJSON(jsonlite::toJSON({{classname}}Object${{baseName}}, auto_unbox = TRUE, digits = NA))
172172
self$`{{name}}` <- {{name}}Object
173173
{{/isPrimitiveType}}
174174
{{/isContainer}}

samples/client/petstore/R/R/special.R

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,14 @@ Special <- R6::R6Class(
6363
},
6464
fromJSON = function(SpecialJson) {
6565
SpecialObject <- jsonlite::fromJSON(SpecialJson)
66-
if (!is.null(SpecialObject$`item_self`)) {
67-
self$`item_self` <- SpecialObject$`item_self`
66+
if (!is.null(SpecialObject$`self`)) {
67+
self$`item_self` <- SpecialObject$`self`
6868
}
69-
if (!is.null(SpecialObject$`item_private`)) {
70-
self$`item_private` <- SpecialObject$`item_private`
69+
if (!is.null(SpecialObject$`private`)) {
70+
self$`item_private` <- SpecialObject$`private`
7171
}
72-
if (!is.null(SpecialObject$`item_super`)) {
73-
self$`item_super` <- SpecialObject$`item_super`
72+
if (!is.null(SpecialObject$`super`)) {
73+
self$`item_super` <- SpecialObject$`super`
7474
}
7575
self
7676
},

samples/client/petstore/R/tests/testthat/test_petstore.R

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,29 @@ test_that("Tests validateJSON", {
139139

140140
})
141141

142+
# test object with special item names: self, private, super
143+
test_that("Tests oneOf", {
144+
special_json <-
145+
'{"self": 123, "private": "red", "super": "something"}'
146+
147+
# test fromJSON
148+
special <- Special$new()$fromJSON(special_json)
149+
expect_equal(special$item_self, 123)
150+
expect_equal(special$item_private, "red")
151+
expect_equal(special$item_super, "something")
152+
153+
# test toJSONString
154+
expect_true(grepl('"private"', special$toJSONString()))
155+
expect_true(grepl('"self"', special$toJSONString()))
156+
expect_true(grepl('"super"', special$toJSONString()))
157+
158+
# round trip test
159+
s1 <- Special$new()$fromJSONString(special_json)
160+
s2 <- Special$new()$fromJSONString(s1$toJSONString())
161+
expect_equal(s1, s2)
162+
163+
})
164+
142165
test_that("Tests oneOf", {
143166
basque_pig_json <-
144167
'{"className": "BasquePig", "color": "red"}'
@@ -153,8 +176,8 @@ test_that("Tests oneOf", {
153176
{"Name" : "Ada", "Occupation" : "Engineer"}
154177
]'
155178

156-
original_danish_pig = DanishPig$new()$fromJSON(danish_pig_json)
157-
original_basque_pig = BasquePig$new()$fromJSON(basque_pig_json)
179+
original_danish_pig <- DanishPig$new()$fromJSON(danish_pig_json)
180+
original_basque_pig <- BasquePig$new()$fromJSON(basque_pig_json)
158181

159182
# test fromJSON, actual_tpye, actual_instance
160183
pig <- Pig$new()
@@ -208,8 +231,8 @@ test_that("Tests anyOf", {
208231
{"Name" : "Ada", "Occupation" : "Engineer"}
209232
]'
210233

211-
original_danish_pig = DanishPig$new()$fromJSON(danish_pig_json)
212-
original_basque_pig = BasquePig$new()$fromJSON(basque_pig_json)
234+
original_danish_pig <- DanishPig$new()$fromJSON(danish_pig_json)
235+
original_basque_pig <- BasquePig$new()$fromJSON(basque_pig_json)
213236

214237
# test fromJSON, actual_tpye, actual_instance
215238
pig <- AnyOfPig$new()

0 commit comments

Comments
 (0)