diff --git a/modules/openapi-generator/src/main/resources/r/modelGeneric.mustache b/modules/openapi-generator/src/main/resources/r/modelGeneric.mustache index 9f244dc43055053eb549c3d85a111dae79d34668..4927eb52699992b3bc8b6681f289afe4b153ff2d 100644 --- a/modules/openapi-generator/src/main/resources/r/modelGeneric.mustache +++ b/modules/openapi-generator/src/main/resources/r/modelGeneric.mustache @@ -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}} diff --git a/samples/client/petstore/R/R/special.R b/samples/client/petstore/R/R/special.R index bcfb665edb01871afc17e1b64efaa9e3a83d7e6b..9efeaac05a96055539338f079646c35fe2d1bfdb 100644 --- a/samples/client/petstore/R/R/special.R +++ b/samples/client/petstore/R/R/special.R @@ -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 }, diff --git a/samples/client/petstore/R/tests/testthat/test_petstore.R b/samples/client/petstore/R/tests/testthat/test_petstore.R index ef23bd4ef0d43070e84f5af148859a027903208f..3387f02f725c4aa705db34f3ddfe8e620027e81b 100644 --- a/samples/client/petstore/R/tests/testthat/test_petstore.R +++ b/samples/client/petstore/R/tests/testthat/test_petstore.R @@ -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()