From 5db8cd0eff640564ff615c7b5c75c66d5245aa9b Mon Sep 17 00:00:00 2001 From: William Cheng Date: Mon, 6 Jun 2022 23:17:35 +0800 Subject: [PATCH] fix toJSON, add tests (#12541) --- .../src/main/resources/r/modelOneOf.mustache | 11 +++++++---- samples/client/petstore/R/R/pig.R | 11 +++++++---- samples/client/petstore/R/test_petstore.R | 12 +++++++----- .../petstore/R/tests/testthat/test_petstore.R | 17 ++++++++++++++++- 4 files changed, 37 insertions(+), 14 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/r/modelOneOf.mustache b/modules/openapi-generator/src/main/resources/r/modelOneOf.mustache index ba75bf337db..a4f92eee806 100644 --- a/modules/openapi-generator/src/main/resources/r/modelOneOf.mustache +++ b/modules/openapi-generator/src/main/resources/r/modelOneOf.mustache @@ -19,10 +19,6 @@ ) { local_optional_var <- list(...) }, - toJSON = function() { - {{classname}}_object <- list() - {{classname}}_object - }, fromJSON = function(input) { matched <- 0 # match counter matched_schemas <- list() #names of matched schemas @@ -59,6 +55,13 @@ } self + }, + toJSON = function() { + if (!is.null(self$actual_instance)) { + self$actual_instance$toJSONString() + } else { + NULL + } } ) ) diff --git a/samples/client/petstore/R/R/pig.R b/samples/client/petstore/R/R/pig.R index 27500b66014..e41f8834161 100644 --- a/samples/client/petstore/R/R/pig.R +++ b/samples/client/petstore/R/R/pig.R @@ -26,10 +26,6 @@ Pig <- R6::R6Class( ) { local_optional_var <- list(...) }, - toJSON = function() { - Pig_object <- list() - Pig_object - }, fromJSON = function(input) { matched <- 0 # match counter matched_schemas <- list() #names of matched schemas @@ -79,6 +75,13 @@ Pig <- R6::R6Class( } self + }, + toJSON = function() { + if (!is.null(self$actual_instance)) { + self$actual_instance$toJSONString() + } else { + NULL + } } ) ) diff --git a/samples/client/petstore/R/test_petstore.R b/samples/client/petstore/R/test_petstore.R index cd263b31f3e..c7aa86d7468 100644 --- a/samples/client/petstore/R/test_petstore.R +++ b/samples/client/petstore/R/test_petstore.R @@ -56,15 +56,17 @@ json <- {}, {"Name" : "Ada", "Occupation" : "Engineer"} ]' - + + print("==========") pig <- Pig$new() basque_pig <- pig$fromJSON(basque_pig_json) #print(basque_pig$actual_instance$color) #expect_equal(basque_pig$actual_type, "BasquePig") - i <- pig$fromJSON(danish_pig_json) - #i <- pig$fromJSON(wrong_json) + pig$fromJSON(danish_pig_json) + #pig$fromJSON(wrong_json) + pig$toJSON() - d <- DanishPig$new() - dp <- d$validateJSON(danish_pig_json) + #d <- DanishPig$new() + #dp <- d$validateJSON(danish_pig_json) diff --git a/samples/client/petstore/R/tests/testthat/test_petstore.R b/samples/client/petstore/R/tests/testthat/test_petstore.R index 5033371b659..951eae6d923 100644 --- a/samples/client/petstore/R/tests/testthat/test_petstore.R +++ b/samples/client/petstore/R/tests/testthat/test_petstore.R @@ -1,4 +1,5 @@ context("basic functionality") + pet_api <- PetApi$new() pet_id <- 123321 pet <- Pet$new("name_test", @@ -152,20 +153,34 @@ 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) + + # test fromJSNO, actual_tpye, actual_instance pig <- Pig$new() danish_pig <- pig$fromJSON(danish_pig_json) expect_equal(danish_pig$actual_type, "DanishPig") expect_equal(danish_pig$actual_instance$size, 7) expect_equal(danish_pig$actual_instance$className, "DanishPig") + expect_equal(pig$actual_type, "DanishPig") + expect_equal(pig$actual_instance$size, 7) + expect_equal(pig$actual_instance$className, "DanishPig") + + # test toJSON + #expect_equal(danish_pig$toJSON(), "") + expect_equal(danish_pig$toJSON(), original_danish_pig$toJSONString()) + basque_pig <- pig$fromJSON(basque_pig_json) expect_equal(basque_pig$actual_type, "BasquePig") expect_equal(basque_pig$actual_instance$color, "red") expect_equal(basque_pig$actual_instance$className, "BasquePig") + expect_equal(basque_pig$toJSON(), original_basque_pig$toJSONString()) - #expect_error(pig$fromJSON(wrong_json), "No match found when deserializing the payload into Pig with oneOf schemas BasquePig, DanishPig. Details: The JSON input ` [\n {\"Name\" : \"Tom\", \"Age\" : 32, \"Occupation\" : \"Consultant\"}, \n {},\n {\"Name\" : \"Ada\", \"Occupation\" : \"Engineer\"}\n ] ` is invalid for BasquePig: the required field `className` is missing., The JSON input ` [\n {\"Name\" : \"Tom\", \"Age\" : 32, \"Occupation\" : \"Consultant\"}, \n {},\n {\"Name\" : \"Ada\", \"Occupation\" : \"Engineer\"}\n ] ` is invalid for DanishPig: the required field `className` is missing.") + # test exception when no matche found expect_error(pig$fromJSON('{}'), 'No match found when deserializing the payload into Pig with oneOf schemas BasquePig, DanishPig. Details: The JSON input ` \\{\\} ` is invalid for BasquePig: the required field `className` is missing\\., The JSON input ` \\{\\} ` is invalid for DanishPig: the required field `className` is missing\\.') + }) #test_that("GetPetById", {