From f29fdab33d095ae4b838d68562682efd015e7f6b Mon Sep 17 00:00:00 2001 From: William Cheng Date: Tue, 21 Jun 2022 09:25:54 +0800 Subject: [PATCH] update constructor for r oneof, anyof objects (#12643) --- .../src/main/resources/r/modelAnyOf.mustache | 12 ++++++++++-- .../src/main/resources/r/modelOneOf.mustache | 11 ++++++++++- samples/client/petstore/R/R/any_of_pig.R | 15 +++++++++++++-- samples/client/petstore/R/R/pig.R | 14 +++++++++++++- .../petstore/R/tests/testthat/test_petstore.R | 11 +++++++++++ 5 files changed, 57 insertions(+), 6 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/r/modelAnyOf.mustache b/modules/openapi-generator/src/main/resources/r/modelAnyOf.mustache index 3897d549b9af..62496d246dac 100644 --- a/modules/openapi-generator/src/main/resources/r/modelAnyOf.mustache +++ b/modules/openapi-generator/src/main/resources/r/modelAnyOf.mustache @@ -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}}}. #' diff --git a/modules/openapi-generator/src/main/resources/r/modelOneOf.mustache b/modules/openapi-generator/src/main/resources/r/modelOneOf.mustache index 4f295c3319c6..2b00fdcc9297 100644 --- a/modules/openapi-generator/src/main/resources/r/modelOneOf.mustache +++ b/modules/openapi-generator/src/main/resources/r/modelOneOf.mustache @@ -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}}}. #' diff --git a/samples/client/petstore/R/R/any_of_pig.R b/samples/client/petstore/R/R/any_of_pig.R index 130e27c6ed63..1819af2d4d71 100644 --- a/samples/client/petstore/R/R/any_of_pig.R +++ b/samples/client/petstore/R/R/any_of_pig.R @@ -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. #' diff --git a/samples/client/petstore/R/R/pig.R b/samples/client/petstore/R/R/pig.R index 65c24ce74080..5b84d3d98894 100644 --- a/samples/client/petstore/R/R/pig.R +++ b/samples/client/petstore/R/R/pig.R @@ -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. #' diff --git a/samples/client/petstore/R/tests/testthat/test_petstore.R b/samples/client/petstore/R/tests/testthat/test_petstore.R index a3481c4805cf..ef23bd4ef0d4 100644 --- a/samples/client/petstore/R/tests/testthat/test_petstore.R +++ b/samples/client/petstore/R/tests/testthat/test_petstore.R @@ -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", {