Skip to content
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

[R] fix deserialization for models with special item names #12658

Merged
merged 1 commit into from
Jun 22, 2022
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 @@ -158,17 +158,17 @@
fromJSON = function({{classname}}Json) {
{{classname}}Object <- jsonlite::fromJSON({{classname}}Json)
{{#vars}}
if (!is.null({{classname}}Object$`{{name}}`)) {
if (!is.null({{classname}}Object$`{{baseName}}`)) {
{{#isContainer}}
self$`{{name}}` <- ApiClient$new()$deserializeObj({{classname}}Object$`{{name}}`, "{{dataType}}", loadNamespace("{{packageName}}"))
self$`{{name}}` <- ApiClient$new()$deserializeObj({{classname}}Object$`{{baseName}}`, "{{dataType}}", loadNamespace("{{packageName}}"))
{{/isContainer}}
{{^isContainer}}
{{#isPrimitiveType}}
self$`{{name}}` <- {{classname}}Object$`{{name}}`
self$`{{name}}` <- {{classname}}Object$`{{baseName}}`
{{/isPrimitiveType}}
{{^isPrimitiveType}}
{{name}}Object <- {{dataType}}$new()
{{name}}Object$fromJSON(jsonlite::toJSON({{classname}}Object${{name}}, auto_unbox = TRUE, digits = NA))
{{name}}Object$fromJSON(jsonlite::toJSON({{classname}}Object${{baseName}}, auto_unbox = TRUE, digits = NA))
self$`{{name}}` <- {{name}}Object
{{/isPrimitiveType}}
{{/isContainer}}
Expand Down
12 changes: 6 additions & 6 deletions samples/client/petstore/R/R/special.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ Special <- R6::R6Class(
},
fromJSON = function(SpecialJson) {
SpecialObject <- jsonlite::fromJSON(SpecialJson)
if (!is.null(SpecialObject$`item_self`)) {
self$`item_self` <- SpecialObject$`item_self`
if (!is.null(SpecialObject$`self`)) {
self$`item_self` <- SpecialObject$`self`
}
if (!is.null(SpecialObject$`item_private`)) {
self$`item_private` <- SpecialObject$`item_private`
if (!is.null(SpecialObject$`private`)) {
self$`item_private` <- SpecialObject$`private`
}
if (!is.null(SpecialObject$`item_super`)) {
self$`item_super` <- SpecialObject$`item_super`
if (!is.null(SpecialObject$`super`)) {
self$`item_super` <- SpecialObject$`super`
}
self
},
Expand Down
31 changes: 27 additions & 4 deletions samples/client/petstore/R/tests/testthat/test_petstore.R
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,29 @@ test_that("Tests validateJSON", {

})

# test object with special item names: self, private, super
test_that("Tests oneOf", {
special_json <-
'{"self": 123, "private": "red", "super": "something"}'

# test fromJSON
special <- Special$new()$fromJSON(special_json)
expect_equal(special$item_self, 123)
expect_equal(special$item_private, "red")
expect_equal(special$item_super, "something")

# test toJSONString
expect_true(grepl('"private"', special$toJSONString()))
expect_true(grepl('"self"', special$toJSONString()))
expect_true(grepl('"super"', special$toJSONString()))

# round trip test
s1 <- Special$new()$fromJSONString(special_json)
s2 <- Special$new()$fromJSONString(s1$toJSONString())
expect_equal(s1, s2)

})

test_that("Tests oneOf", {
basque_pig_json <-
'{"className": "BasquePig", "color": "red"}'
Expand All @@ -153,8 +176,8 @@ test_that("Tests oneOf", {
{"Name" : "Ada", "Occupation" : "Engineer"}
]'

original_danish_pig = DanishPig$new()$fromJSON(danish_pig_json)
original_basque_pig = BasquePig$new()$fromJSON(basque_pig_json)
original_danish_pig <- DanishPig$new()$fromJSON(danish_pig_json)
original_basque_pig <- BasquePig$new()$fromJSON(basque_pig_json)

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

original_danish_pig = DanishPig$new()$fromJSON(danish_pig_json)
original_basque_pig = BasquePig$new()$fromJSON(basque_pig_json)
original_danish_pig <- DanishPig$new()$fromJSON(danish_pig_json)
original_basque_pig <- BasquePig$new()$fromJSON(basque_pig_json)

# test fromJSON, actual_tpye, actual_instance
pig <- AnyOfPig$new()
Expand Down