update constructor for r oneof, anyof objects (#12643)

This commit is contained in:
William Cheng 2022-06-21 09:25:54 +08:00 committed by GitHub
parent 1440a68974
commit f29fdab33d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 57 additions and 6 deletions

View File

@ -22,10 +22,18 @@
#' @description #' @description
#' Initialize a new {{{classname}}}. #' Initialize a new {{{classname}}}.
#' #'
#' @param instance an instance of the object defined in the anyOf schemas: {{#anyOf}}"{{{.}}}"{{^-last}}, {{/-last}}{{/anyOf}}
#' @export #' @export
#' @md #' @md
initialize = function( initialize = function(instance = NULL) {
) { if (is.null(instance)) {
# do nothing
} {{#anyOf}}else if (get(class(instance)[[1]], pos = -1)$classname == "{{{.}}}") {
self$actual_instance = instance
self$actual_type = "{{{.}}}"
} {{/anyOf}}else {
stop(paste("Failed to initialize {{{classname}}} with anyOf schemas {{#anyOf}}{{{.}}}{{^-last}}, {{/-last}}{{/anyOf}}. Provided class name: ", get(class(instance)[[1]], pos = -1)$classname))
}
}, },
#' Deserialize JSON string into an instance of {{{classname}}}. #' Deserialize JSON string into an instance of {{{classname}}}.
#' #'

View File

@ -22,9 +22,18 @@
#' @description #' @description
#' Initialize a new {{{classname}}}. #' Initialize a new {{{classname}}}.
#' #'
#' @param instance an instance of the object defined in the oneOf schemas: {{#oneOf}}"{{{.}}}"{{^-last}}, {{/-last}}{{/oneOf}}
#' @export #' @export
#' @md #' @md
initialize = function() { initialize = function(instance = NULL) {
if (is.null(instance)) {
# do nothing
} {{#oneOf}}else if (get(class(instance)[[1]], pos = -1)$classname == "{{{.}}}") {
self$actual_instance = instance
self$actual_type = "{{{.}}}"
} {{/oneOf}}else {
stop(paste("Failed to initialize {{{classname}}} with oneOf schemas {{#oneOf}}{{{.}}}{{^-last}}, {{/-last}}{{/oneOf}}. Provided class name: ", get(class(instance)[[1]], pos = -1)$classname))
}
}, },
#' Deserialize JSON string into an instance of {{{classname}}}. #' Deserialize JSON string into an instance of {{{classname}}}.
#' #'

View File

@ -30,10 +30,21 @@ AnyOfPig <- R6::R6Class(
#' @description #' @description
#' Initialize a new AnyOfPig. #' Initialize a new AnyOfPig.
#' #'
#' @param instance an instance of the object defined in the anyOf schemas: "BasquePig", "DanishPig"
#' @export #' @export
#' @md #' @md
initialize = function( initialize = function(instance = NULL) {
) { if (is.null(instance)) {
# do nothing
} else if (get(class(instance)[[1]], pos = -1)$classname == "BasquePig") {
self$actual_instance = instance
self$actual_type = "BasquePig"
} else if (get(class(instance)[[1]], pos = -1)$classname == "DanishPig") {
self$actual_instance = instance
self$actual_type = "DanishPig"
} else {
stop(paste("Failed to initialize AnyOfPig with anyOf schemas BasquePig, DanishPig. Provided class name: ", get(class(instance)[[1]], pos = -1)$classname))
}
}, },
#' Deserialize JSON string into an instance of AnyOfPig. #' Deserialize JSON string into an instance of AnyOfPig.
#' #'

View File

@ -30,9 +30,21 @@ Pig <- R6::R6Class(
#' @description #' @description
#' Initialize a new Pig. #' Initialize a new Pig.
#' #'
#' @param instance an instance of the object defined in the oneOf schemas: "BasquePig", "DanishPig"
#' @export #' @export
#' @md #' @md
initialize = function() { initialize = function(instance = NULL) {
if (is.null(instance)) {
# do nothing
} else if (get(class(instance)[[1]], pos = -1)$classname == "BasquePig") {
self$actual_instance = instance
self$actual_type = "BasquePig"
} else if (get(class(instance)[[1]], pos = -1)$classname == "DanishPig") {
self$actual_instance = instance
self$actual_type = "DanishPig"
} else {
stop(paste("Failed to initialize Pig with oneOf schemas BasquePig, DanishPig. Provided class name: ", get(class(instance)[[1]], pos = -1)$classname))
}
}, },
#' Deserialize JSON string into an instance of Pig. #' Deserialize JSON string into an instance of Pig.
#' #'

View File

@ -181,6 +181,17 @@ test_that("Tests oneOf", {
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\\.') 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\\.')
# class name test
expect_equal(get(class(basque_pig$actual_instance)[[1]], pos = -1)$classname, "BasquePig")
# test contructors
pig2 <- Pig$new(instance = basque_pig$actual_instance)
expect_equal(pig2$actual_type, "BasquePig")
expect_equal(pig2$actual_instance$color, "red")
expect_equal(pig2$actual_instance$className, "BasquePig")
expect_equal(pig2$toJSON(), original_basque_pig$toJSONString())
expect_error(Pig$new(instance = basque_pig), 'Failed to initialize Pig with oneOf schemas BasquePig, DanishPig. Provided class name: Pig')
}) })
test_that("Tests anyOf", { test_that("Tests anyOf", {