diff --git a/.gitignore b/.gitignore index c31896c826d..3e0166838b2 100644 --- a/.gitignore +++ b/.gitignore @@ -251,6 +251,7 @@ samples/server/petstore/erlang-server/rebar.lock # JS samples/client/petstore/javascript-es6/package-lock.json samples/client/petstore/javascript-promise-es6/package-lock.json +samples/client/petstore/javascript-apollo/package-lock.json # elm samples/client/petstore/elm/index.html diff --git a/modules/openapi-generator/src/main/resources/r/modelAnyOf.mustache b/modules/openapi-generator/src/main/resources/r/modelAnyOf.mustache index 688ddeb6eb2..37881ae4369 100644 --- a/modules/openapi-generator/src/main/resources/r/modelAnyOf.mustache +++ b/modules/openapi-generator/src/main/resources/r/modelAnyOf.mustache @@ -58,22 +58,32 @@ fromJSON = function(input) { error_messages <- list() - {{#anyOf}} - {{{.}}}_result <- tryCatch({ - {{{.}}}$public_methods$validateJSON(input) - {{{.}}}_instance <- {{{.}}}$new() - self$actual_instance <- {{{.}}}_instance$fromJSON(input) - self$actual_type <- "{{{.}}}" + {{#composedSchemas.anyOf}} + {{^isNull}} + {{{dataType}}}_result <- tryCatch({ + {{#isPrimitiveType}} + instance <- jsonlite::fromJSON(input, simplifyVector = FALSE) + if (typeof(instance) != "{{{dataType}}}") { + stop(sprintf("Data type doesn't match. Expected: %s. Actual: %s.", "{{{dataType}}}", typeof(instance))) + } + {{/isPrimitiveType}} + {{^isPrimitiveType}} + {{{dataType}}}$public_methods$validateJSON(input) + {{{dataType}}}_instance <- {{{dataType}}}$new() + {{/isPrimitiveType}} + self$actual_instance <- {{{dataType}}}_instance$fromJSON(input) + self$actual_type <- "{{{dataType}}}" return(self) }, error = function(err) err ) - if (!is.null({{{.}}}_result["error"])) { - error_messages <- append(error_messages, {{{.}}}_result["message"]) + if (!is.null({{{dataType}}}_result["error"])) { + error_messages <- append(error_messages, {{{dataType}}}_result["message"]) } - {{/anyOf}} + {{/isNull}} + {{/composedSchemas.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 = ", "))) diff --git a/modules/openapi-generator/src/main/resources/r/modelOneOf.mustache b/modules/openapi-generator/src/main/resources/r/modelOneOf.mustache index fc2b147940e..fa798be2110 100644 --- a/modules/openapi-generator/src/main/resources/r/modelOneOf.mustache +++ b/modules/openapi-generator/src/main/resources/r/modelOneOf.mustache @@ -15,7 +15,7 @@ actual_instance = NULL, #' @field actual_type the type of the object stored in this instance. actual_type = NULL, - #' @field one_of a list of object types defined in the oneOf schema. + #' @field one_of a list of types defined in the oneOf schema. one_of = list({{#oneOf}}"{{{.}}}"{{^-last}}, {{/-last}}{{/oneOf}}), #' Initialize a new {{{classname}}}. #' @@ -61,23 +61,33 @@ error_messages <- list() instance <- NULL - {{#oneOf}} - {{{.}}}_result <- tryCatch({ - {{{.}}}$public_methods$validateJSON(input) - {{{.}}}_instance <- {{{.}}}$new() - instance <- {{{.}}}_instance$fromJSON(input) - instance_type <- "{{{.}}}" - matched_schemas <- append(matched_schemas, "{{{.}}}") + {{#composedSchemas.oneOf}} + {{^isNull}} + {{{dataType}}}_result <- tryCatch({ + {{#isPrimitiveType}} + instance <- jsonlite::fromJSON(input, simplifyVector = FALSE) + if (typeof(instance) != "{{{dataType}}}") { + stop(sprintf("Data type doesn't match. Expected: %s. Actual: %s.", "{{{dataType}}}", typeof(instance))) + } + {{/isPrimitiveType}} + {{^isPrimitiveType}} + {{{dataType}}}$public_methods$validateJSON(input) + {{{dataType}}}_instance <- {{{dataType}}}$new() + instance <- {{{dataType}}}_instance$fromJSON(input) + {{/isPrimitiveType}} + instance_type <- "{{{dataType}}}" + matched_schemas <- append(matched_schemas, "{{{dataType}}}") matched <- matched + 1 }, error = function(err) err ) - if (!is.null({{{.}}}_result["error"])) { - error_messages <- append(error_messages, {{{.}}}_result["message"]) + if (!is.null({{{dataType}}}_result["error"])) { + error_messages <- append(error_messages, {{{dataType}}}_result["message"]) } - {{/oneOf}} + {{/isNull}} + {{/composedSchemas.oneOf}} if (matched == 1) { # successfully match exactly 1 schema specified in oneOf self$actual_instance <- instance 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 65cc1314d65..9ee786b1052 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 @@ -933,6 +933,14 @@ components: - $ref: '#/components/schemas/DanishPig' discriminator: propertyName: className + OneOfPrimitiveTypeTest: + oneOf: + - type: "integer" + - type: "string" + AnyOfPrimitiveTypeTest: + oneOf: + - type: "integer" + - type: "string" BasquePig: type: object properties: diff --git a/samples/client/petstore/R-httr2-wrapper/.openapi-generator/FILES b/samples/client/petstore/R-httr2-wrapper/.openapi-generator/FILES index 47f88c5a6b6..881a2ab283e 100644 --- a/samples/client/petstore/R-httr2-wrapper/.openapi-generator/FILES +++ b/samples/client/petstore/R-httr2-wrapper/.openapi-generator/FILES @@ -8,6 +8,7 @@ NAMESPACE R/allof_tag_api_response.R R/animal.R R/any_of_pig.R +R/any_of_primitive_type_test.R R/api_client.R R/api_exception.R R/api_response.R @@ -21,6 +22,7 @@ R/dog_all_of.R R/fake_api.R R/model_api_response.R R/nested_one_of.R +R/one_of_primitive_type_test.R R/order.R R/pet.R R/pet_api.R @@ -36,6 +38,7 @@ README.md docs/AllofTagApiResponse.md docs/Animal.md docs/AnyOfPig.md +docs/AnyOfPrimitiveTypeTest.md docs/BasquePig.md docs/Cat.md docs/CatAllOf.md @@ -46,6 +49,7 @@ docs/DogAllOf.md docs/FakeApi.md docs/ModelApiResponse.md docs/NestedOneOf.md +docs/OneOfPrimitiveTypeTest.md docs/Order.md docs/Pet.md docs/PetApi.md diff --git a/samples/client/petstore/R-httr2-wrapper/NAMESPACE b/samples/client/petstore/R-httr2-wrapper/NAMESPACE index 049543f1a55..7af9abbfc48 100644 --- a/samples/client/petstore/R-httr2-wrapper/NAMESPACE +++ b/samples/client/petstore/R-httr2-wrapper/NAMESPACE @@ -19,6 +19,7 @@ export(ApiException) export(AllofTagApiResponse) export(Animal) export(AnyOfPig) +export(AnyOfPrimitiveTypeTest) export(BasquePig) export(Cat) export(CatAllOf) @@ -28,6 +29,7 @@ export(Dog) export(DogAllOf) export(ModelApiResponse) export(NestedOneOf) +export(OneOfPrimitiveTypeTest) export(Order) export(Pet) export(Pig) diff --git a/samples/client/petstore/R-httr2-wrapper/R/any_of_primitive_type_test.R b/samples/client/petstore/R-httr2-wrapper/R/any_of_primitive_type_test.R new file mode 100644 index 00000000000..7e668710023 --- /dev/null +++ b/samples/client/petstore/R-httr2-wrapper/R/any_of_primitive_type_test.R @@ -0,0 +1,186 @@ +#' 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 AnyOfPrimitiveTypeTest +#' +#' @description AnyOfPrimitiveTypeTest Class +#' +#' @format An \code{R6Class} generator object +#' +#' @importFrom R6 R6Class +#' @importFrom jsonlite fromJSON toJSON +#' @export +AnyOfPrimitiveTypeTest <- R6::R6Class( + "AnyOfPrimitiveTypeTest", + public = list( + #' @field actual_instance the object stored in this instance. + actual_instance = NULL, + #' @field actual_type the type of the object stored in this instance. + actual_type = NULL, + #' @field one_of a list of types defined in the oneOf schema. + one_of = list("character", "integer"), + #' Initialize a new AnyOfPrimitiveTypeTest. + #' + #' @description + #' Initialize a new AnyOfPrimitiveTypeTest. + #' + #' @param instance an instance of the object defined in the oneOf schemas: "character", "integer" + #' @export + initialize = function(instance = NULL) { + if (is.null(instance)) { + # do nothing + } else if (get(class(instance)[[1]], pos = -1)$classname == "character") { + self$actual_instance <- instance + self$actual_type <- "character" + } else if (get(class(instance)[[1]], pos = -1)$classname == "integer") { + self$actual_instance <- instance + self$actual_type <- "integer" + } else { + stop(paste("Failed to initialize AnyOfPrimitiveTypeTest with oneOf schemas character, integer. Provided class name: ", + get(class(instance)[[1]], pos = -1)$classname)) + } + }, + #' Deserialize JSON string into an instance of AnyOfPrimitiveTypeTest. + #' + #' @description + #' Deserialize JSON string into an instance of AnyOfPrimitiveTypeTest. + #' An alias to the method `fromJSON` . + #' + #' @param input The input JSON. + #' @return An instance of AnyOfPrimitiveTypeTest. + #' @export + fromJSONString = function(input) { + self$fromJSON(input) + }, + #' Deserialize JSON string into an instance of AnyOfPrimitiveTypeTest. + #' + #' @description + #' Deserialize JSON string into an instance of AnyOfPrimitiveTypeTest. + #' + #' @param input The input JSON. + #' @return An instance of AnyOfPrimitiveTypeTest. + #' @export + fromJSON = function(input) { + matched <- 0 # match counter + matched_schemas <- list() #names of matched schemas + error_messages <- list() + instance <- NULL + + integer_result <- tryCatch({ + instance <- jsonlite::fromJSON(input, simplifyVector = FALSE) + if (typeof(instance) != "integer") { + stop(sprintf("Data type doesn't match. Expected: %s. Actual: %s.", "integer", typeof(instance))) + } + instance_type <- "integer" + matched_schemas <- append(matched_schemas, "integer") + matched <- matched + 1 + }, + error = function(err) err + ) + + if (!is.null(integer_result["error"])) { + error_messages <- append(error_messages, integer_result["message"]) + } + + character_result <- tryCatch({ + instance <- jsonlite::fromJSON(input, simplifyVector = FALSE) + if (typeof(instance) != "character") { + stop(sprintf("Data type doesn't match. Expected: %s. Actual: %s.", "character", typeof(instance))) + } + instance_type <- "character" + matched_schemas <- append(matched_schemas, "character") + matched <- matched + 1 + }, + error = function(err) err + ) + + if (!is.null(character_result["error"])) { + error_messages <- append(error_messages, character_result["message"]) + } + + if (matched == 1) { + # successfully match exactly 1 schema specified in oneOf + self$actual_instance <- instance + self$actual_type <- instance_type + } else if (matched > 1) { + # more than 1 match + stop("Multiple matches found when deserializing the payload into AnyOfPrimitiveTypeTest with oneOf schemas character, integer.") + } else { + # no match + stop(paste("No match found when deserializing the payload into AnyOfPrimitiveTypeTest with oneOf schemas character, integer. Details: ", + paste(error_messages, collapse = ", "))) + } + + self + }, + #' Serialize AnyOfPrimitiveTypeTest to JSON string. + #' + #' @description + #' Serialize AnyOfPrimitiveTypeTest to JSON string. + #' + #' @return JSON string representation of the AnyOfPrimitiveTypeTest. + #' @export + toJSONString = function() { + if (!is.null(self$actual_instance)) { + as.character(jsonlite::minify(self$actual_instance$toJSONString())) + } else { + NULL + } + }, + #' Serialize AnyOfPrimitiveTypeTest to JSON. + #' + #' @description + #' Serialize AnyOfPrimitiveTypeTest to JSON. + #' + #' @return JSON representation of the AnyOfPrimitiveTypeTest. + #' @export + toJSON = function() { + if (!is.null(self$actual_instance)) { + self$actual_instance$toJSON() + } else { + NULL + } + }, + #' Validate the input JSON with respect to AnyOfPrimitiveTypeTest. + #' + #' @description + #' Validate the input JSON with respect to AnyOfPrimitiveTypeTest and + #' throw exception if invalid. + #' + #' @param input The input JSON. + #' @export + 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 + }, + #' Returns the string representation of the instance. + #' + #' @description + #' Returns the string representation of the instance. + #' + #' @return The string representation of the instance. + #' @export + toString = function() { + jsoncontent <- c( + sprintf('"actual_instance": %s', if (is.null(self$actual_instance)) NULL else self$actual_instance$toJSONString()), + sprintf('"actual_type": "%s"', self$actual_type), + sprintf('"one_of": "%s"', paste(unlist(self$one_of), collapse = ", ")) + ) + jsoncontent <- paste(jsoncontent, collapse = ",") + as.character(jsonlite::prettify(paste("{", jsoncontent, "}", sep = ""))) + } + ) +) diff --git a/samples/client/petstore/R-httr2-wrapper/R/one_of_primitive_type_test.R b/samples/client/petstore/R-httr2-wrapper/R/one_of_primitive_type_test.R new file mode 100644 index 00000000000..8815ba1d3ee --- /dev/null +++ b/samples/client/petstore/R-httr2-wrapper/R/one_of_primitive_type_test.R @@ -0,0 +1,186 @@ +#' 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 OneOfPrimitiveTypeTest +#' +#' @description OneOfPrimitiveTypeTest Class +#' +#' @format An \code{R6Class} generator object +#' +#' @importFrom R6 R6Class +#' @importFrom jsonlite fromJSON toJSON +#' @export +OneOfPrimitiveTypeTest <- R6::R6Class( + "OneOfPrimitiveTypeTest", + public = list( + #' @field actual_instance the object stored in this instance. + actual_instance = NULL, + #' @field actual_type the type of the object stored in this instance. + actual_type = NULL, + #' @field one_of a list of types defined in the oneOf schema. + one_of = list("character", "integer"), + #' Initialize a new OneOfPrimitiveTypeTest. + #' + #' @description + #' Initialize a new OneOfPrimitiveTypeTest. + #' + #' @param instance an instance of the object defined in the oneOf schemas: "character", "integer" + #' @export + initialize = function(instance = NULL) { + if (is.null(instance)) { + # do nothing + } else if (get(class(instance)[[1]], pos = -1)$classname == "character") { + self$actual_instance <- instance + self$actual_type <- "character" + } else if (get(class(instance)[[1]], pos = -1)$classname == "integer") { + self$actual_instance <- instance + self$actual_type <- "integer" + } else { + stop(paste("Failed to initialize OneOfPrimitiveTypeTest with oneOf schemas character, integer. Provided class name: ", + get(class(instance)[[1]], pos = -1)$classname)) + } + }, + #' Deserialize JSON string into an instance of OneOfPrimitiveTypeTest. + #' + #' @description + #' Deserialize JSON string into an instance of OneOfPrimitiveTypeTest. + #' An alias to the method `fromJSON` . + #' + #' @param input The input JSON. + #' @return An instance of OneOfPrimitiveTypeTest. + #' @export + fromJSONString = function(input) { + self$fromJSON(input) + }, + #' Deserialize JSON string into an instance of OneOfPrimitiveTypeTest. + #' + #' @description + #' Deserialize JSON string into an instance of OneOfPrimitiveTypeTest. + #' + #' @param input The input JSON. + #' @return An instance of OneOfPrimitiveTypeTest. + #' @export + fromJSON = function(input) { + matched <- 0 # match counter + matched_schemas <- list() #names of matched schemas + error_messages <- list() + instance <- NULL + + integer_result <- tryCatch({ + instance <- jsonlite::fromJSON(input, simplifyVector = FALSE) + if (typeof(instance) != "integer") { + stop(sprintf("Data type doesn't match. Expected: %s. Actual: %s.", "integer", typeof(instance))) + } + instance_type <- "integer" + matched_schemas <- append(matched_schemas, "integer") + matched <- matched + 1 + }, + error = function(err) err + ) + + if (!is.null(integer_result["error"])) { + error_messages <- append(error_messages, integer_result["message"]) + } + + character_result <- tryCatch({ + instance <- jsonlite::fromJSON(input, simplifyVector = FALSE) + if (typeof(instance) != "character") { + stop(sprintf("Data type doesn't match. Expected: %s. Actual: %s.", "character", typeof(instance))) + } + instance_type <- "character" + matched_schemas <- append(matched_schemas, "character") + matched <- matched + 1 + }, + error = function(err) err + ) + + if (!is.null(character_result["error"])) { + error_messages <- append(error_messages, character_result["message"]) + } + + if (matched == 1) { + # successfully match exactly 1 schema specified in oneOf + self$actual_instance <- instance + self$actual_type <- instance_type + } else if (matched > 1) { + # more than 1 match + stop("Multiple matches found when deserializing the payload into OneOfPrimitiveTypeTest with oneOf schemas character, integer.") + } else { + # no match + stop(paste("No match found when deserializing the payload into OneOfPrimitiveTypeTest with oneOf schemas character, integer. Details: ", + paste(error_messages, collapse = ", "))) + } + + self + }, + #' Serialize OneOfPrimitiveTypeTest to JSON string. + #' + #' @description + #' Serialize OneOfPrimitiveTypeTest to JSON string. + #' + #' @return JSON string representation of the OneOfPrimitiveTypeTest. + #' @export + toJSONString = function() { + if (!is.null(self$actual_instance)) { + as.character(jsonlite::minify(self$actual_instance$toJSONString())) + } else { + NULL + } + }, + #' Serialize OneOfPrimitiveTypeTest to JSON. + #' + #' @description + #' Serialize OneOfPrimitiveTypeTest to JSON. + #' + #' @return JSON representation of the OneOfPrimitiveTypeTest. + #' @export + toJSON = function() { + if (!is.null(self$actual_instance)) { + self$actual_instance$toJSON() + } else { + NULL + } + }, + #' Validate the input JSON with respect to OneOfPrimitiveTypeTest. + #' + #' @description + #' Validate the input JSON with respect to OneOfPrimitiveTypeTest and + #' throw exception if invalid. + #' + #' @param input The input JSON. + #' @export + 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 + }, + #' Returns the string representation of the instance. + #' + #' @description + #' Returns the string representation of the instance. + #' + #' @return The string representation of the instance. + #' @export + toString = function() { + jsoncontent <- c( + sprintf('"actual_instance": %s', if (is.null(self$actual_instance)) NULL else self$actual_instance$toJSONString()), + sprintf('"actual_type": "%s"', self$actual_type), + sprintf('"one_of": "%s"', paste(unlist(self$one_of), collapse = ", ")) + ) + jsoncontent <- paste(jsoncontent, collapse = ",") + as.character(jsonlite::prettify(paste("{", jsoncontent, "}", sep = ""))) + } + ) +) diff --git a/samples/client/petstore/R-httr2-wrapper/R/pig.R b/samples/client/petstore/R-httr2-wrapper/R/pig.R index dbe013e4168..9cdb7a38770 100644 --- a/samples/client/petstore/R-httr2-wrapper/R/pig.R +++ b/samples/client/petstore/R-httr2-wrapper/R/pig.R @@ -22,7 +22,7 @@ Pig <- R6::R6Class( actual_instance = NULL, #' @field actual_type the type of the object stored in this instance. actual_type = NULL, - #' @field one_of a list of object types defined in the oneOf schema. + #' @field one_of a list of types defined in the oneOf schema. one_of = list("BasquePig", "DanishPig"), #' Initialize a new Pig. #' diff --git a/samples/client/petstore/R-httr2-wrapper/README.md b/samples/client/petstore/R-httr2-wrapper/README.md index eeed062a6ff..7b97c29fe51 100644 --- a/samples/client/petstore/R-httr2-wrapper/README.md +++ b/samples/client/petstore/R-httr2-wrapper/README.md @@ -92,6 +92,7 @@ Class | Method | HTTP request | Description - [AllofTagApiResponse](docs/AllofTagApiResponse.md) - [Animal](docs/Animal.md) - [AnyOfPig](docs/AnyOfPig.md) + - [AnyOfPrimitiveTypeTest](docs/AnyOfPrimitiveTypeTest.md) - [BasquePig](docs/BasquePig.md) - [Cat](docs/Cat.md) - [CatAllOf](docs/CatAllOf.md) @@ -101,6 +102,7 @@ Class | Method | HTTP request | Description - [DogAllOf](docs/DogAllOf.md) - [ModelApiResponse](docs/ModelApiResponse.md) - [NestedOneOf](docs/NestedOneOf.md) + - [OneOfPrimitiveTypeTest](docs/OneOfPrimitiveTypeTest.md) - [Order](docs/Order.md) - [Pet](docs/Pet.md) - [Pig](docs/Pig.md) diff --git a/samples/client/petstore/R-httr2-wrapper/docs/AnyOfPrimitiveTypeTest.md b/samples/client/petstore/R-httr2-wrapper/docs/AnyOfPrimitiveTypeTest.md new file mode 100644 index 00000000000..0d8ba0e63b1 --- /dev/null +++ b/samples/client/petstore/R-httr2-wrapper/docs/AnyOfPrimitiveTypeTest.md @@ -0,0 +1,8 @@ +# petstore::AnyOfPrimitiveTypeTest + + +## Properties +Name | Type | Description | Notes +------------ | ------------- | ------------- | ------------- + + diff --git a/samples/client/petstore/R-httr2-wrapper/docs/OneOfPrimitiveTypeTest.md b/samples/client/petstore/R-httr2-wrapper/docs/OneOfPrimitiveTypeTest.md new file mode 100644 index 00000000000..82cf318431f --- /dev/null +++ b/samples/client/petstore/R-httr2-wrapper/docs/OneOfPrimitiveTypeTest.md @@ -0,0 +1,8 @@ +# petstore::OneOfPrimitiveTypeTest + + +## Properties +Name | Type | Description | Notes +------------ | ------------- | ------------- | ------------- + + diff --git a/samples/client/petstore/R-httr2-wrapper/tests/testthat/test_any_of_primitive_type_test.R b/samples/client/petstore/R-httr2-wrapper/tests/testthat/test_any_of_primitive_type_test.R new file mode 100644 index 00000000000..0679cfaf443 --- /dev/null +++ b/samples/client/petstore/R-httr2-wrapper/tests/testthat/test_any_of_primitive_type_test.R @@ -0,0 +1,4 @@ +# Automatically generated by openapi-generator (https://openapi-generator.tech) +# Please update as you see appropriate + +context("Test AnyOfPrimitiveTypeTest") diff --git a/samples/client/petstore/R-httr2-wrapper/tests/testthat/test_one_of_primitive_type_test.R b/samples/client/petstore/R-httr2-wrapper/tests/testthat/test_one_of_primitive_type_test.R new file mode 100644 index 00000000000..f9e7158359b --- /dev/null +++ b/samples/client/petstore/R-httr2-wrapper/tests/testthat/test_one_of_primitive_type_test.R @@ -0,0 +1,4 @@ +# Automatically generated by openapi-generator (https://openapi-generator.tech) +# Please update as you see appropriate + +context("Test OneOfPrimitiveTypeTest") diff --git a/samples/client/petstore/R-httr2/.openapi-generator/FILES b/samples/client/petstore/R-httr2/.openapi-generator/FILES index 7a05f5fdda5..11069aa4bcc 100644 --- a/samples/client/petstore/R-httr2/.openapi-generator/FILES +++ b/samples/client/petstore/R-httr2/.openapi-generator/FILES @@ -8,6 +8,7 @@ NAMESPACE R/allof_tag_api_response.R R/animal.R R/any_of_pig.R +R/any_of_primitive_type_test.R R/api_client.R R/api_exception.R R/api_response.R @@ -21,6 +22,7 @@ R/dog_all_of.R R/fake_api.R R/model_api_response.R R/nested_one_of.R +R/one_of_primitive_type_test.R R/order.R R/pet.R R/pet_api.R @@ -35,6 +37,7 @@ README.md docs/AllofTagApiResponse.md docs/Animal.md docs/AnyOfPig.md +docs/AnyOfPrimitiveTypeTest.md docs/BasquePig.md docs/Cat.md docs/CatAllOf.md @@ -45,6 +48,7 @@ docs/DogAllOf.md docs/FakeApi.md docs/ModelApiResponse.md docs/NestedOneOf.md +docs/OneOfPrimitiveTypeTest.md docs/Order.md docs/Pet.md docs/PetApi.md diff --git a/samples/client/petstore/R-httr2/NAMESPACE b/samples/client/petstore/R-httr2/NAMESPACE index d1c5432b4f7..d26ce29357a 100644 --- a/samples/client/petstore/R-httr2/NAMESPACE +++ b/samples/client/petstore/R-httr2/NAMESPACE @@ -17,6 +17,7 @@ export(ApiException) export(AllofTagApiResponse) export(Animal) export(AnyOfPig) +export(AnyOfPrimitiveTypeTest) export(BasquePig) export(Cat) export(CatAllOf) @@ -26,6 +27,7 @@ export(Dog) export(DogAllOf) export(ModelApiResponse) export(NestedOneOf) +export(OneOfPrimitiveTypeTest) export(Order) export(Pet) export(Pig) diff --git a/samples/client/petstore/R-httr2/R/any_of_primitive_type_test.R b/samples/client/petstore/R-httr2/R/any_of_primitive_type_test.R new file mode 100644 index 00000000000..7e668710023 --- /dev/null +++ b/samples/client/petstore/R-httr2/R/any_of_primitive_type_test.R @@ -0,0 +1,186 @@ +#' 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 AnyOfPrimitiveTypeTest +#' +#' @description AnyOfPrimitiveTypeTest Class +#' +#' @format An \code{R6Class} generator object +#' +#' @importFrom R6 R6Class +#' @importFrom jsonlite fromJSON toJSON +#' @export +AnyOfPrimitiveTypeTest <- R6::R6Class( + "AnyOfPrimitiveTypeTest", + public = list( + #' @field actual_instance the object stored in this instance. + actual_instance = NULL, + #' @field actual_type the type of the object stored in this instance. + actual_type = NULL, + #' @field one_of a list of types defined in the oneOf schema. + one_of = list("character", "integer"), + #' Initialize a new AnyOfPrimitiveTypeTest. + #' + #' @description + #' Initialize a new AnyOfPrimitiveTypeTest. + #' + #' @param instance an instance of the object defined in the oneOf schemas: "character", "integer" + #' @export + initialize = function(instance = NULL) { + if (is.null(instance)) { + # do nothing + } else if (get(class(instance)[[1]], pos = -1)$classname == "character") { + self$actual_instance <- instance + self$actual_type <- "character" + } else if (get(class(instance)[[1]], pos = -1)$classname == "integer") { + self$actual_instance <- instance + self$actual_type <- "integer" + } else { + stop(paste("Failed to initialize AnyOfPrimitiveTypeTest with oneOf schemas character, integer. Provided class name: ", + get(class(instance)[[1]], pos = -1)$classname)) + } + }, + #' Deserialize JSON string into an instance of AnyOfPrimitiveTypeTest. + #' + #' @description + #' Deserialize JSON string into an instance of AnyOfPrimitiveTypeTest. + #' An alias to the method `fromJSON` . + #' + #' @param input The input JSON. + #' @return An instance of AnyOfPrimitiveTypeTest. + #' @export + fromJSONString = function(input) { + self$fromJSON(input) + }, + #' Deserialize JSON string into an instance of AnyOfPrimitiveTypeTest. + #' + #' @description + #' Deserialize JSON string into an instance of AnyOfPrimitiveTypeTest. + #' + #' @param input The input JSON. + #' @return An instance of AnyOfPrimitiveTypeTest. + #' @export + fromJSON = function(input) { + matched <- 0 # match counter + matched_schemas <- list() #names of matched schemas + error_messages <- list() + instance <- NULL + + integer_result <- tryCatch({ + instance <- jsonlite::fromJSON(input, simplifyVector = FALSE) + if (typeof(instance) != "integer") { + stop(sprintf("Data type doesn't match. Expected: %s. Actual: %s.", "integer", typeof(instance))) + } + instance_type <- "integer" + matched_schemas <- append(matched_schemas, "integer") + matched <- matched + 1 + }, + error = function(err) err + ) + + if (!is.null(integer_result["error"])) { + error_messages <- append(error_messages, integer_result["message"]) + } + + character_result <- tryCatch({ + instance <- jsonlite::fromJSON(input, simplifyVector = FALSE) + if (typeof(instance) != "character") { + stop(sprintf("Data type doesn't match. Expected: %s. Actual: %s.", "character", typeof(instance))) + } + instance_type <- "character" + matched_schemas <- append(matched_schemas, "character") + matched <- matched + 1 + }, + error = function(err) err + ) + + if (!is.null(character_result["error"])) { + error_messages <- append(error_messages, character_result["message"]) + } + + if (matched == 1) { + # successfully match exactly 1 schema specified in oneOf + self$actual_instance <- instance + self$actual_type <- instance_type + } else if (matched > 1) { + # more than 1 match + stop("Multiple matches found when deserializing the payload into AnyOfPrimitiveTypeTest with oneOf schemas character, integer.") + } else { + # no match + stop(paste("No match found when deserializing the payload into AnyOfPrimitiveTypeTest with oneOf schemas character, integer. Details: ", + paste(error_messages, collapse = ", "))) + } + + self + }, + #' Serialize AnyOfPrimitiveTypeTest to JSON string. + #' + #' @description + #' Serialize AnyOfPrimitiveTypeTest to JSON string. + #' + #' @return JSON string representation of the AnyOfPrimitiveTypeTest. + #' @export + toJSONString = function() { + if (!is.null(self$actual_instance)) { + as.character(jsonlite::minify(self$actual_instance$toJSONString())) + } else { + NULL + } + }, + #' Serialize AnyOfPrimitiveTypeTest to JSON. + #' + #' @description + #' Serialize AnyOfPrimitiveTypeTest to JSON. + #' + #' @return JSON representation of the AnyOfPrimitiveTypeTest. + #' @export + toJSON = function() { + if (!is.null(self$actual_instance)) { + self$actual_instance$toJSON() + } else { + NULL + } + }, + #' Validate the input JSON with respect to AnyOfPrimitiveTypeTest. + #' + #' @description + #' Validate the input JSON with respect to AnyOfPrimitiveTypeTest and + #' throw exception if invalid. + #' + #' @param input The input JSON. + #' @export + 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 + }, + #' Returns the string representation of the instance. + #' + #' @description + #' Returns the string representation of the instance. + #' + #' @return The string representation of the instance. + #' @export + toString = function() { + jsoncontent <- c( + sprintf('"actual_instance": %s', if (is.null(self$actual_instance)) NULL else self$actual_instance$toJSONString()), + sprintf('"actual_type": "%s"', self$actual_type), + sprintf('"one_of": "%s"', paste(unlist(self$one_of), collapse = ", ")) + ) + jsoncontent <- paste(jsoncontent, collapse = ",") + as.character(jsonlite::prettify(paste("{", jsoncontent, "}", sep = ""))) + } + ) +) diff --git a/samples/client/petstore/R-httr2/R/one_of_primitive_type_test.R b/samples/client/petstore/R-httr2/R/one_of_primitive_type_test.R new file mode 100644 index 00000000000..8815ba1d3ee --- /dev/null +++ b/samples/client/petstore/R-httr2/R/one_of_primitive_type_test.R @@ -0,0 +1,186 @@ +#' 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 OneOfPrimitiveTypeTest +#' +#' @description OneOfPrimitiveTypeTest Class +#' +#' @format An \code{R6Class} generator object +#' +#' @importFrom R6 R6Class +#' @importFrom jsonlite fromJSON toJSON +#' @export +OneOfPrimitiveTypeTest <- R6::R6Class( + "OneOfPrimitiveTypeTest", + public = list( + #' @field actual_instance the object stored in this instance. + actual_instance = NULL, + #' @field actual_type the type of the object stored in this instance. + actual_type = NULL, + #' @field one_of a list of types defined in the oneOf schema. + one_of = list("character", "integer"), + #' Initialize a new OneOfPrimitiveTypeTest. + #' + #' @description + #' Initialize a new OneOfPrimitiveTypeTest. + #' + #' @param instance an instance of the object defined in the oneOf schemas: "character", "integer" + #' @export + initialize = function(instance = NULL) { + if (is.null(instance)) { + # do nothing + } else if (get(class(instance)[[1]], pos = -1)$classname == "character") { + self$actual_instance <- instance + self$actual_type <- "character" + } else if (get(class(instance)[[1]], pos = -1)$classname == "integer") { + self$actual_instance <- instance + self$actual_type <- "integer" + } else { + stop(paste("Failed to initialize OneOfPrimitiveTypeTest with oneOf schemas character, integer. Provided class name: ", + get(class(instance)[[1]], pos = -1)$classname)) + } + }, + #' Deserialize JSON string into an instance of OneOfPrimitiveTypeTest. + #' + #' @description + #' Deserialize JSON string into an instance of OneOfPrimitiveTypeTest. + #' An alias to the method `fromJSON` . + #' + #' @param input The input JSON. + #' @return An instance of OneOfPrimitiveTypeTest. + #' @export + fromJSONString = function(input) { + self$fromJSON(input) + }, + #' Deserialize JSON string into an instance of OneOfPrimitiveTypeTest. + #' + #' @description + #' Deserialize JSON string into an instance of OneOfPrimitiveTypeTest. + #' + #' @param input The input JSON. + #' @return An instance of OneOfPrimitiveTypeTest. + #' @export + fromJSON = function(input) { + matched <- 0 # match counter + matched_schemas <- list() #names of matched schemas + error_messages <- list() + instance <- NULL + + integer_result <- tryCatch({ + instance <- jsonlite::fromJSON(input, simplifyVector = FALSE) + if (typeof(instance) != "integer") { + stop(sprintf("Data type doesn't match. Expected: %s. Actual: %s.", "integer", typeof(instance))) + } + instance_type <- "integer" + matched_schemas <- append(matched_schemas, "integer") + matched <- matched + 1 + }, + error = function(err) err + ) + + if (!is.null(integer_result["error"])) { + error_messages <- append(error_messages, integer_result["message"]) + } + + character_result <- tryCatch({ + instance <- jsonlite::fromJSON(input, simplifyVector = FALSE) + if (typeof(instance) != "character") { + stop(sprintf("Data type doesn't match. Expected: %s. Actual: %s.", "character", typeof(instance))) + } + instance_type <- "character" + matched_schemas <- append(matched_schemas, "character") + matched <- matched + 1 + }, + error = function(err) err + ) + + if (!is.null(character_result["error"])) { + error_messages <- append(error_messages, character_result["message"]) + } + + if (matched == 1) { + # successfully match exactly 1 schema specified in oneOf + self$actual_instance <- instance + self$actual_type <- instance_type + } else if (matched > 1) { + # more than 1 match + stop("Multiple matches found when deserializing the payload into OneOfPrimitiveTypeTest with oneOf schemas character, integer.") + } else { + # no match + stop(paste("No match found when deserializing the payload into OneOfPrimitiveTypeTest with oneOf schemas character, integer. Details: ", + paste(error_messages, collapse = ", "))) + } + + self + }, + #' Serialize OneOfPrimitiveTypeTest to JSON string. + #' + #' @description + #' Serialize OneOfPrimitiveTypeTest to JSON string. + #' + #' @return JSON string representation of the OneOfPrimitiveTypeTest. + #' @export + toJSONString = function() { + if (!is.null(self$actual_instance)) { + as.character(jsonlite::minify(self$actual_instance$toJSONString())) + } else { + NULL + } + }, + #' Serialize OneOfPrimitiveTypeTest to JSON. + #' + #' @description + #' Serialize OneOfPrimitiveTypeTest to JSON. + #' + #' @return JSON representation of the OneOfPrimitiveTypeTest. + #' @export + toJSON = function() { + if (!is.null(self$actual_instance)) { + self$actual_instance$toJSON() + } else { + NULL + } + }, + #' Validate the input JSON with respect to OneOfPrimitiveTypeTest. + #' + #' @description + #' Validate the input JSON with respect to OneOfPrimitiveTypeTest and + #' throw exception if invalid. + #' + #' @param input The input JSON. + #' @export + 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 + }, + #' Returns the string representation of the instance. + #' + #' @description + #' Returns the string representation of the instance. + #' + #' @return The string representation of the instance. + #' @export + toString = function() { + jsoncontent <- c( + sprintf('"actual_instance": %s', if (is.null(self$actual_instance)) NULL else self$actual_instance$toJSONString()), + sprintf('"actual_type": "%s"', self$actual_type), + sprintf('"one_of": "%s"', paste(unlist(self$one_of), collapse = ", ")) + ) + jsoncontent <- paste(jsoncontent, collapse = ",") + as.character(jsonlite::prettify(paste("{", jsoncontent, "}", sep = ""))) + } + ) +) diff --git a/samples/client/petstore/R-httr2/R/pig.R b/samples/client/petstore/R-httr2/R/pig.R index dbe013e4168..9cdb7a38770 100644 --- a/samples/client/petstore/R-httr2/R/pig.R +++ b/samples/client/petstore/R-httr2/R/pig.R @@ -22,7 +22,7 @@ Pig <- R6::R6Class( actual_instance = NULL, #' @field actual_type the type of the object stored in this instance. actual_type = NULL, - #' @field one_of a list of object types defined in the oneOf schema. + #' @field one_of a list of types defined in the oneOf schema. one_of = list("BasquePig", "DanishPig"), #' Initialize a new Pig. #' diff --git a/samples/client/petstore/R-httr2/README.md b/samples/client/petstore/R-httr2/README.md index eeed062a6ff..7b97c29fe51 100644 --- a/samples/client/petstore/R-httr2/README.md +++ b/samples/client/petstore/R-httr2/README.md @@ -92,6 +92,7 @@ Class | Method | HTTP request | Description - [AllofTagApiResponse](docs/AllofTagApiResponse.md) - [Animal](docs/Animal.md) - [AnyOfPig](docs/AnyOfPig.md) + - [AnyOfPrimitiveTypeTest](docs/AnyOfPrimitiveTypeTest.md) - [BasquePig](docs/BasquePig.md) - [Cat](docs/Cat.md) - [CatAllOf](docs/CatAllOf.md) @@ -101,6 +102,7 @@ Class | Method | HTTP request | Description - [DogAllOf](docs/DogAllOf.md) - [ModelApiResponse](docs/ModelApiResponse.md) - [NestedOneOf](docs/NestedOneOf.md) + - [OneOfPrimitiveTypeTest](docs/OneOfPrimitiveTypeTest.md) - [Order](docs/Order.md) - [Pet](docs/Pet.md) - [Pig](docs/Pig.md) diff --git a/samples/client/petstore/R-httr2/docs/AnyOfPrimitiveTypeTest.md b/samples/client/petstore/R-httr2/docs/AnyOfPrimitiveTypeTest.md new file mode 100644 index 00000000000..0d8ba0e63b1 --- /dev/null +++ b/samples/client/petstore/R-httr2/docs/AnyOfPrimitiveTypeTest.md @@ -0,0 +1,8 @@ +# petstore::AnyOfPrimitiveTypeTest + + +## Properties +Name | Type | Description | Notes +------------ | ------------- | ------------- | ------------- + + diff --git a/samples/client/petstore/R-httr2/docs/OneOfPrimitiveTypeTest.md b/samples/client/petstore/R-httr2/docs/OneOfPrimitiveTypeTest.md new file mode 100644 index 00000000000..82cf318431f --- /dev/null +++ b/samples/client/petstore/R-httr2/docs/OneOfPrimitiveTypeTest.md @@ -0,0 +1,8 @@ +# petstore::OneOfPrimitiveTypeTest + + +## Properties +Name | Type | Description | Notes +------------ | ------------- | ------------- | ------------- + + diff --git a/samples/client/petstore/R-httr2/test_petstore.R b/samples/client/petstore/R-httr2/test_petstore.R index a0a15a2972b..fb9f30a7e78 100644 --- a/samples/client/petstore/R-httr2/test_petstore.R +++ b/samples/client/petstore/R-httr2/test_petstore.R @@ -3,27 +3,32 @@ install.packages("petstore_1.0.0.tar.gz",repos=NULL, type="source") library(petstore) library(jsonlite) -var_pet <- Pet$new("name_example", list("photoUrls_example"), 56, Category$new(56, "name_example"), list(Tag$new(56, "name_example")), "available") # Pet | Pet object that needs to be added to the store +t <- OneOfPrimitiveTypeTest$new() -#Add a new pet to the store -api_instance <- PetApi$new() -# Configure OAuth2 access token for authorization: petstore_auth -api_instance$api_client$access_token <- 'TODO_YOUR_ACCESS_TOKEN'; -result <- tryCatch( - # to save the result into a file, simply add the optional `data_file` parameter, e.g. - # api_instance$AddPet(var_pet, data_file = "result.txt"), - api_instance$add_pet(var_pet), - ApiException = function(ex) ex - ) +t$fromJSONString("[1,2,3]") -var_pet_id <- 56 # integer | ID of pet to return -pet_response <- api_instance$get_pet_by_id(var_pet_id, data_file = "get_pet_by_id.json") -response <- read_json("get_pet_by_id.json") -dput(response) - -# test streaming -api_instance$get_pet_by_id_streaming(var_pet_id, stream_callback = function(x) { print(x) }) +###var_pet <- Pet$new("name_example", list("photoUrls_example"), 56, Category$new(56, "name_example"), list(Tag$new(56, "name_example")), "available") # Pet | Pet object that needs to be added to the store +### +####Add a new pet to the store +###api_instance <- PetApi$new() +#### Configure OAuth2 access token for authorization: petstore_auth +###api_instance$api_client$access_token <- 'TODO_YOUR_ACCESS_TOKEN'; +###result <- tryCatch( +### # to save the result into a file, simply add the optional `data_file` parameter, e.g. +### # api_instance$AddPet(var_pet, data_file = "result.txt"), +### api_instance$add_pet(var_pet), +### ApiException = function(ex) ex +### ) +### +###var_pet_id <- 56 # integer | ID of pet to return +### +###pet_response <- api_instance$get_pet_by_id(var_pet_id, data_file = "get_pet_by_id.json") +###response <- read_json("get_pet_by_id.json") +###dput(response) +### +#### test streaming +###api_instance$get_pet_by_id_streaming(var_pet_id, stream_callback = function(x) { print(x) }) ##Find pet by ID (streaming) #api_instance <- PetApi$new() diff --git a/samples/client/petstore/R-httr2/tests/testthat/test_any_of_primitive_type_test.R b/samples/client/petstore/R-httr2/tests/testthat/test_any_of_primitive_type_test.R new file mode 100644 index 00000000000..0679cfaf443 --- /dev/null +++ b/samples/client/petstore/R-httr2/tests/testthat/test_any_of_primitive_type_test.R @@ -0,0 +1,4 @@ +# Automatically generated by openapi-generator (https://openapi-generator.tech) +# Please update as you see appropriate + +context("Test AnyOfPrimitiveTypeTest") diff --git a/samples/client/petstore/R-httr2/tests/testthat/test_one_of_primitive_type_test.R b/samples/client/petstore/R-httr2/tests/testthat/test_one_of_primitive_type_test.R new file mode 100644 index 00000000000..f9e7158359b --- /dev/null +++ b/samples/client/petstore/R-httr2/tests/testthat/test_one_of_primitive_type_test.R @@ -0,0 +1,4 @@ +# Automatically generated by openapi-generator (https://openapi-generator.tech) +# Please update as you see appropriate + +context("Test OneOfPrimitiveTypeTest") diff --git a/samples/client/petstore/R-httr2/tests/testthat/test_petstore.R b/samples/client/petstore/R-httr2/tests/testthat/test_petstore.R index bd5f6d326b6..73723ba1d63 100644 --- a/samples/client/petstore/R-httr2/tests/testthat/test_petstore.R +++ b/samples/client/petstore/R-httr2/tests/testthat/test_petstore.R @@ -260,6 +260,32 @@ test_that ("Tests validations", { }) +test_that("Tests oneOf primitive types", { + test <- OneOfPrimitiveTypeTest$new() + test$fromJSONString("\"123abc\"") + expect_equal(test$actual_instance, '123abc') + expect_equal(test$actual_type, 'character') + + test$fromJSONString("456") + expect_equal(test$actual_instance, 456) + expect_equal(test$actual_type, 'integer') + + expect_error(test$fromJSONString("[45,12]"), "No match found when deserializing the payload into OneOfPrimitiveTypeTest with oneOf schemas character, integer. Details: Data type doesn't match. Expected: integer. Actual: list., Data type doesn't match. Expected: character. Actual: list.") # should throw an error +}) + +test_that("Tests anyOf primitive types", { + test <- AnyOfPrimitiveTypeTest$new() + test$fromJSONString("\"123abc\"") + expect_equal(test$actual_instance, '123abc') + expect_equal(test$actual_type, 'character') + + test$fromJSONString("456") + expect_equal(test$actual_instance, 456) + expect_equal(test$actual_type, 'integer') + + expect_error(test$fromJSONString("[45,12]"), "No match found when deserializing the payload into AnyOfPrimitiveTypeTest with oneOf schemas character, integer. Details: Data type doesn't match. Expected: integer. Actual: list., Data type doesn't match. Expected: character. Actual: list.") # should throw an error +}) + test_that("Tests oneOf", { basque_pig_json <- '{"className": "BasquePig", "color": "red"}' diff --git a/samples/client/petstore/R/.openapi-generator/FILES b/samples/client/petstore/R/.openapi-generator/FILES index 7a05f5fdda5..11069aa4bcc 100644 --- a/samples/client/petstore/R/.openapi-generator/FILES +++ b/samples/client/petstore/R/.openapi-generator/FILES @@ -8,6 +8,7 @@ NAMESPACE R/allof_tag_api_response.R R/animal.R R/any_of_pig.R +R/any_of_primitive_type_test.R R/api_client.R R/api_exception.R R/api_response.R @@ -21,6 +22,7 @@ R/dog_all_of.R R/fake_api.R R/model_api_response.R R/nested_one_of.R +R/one_of_primitive_type_test.R R/order.R R/pet.R R/pet_api.R @@ -35,6 +37,7 @@ README.md docs/AllofTagApiResponse.md docs/Animal.md docs/AnyOfPig.md +docs/AnyOfPrimitiveTypeTest.md docs/BasquePig.md docs/Cat.md docs/CatAllOf.md @@ -45,6 +48,7 @@ docs/DogAllOf.md docs/FakeApi.md docs/ModelApiResponse.md docs/NestedOneOf.md +docs/OneOfPrimitiveTypeTest.md docs/Order.md docs/Pet.md docs/PetApi.md diff --git a/samples/client/petstore/R/NAMESPACE b/samples/client/petstore/R/NAMESPACE index 4dbc737fd94..d8e1c837ba1 100644 --- a/samples/client/petstore/R/NAMESPACE +++ b/samples/client/petstore/R/NAMESPACE @@ -17,6 +17,7 @@ export(ApiException) export(AllofTagApiResponse) export(Animal) export(AnyOfPig) +export(AnyOfPrimitiveTypeTest) export(BasquePig) export(Cat) export(CatAllOf) @@ -26,6 +27,7 @@ export(Dog) export(DogAllOf) export(ModelApiResponse) export(NestedOneOf) +export(OneOfPrimitiveTypeTest) export(Order) export(Pet) export(Pig) diff --git a/samples/client/petstore/R/R/any_of_primitive_type_test.R b/samples/client/petstore/R/R/any_of_primitive_type_test.R new file mode 100644 index 00000000000..7e668710023 --- /dev/null +++ b/samples/client/petstore/R/R/any_of_primitive_type_test.R @@ -0,0 +1,186 @@ +#' 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 AnyOfPrimitiveTypeTest +#' +#' @description AnyOfPrimitiveTypeTest Class +#' +#' @format An \code{R6Class} generator object +#' +#' @importFrom R6 R6Class +#' @importFrom jsonlite fromJSON toJSON +#' @export +AnyOfPrimitiveTypeTest <- R6::R6Class( + "AnyOfPrimitiveTypeTest", + public = list( + #' @field actual_instance the object stored in this instance. + actual_instance = NULL, + #' @field actual_type the type of the object stored in this instance. + actual_type = NULL, + #' @field one_of a list of types defined in the oneOf schema. + one_of = list("character", "integer"), + #' Initialize a new AnyOfPrimitiveTypeTest. + #' + #' @description + #' Initialize a new AnyOfPrimitiveTypeTest. + #' + #' @param instance an instance of the object defined in the oneOf schemas: "character", "integer" + #' @export + initialize = function(instance = NULL) { + if (is.null(instance)) { + # do nothing + } else if (get(class(instance)[[1]], pos = -1)$classname == "character") { + self$actual_instance <- instance + self$actual_type <- "character" + } else if (get(class(instance)[[1]], pos = -1)$classname == "integer") { + self$actual_instance <- instance + self$actual_type <- "integer" + } else { + stop(paste("Failed to initialize AnyOfPrimitiveTypeTest with oneOf schemas character, integer. Provided class name: ", + get(class(instance)[[1]], pos = -1)$classname)) + } + }, + #' Deserialize JSON string into an instance of AnyOfPrimitiveTypeTest. + #' + #' @description + #' Deserialize JSON string into an instance of AnyOfPrimitiveTypeTest. + #' An alias to the method `fromJSON` . + #' + #' @param input The input JSON. + #' @return An instance of AnyOfPrimitiveTypeTest. + #' @export + fromJSONString = function(input) { + self$fromJSON(input) + }, + #' Deserialize JSON string into an instance of AnyOfPrimitiveTypeTest. + #' + #' @description + #' Deserialize JSON string into an instance of AnyOfPrimitiveTypeTest. + #' + #' @param input The input JSON. + #' @return An instance of AnyOfPrimitiveTypeTest. + #' @export + fromJSON = function(input) { + matched <- 0 # match counter + matched_schemas <- list() #names of matched schemas + error_messages <- list() + instance <- NULL + + integer_result <- tryCatch({ + instance <- jsonlite::fromJSON(input, simplifyVector = FALSE) + if (typeof(instance) != "integer") { + stop(sprintf("Data type doesn't match. Expected: %s. Actual: %s.", "integer", typeof(instance))) + } + instance_type <- "integer" + matched_schemas <- append(matched_schemas, "integer") + matched <- matched + 1 + }, + error = function(err) err + ) + + if (!is.null(integer_result["error"])) { + error_messages <- append(error_messages, integer_result["message"]) + } + + character_result <- tryCatch({ + instance <- jsonlite::fromJSON(input, simplifyVector = FALSE) + if (typeof(instance) != "character") { + stop(sprintf("Data type doesn't match. Expected: %s. Actual: %s.", "character", typeof(instance))) + } + instance_type <- "character" + matched_schemas <- append(matched_schemas, "character") + matched <- matched + 1 + }, + error = function(err) err + ) + + if (!is.null(character_result["error"])) { + error_messages <- append(error_messages, character_result["message"]) + } + + if (matched == 1) { + # successfully match exactly 1 schema specified in oneOf + self$actual_instance <- instance + self$actual_type <- instance_type + } else if (matched > 1) { + # more than 1 match + stop("Multiple matches found when deserializing the payload into AnyOfPrimitiveTypeTest with oneOf schemas character, integer.") + } else { + # no match + stop(paste("No match found when deserializing the payload into AnyOfPrimitiveTypeTest with oneOf schemas character, integer. Details: ", + paste(error_messages, collapse = ", "))) + } + + self + }, + #' Serialize AnyOfPrimitiveTypeTest to JSON string. + #' + #' @description + #' Serialize AnyOfPrimitiveTypeTest to JSON string. + #' + #' @return JSON string representation of the AnyOfPrimitiveTypeTest. + #' @export + toJSONString = function() { + if (!is.null(self$actual_instance)) { + as.character(jsonlite::minify(self$actual_instance$toJSONString())) + } else { + NULL + } + }, + #' Serialize AnyOfPrimitiveTypeTest to JSON. + #' + #' @description + #' Serialize AnyOfPrimitiveTypeTest to JSON. + #' + #' @return JSON representation of the AnyOfPrimitiveTypeTest. + #' @export + toJSON = function() { + if (!is.null(self$actual_instance)) { + self$actual_instance$toJSON() + } else { + NULL + } + }, + #' Validate the input JSON with respect to AnyOfPrimitiveTypeTest. + #' + #' @description + #' Validate the input JSON with respect to AnyOfPrimitiveTypeTest and + #' throw exception if invalid. + #' + #' @param input The input JSON. + #' @export + 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 + }, + #' Returns the string representation of the instance. + #' + #' @description + #' Returns the string representation of the instance. + #' + #' @return The string representation of the instance. + #' @export + toString = function() { + jsoncontent <- c( + sprintf('"actual_instance": %s', if (is.null(self$actual_instance)) NULL else self$actual_instance$toJSONString()), + sprintf('"actual_type": "%s"', self$actual_type), + sprintf('"one_of": "%s"', paste(unlist(self$one_of), collapse = ", ")) + ) + jsoncontent <- paste(jsoncontent, collapse = ",") + as.character(jsonlite::prettify(paste("{", jsoncontent, "}", sep = ""))) + } + ) +) diff --git a/samples/client/petstore/R/R/one_of_primitive_type_test.R b/samples/client/petstore/R/R/one_of_primitive_type_test.R new file mode 100644 index 00000000000..8815ba1d3ee --- /dev/null +++ b/samples/client/petstore/R/R/one_of_primitive_type_test.R @@ -0,0 +1,186 @@ +#' 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 OneOfPrimitiveTypeTest +#' +#' @description OneOfPrimitiveTypeTest Class +#' +#' @format An \code{R6Class} generator object +#' +#' @importFrom R6 R6Class +#' @importFrom jsonlite fromJSON toJSON +#' @export +OneOfPrimitiveTypeTest <- R6::R6Class( + "OneOfPrimitiveTypeTest", + public = list( + #' @field actual_instance the object stored in this instance. + actual_instance = NULL, + #' @field actual_type the type of the object stored in this instance. + actual_type = NULL, + #' @field one_of a list of types defined in the oneOf schema. + one_of = list("character", "integer"), + #' Initialize a new OneOfPrimitiveTypeTest. + #' + #' @description + #' Initialize a new OneOfPrimitiveTypeTest. + #' + #' @param instance an instance of the object defined in the oneOf schemas: "character", "integer" + #' @export + initialize = function(instance = NULL) { + if (is.null(instance)) { + # do nothing + } else if (get(class(instance)[[1]], pos = -1)$classname == "character") { + self$actual_instance <- instance + self$actual_type <- "character" + } else if (get(class(instance)[[1]], pos = -1)$classname == "integer") { + self$actual_instance <- instance + self$actual_type <- "integer" + } else { + stop(paste("Failed to initialize OneOfPrimitiveTypeTest with oneOf schemas character, integer. Provided class name: ", + get(class(instance)[[1]], pos = -1)$classname)) + } + }, + #' Deserialize JSON string into an instance of OneOfPrimitiveTypeTest. + #' + #' @description + #' Deserialize JSON string into an instance of OneOfPrimitiveTypeTest. + #' An alias to the method `fromJSON` . + #' + #' @param input The input JSON. + #' @return An instance of OneOfPrimitiveTypeTest. + #' @export + fromJSONString = function(input) { + self$fromJSON(input) + }, + #' Deserialize JSON string into an instance of OneOfPrimitiveTypeTest. + #' + #' @description + #' Deserialize JSON string into an instance of OneOfPrimitiveTypeTest. + #' + #' @param input The input JSON. + #' @return An instance of OneOfPrimitiveTypeTest. + #' @export + fromJSON = function(input) { + matched <- 0 # match counter + matched_schemas <- list() #names of matched schemas + error_messages <- list() + instance <- NULL + + integer_result <- tryCatch({ + instance <- jsonlite::fromJSON(input, simplifyVector = FALSE) + if (typeof(instance) != "integer") { + stop(sprintf("Data type doesn't match. Expected: %s. Actual: %s.", "integer", typeof(instance))) + } + instance_type <- "integer" + matched_schemas <- append(matched_schemas, "integer") + matched <- matched + 1 + }, + error = function(err) err + ) + + if (!is.null(integer_result["error"])) { + error_messages <- append(error_messages, integer_result["message"]) + } + + character_result <- tryCatch({ + instance <- jsonlite::fromJSON(input, simplifyVector = FALSE) + if (typeof(instance) != "character") { + stop(sprintf("Data type doesn't match. Expected: %s. Actual: %s.", "character", typeof(instance))) + } + instance_type <- "character" + matched_schemas <- append(matched_schemas, "character") + matched <- matched + 1 + }, + error = function(err) err + ) + + if (!is.null(character_result["error"])) { + error_messages <- append(error_messages, character_result["message"]) + } + + if (matched == 1) { + # successfully match exactly 1 schema specified in oneOf + self$actual_instance <- instance + self$actual_type <- instance_type + } else if (matched > 1) { + # more than 1 match + stop("Multiple matches found when deserializing the payload into OneOfPrimitiveTypeTest with oneOf schemas character, integer.") + } else { + # no match + stop(paste("No match found when deserializing the payload into OneOfPrimitiveTypeTest with oneOf schemas character, integer. Details: ", + paste(error_messages, collapse = ", "))) + } + + self + }, + #' Serialize OneOfPrimitiveTypeTest to JSON string. + #' + #' @description + #' Serialize OneOfPrimitiveTypeTest to JSON string. + #' + #' @return JSON string representation of the OneOfPrimitiveTypeTest. + #' @export + toJSONString = function() { + if (!is.null(self$actual_instance)) { + as.character(jsonlite::minify(self$actual_instance$toJSONString())) + } else { + NULL + } + }, + #' Serialize OneOfPrimitiveTypeTest to JSON. + #' + #' @description + #' Serialize OneOfPrimitiveTypeTest to JSON. + #' + #' @return JSON representation of the OneOfPrimitiveTypeTest. + #' @export + toJSON = function() { + if (!is.null(self$actual_instance)) { + self$actual_instance$toJSON() + } else { + NULL + } + }, + #' Validate the input JSON with respect to OneOfPrimitiveTypeTest. + #' + #' @description + #' Validate the input JSON with respect to OneOfPrimitiveTypeTest and + #' throw exception if invalid. + #' + #' @param input The input JSON. + #' @export + 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 + }, + #' Returns the string representation of the instance. + #' + #' @description + #' Returns the string representation of the instance. + #' + #' @return The string representation of the instance. + #' @export + toString = function() { + jsoncontent <- c( + sprintf('"actual_instance": %s', if (is.null(self$actual_instance)) NULL else self$actual_instance$toJSONString()), + sprintf('"actual_type": "%s"', self$actual_type), + sprintf('"one_of": "%s"', paste(unlist(self$one_of), collapse = ", ")) + ) + jsoncontent <- paste(jsoncontent, collapse = ",") + as.character(jsonlite::prettify(paste("{", jsoncontent, "}", sep = ""))) + } + ) +) diff --git a/samples/client/petstore/R/R/pig.R b/samples/client/petstore/R/R/pig.R index dbe013e4168..9cdb7a38770 100644 --- a/samples/client/petstore/R/R/pig.R +++ b/samples/client/petstore/R/R/pig.R @@ -22,7 +22,7 @@ Pig <- R6::R6Class( actual_instance = NULL, #' @field actual_type the type of the object stored in this instance. actual_type = NULL, - #' @field one_of a list of object types defined in the oneOf schema. + #' @field one_of a list of types defined in the oneOf schema. one_of = list("BasquePig", "DanishPig"), #' Initialize a new Pig. #' diff --git a/samples/client/petstore/R/README.md b/samples/client/petstore/R/README.md index 79814bf139a..31a85b19511 100644 --- a/samples/client/petstore/R/README.md +++ b/samples/client/petstore/R/README.md @@ -92,6 +92,7 @@ Class | Method | HTTP request | Description - [AllofTagApiResponse](docs/AllofTagApiResponse.md) - [Animal](docs/Animal.md) - [AnyOfPig](docs/AnyOfPig.md) + - [AnyOfPrimitiveTypeTest](docs/AnyOfPrimitiveTypeTest.md) - [BasquePig](docs/BasquePig.md) - [Cat](docs/Cat.md) - [CatAllOf](docs/CatAllOf.md) @@ -101,6 +102,7 @@ Class | Method | HTTP request | Description - [DogAllOf](docs/DogAllOf.md) - [ModelApiResponse](docs/ModelApiResponse.md) - [NestedOneOf](docs/NestedOneOf.md) + - [OneOfPrimitiveTypeTest](docs/OneOfPrimitiveTypeTest.md) - [Order](docs/Order.md) - [Pet](docs/Pet.md) - [Pig](docs/Pig.md) diff --git a/samples/client/petstore/R/docs/AnyOfPrimitiveTypeTest.md b/samples/client/petstore/R/docs/AnyOfPrimitiveTypeTest.md new file mode 100644 index 00000000000..0d8ba0e63b1 --- /dev/null +++ b/samples/client/petstore/R/docs/AnyOfPrimitiveTypeTest.md @@ -0,0 +1,8 @@ +# petstore::AnyOfPrimitiveTypeTest + + +## Properties +Name | Type | Description | Notes +------------ | ------------- | ------------- | ------------- + + diff --git a/samples/client/petstore/R/docs/OneOfPrimitiveTypeTest.md b/samples/client/petstore/R/docs/OneOfPrimitiveTypeTest.md new file mode 100644 index 00000000000..82cf318431f --- /dev/null +++ b/samples/client/petstore/R/docs/OneOfPrimitiveTypeTest.md @@ -0,0 +1,8 @@ +# petstore::OneOfPrimitiveTypeTest + + +## Properties +Name | Type | Description | Notes +------------ | ------------- | ------------- | ------------- + + diff --git a/samples/client/petstore/R/tests/testthat/test_any_of_primitive_type_test.R b/samples/client/petstore/R/tests/testthat/test_any_of_primitive_type_test.R new file mode 100644 index 00000000000..0679cfaf443 --- /dev/null +++ b/samples/client/petstore/R/tests/testthat/test_any_of_primitive_type_test.R @@ -0,0 +1,4 @@ +# Automatically generated by openapi-generator (https://openapi-generator.tech) +# Please update as you see appropriate + +context("Test AnyOfPrimitiveTypeTest") diff --git a/samples/client/petstore/R/tests/testthat/test_one_of_primitive_type_test.R b/samples/client/petstore/R/tests/testthat/test_one_of_primitive_type_test.R new file mode 100644 index 00000000000..f9e7158359b --- /dev/null +++ b/samples/client/petstore/R/tests/testthat/test_one_of_primitive_type_test.R @@ -0,0 +1,4 @@ +# Automatically generated by openapi-generator (https://openapi-generator.tech) +# Please update as you see appropriate + +context("Test OneOfPrimitiveTypeTest")