[R] Add validateJSON to oneOf, anyOf models (#12548)

* add validateJSON to r oneof, anyof models

* update tests
This commit is contained in:
William Cheng 2022-06-08 00:35:40 +08:00 committed by GitHub
parent 5462681348
commit 14aef2c93d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 51 additions and 2 deletions

View File

@ -47,6 +47,18 @@
} else { } else {
NULL 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
} }
) )
) )

View File

@ -62,6 +62,18 @@
} else { } else {
NULL 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
} }
) )
) )

View File

@ -66,6 +66,18 @@ AnyOfPig <- R6::R6Class(
} else { } else {
NULL 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
} }
) )
) )

View File

@ -82,6 +82,18 @@ Pig <- R6::R6Class(
} else { } else {
NULL 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
} }
) )
) )

View File

@ -159,6 +159,7 @@ test_that("Tests oneOf", {
# test fromJSON, actual_tpye, actual_instance # test fromJSON, actual_tpye, actual_instance
pig <- Pig$new() pig <- Pig$new()
danish_pig <- pig$fromJSON(danish_pig_json) 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_type, "DanishPig")
expect_equal(danish_pig$actual_instance$size, 7) expect_equal(danish_pig$actual_instance$size, 7)
expect_equal(danish_pig$actual_instance$className, "DanishPig") expect_equal(danish_pig$actual_instance$className, "DanishPig")
@ -178,7 +179,7 @@ test_that("Tests oneOf", {
# test exception when no matche found # 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$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 # 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$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\\.')
}) })