diff --git a/modules/openapi-generator/src/main/resources/r/modelAnyOf.mustache b/modules/openapi-generator/src/main/resources/r/modelAnyOf.mustache index 287264a1ceb..d3b86f23ee8 100644 --- a/modules/openapi-generator/src/main/resources/r/modelAnyOf.mustache +++ b/modules/openapi-generator/src/main/resources/r/modelAnyOf.mustache @@ -47,6 +47,18 @@ } else { NULL } + }, + validateJSON = function(input) { + # backup current values + actual_instance_bak <- self$actual_instance + actual_type_bak <- self$actual_type + + # if it's not valid, an error will be thrown + self$fromJSON(input) + + # no error thrown, restore old values + self$actual_instance <- actual_instance_bak + self$actual_type <- actual_type_bak } ) ) diff --git a/modules/openapi-generator/src/main/resources/r/modelOneOf.mustache b/modules/openapi-generator/src/main/resources/r/modelOneOf.mustache index a4f92eee806..1e33da9e7ed 100644 --- a/modules/openapi-generator/src/main/resources/r/modelOneOf.mustache +++ b/modules/openapi-generator/src/main/resources/r/modelOneOf.mustache @@ -62,6 +62,18 @@ } else { NULL } + }, + validateJSON = function(input) { + # backup current values + actual_instance_bak <- self$actual_instance + actual_type_bak <- self$actual_type + + # if it's not valid, an error will be thrown + self$fromJSON(input) + + # no error thrown, restore old values + self$actual_instance <- actual_instance_bak + self$actual_type <- actual_type_bak } ) ) diff --git a/samples/client/petstore/R/R/any_of_pig.R b/samples/client/petstore/R/R/any_of_pig.R index 7197d52b438..d4e5fffe068 100644 --- a/samples/client/petstore/R/R/any_of_pig.R +++ b/samples/client/petstore/R/R/any_of_pig.R @@ -66,6 +66,18 @@ AnyOfPig <- R6::R6Class( } else { NULL } + }, + validateJSON = function(input) { + # backup current values + actual_instance_bak <- self$actual_instance + actual_type_bak <- self$actual_type + + # if it's not valid, an error will be thrown + self$fromJSON(input) + + # no error thrown, restore old values + self$actual_instance <- actual_instance_bak + self$actual_type <- actual_type_bak } ) ) diff --git a/samples/client/petstore/R/R/pig.R b/samples/client/petstore/R/R/pig.R index e41f8834161..5afa7dc8943 100644 --- a/samples/client/petstore/R/R/pig.R +++ b/samples/client/petstore/R/R/pig.R @@ -82,6 +82,18 @@ Pig <- R6::R6Class( } else { NULL } + }, + validateJSON = function(input) { + # backup current values + actual_instance_bak <- self$actual_instance + actual_type_bak <- self$actual_type + + # if it's not valid, an error will be thrown + self$fromJSON(input) + + # no error thrown, restore old values + self$actual_instance <- actual_instance_bak + self$actual_type <- actual_type_bak } ) ) diff --git a/samples/client/petstore/R/tests/testthat/test_petstore.R b/samples/client/petstore/R/tests/testthat/test_petstore.R index bb3df6fd168..a3481c4805c 100644 --- a/samples/client/petstore/R/tests/testthat/test_petstore.R +++ b/samples/client/petstore/R/tests/testthat/test_petstore.R @@ -159,6 +159,7 @@ test_that("Tests oneOf", { # test fromJSON, actual_tpye, actual_instance pig <- Pig$new() danish_pig <- pig$fromJSON(danish_pig_json) + pig$validateJSON(basque_pig_json) # validate JSON to ensure its actual_instance, actual_type are not updated expect_equal(danish_pig$actual_type, "DanishPig") expect_equal(danish_pig$actual_instance$size, 7) expect_equal(danish_pig$actual_instance$className, "DanishPig") @@ -178,7 +179,7 @@ test_that("Tests oneOf", { # 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\\.') - + expect_error(pig$validateJSON('{}'), '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\\.') }) @@ -221,7 +222,7 @@ test_that("Tests anyOf", { # test exception when no matche found expect_error(pig$fromJSON('{}'), 'No match found when deserializing the payload into AnyOfPig with anyOf 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\\.') - + expect_error(pig$validateJSON('{}'), 'No match found when deserializing the payload into AnyOfPig with anyOf 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\\.') })