fix deserialization for r model with special item name (#12658)

This commit is contained in:
William Cheng 2022-06-22 15:06:56 +08:00 committed by GitHub
parent f2cc3b8611
commit a599ae927f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 14 deletions

View File

@ -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}}

View File

@ -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
},

View File

@ -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"}'
@ -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()
@ -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()