Skip to content

Commit

Permalink
update constructor for r oneof, anyof objects (#12643)
Browse files Browse the repository at this point in the history
  • Loading branch information
wing328 authored Jun 21, 2022
1 parent 1440a68 commit f29fdab
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 6 deletions.
12 changes: 10 additions & 2 deletions modules/openapi-generator/src/main/resources/r/modelAnyOf.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,18 @@
#' @description
#' Initialize a new {{{classname}}}.
#'
#' @param instance an instance of the object defined in the anyOf schemas: {{#anyOf}}"{{{.}}}"{{^-last}}, {{/-last}}{{/anyOf}}
#' @export
#' @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}}}.
#'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,18 @@
#' @description
#' Initialize a new {{{classname}}}.
#'
#' @param instance an instance of the object defined in the oneOf schemas: {{#oneOf}}"{{{.}}}"{{^-last}}, {{/-last}}{{/oneOf}}
#' @export
#' @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}}}.
#'
Expand Down
15 changes: 13 additions & 2 deletions samples/client/petstore/R/R/any_of_pig.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,21 @@ AnyOfPig <- R6::R6Class(
#' @description
#' Initialize a new AnyOfPig.
#'
#' @param instance an instance of the object defined in the anyOf schemas: "BasquePig", "DanishPig"
#' @export
#' @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.
#'
Expand Down
14 changes: 13 additions & 1 deletion samples/client/petstore/R/R/pig.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,21 @@ Pig <- R6::R6Class(
#' @description
#' Initialize a new Pig.
#'
#' @param instance an instance of the object defined in the oneOf schemas: "BasquePig", "DanishPig"
#' @export
#' @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.
#'
Expand Down
11 changes: 11 additions & 0 deletions samples/client/petstore/R/tests/testthat/test_petstore.R
Original file line number Diff line number Diff line change
Expand Up @@ -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$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", {
Expand Down

0 comments on commit f29fdab

Please sign in to comment.