diff --git a/modules/openapi-generator/src/main/resources/r/modelAnyOf.mustache b/modules/openapi-generator/src/main/resources/r/modelAnyOf.mustache new file mode 100644 index 00000000000..287264a1ceb --- /dev/null +++ b/modules/openapi-generator/src/main/resources/r/modelAnyOf.mustache @@ -0,0 +1,52 @@ +#' @docType class +#' @title {{classname}} +#' +#' @description {{classname}} Class +#' +#' @format An \code{R6Class} generator object +#' +#' @importFrom R6 R6Class +#' @importFrom jsonlite fromJSON toJSON +#' @export +{{classname}} <- R6::R6Class( + '{{classname}}', + public = list( + actual_instance = NULL, + actual_type = NULL, + any_of = list({{#anyOf}}"{{{.}}}"{{^-last}}, {{/-last}}{{/anyOf}}), + initialize = function( + {{#requiredVars}}`{{baseName}}`, {{/requiredVars}}{{#optionalVars}}`{{baseName}}`={{{defaultValue}}}{{^defaultValue}}NULL{{/defaultValue}}, {{/optionalVars}}... + ) { + local_optional_var <- list(...) + }, + fromJSON = function(input) { + error_messages <- list() + + {{#anyOf}} + {{{.}}}_result <- tryCatch({ + {{{.}}}$public_methods$validateJSON(input) + {{{.}}}_instance <- {{{.}}}$new() + self$actual_instance <- {{{.}}}_instance$fromJSON(input) + self$actual_type <- "{{{.}}}" + return(self) + }, + error = function(err) err + ) + + if (!is.null({{{.}}}_result['error'])) { + error_messages <- append(error_messages, {{{.}}}_result['message']) + } + + {{/anyOf}} + # no match + stop(paste("No match found when deserializing the payload into {{{classname}}} with anyOf schemas {{#anyOf}}{{{.}}}{{^-last}}, {{/-last}}{{/anyOf}}. Details: ", paste(error_messages, collapse = ', '))) + }, + toJSON = function() { + if (!is.null(self$actual_instance)) { + self$actual_instance$toJSONString() + } else { + NULL + } + } + ) +) diff --git a/modules/openapi-generator/src/test/resources/3_0/r/petstore.yaml b/modules/openapi-generator/src/test/resources/3_0/r/petstore.yaml index c9827af375e..c6bb52c52ee 100644 --- a/modules/openapi-generator/src/test/resources/3_0/r/petstore.yaml +++ b/modules/openapi-generator/src/test/resources/3_0/r/petstore.yaml @@ -801,6 +801,10 @@ components: allOf: - $ref: '#/components/schemas/Tag' - $ref: '#/components/schemas/ApiResponse' + AnyOfPig: + anyOf: + - $ref: '#/components/schemas/BasquePig' + - $ref: '#/components/schemas/DanishPig' Pig: oneOf: - $ref: '#/components/schemas/BasquePig' diff --git a/samples/client/petstore/R/.openapi-generator/FILES b/samples/client/petstore/R/.openapi-generator/FILES index 32f11b6dbb2..0549314051d 100644 --- a/samples/client/petstore/R/.openapi-generator/FILES +++ b/samples/client/petstore/R/.openapi-generator/FILES @@ -5,6 +5,7 @@ DESCRIPTION NAMESPACE R/allof_tag_api_response.R R/animal.R +R/any_of_pig.R R/api_client.R R/api_exception.R R/api_response.R @@ -28,6 +29,7 @@ R/user_api.R README.md docs/AllofTagApiResponse.md docs/Animal.md +docs/AnyOfPig.md docs/BasquePig.md docs/Cat.md docs/CatAllOf.md diff --git a/samples/client/petstore/R/NAMESPACE b/samples/client/petstore/R/NAMESPACE index 95a53392965..77557a91938 100644 --- a/samples/client/petstore/R/NAMESPACE +++ b/samples/client/petstore/R/NAMESPACE @@ -14,6 +14,7 @@ export(ApiException) # Models export(AllofTagApiResponse) export(Animal) +export(AnyOfPig) export(BasquePig) export(Cat) export(CatAllOf) diff --git a/samples/client/petstore/R/R/any_of_pig.R b/samples/client/petstore/R/R/any_of_pig.R new file mode 100644 index 00000000000..7197d52b438 --- /dev/null +++ b/samples/client/petstore/R/R/any_of_pig.R @@ -0,0 +1,72 @@ +# OpenAPI Petstore +# +# This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. +# +# The version of the OpenAPI document: 1.0.0 +# Generated by: https://openapi-generator.tech + +#' @docType class +#' @title AnyOfPig +#' +#' @description AnyOfPig Class +#' +#' @format An \code{R6Class} generator object +#' +#' @importFrom R6 R6Class +#' @importFrom jsonlite fromJSON toJSON +#' @export +AnyOfPig <- R6::R6Class( + 'AnyOfPig', + public = list( + actual_instance = NULL, + actual_type = NULL, + any_of = list("BasquePig", "DanishPig"), + initialize = function( + `className`, `color`, `size`, ... + ) { + local_optional_var <- list(...) + }, + fromJSON = function(input) { + error_messages <- list() + + BasquePig_result <- tryCatch({ + BasquePig$public_methods$validateJSON(input) + BasquePig_instance <- BasquePig$new() + self$actual_instance <- BasquePig_instance$fromJSON(input) + self$actual_type <- "BasquePig" + return(self) + }, + error = function(err) err + ) + + if (!is.null(BasquePig_result['error'])) { + error_messages <- append(error_messages, BasquePig_result['message']) + } + + DanishPig_result <- tryCatch({ + DanishPig$public_methods$validateJSON(input) + DanishPig_instance <- DanishPig$new() + self$actual_instance <- DanishPig_instance$fromJSON(input) + self$actual_type <- "DanishPig" + return(self) + }, + error = function(err) err + ) + + if (!is.null(DanishPig_result['error'])) { + error_messages <- append(error_messages, DanishPig_result['message']) + } + + # no match + stop(paste("No match found when deserializing the payload into AnyOfPig with anyOf schemas BasquePig, DanishPig. Details: ", paste(error_messages, collapse = ', '))) + }, + toJSON = function() { + if (!is.null(self$actual_instance)) { + self$actual_instance$toJSONString() + } else { + NULL + } + } + ) +) + diff --git a/samples/client/petstore/R/README.md b/samples/client/petstore/R/README.md index 4c75bb991bf..538251cf406 100644 --- a/samples/client/petstore/R/README.md +++ b/samples/client/petstore/R/README.md @@ -83,6 +83,7 @@ Class | Method | HTTP request | Description - [AllofTagApiResponse](docs/AllofTagApiResponse.md) - [Animal](docs/Animal.md) + - [AnyOfPig](docs/AnyOfPig.md) - [BasquePig](docs/BasquePig.md) - [Cat](docs/Cat.md) - [CatAllOf](docs/CatAllOf.md) diff --git a/samples/client/petstore/R/docs/AnyOfPig.md b/samples/client/petstore/R/docs/AnyOfPig.md new file mode 100644 index 00000000000..296d7c7e334 --- /dev/null +++ b/samples/client/petstore/R/docs/AnyOfPig.md @@ -0,0 +1,11 @@ +# petstore::AnyOfPig + + +## Properties +Name | Type | Description | Notes +------------ | ------------- | ------------- | ------------- +**className** | **character** | | +**color** | **character** | | +**size** | **integer** | | + + diff --git a/samples/client/petstore/R/tests/testthat/test_any_of_pig.R b/samples/client/petstore/R/tests/testthat/test_any_of_pig.R new file mode 100644 index 00000000000..8ca3b8e73d1 --- /dev/null +++ b/samples/client/petstore/R/tests/testthat/test_any_of_pig.R @@ -0,0 +1,27 @@ +# Automatically generated by openapi-generator (https://openapi-generator.tech) +# Please update as you see appropriate + +context("Test AnyOfPig") + +model_instance <- AnyOfPig$new() + +test_that("className", { + # tests for the property `className` (character) + + # uncomment below to test the property + #expect_equal(model.instance$`className`, "EXPECTED_RESULT") +}) + +test_that("color", { + # tests for the property `color` (character) + + # uncomment below to test the property + #expect_equal(model.instance$`color`, "EXPECTED_RESULT") +}) + +test_that("size", { + # tests for the property `size` (integer) + + # uncomment below to test the property + #expect_equal(model.instance$`size`, "EXPECTED_RESULT") +}) diff --git a/samples/client/petstore/R/tests/testthat/test_petstore.R b/samples/client/petstore/R/tests/testthat/test_petstore.R index 951eae6d923..bb3df6fd168 100644 --- a/samples/client/petstore/R/tests/testthat/test_petstore.R +++ b/samples/client/petstore/R/tests/testthat/test_petstore.R @@ -156,7 +156,7 @@ test_that("Tests oneOf", { original_danish_pig = DanishPig$new()$fromJSON(danish_pig_json) original_basque_pig = BasquePig$new()$fromJSON(basque_pig_json) - # test fromJSNO, actual_tpye, actual_instance + # test fromJSON, actual_tpye, actual_instance pig <- Pig$new() danish_pig <- pig$fromJSON(danish_pig_json) expect_equal(danish_pig$actual_type, "DanishPig") @@ -168,7 +168,6 @@ test_that("Tests oneOf", { expect_equal(pig$actual_instance$className, "DanishPig") # test toJSON - #expect_equal(danish_pig$toJSON(), "") expect_equal(danish_pig$toJSON(), original_danish_pig$toJSONString()) basque_pig <- pig$fromJSON(basque_pig_json) @@ -181,6 +180,49 @@ 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\\.') +}) + +test_that("Tests anyOf", { + basque_pig_json <- + '{"className": "BasquePig", "color": "red"}' + + danish_pig_json <- + '{"className": "DanishPig", "size": 7}' + + wrong_json <- + '[ + {"Name" : "Tom", "Age" : 32, "Occupation" : "Consultant"}, + {}, + {"Name" : "Ada", "Occupation" : "Engineer"} + ]' + + original_danish_pig = DanishPig$new()$fromJSON(danish_pig_json) + original_basque_pig = BasquePig$new()$fromJSON(basque_pig_json) + + # test fromJSON, actual_tpye, actual_instance + pig <- AnyOfPig$new() + danish_pig <- pig$fromJSON(danish_pig_json) + expect_equal(danish_pig$actual_type, "DanishPig") + expect_equal(danish_pig$actual_instance$size, 7) + expect_equal(danish_pig$actual_instance$className, "DanishPig") + + expect_equal(pig$actual_type, "DanishPig") + expect_equal(pig$actual_instance$size, 7) + expect_equal(pig$actual_instance$className, "DanishPig") + + # test toJSON + expect_equal(danish_pig$toJSON(), original_danish_pig$toJSONString()) + + basque_pig <- pig$fromJSON(basque_pig_json) + expect_equal(basque_pig$actual_type, "BasquePig") + expect_equal(basque_pig$actual_instance$color, "red") + expect_equal(basque_pig$actual_instance$className, "BasquePig") + expect_equal(basque_pig$toJSON(), original_basque_pig$toJSONString()) + + # 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\\.') + + }) #test_that("GetPetById", {