diff --git a/bin/configs/r-httr2-wrapper-client.yaml b/bin/configs/r-httr2-wrapper-client.yaml index 3fd80f5fdb2..796479f0dce 100644 --- a/bin/configs/r-httr2-wrapper-client.yaml +++ b/bin/configs/r-httr2-wrapper-client.yaml @@ -12,3 +12,4 @@ additionalProperties: errorObjectType: "ModelApiResponse" operationIdNaming: snake_case generateWrapper: true + useOneOfDiscriminatorLookup: true diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RClientCodegen.java index 936de8179b4..98670a7b596 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RClientCodegen.java @@ -76,6 +76,7 @@ public class RClientCodegen extends DefaultCodegen implements CodegenConfig { protected String errorObjectType; protected String operationIdNaming; protected boolean generateWrapper; + protected boolean useOneOfDiscriminatorLookup = false; // use oneOf discriminator's mapping for model lookup private Map schemaKeyToModelNameCache = new HashMap<>(); @@ -265,6 +266,12 @@ public class RClientCodegen extends DefaultCodegen implements CodegenConfig { this.setGenerateWrapper(false); } + if (additionalProperties.containsKey(CodegenConstants.USE_ONEOF_DISCRIMINATOR_LOOKUP)) { + setUseOneOfDiscriminatorLookup(convertPropertyToBooleanAndWriteBack(CodegenConstants.USE_ONEOF_DISCRIMINATOR_LOOKUP)); + } else { + additionalProperties.put(CodegenConstants.USE_ONEOF_DISCRIMINATOR_LOOKUP, useOneOfDiscriminatorLookup); + } + additionalProperties.put(CodegenConstants.PACKAGE_NAME, packageName); additionalProperties.put(CodegenConstants.PACKAGE_VERSION, packageVersion); additionalProperties.put(CodegenConstants.EXCEPTION_ON_FAILURE, returnExceptionOnFailure); @@ -623,6 +630,14 @@ public class RClientCodegen extends DefaultCodegen implements CodegenConfig { this.generateWrapper = generateWrapper; } + public void setUseOneOfDiscriminatorLookup(boolean useOneOfDiscriminatorLookup) { + this.useOneOfDiscriminatorLookup = useOneOfDiscriminatorLookup; + } + + public boolean getUseOneOfDiscriminatorLookup() { + return this.useOneOfDiscriminatorLookup; + } + public void setOperationIdNaming(final String operationIdNaming) { if (!("PascalCase".equals(operationIdNaming) || "camelCase".equals(operationIdNaming) || "snake_case".equals(operationIdNaming))) { diff --git a/modules/openapi-generator/src/main/resources/r/modelOneOf.mustache b/modules/openapi-generator/src/main/resources/r/modelOneOf.mustache index 04f89c16e9c..5287132ea61 100644 --- a/modules/openapi-generator/src/main/resources/r/modelOneOf.mustache +++ b/modules/openapi-generator/src/main/resources/r/modelOneOf.mustache @@ -61,6 +61,39 @@ error_messages <- list() instance <- NULL + {{#useOneOfDiscriminatorLookup}} + {{#discriminator}} + oneof_lookup_result <- tryCatch({ + discriminatorValue <- (jsonlite::fromJSON(input, simplifyVector = FALSE))$`{{{propertyBaseName}}}` + switch(discriminatorValue, + {{#mappedModels}} + {{{mappingName}}}={ + {{{modelName}}}$public_methods$validateJSON(input) + {{{modelName}}}_instance <- {{{modelName}}}$new() + self$actual_instance <- {{{modelName}}}_instance$fromJSON(input) + self$actual_type <- "{{{modelName}}}" + return(self) + }{{^-last}},{{/-last}}{{#-last}})},{{/-last}} + {{/mappedModels}} + {{^mappedModels}} + {{#oneOf}} + {{{.}}}={ + {{{.}}}$public_methods$validateJSON(input) + {{{.}}}_instance <- {{{.}}}$new() + self$actual_instance <- {{{.}}}_instance$fromJSON(input) + self$actual_type <- "{{{.}}}" + return(self) + }{{^-last}},{{/-last}}{{#-last}})},{{/-last}} + {{/oneOf}} + {{/mappedModels}} + error = function(err) err + ) + if (!is.null(oneof_lookup_result["error"])) { + error_messages <- append(error_messages, sprintf("Failed to lookup discriminator value for {{classname}}. Error message: %s. Input: %s", oneof_lookup_result["message"], input)) + } + + {{/discriminator}} + {{/useOneOfDiscriminatorLookup}} {{#composedSchemas.oneOf}} {{^isNull}} {{{dataType}}}_result <- tryCatch({ 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 9ee786b1052..084ced168fc 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,14 +933,6 @@ components: - $ref: '#/components/schemas/DanishPig' discriminator: propertyName: className - OneOfPrimitiveTypeTest: - oneOf: - - type: "integer" - - type: "string" - AnyOfPrimitiveTypeTest: - oneOf: - - type: "integer" - - type: "string" BasquePig: type: object properties: @@ -968,3 +960,44 @@ components: type: integer nested_pig: $ref: '#/components/schemas/Pig' + OneOfPrimitiveTypeTest: + oneOf: + - type: "integer" + - type: "string" + AnyOfPrimitiveTypeTest: + oneOf: + - type: "integer" + - type: "string" + mammal: + oneOf: + - $ref: '#/components/schemas/whale' + - $ref: '#/components/schemas/zebra' + discriminator: + propertyName: className + mapping: + whale: '#/components/schemas/whale' + zebra: '#/components/schemas/zebra' + whale: + type: object + properties: + hasBaleen: + type: boolean + hasTeeth: + type: boolean + className: + type: string + required: + - className + zebra: + type: object + properties: + type: + type: string + enum: + - plains + - mountain + - grevys + className: + type: string + required: + - className diff --git a/samples/client/petstore/R-httr2-wrapper/.openapi-generator/FILES b/samples/client/petstore/R-httr2-wrapper/.openapi-generator/FILES index 881a2ab283e..b2a3dba4543 100644 --- a/samples/client/petstore/R-httr2-wrapper/.openapi-generator/FILES +++ b/samples/client/petstore/R-httr2-wrapper/.openapi-generator/FILES @@ -20,6 +20,7 @@ R/danish_pig.R R/dog.R R/dog_all_of.R R/fake_api.R +R/mammal.R R/model_api_response.R R/nested_one_of.R R/one_of_primitive_type_test.R @@ -34,6 +35,8 @@ R/tag.R R/update_pet_request.R R/user.R R/user_api.R +R/whale.R +R/zebra.R README.md docs/AllofTagApiResponse.md docs/Animal.md @@ -47,6 +50,7 @@ docs/DanishPig.md docs/Dog.md docs/DogAllOf.md docs/FakeApi.md +docs/Mammal.md docs/ModelApiResponse.md docs/NestedOneOf.md docs/OneOfPrimitiveTypeTest.md @@ -60,5 +64,7 @@ docs/Tag.md docs/UpdatePetRequest.md docs/User.md docs/UserApi.md +docs/Whale.md +docs/Zebra.md git_push.sh tests/testthat.R diff --git a/samples/client/petstore/R-httr2-wrapper/NAMESPACE b/samples/client/petstore/R-httr2-wrapper/NAMESPACE index 7af9abbfc48..f4687ea9592 100644 --- a/samples/client/petstore/R-httr2-wrapper/NAMESPACE +++ b/samples/client/petstore/R-httr2-wrapper/NAMESPACE @@ -27,6 +27,7 @@ export(Category) export(DanishPig) export(Dog) export(DogAllOf) +export(Mammal) export(ModelApiResponse) export(NestedOneOf) export(OneOfPrimitiveTypeTest) @@ -37,6 +38,8 @@ export(Special) export(Tag) export(UpdatePetRequest) export(User) +export(Whale) +export(Zebra) # APIs export(FakeApi) diff --git a/samples/client/petstore/R-httr2-wrapper/R/mammal.R b/samples/client/petstore/R-httr2-wrapper/R/mammal.R new file mode 100644 index 00000000000..9c26bbed2b3 --- /dev/null +++ b/samples/client/petstore/R-httr2-wrapper/R/mammal.R @@ -0,0 +1,220 @@ +#' @docType class +#' @title Mammal +#' +#' @description Mammal Class +#' +#' @format An \code{R6Class} generator object +#' +#' @importFrom R6 R6Class +#' @importFrom jsonlite fromJSON toJSON +#' @export +Mammal <- R6::R6Class( + "Mammal", + 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("Whale", "Zebra"), + #' Initialize a new Mammal. + #' + #' @description + #' Initialize a new Mammal. + #' + #' @param instance an instance of the object defined in the oneOf schemas: "Whale", "Zebra" + #' @export + initialize = function(instance = NULL) { + if (is.null(instance)) { + # do nothing + } else if (get(class(instance)[[1]], pos = -1)$classname == "Whale") { + self$actual_instance <- instance + self$actual_type <- "Whale" + } else if (get(class(instance)[[1]], pos = -1)$classname == "Zebra") { + self$actual_instance <- instance + self$actual_type <- "Zebra" + } else { + stop(paste("Failed to initialize Mammal with oneOf schemas Whale, Zebra. Provided class name: ", + get(class(instance)[[1]], pos = -1)$classname)) + } + }, + #' Deserialize JSON string into an instance of Mammal. + #' + #' @description + #' Deserialize JSON string into an instance of Mammal. + #' An alias to the method `fromJSON` . + #' + #' @param input The input JSON. + #' @return An instance of Mammal. + #' @export + fromJSONString = function(input) { + self$fromJSON(input) + }, + #' Deserialize JSON string into an instance of Mammal. + #' + #' @description + #' Deserialize JSON string into an instance of Mammal. + #' + #' @param input The input JSON. + #' @return An instance of Mammal. + #' @export + fromJSON = function(input) { + matched <- 0 # match counter + matched_schemas <- list() #names of matched schemas + error_messages <- list() + instance <- NULL + + oneof_lookup_result <- tryCatch({ + discriminatorValue <- (jsonlite::fromJSON(input, simplifyVector = FALSE))$`className` + switch(discriminatorValue, + whale={ + Whale$public_methods$validateJSON(input) + Whale_instance <- Whale$new() + self$actual_instance <- Whale_instance$fromJSON(input) + self$actual_type <- "Whale" + return(self) + }, + zebra={ + Zebra$public_methods$validateJSON(input) + Zebra_instance <- Zebra$new() + self$actual_instance <- Zebra_instance$fromJSON(input) + self$actual_type <- "Zebra" + return(self) + })}, + error = function(err) err + ) + if (!is.null(oneof_lookup_result["error"])) { + error_messages <- append(error_messages, sprintf("Failed to lookup discriminator value for Mammal. Error message: %s. Input: %s", oneof_lookup_result["message"], input)) + } + + Whale_result <- tryCatch({ + Whale$public_methods$validateJSON(input) + Whale_instance <- Whale$new() + instance <- Whale_instance$fromJSON(input) + instance_type <- "Whale" + matched_schemas <- append(matched_schemas, "Whale") + matched <- matched + 1 + }, + error = function(err) err + ) + + if (!is.null(Whale_result["error"])) { + error_messages <- append(error_messages, Whale_result["message"]) + } + + Zebra_result <- tryCatch({ + Zebra$public_methods$validateJSON(input) + Zebra_instance <- Zebra$new() + instance <- Zebra_instance$fromJSON(input) + instance_type <- "Zebra" + matched_schemas <- append(matched_schemas, "Zebra") + matched <- matched + 1 + }, + error = function(err) err + ) + + if (!is.null(Zebra_result["error"])) { + error_messages <- append(error_messages, Zebra_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 Mammal with oneOf schemas Whale, Zebra.") + } else { + # no match + stop(paste("No match found when deserializing the payload into Mammal with oneOf schemas Whale, Zebra. Details: ", + paste(error_messages, collapse = ", "))) + } + + self + }, + #' Serialize Mammal to JSON string. + #' + #' @description + #' Serialize Mammal to JSON string. + #' + #' @return JSON string representation of the Mammal. + #' @export + toJSONString = function() { + if (!is.null(self$actual_instance)) { + as.character(jsonlite::minify(self$actual_instance$toJSONString())) + } else { + NULL + } + }, + #' Serialize Mammal to JSON. + #' + #' @description + #' Serialize Mammal to JSON. + #' + #' @return JSON representation of the Mammal. + #' @export + toJSON = function() { + if (!is.null(self$actual_instance)) { + self$actual_instance$toJSON() + } else { + NULL + } + }, + #' Validate the input JSON with respect to Mammal. + #' + #' @description + #' Validate the input JSON with respect to Mammal 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 = ""))) + } + ), + # Lock the class to prevent modifications to the method or field + lock_class = TRUE +) + +# Unlock the class to allow modifications of the method or field +Mammal$unlock() + +#' Print the object +#' +#' @description +#' Print the object +#' +#' @export +Mammal$set("public", "print", function(...) { + print(jsonlite::prettify(self$toJSONString())) + invisible(self) +}) + +# Lock the class to prevent modifications to the method or field +Mammal$lock() + diff --git a/samples/client/petstore/R-httr2-wrapper/R/pig.R b/samples/client/petstore/R-httr2-wrapper/R/pig.R index ee046be77ff..dd2e91ffcb2 100644 --- a/samples/client/petstore/R-httr2-wrapper/R/pig.R +++ b/samples/client/petstore/R-httr2-wrapper/R/pig.R @@ -64,6 +64,29 @@ Pig <- R6::R6Class( error_messages <- list() instance <- NULL + oneof_lookup_result <- tryCatch({ + discriminatorValue <- (jsonlite::fromJSON(input, simplifyVector = FALSE))$`className` + switch(discriminatorValue, + BasquePig={ + BasquePig$public_methods$validateJSON(input) + BasquePig_instance <- BasquePig$new() + self$actual_instance <- BasquePig_instance$fromJSON(input) + self$actual_type <- "BasquePig" + return(self) + }, + DanishPig={ + 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(oneof_lookup_result["error"])) { + error_messages <- append(error_messages, sprintf("Failed to lookup discriminator value for Pig. Error message: %s. Input: %s", oneof_lookup_result["message"], input)) + } + BasquePig_result <- tryCatch({ BasquePig$public_methods$validateJSON(input) BasquePig_instance <- BasquePig$new() diff --git a/samples/client/petstore/R-httr2-wrapper/R/whale.R b/samples/client/petstore/R-httr2-wrapper/R/whale.R new file mode 100644 index 00000000000..31738ea3622 --- /dev/null +++ b/samples/client/petstore/R-httr2-wrapper/R/whale.R @@ -0,0 +1,223 @@ +#' Create a new Whale +#' +#' @description +#' Whale Class +#' +#' @docType class +#' @title Whale +#' @description Whale Class +#' @format An \code{R6Class} generator object +#' @field hasBaleen character [optional] +#' @field hasTeeth character [optional] +#' @field className character +#' @importFrom R6 R6Class +#' @importFrom jsonlite fromJSON toJSON +#' @export +Whale <- R6::R6Class( + "Whale", + public = list( + `hasBaleen` = NULL, + `hasTeeth` = NULL, + `className` = NULL, + #' Initialize a new Whale class. + #' + #' @description + #' Initialize a new Whale class. + #' + #' @param className className + #' @param hasBaleen hasBaleen + #' @param hasTeeth hasTeeth + #' @param ... Other optional arguments. + #' @export + initialize = function( + `className`, `hasBaleen` = NULL, `hasTeeth` = NULL, ... + ) { + if (!missing(`className`)) { + stopifnot(is.character(`className`), length(`className`) == 1) + self$`className` <- `className` + } + if (!is.null(`hasBaleen`)) { + stopifnot(is.logical(`hasBaleen`), length(`hasBaleen`) == 1) + self$`hasBaleen` <- `hasBaleen` + } + if (!is.null(`hasTeeth`)) { + stopifnot(is.logical(`hasTeeth`), length(`hasTeeth`) == 1) + self$`hasTeeth` <- `hasTeeth` + } + }, + #' To JSON string + #' + #' @description + #' To JSON String + #' + #' @return Whale in JSON format + #' @export + toJSON = function() { + WhaleObject <- list() + if (!is.null(self$`hasBaleen`)) { + WhaleObject[["hasBaleen"]] <- + self$`hasBaleen` + } + if (!is.null(self$`hasTeeth`)) { + WhaleObject[["hasTeeth"]] <- + self$`hasTeeth` + } + if (!is.null(self$`className`)) { + WhaleObject[["className"]] <- + self$`className` + } + + WhaleObject + }, + #' Deserialize JSON string into an instance of Whale + #' + #' @description + #' Deserialize JSON string into an instance of Whale + #' + #' @param input_json the JSON input + #' @return the instance of Whale + #' @export + fromJSON = function(input_json) { + this_object <- jsonlite::fromJSON(input_json) + if (!is.null(this_object$`hasBaleen`)) { + self$`hasBaleen` <- this_object$`hasBaleen` + } + if (!is.null(this_object$`hasTeeth`)) { + self$`hasTeeth` <- this_object$`hasTeeth` + } + if (!is.null(this_object$`className`)) { + self$`className` <- this_object$`className` + } + self + }, + #' To JSON string + #' + #' @description + #' To JSON String + #' + #' @return Whale in JSON format + #' @export + toJSONString = function() { + jsoncontent <- c( + if (!is.null(self$`hasBaleen`)) { + sprintf( + '"hasBaleen": + %s + ', + tolower(self$`hasBaleen`) + ) + }, + if (!is.null(self$`hasTeeth`)) { + sprintf( + '"hasTeeth": + %s + ', + tolower(self$`hasTeeth`) + ) + }, + if (!is.null(self$`className`)) { + sprintf( + '"className": + "%s" + ', + self$`className` + ) + } + ) + jsoncontent <- paste(jsoncontent, collapse = ",") + as.character(jsonlite::minify(paste("{", jsoncontent, "}", sep = ""))) + }, + #' Deserialize JSON string into an instance of Whale + #' + #' @description + #' Deserialize JSON string into an instance of Whale + #' + #' @param input_json the JSON input + #' @return the instance of Whale + #' @export + fromJSONString = function(input_json) { + this_object <- jsonlite::fromJSON(input_json) + self$`hasBaleen` <- this_object$`hasBaleen` + self$`hasTeeth` <- this_object$`hasTeeth` + self$`className` <- this_object$`className` + self + }, + #' Validate JSON input with respect to Whale + #' + #' @description + #' Validate JSON input with respect to Whale and throw an exception if invalid + #' + #' @param input the JSON input + #' @export + validateJSON = function(input) { + input_json <- jsonlite::fromJSON(input) + # check the required field `className` + if (!is.null(input_json$`className`)) { + stopifnot(is.character(input_json$`className`), length(input_json$`className`) == 1) + } else { + stop(paste("The JSON input `", input, "` is invalid for Whale: the required field `className` is missing.")) + } + }, + #' To string (JSON format) + #' + #' @description + #' To string (JSON format) + #' + #' @return String representation of Whale + #' @export + toString = function() { + self$toJSONString() + }, + #' Return true if the values in all fields are valid. + #' + #' @description + #' Return true if the values in all fields are valid. + #' + #' @return true if the values in all fields are valid. + #' @export + isValid = function() { + # check if the required `className` is null + if (is.null(self$`className`)) { + return(FALSE) + } + + TRUE + }, + #' Return a list of invalid fields (if any). + #' + #' @description + #' Return a list of invalid fields (if any). + #' + #' @return A list of invalid fields (if any). + #' @export + getInvalidFields = function() { + invalid_fields <- list() + # check if the required `className` is null + if (is.null(self$`className`)) { + invalid_fields["className"] <- "Non-nullable required field `className` cannot be null." + } + + invalid_fields + } + ), + # Lock the class to prevent modifications to the method or field + lock_class = TRUE +) + +# Unlock the class to allow modifications of the method or field +Whale$unlock() + +#' Print the object +#' +#' @description +#' Print the object +#' +#' @export +Whale$set("public", "print", function(...) { + print(jsonlite::prettify(self$toJSONString())) + invisible(self) +}) + +# Lock the class to prevent modifications to the method or field +Whale$lock() + diff --git a/samples/client/petstore/R-httr2-wrapper/R/zebra.R b/samples/client/petstore/R-httr2-wrapper/R/zebra.R new file mode 100644 index 00000000000..d9d2788aca9 --- /dev/null +++ b/samples/client/petstore/R-httr2-wrapper/R/zebra.R @@ -0,0 +1,200 @@ +#' Create a new Zebra +#' +#' @description +#' Zebra Class +#' +#' @docType class +#' @title Zebra +#' @description Zebra Class +#' @format An \code{R6Class} generator object +#' @field type character [optional] +#' @field className character +#' @importFrom R6 R6Class +#' @importFrom jsonlite fromJSON toJSON +#' @export +Zebra <- R6::R6Class( + "Zebra", + public = list( + `type` = NULL, + `className` = NULL, + #' Initialize a new Zebra class. + #' + #' @description + #' Initialize a new Zebra class. + #' + #' @param className className + #' @param type type + #' @param ... Other optional arguments. + #' @export + initialize = function( + `className`, `type` = NULL, ... + ) { + if (!missing(`className`)) { + stopifnot(is.character(`className`), length(`className`) == 1) + self$`className` <- `className` + } + if (!is.null(`type`)) { + stopifnot(is.character(`type`), length(`type`) == 1) + self$`type` <- `type` + } + }, + #' To JSON string + #' + #' @description + #' To JSON String + #' + #' @return Zebra in JSON format + #' @export + toJSON = function() { + ZebraObject <- list() + if (!is.null(self$`type`)) { + ZebraObject[["type"]] <- + self$`type` + } + if (!is.null(self$`className`)) { + ZebraObject[["className"]] <- + self$`className` + } + + ZebraObject + }, + #' Deserialize JSON string into an instance of Zebra + #' + #' @description + #' Deserialize JSON string into an instance of Zebra + #' + #' @param input_json the JSON input + #' @return the instance of Zebra + #' @export + fromJSON = function(input_json) { + this_object <- jsonlite::fromJSON(input_json) + if (!is.null(this_object$`type`)) { + self$`type` <- this_object$`type` + } + if (!is.null(this_object$`className`)) { + self$`className` <- this_object$`className` + } + self + }, + #' To JSON string + #' + #' @description + #' To JSON String + #' + #' @return Zebra in JSON format + #' @export + toJSONString = function() { + jsoncontent <- c( + if (!is.null(self$`type`)) { + sprintf( + '"type": + "%s" + ', + self$`type` + ) + }, + if (!is.null(self$`className`)) { + sprintf( + '"className": + "%s" + ', + self$`className` + ) + } + ) + jsoncontent <- paste(jsoncontent, collapse = ",") + as.character(jsonlite::minify(paste("{", jsoncontent, "}", sep = ""))) + }, + #' Deserialize JSON string into an instance of Zebra + #' + #' @description + #' Deserialize JSON string into an instance of Zebra + #' + #' @param input_json the JSON input + #' @return the instance of Zebra + #' @export + fromJSONString = function(input_json) { + this_object <- jsonlite::fromJSON(input_json) + self$`type` <- this_object$`type` + self$`className` <- this_object$`className` + self + }, + #' Validate JSON input with respect to Zebra + #' + #' @description + #' Validate JSON input with respect to Zebra and throw an exception if invalid + #' + #' @param input the JSON input + #' @export + validateJSON = function(input) { + input_json <- jsonlite::fromJSON(input) + # check the required field `className` + if (!is.null(input_json$`className`)) { + stopifnot(is.character(input_json$`className`), length(input_json$`className`) == 1) + } else { + stop(paste("The JSON input `", input, "` is invalid for Zebra: the required field `className` is missing.")) + } + }, + #' To string (JSON format) + #' + #' @description + #' To string (JSON format) + #' + #' @return String representation of Zebra + #' @export + toString = function() { + self$toJSONString() + }, + #' Return true if the values in all fields are valid. + #' + #' @description + #' Return true if the values in all fields are valid. + #' + #' @return true if the values in all fields are valid. + #' @export + isValid = function() { + # check if the required `className` is null + if (is.null(self$`className`)) { + return(FALSE) + } + + TRUE + }, + #' Return a list of invalid fields (if any). + #' + #' @description + #' Return a list of invalid fields (if any). + #' + #' @return A list of invalid fields (if any). + #' @export + getInvalidFields = function() { + invalid_fields <- list() + # check if the required `className` is null + if (is.null(self$`className`)) { + invalid_fields["className"] <- "Non-nullable required field `className` cannot be null." + } + + invalid_fields + } + ), + # Lock the class to prevent modifications to the method or field + lock_class = TRUE +) + +# Unlock the class to allow modifications of the method or field +Zebra$unlock() + +#' Print the object +#' +#' @description +#' Print the object +#' +#' @export +Zebra$set("public", "print", function(...) { + print(jsonlite::prettify(self$toJSONString())) + invisible(self) +}) + +# Lock the class to prevent modifications to the method or field +Zebra$lock() + diff --git a/samples/client/petstore/R-httr2-wrapper/README.md b/samples/client/petstore/R-httr2-wrapper/README.md index 7b97c29fe51..b71e5202220 100644 --- a/samples/client/petstore/R-httr2-wrapper/README.md +++ b/samples/client/petstore/R-httr2-wrapper/README.md @@ -100,6 +100,7 @@ Class | Method | HTTP request | Description - [DanishPig](docs/DanishPig.md) - [Dog](docs/Dog.md) - [DogAllOf](docs/DogAllOf.md) + - [Mammal](docs/Mammal.md) - [ModelApiResponse](docs/ModelApiResponse.md) - [NestedOneOf](docs/NestedOneOf.md) - [OneOfPrimitiveTypeTest](docs/OneOfPrimitiveTypeTest.md) @@ -110,6 +111,8 @@ Class | Method | HTTP request | Description - [Tag](docs/Tag.md) - [UpdatePetRequest](docs/UpdatePetRequest.md) - [User](docs/User.md) + - [Whale](docs/Whale.md) + - [Zebra](docs/Zebra.md) ## Documentation for Authorization diff --git a/samples/client/petstore/R-httr2-wrapper/docs/Mammal.md b/samples/client/petstore/R-httr2-wrapper/docs/Mammal.md new file mode 100644 index 00000000000..d9746cd2df1 --- /dev/null +++ b/samples/client/petstore/R-httr2-wrapper/docs/Mammal.md @@ -0,0 +1,12 @@ +# petstore::Mammal + + +## Properties +Name | Type | Description | Notes +------------ | ------------- | ------------- | ------------- +**hasBaleen** | **character** | | [optional] +**hasTeeth** | **character** | | [optional] +**className** | **character** | | +**type** | **character** | | [optional] + + diff --git a/samples/client/petstore/R-httr2-wrapper/docs/Whale.md b/samples/client/petstore/R-httr2-wrapper/docs/Whale.md new file mode 100644 index 00000000000..bd0d148686f --- /dev/null +++ b/samples/client/petstore/R-httr2-wrapper/docs/Whale.md @@ -0,0 +1,11 @@ +# petstore::Whale + + +## Properties +Name | Type | Description | Notes +------------ | ------------- | ------------- | ------------- +**hasBaleen** | **character** | | [optional] +**hasTeeth** | **character** | | [optional] +**className** | **character** | | + + diff --git a/samples/client/petstore/R-httr2-wrapper/docs/Zebra.md b/samples/client/petstore/R-httr2-wrapper/docs/Zebra.md new file mode 100644 index 00000000000..84e4d20ce94 --- /dev/null +++ b/samples/client/petstore/R-httr2-wrapper/docs/Zebra.md @@ -0,0 +1,10 @@ +# petstore::Zebra + + +## Properties +Name | Type | Description | Notes +------------ | ------------- | ------------- | ------------- +**type** | **character** | | [optional] +**className** | **character** | | + + diff --git a/samples/client/petstore/R-httr2-wrapper/test_petstore.R b/samples/client/petstore/R-httr2-wrapper/test_petstore.R index ed769f18f6f..786df7d788d 100644 --- a/samples/client/petstore/R-httr2-wrapper/test_petstore.R +++ b/samples/client/petstore/R-httr2-wrapper/test_petstore.R @@ -3,6 +3,12 @@ install.packages("petstore_1.0.0.tar.gz",repos=NULL, type="source") library(petstore) library(jsonlite) + whale_json <- '{"className": "whale", "hasBaleen": true, "hasTeeth": true}' + zebra_json <- '{"className": "zebra", "type": "plains"}' + + mammal <- Mammal$new() + mammal$fromJSON(whale_json) + api_client <- ApiClient$new() api_client$username <- "username999" api_client$password <- "password888" diff --git a/samples/client/petstore/R-httr2-wrapper/tests/testthat/test_mammal.R b/samples/client/petstore/R-httr2-wrapper/tests/testthat/test_mammal.R new file mode 100644 index 00000000000..bc061ea799a --- /dev/null +++ b/samples/client/petstore/R-httr2-wrapper/tests/testthat/test_mammal.R @@ -0,0 +1,34 @@ +# Automatically generated by openapi-generator (https://openapi-generator.tech) +# Please update as you see appropriate + +context("Test Mammal") + +model_instance <- Mammal$new() + +test_that("hasBaleen", { + # tests for the property `hasBaleen` (character) + + # uncomment below to test the property + #expect_equal(model.instance$`hasBaleen`, "EXPECTED_RESULT") +}) + +test_that("hasTeeth", { + # tests for the property `hasTeeth` (character) + + # uncomment below to test the property + #expect_equal(model.instance$`hasTeeth`, "EXPECTED_RESULT") +}) + +test_that("className", { + # tests for the property `className` (character) + + # uncomment below to test the property + #expect_equal(model.instance$`className`, "EXPECTED_RESULT") +}) + +test_that("type", { + # tests for the property `type` (character) + + # uncomment below to test the property + #expect_equal(model.instance$`type`, "EXPECTED_RESULT") +}) diff --git a/samples/client/petstore/R-httr2-wrapper/tests/testthat/test_petstore.R b/samples/client/petstore/R-httr2-wrapper/tests/testthat/test_petstore.R index 1b1beaae5cf..21b6e4e9132 100644 --- a/samples/client/petstore/R-httr2-wrapper/tests/testthat/test_petstore.R +++ b/samples/client/petstore/R-httr2-wrapper/tests/testthat/test_petstore.R @@ -294,6 +294,22 @@ test_that ("Tests validations", { }) +test_that("Tests oneOf discriminator mapping", { + whale_json <- '{"className": "whale", "hasBaleen": true, "hasTeeth": true}' + zebra_json <- '{"className": "zebra", "type": "plains"}' + + mammal <- Mammal$new() + mammal$fromJSON(whale_json) + expect_equal(mammal$actual_type, "Whale") + expect_equal(mammal$actual_instance$hasBaleen, TRUE) + expect_equal(mammal$actual_instance$hasTeeth, TRUE) + + mammal$fromJSON(zebra_json) + expect_equal(mammal$actual_type, "Zebra") + expect_equal(mammal$actual_instance$type, "plains") + +}) + test_that("Tests oneOf", { basque_pig_json <- '{"className": "BasquePig", "color": "red"}' @@ -333,8 +349,8 @@ test_that("Tests oneOf", { expect_equal(basque_pig$toJSONString(), original_basque_pig$toJSONString()) # test exception when no matche found - 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$fromJSON('{}'), 'No match found when deserializing the payload into Pig with oneOf schemas BasquePig, DanishPig. Details: Failed to lookup discriminator value for Pig. Error message: EXPR must be a length 1 vector. Input: \\{\\}, 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: Failed to lookup discriminator value for Pig. Error message: EXPR must be a length 1 vector. Input: \\{\\}, 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") diff --git a/samples/client/petstore/R-httr2-wrapper/tests/testthat/test_whale.R b/samples/client/petstore/R-httr2-wrapper/tests/testthat/test_whale.R new file mode 100644 index 00000000000..b19c7b9eb3e --- /dev/null +++ b/samples/client/petstore/R-httr2-wrapper/tests/testthat/test_whale.R @@ -0,0 +1,27 @@ +# Automatically generated by openapi-generator (https://openapi-generator.tech) +# Please update as you see appropriate + +context("Test Whale") + +model_instance <- Whale$new() + +test_that("hasBaleen", { + # tests for the property `hasBaleen` (character) + + # uncomment below to test the property + #expect_equal(model.instance$`hasBaleen`, "EXPECTED_RESULT") +}) + +test_that("hasTeeth", { + # tests for the property `hasTeeth` (character) + + # uncomment below to test the property + #expect_equal(model.instance$`hasTeeth`, "EXPECTED_RESULT") +}) + +test_that("className", { + # tests for the property `className` (character) + + # uncomment below to test the property + #expect_equal(model.instance$`className`, "EXPECTED_RESULT") +}) diff --git a/samples/client/petstore/R-httr2-wrapper/tests/testthat/test_zebra.R b/samples/client/petstore/R-httr2-wrapper/tests/testthat/test_zebra.R new file mode 100644 index 00000000000..444da2f8f77 --- /dev/null +++ b/samples/client/petstore/R-httr2-wrapper/tests/testthat/test_zebra.R @@ -0,0 +1,20 @@ +# Automatically generated by openapi-generator (https://openapi-generator.tech) +# Please update as you see appropriate + +context("Test Zebra") + +model_instance <- Zebra$new() + +test_that("type", { + # tests for the property `type` (character) + + # uncomment below to test the property + #expect_equal(model.instance$`type`, "EXPECTED_RESULT") +}) + +test_that("className", { + # tests for the property `className` (character) + + # uncomment below to test the property + #expect_equal(model.instance$`className`, "EXPECTED_RESULT") +}) diff --git a/samples/client/petstore/R-httr2/.openapi-generator/FILES b/samples/client/petstore/R-httr2/.openapi-generator/FILES index 11069aa4bcc..2e11de9d1ab 100644 --- a/samples/client/petstore/R-httr2/.openapi-generator/FILES +++ b/samples/client/petstore/R-httr2/.openapi-generator/FILES @@ -20,6 +20,7 @@ R/danish_pig.R R/dog.R R/dog_all_of.R R/fake_api.R +R/mammal.R R/model_api_response.R R/nested_one_of.R R/one_of_primitive_type_test.R @@ -33,6 +34,8 @@ R/tag.R R/update_pet_request.R R/user.R R/user_api.R +R/whale.R +R/zebra.R README.md docs/AllofTagApiResponse.md docs/Animal.md @@ -46,6 +49,7 @@ docs/DanishPig.md docs/Dog.md docs/DogAllOf.md docs/FakeApi.md +docs/Mammal.md docs/ModelApiResponse.md docs/NestedOneOf.md docs/OneOfPrimitiveTypeTest.md @@ -59,5 +63,7 @@ docs/Tag.md docs/UpdatePetRequest.md docs/User.md docs/UserApi.md +docs/Whale.md +docs/Zebra.md git_push.sh tests/testthat.R diff --git a/samples/client/petstore/R-httr2/NAMESPACE b/samples/client/petstore/R-httr2/NAMESPACE index d26ce29357a..9a41dbdcab3 100644 --- a/samples/client/petstore/R-httr2/NAMESPACE +++ b/samples/client/petstore/R-httr2/NAMESPACE @@ -25,6 +25,7 @@ export(Category) export(DanishPig) export(Dog) export(DogAllOf) +export(Mammal) export(ModelApiResponse) export(NestedOneOf) export(OneOfPrimitiveTypeTest) @@ -35,6 +36,8 @@ export(Special) export(Tag) export(UpdatePetRequest) export(User) +export(Whale) +export(Zebra) # APIs export(FakeApi) diff --git a/samples/client/petstore/R-httr2/R/mammal.R b/samples/client/petstore/R-httr2/R/mammal.R new file mode 100644 index 00000000000..14857c098e4 --- /dev/null +++ b/samples/client/petstore/R-httr2/R/mammal.R @@ -0,0 +1,197 @@ +#' @docType class +#' @title Mammal +#' +#' @description Mammal Class +#' +#' @format An \code{R6Class} generator object +#' +#' @importFrom R6 R6Class +#' @importFrom jsonlite fromJSON toJSON +#' @export +Mammal <- R6::R6Class( + "Mammal", + 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("Whale", "Zebra"), + #' Initialize a new Mammal. + #' + #' @description + #' Initialize a new Mammal. + #' + #' @param instance an instance of the object defined in the oneOf schemas: "Whale", "Zebra" + #' @export + initialize = function(instance = NULL) { + if (is.null(instance)) { + # do nothing + } else if (get(class(instance)[[1]], pos = -1)$classname == "Whale") { + self$actual_instance <- instance + self$actual_type <- "Whale" + } else if (get(class(instance)[[1]], pos = -1)$classname == "Zebra") { + self$actual_instance <- instance + self$actual_type <- "Zebra" + } else { + stop(paste("Failed to initialize Mammal with oneOf schemas Whale, Zebra. Provided class name: ", + get(class(instance)[[1]], pos = -1)$classname)) + } + }, + #' Deserialize JSON string into an instance of Mammal. + #' + #' @description + #' Deserialize JSON string into an instance of Mammal. + #' An alias to the method `fromJSON` . + #' + #' @param input The input JSON. + #' @return An instance of Mammal. + #' @export + fromJSONString = function(input) { + self$fromJSON(input) + }, + #' Deserialize JSON string into an instance of Mammal. + #' + #' @description + #' Deserialize JSON string into an instance of Mammal. + #' + #' @param input The input JSON. + #' @return An instance of Mammal. + #' @export + fromJSON = function(input) { + matched <- 0 # match counter + matched_schemas <- list() #names of matched schemas + error_messages <- list() + instance <- NULL + + Whale_result <- tryCatch({ + Whale$public_methods$validateJSON(input) + Whale_instance <- Whale$new() + instance <- Whale_instance$fromJSON(input) + instance_type <- "Whale" + matched_schemas <- append(matched_schemas, "Whale") + matched <- matched + 1 + }, + error = function(err) err + ) + + if (!is.null(Whale_result["error"])) { + error_messages <- append(error_messages, Whale_result["message"]) + } + + Zebra_result <- tryCatch({ + Zebra$public_methods$validateJSON(input) + Zebra_instance <- Zebra$new() + instance <- Zebra_instance$fromJSON(input) + instance_type <- "Zebra" + matched_schemas <- append(matched_schemas, "Zebra") + matched <- matched + 1 + }, + error = function(err) err + ) + + if (!is.null(Zebra_result["error"])) { + error_messages <- append(error_messages, Zebra_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 Mammal with oneOf schemas Whale, Zebra.") + } else { + # no match + stop(paste("No match found when deserializing the payload into Mammal with oneOf schemas Whale, Zebra. Details: ", + paste(error_messages, collapse = ", "))) + } + + self + }, + #' Serialize Mammal to JSON string. + #' + #' @description + #' Serialize Mammal to JSON string. + #' + #' @return JSON string representation of the Mammal. + #' @export + toJSONString = function() { + if (!is.null(self$actual_instance)) { + as.character(jsonlite::minify(self$actual_instance$toJSONString())) + } else { + NULL + } + }, + #' Serialize Mammal to JSON. + #' + #' @description + #' Serialize Mammal to JSON. + #' + #' @return JSON representation of the Mammal. + #' @export + toJSON = function() { + if (!is.null(self$actual_instance)) { + self$actual_instance$toJSON() + } else { + NULL + } + }, + #' Validate the input JSON with respect to Mammal. + #' + #' @description + #' Validate the input JSON with respect to Mammal 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 = ""))) + } + ), + # Lock the class to prevent modifications to the method or field + lock_class = TRUE +) + +# Unlock the class to allow modifications of the method or field +Mammal$unlock() + +#' Print the object +#' +#' @description +#' Print the object +#' +#' @export +Mammal$set("public", "print", function(...) { + print(jsonlite::prettify(self$toJSONString())) + invisible(self) +}) + +# Lock the class to prevent modifications to the method or field +Mammal$lock() + diff --git a/samples/client/petstore/R-httr2/R/whale.R b/samples/client/petstore/R-httr2/R/whale.R new file mode 100644 index 00000000000..31738ea3622 --- /dev/null +++ b/samples/client/petstore/R-httr2/R/whale.R @@ -0,0 +1,223 @@ +#' Create a new Whale +#' +#' @description +#' Whale Class +#' +#' @docType class +#' @title Whale +#' @description Whale Class +#' @format An \code{R6Class} generator object +#' @field hasBaleen character [optional] +#' @field hasTeeth character [optional] +#' @field className character +#' @importFrom R6 R6Class +#' @importFrom jsonlite fromJSON toJSON +#' @export +Whale <- R6::R6Class( + "Whale", + public = list( + `hasBaleen` = NULL, + `hasTeeth` = NULL, + `className` = NULL, + #' Initialize a new Whale class. + #' + #' @description + #' Initialize a new Whale class. + #' + #' @param className className + #' @param hasBaleen hasBaleen + #' @param hasTeeth hasTeeth + #' @param ... Other optional arguments. + #' @export + initialize = function( + `className`, `hasBaleen` = NULL, `hasTeeth` = NULL, ... + ) { + if (!missing(`className`)) { + stopifnot(is.character(`className`), length(`className`) == 1) + self$`className` <- `className` + } + if (!is.null(`hasBaleen`)) { + stopifnot(is.logical(`hasBaleen`), length(`hasBaleen`) == 1) + self$`hasBaleen` <- `hasBaleen` + } + if (!is.null(`hasTeeth`)) { + stopifnot(is.logical(`hasTeeth`), length(`hasTeeth`) == 1) + self$`hasTeeth` <- `hasTeeth` + } + }, + #' To JSON string + #' + #' @description + #' To JSON String + #' + #' @return Whale in JSON format + #' @export + toJSON = function() { + WhaleObject <- list() + if (!is.null(self$`hasBaleen`)) { + WhaleObject[["hasBaleen"]] <- + self$`hasBaleen` + } + if (!is.null(self$`hasTeeth`)) { + WhaleObject[["hasTeeth"]] <- + self$`hasTeeth` + } + if (!is.null(self$`className`)) { + WhaleObject[["className"]] <- + self$`className` + } + + WhaleObject + }, + #' Deserialize JSON string into an instance of Whale + #' + #' @description + #' Deserialize JSON string into an instance of Whale + #' + #' @param input_json the JSON input + #' @return the instance of Whale + #' @export + fromJSON = function(input_json) { + this_object <- jsonlite::fromJSON(input_json) + if (!is.null(this_object$`hasBaleen`)) { + self$`hasBaleen` <- this_object$`hasBaleen` + } + if (!is.null(this_object$`hasTeeth`)) { + self$`hasTeeth` <- this_object$`hasTeeth` + } + if (!is.null(this_object$`className`)) { + self$`className` <- this_object$`className` + } + self + }, + #' To JSON string + #' + #' @description + #' To JSON String + #' + #' @return Whale in JSON format + #' @export + toJSONString = function() { + jsoncontent <- c( + if (!is.null(self$`hasBaleen`)) { + sprintf( + '"hasBaleen": + %s + ', + tolower(self$`hasBaleen`) + ) + }, + if (!is.null(self$`hasTeeth`)) { + sprintf( + '"hasTeeth": + %s + ', + tolower(self$`hasTeeth`) + ) + }, + if (!is.null(self$`className`)) { + sprintf( + '"className": + "%s" + ', + self$`className` + ) + } + ) + jsoncontent <- paste(jsoncontent, collapse = ",") + as.character(jsonlite::minify(paste("{", jsoncontent, "}", sep = ""))) + }, + #' Deserialize JSON string into an instance of Whale + #' + #' @description + #' Deserialize JSON string into an instance of Whale + #' + #' @param input_json the JSON input + #' @return the instance of Whale + #' @export + fromJSONString = function(input_json) { + this_object <- jsonlite::fromJSON(input_json) + self$`hasBaleen` <- this_object$`hasBaleen` + self$`hasTeeth` <- this_object$`hasTeeth` + self$`className` <- this_object$`className` + self + }, + #' Validate JSON input with respect to Whale + #' + #' @description + #' Validate JSON input with respect to Whale and throw an exception if invalid + #' + #' @param input the JSON input + #' @export + validateJSON = function(input) { + input_json <- jsonlite::fromJSON(input) + # check the required field `className` + if (!is.null(input_json$`className`)) { + stopifnot(is.character(input_json$`className`), length(input_json$`className`) == 1) + } else { + stop(paste("The JSON input `", input, "` is invalid for Whale: the required field `className` is missing.")) + } + }, + #' To string (JSON format) + #' + #' @description + #' To string (JSON format) + #' + #' @return String representation of Whale + #' @export + toString = function() { + self$toJSONString() + }, + #' Return true if the values in all fields are valid. + #' + #' @description + #' Return true if the values in all fields are valid. + #' + #' @return true if the values in all fields are valid. + #' @export + isValid = function() { + # check if the required `className` is null + if (is.null(self$`className`)) { + return(FALSE) + } + + TRUE + }, + #' Return a list of invalid fields (if any). + #' + #' @description + #' Return a list of invalid fields (if any). + #' + #' @return A list of invalid fields (if any). + #' @export + getInvalidFields = function() { + invalid_fields <- list() + # check if the required `className` is null + if (is.null(self$`className`)) { + invalid_fields["className"] <- "Non-nullable required field `className` cannot be null." + } + + invalid_fields + } + ), + # Lock the class to prevent modifications to the method or field + lock_class = TRUE +) + +# Unlock the class to allow modifications of the method or field +Whale$unlock() + +#' Print the object +#' +#' @description +#' Print the object +#' +#' @export +Whale$set("public", "print", function(...) { + print(jsonlite::prettify(self$toJSONString())) + invisible(self) +}) + +# Lock the class to prevent modifications to the method or field +Whale$lock() + diff --git a/samples/client/petstore/R-httr2/R/zebra.R b/samples/client/petstore/R-httr2/R/zebra.R new file mode 100644 index 00000000000..d9d2788aca9 --- /dev/null +++ b/samples/client/petstore/R-httr2/R/zebra.R @@ -0,0 +1,200 @@ +#' Create a new Zebra +#' +#' @description +#' Zebra Class +#' +#' @docType class +#' @title Zebra +#' @description Zebra Class +#' @format An \code{R6Class} generator object +#' @field type character [optional] +#' @field className character +#' @importFrom R6 R6Class +#' @importFrom jsonlite fromJSON toJSON +#' @export +Zebra <- R6::R6Class( + "Zebra", + public = list( + `type` = NULL, + `className` = NULL, + #' Initialize a new Zebra class. + #' + #' @description + #' Initialize a new Zebra class. + #' + #' @param className className + #' @param type type + #' @param ... Other optional arguments. + #' @export + initialize = function( + `className`, `type` = NULL, ... + ) { + if (!missing(`className`)) { + stopifnot(is.character(`className`), length(`className`) == 1) + self$`className` <- `className` + } + if (!is.null(`type`)) { + stopifnot(is.character(`type`), length(`type`) == 1) + self$`type` <- `type` + } + }, + #' To JSON string + #' + #' @description + #' To JSON String + #' + #' @return Zebra in JSON format + #' @export + toJSON = function() { + ZebraObject <- list() + if (!is.null(self$`type`)) { + ZebraObject[["type"]] <- + self$`type` + } + if (!is.null(self$`className`)) { + ZebraObject[["className"]] <- + self$`className` + } + + ZebraObject + }, + #' Deserialize JSON string into an instance of Zebra + #' + #' @description + #' Deserialize JSON string into an instance of Zebra + #' + #' @param input_json the JSON input + #' @return the instance of Zebra + #' @export + fromJSON = function(input_json) { + this_object <- jsonlite::fromJSON(input_json) + if (!is.null(this_object$`type`)) { + self$`type` <- this_object$`type` + } + if (!is.null(this_object$`className`)) { + self$`className` <- this_object$`className` + } + self + }, + #' To JSON string + #' + #' @description + #' To JSON String + #' + #' @return Zebra in JSON format + #' @export + toJSONString = function() { + jsoncontent <- c( + if (!is.null(self$`type`)) { + sprintf( + '"type": + "%s" + ', + self$`type` + ) + }, + if (!is.null(self$`className`)) { + sprintf( + '"className": + "%s" + ', + self$`className` + ) + } + ) + jsoncontent <- paste(jsoncontent, collapse = ",") + as.character(jsonlite::minify(paste("{", jsoncontent, "}", sep = ""))) + }, + #' Deserialize JSON string into an instance of Zebra + #' + #' @description + #' Deserialize JSON string into an instance of Zebra + #' + #' @param input_json the JSON input + #' @return the instance of Zebra + #' @export + fromJSONString = function(input_json) { + this_object <- jsonlite::fromJSON(input_json) + self$`type` <- this_object$`type` + self$`className` <- this_object$`className` + self + }, + #' Validate JSON input with respect to Zebra + #' + #' @description + #' Validate JSON input with respect to Zebra and throw an exception if invalid + #' + #' @param input the JSON input + #' @export + validateJSON = function(input) { + input_json <- jsonlite::fromJSON(input) + # check the required field `className` + if (!is.null(input_json$`className`)) { + stopifnot(is.character(input_json$`className`), length(input_json$`className`) == 1) + } else { + stop(paste("The JSON input `", input, "` is invalid for Zebra: the required field `className` is missing.")) + } + }, + #' To string (JSON format) + #' + #' @description + #' To string (JSON format) + #' + #' @return String representation of Zebra + #' @export + toString = function() { + self$toJSONString() + }, + #' Return true if the values in all fields are valid. + #' + #' @description + #' Return true if the values in all fields are valid. + #' + #' @return true if the values in all fields are valid. + #' @export + isValid = function() { + # check if the required `className` is null + if (is.null(self$`className`)) { + return(FALSE) + } + + TRUE + }, + #' Return a list of invalid fields (if any). + #' + #' @description + #' Return a list of invalid fields (if any). + #' + #' @return A list of invalid fields (if any). + #' @export + getInvalidFields = function() { + invalid_fields <- list() + # check if the required `className` is null + if (is.null(self$`className`)) { + invalid_fields["className"] <- "Non-nullable required field `className` cannot be null." + } + + invalid_fields + } + ), + # Lock the class to prevent modifications to the method or field + lock_class = TRUE +) + +# Unlock the class to allow modifications of the method or field +Zebra$unlock() + +#' Print the object +#' +#' @description +#' Print the object +#' +#' @export +Zebra$set("public", "print", function(...) { + print(jsonlite::prettify(self$toJSONString())) + invisible(self) +}) + +# Lock the class to prevent modifications to the method or field +Zebra$lock() + diff --git a/samples/client/petstore/R-httr2/README.md b/samples/client/petstore/R-httr2/README.md index 7b97c29fe51..b71e5202220 100644 --- a/samples/client/petstore/R-httr2/README.md +++ b/samples/client/petstore/R-httr2/README.md @@ -100,6 +100,7 @@ Class | Method | HTTP request | Description - [DanishPig](docs/DanishPig.md) - [Dog](docs/Dog.md) - [DogAllOf](docs/DogAllOf.md) + - [Mammal](docs/Mammal.md) - [ModelApiResponse](docs/ModelApiResponse.md) - [NestedOneOf](docs/NestedOneOf.md) - [OneOfPrimitiveTypeTest](docs/OneOfPrimitiveTypeTest.md) @@ -110,6 +111,8 @@ Class | Method | HTTP request | Description - [Tag](docs/Tag.md) - [UpdatePetRequest](docs/UpdatePetRequest.md) - [User](docs/User.md) + - [Whale](docs/Whale.md) + - [Zebra](docs/Zebra.md) ## Documentation for Authorization diff --git a/samples/client/petstore/R-httr2/docs/Mammal.md b/samples/client/petstore/R-httr2/docs/Mammal.md new file mode 100644 index 00000000000..d9746cd2df1 --- /dev/null +++ b/samples/client/petstore/R-httr2/docs/Mammal.md @@ -0,0 +1,12 @@ +# petstore::Mammal + + +## Properties +Name | Type | Description | Notes +------------ | ------------- | ------------- | ------------- +**hasBaleen** | **character** | | [optional] +**hasTeeth** | **character** | | [optional] +**className** | **character** | | +**type** | **character** | | [optional] + + diff --git a/samples/client/petstore/R-httr2/docs/Whale.md b/samples/client/petstore/R-httr2/docs/Whale.md new file mode 100644 index 00000000000..bd0d148686f --- /dev/null +++ b/samples/client/petstore/R-httr2/docs/Whale.md @@ -0,0 +1,11 @@ +# petstore::Whale + + +## Properties +Name | Type | Description | Notes +------------ | ------------- | ------------- | ------------- +**hasBaleen** | **character** | | [optional] +**hasTeeth** | **character** | | [optional] +**className** | **character** | | + + diff --git a/samples/client/petstore/R-httr2/docs/Zebra.md b/samples/client/petstore/R-httr2/docs/Zebra.md new file mode 100644 index 00000000000..84e4d20ce94 --- /dev/null +++ b/samples/client/petstore/R-httr2/docs/Zebra.md @@ -0,0 +1,10 @@ +# petstore::Zebra + + +## Properties +Name | Type | Description | Notes +------------ | ------------- | ------------- | ------------- +**type** | **character** | | [optional] +**className** | **character** | | + + diff --git a/samples/client/petstore/R-httr2/tests/testthat/test_mammal.R b/samples/client/petstore/R-httr2/tests/testthat/test_mammal.R new file mode 100644 index 00000000000..bc061ea799a --- /dev/null +++ b/samples/client/petstore/R-httr2/tests/testthat/test_mammal.R @@ -0,0 +1,34 @@ +# Automatically generated by openapi-generator (https://openapi-generator.tech) +# Please update as you see appropriate + +context("Test Mammal") + +model_instance <- Mammal$new() + +test_that("hasBaleen", { + # tests for the property `hasBaleen` (character) + + # uncomment below to test the property + #expect_equal(model.instance$`hasBaleen`, "EXPECTED_RESULT") +}) + +test_that("hasTeeth", { + # tests for the property `hasTeeth` (character) + + # uncomment below to test the property + #expect_equal(model.instance$`hasTeeth`, "EXPECTED_RESULT") +}) + +test_that("className", { + # tests for the property `className` (character) + + # uncomment below to test the property + #expect_equal(model.instance$`className`, "EXPECTED_RESULT") +}) + +test_that("type", { + # tests for the property `type` (character) + + # uncomment below to test the property + #expect_equal(model.instance$`type`, "EXPECTED_RESULT") +}) diff --git a/samples/client/petstore/R-httr2/tests/testthat/test_whale.R b/samples/client/petstore/R-httr2/tests/testthat/test_whale.R new file mode 100644 index 00000000000..b19c7b9eb3e --- /dev/null +++ b/samples/client/petstore/R-httr2/tests/testthat/test_whale.R @@ -0,0 +1,27 @@ +# Automatically generated by openapi-generator (https://openapi-generator.tech) +# Please update as you see appropriate + +context("Test Whale") + +model_instance <- Whale$new() + +test_that("hasBaleen", { + # tests for the property `hasBaleen` (character) + + # uncomment below to test the property + #expect_equal(model.instance$`hasBaleen`, "EXPECTED_RESULT") +}) + +test_that("hasTeeth", { + # tests for the property `hasTeeth` (character) + + # uncomment below to test the property + #expect_equal(model.instance$`hasTeeth`, "EXPECTED_RESULT") +}) + +test_that("className", { + # tests for the property `className` (character) + + # uncomment below to test the property + #expect_equal(model.instance$`className`, "EXPECTED_RESULT") +}) diff --git a/samples/client/petstore/R-httr2/tests/testthat/test_zebra.R b/samples/client/petstore/R-httr2/tests/testthat/test_zebra.R new file mode 100644 index 00000000000..444da2f8f77 --- /dev/null +++ b/samples/client/petstore/R-httr2/tests/testthat/test_zebra.R @@ -0,0 +1,20 @@ +# Automatically generated by openapi-generator (https://openapi-generator.tech) +# Please update as you see appropriate + +context("Test Zebra") + +model_instance <- Zebra$new() + +test_that("type", { + # tests for the property `type` (character) + + # uncomment below to test the property + #expect_equal(model.instance$`type`, "EXPECTED_RESULT") +}) + +test_that("className", { + # tests for the property `className` (character) + + # uncomment below to test the property + #expect_equal(model.instance$`className`, "EXPECTED_RESULT") +}) diff --git a/samples/client/petstore/R/.openapi-generator/FILES b/samples/client/petstore/R/.openapi-generator/FILES index 11069aa4bcc..2e11de9d1ab 100644 --- a/samples/client/petstore/R/.openapi-generator/FILES +++ b/samples/client/petstore/R/.openapi-generator/FILES @@ -20,6 +20,7 @@ R/danish_pig.R R/dog.R R/dog_all_of.R R/fake_api.R +R/mammal.R R/model_api_response.R R/nested_one_of.R R/one_of_primitive_type_test.R @@ -33,6 +34,8 @@ R/tag.R R/update_pet_request.R R/user.R R/user_api.R +R/whale.R +R/zebra.R README.md docs/AllofTagApiResponse.md docs/Animal.md @@ -46,6 +49,7 @@ docs/DanishPig.md docs/Dog.md docs/DogAllOf.md docs/FakeApi.md +docs/Mammal.md docs/ModelApiResponse.md docs/NestedOneOf.md docs/OneOfPrimitiveTypeTest.md @@ -59,5 +63,7 @@ docs/Tag.md docs/UpdatePetRequest.md docs/User.md docs/UserApi.md +docs/Whale.md +docs/Zebra.md git_push.sh tests/testthat.R diff --git a/samples/client/petstore/R/NAMESPACE b/samples/client/petstore/R/NAMESPACE index d8e1c837ba1..a62165a21d8 100644 --- a/samples/client/petstore/R/NAMESPACE +++ b/samples/client/petstore/R/NAMESPACE @@ -25,6 +25,7 @@ export(Category) export(DanishPig) export(Dog) export(DogAllOf) +export(Mammal) export(ModelApiResponse) export(NestedOneOf) export(OneOfPrimitiveTypeTest) @@ -35,6 +36,8 @@ export(Special) export(Tag) export(UpdatePetRequest) export(User) +export(Whale) +export(Zebra) # APIs export(FakeApi) diff --git a/samples/client/petstore/R/R/mammal.R b/samples/client/petstore/R/R/mammal.R new file mode 100644 index 00000000000..14857c098e4 --- /dev/null +++ b/samples/client/petstore/R/R/mammal.R @@ -0,0 +1,197 @@ +#' @docType class +#' @title Mammal +#' +#' @description Mammal Class +#' +#' @format An \code{R6Class} generator object +#' +#' @importFrom R6 R6Class +#' @importFrom jsonlite fromJSON toJSON +#' @export +Mammal <- R6::R6Class( + "Mammal", + 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("Whale", "Zebra"), + #' Initialize a new Mammal. + #' + #' @description + #' Initialize a new Mammal. + #' + #' @param instance an instance of the object defined in the oneOf schemas: "Whale", "Zebra" + #' @export + initialize = function(instance = NULL) { + if (is.null(instance)) { + # do nothing + } else if (get(class(instance)[[1]], pos = -1)$classname == "Whale") { + self$actual_instance <- instance + self$actual_type <- "Whale" + } else if (get(class(instance)[[1]], pos = -1)$classname == "Zebra") { + self$actual_instance <- instance + self$actual_type <- "Zebra" + } else { + stop(paste("Failed to initialize Mammal with oneOf schemas Whale, Zebra. Provided class name: ", + get(class(instance)[[1]], pos = -1)$classname)) + } + }, + #' Deserialize JSON string into an instance of Mammal. + #' + #' @description + #' Deserialize JSON string into an instance of Mammal. + #' An alias to the method `fromJSON` . + #' + #' @param input The input JSON. + #' @return An instance of Mammal. + #' @export + fromJSONString = function(input) { + self$fromJSON(input) + }, + #' Deserialize JSON string into an instance of Mammal. + #' + #' @description + #' Deserialize JSON string into an instance of Mammal. + #' + #' @param input The input JSON. + #' @return An instance of Mammal. + #' @export + fromJSON = function(input) { + matched <- 0 # match counter + matched_schemas <- list() #names of matched schemas + error_messages <- list() + instance <- NULL + + Whale_result <- tryCatch({ + Whale$public_methods$validateJSON(input) + Whale_instance <- Whale$new() + instance <- Whale_instance$fromJSON(input) + instance_type <- "Whale" + matched_schemas <- append(matched_schemas, "Whale") + matched <- matched + 1 + }, + error = function(err) err + ) + + if (!is.null(Whale_result["error"])) { + error_messages <- append(error_messages, Whale_result["message"]) + } + + Zebra_result <- tryCatch({ + Zebra$public_methods$validateJSON(input) + Zebra_instance <- Zebra$new() + instance <- Zebra_instance$fromJSON(input) + instance_type <- "Zebra" + matched_schemas <- append(matched_schemas, "Zebra") + matched <- matched + 1 + }, + error = function(err) err + ) + + if (!is.null(Zebra_result["error"])) { + error_messages <- append(error_messages, Zebra_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 Mammal with oneOf schemas Whale, Zebra.") + } else { + # no match + stop(paste("No match found when deserializing the payload into Mammal with oneOf schemas Whale, Zebra. Details: ", + paste(error_messages, collapse = ", "))) + } + + self + }, + #' Serialize Mammal to JSON string. + #' + #' @description + #' Serialize Mammal to JSON string. + #' + #' @return JSON string representation of the Mammal. + #' @export + toJSONString = function() { + if (!is.null(self$actual_instance)) { + as.character(jsonlite::minify(self$actual_instance$toJSONString())) + } else { + NULL + } + }, + #' Serialize Mammal to JSON. + #' + #' @description + #' Serialize Mammal to JSON. + #' + #' @return JSON representation of the Mammal. + #' @export + toJSON = function() { + if (!is.null(self$actual_instance)) { + self$actual_instance$toJSON() + } else { + NULL + } + }, + #' Validate the input JSON with respect to Mammal. + #' + #' @description + #' Validate the input JSON with respect to Mammal 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 = ""))) + } + ), + # Lock the class to prevent modifications to the method or field + lock_class = TRUE +) + +# Unlock the class to allow modifications of the method or field +Mammal$unlock() + +#' Print the object +#' +#' @description +#' Print the object +#' +#' @export +Mammal$set("public", "print", function(...) { + print(jsonlite::prettify(self$toJSONString())) + invisible(self) +}) + +# Lock the class to prevent modifications to the method or field +Mammal$lock() + diff --git a/samples/client/petstore/R/R/whale.R b/samples/client/petstore/R/R/whale.R new file mode 100644 index 00000000000..31738ea3622 --- /dev/null +++ b/samples/client/petstore/R/R/whale.R @@ -0,0 +1,223 @@ +#' Create a new Whale +#' +#' @description +#' Whale Class +#' +#' @docType class +#' @title Whale +#' @description Whale Class +#' @format An \code{R6Class} generator object +#' @field hasBaleen character [optional] +#' @field hasTeeth character [optional] +#' @field className character +#' @importFrom R6 R6Class +#' @importFrom jsonlite fromJSON toJSON +#' @export +Whale <- R6::R6Class( + "Whale", + public = list( + `hasBaleen` = NULL, + `hasTeeth` = NULL, + `className` = NULL, + #' Initialize a new Whale class. + #' + #' @description + #' Initialize a new Whale class. + #' + #' @param className className + #' @param hasBaleen hasBaleen + #' @param hasTeeth hasTeeth + #' @param ... Other optional arguments. + #' @export + initialize = function( + `className`, `hasBaleen` = NULL, `hasTeeth` = NULL, ... + ) { + if (!missing(`className`)) { + stopifnot(is.character(`className`), length(`className`) == 1) + self$`className` <- `className` + } + if (!is.null(`hasBaleen`)) { + stopifnot(is.logical(`hasBaleen`), length(`hasBaleen`) == 1) + self$`hasBaleen` <- `hasBaleen` + } + if (!is.null(`hasTeeth`)) { + stopifnot(is.logical(`hasTeeth`), length(`hasTeeth`) == 1) + self$`hasTeeth` <- `hasTeeth` + } + }, + #' To JSON string + #' + #' @description + #' To JSON String + #' + #' @return Whale in JSON format + #' @export + toJSON = function() { + WhaleObject <- list() + if (!is.null(self$`hasBaleen`)) { + WhaleObject[["hasBaleen"]] <- + self$`hasBaleen` + } + if (!is.null(self$`hasTeeth`)) { + WhaleObject[["hasTeeth"]] <- + self$`hasTeeth` + } + if (!is.null(self$`className`)) { + WhaleObject[["className"]] <- + self$`className` + } + + WhaleObject + }, + #' Deserialize JSON string into an instance of Whale + #' + #' @description + #' Deserialize JSON string into an instance of Whale + #' + #' @param input_json the JSON input + #' @return the instance of Whale + #' @export + fromJSON = function(input_json) { + this_object <- jsonlite::fromJSON(input_json) + if (!is.null(this_object$`hasBaleen`)) { + self$`hasBaleen` <- this_object$`hasBaleen` + } + if (!is.null(this_object$`hasTeeth`)) { + self$`hasTeeth` <- this_object$`hasTeeth` + } + if (!is.null(this_object$`className`)) { + self$`className` <- this_object$`className` + } + self + }, + #' To JSON string + #' + #' @description + #' To JSON String + #' + #' @return Whale in JSON format + #' @export + toJSONString = function() { + jsoncontent <- c( + if (!is.null(self$`hasBaleen`)) { + sprintf( + '"hasBaleen": + %s + ', + tolower(self$`hasBaleen`) + ) + }, + if (!is.null(self$`hasTeeth`)) { + sprintf( + '"hasTeeth": + %s + ', + tolower(self$`hasTeeth`) + ) + }, + if (!is.null(self$`className`)) { + sprintf( + '"className": + "%s" + ', + self$`className` + ) + } + ) + jsoncontent <- paste(jsoncontent, collapse = ",") + as.character(jsonlite::minify(paste("{", jsoncontent, "}", sep = ""))) + }, + #' Deserialize JSON string into an instance of Whale + #' + #' @description + #' Deserialize JSON string into an instance of Whale + #' + #' @param input_json the JSON input + #' @return the instance of Whale + #' @export + fromJSONString = function(input_json) { + this_object <- jsonlite::fromJSON(input_json) + self$`hasBaleen` <- this_object$`hasBaleen` + self$`hasTeeth` <- this_object$`hasTeeth` + self$`className` <- this_object$`className` + self + }, + #' Validate JSON input with respect to Whale + #' + #' @description + #' Validate JSON input with respect to Whale and throw an exception if invalid + #' + #' @param input the JSON input + #' @export + validateJSON = function(input) { + input_json <- jsonlite::fromJSON(input) + # check the required field `className` + if (!is.null(input_json$`className`)) { + stopifnot(is.character(input_json$`className`), length(input_json$`className`) == 1) + } else { + stop(paste("The JSON input `", input, "` is invalid for Whale: the required field `className` is missing.")) + } + }, + #' To string (JSON format) + #' + #' @description + #' To string (JSON format) + #' + #' @return String representation of Whale + #' @export + toString = function() { + self$toJSONString() + }, + #' Return true if the values in all fields are valid. + #' + #' @description + #' Return true if the values in all fields are valid. + #' + #' @return true if the values in all fields are valid. + #' @export + isValid = function() { + # check if the required `className` is null + if (is.null(self$`className`)) { + return(FALSE) + } + + TRUE + }, + #' Return a list of invalid fields (if any). + #' + #' @description + #' Return a list of invalid fields (if any). + #' + #' @return A list of invalid fields (if any). + #' @export + getInvalidFields = function() { + invalid_fields <- list() + # check if the required `className` is null + if (is.null(self$`className`)) { + invalid_fields["className"] <- "Non-nullable required field `className` cannot be null." + } + + invalid_fields + } + ), + # Lock the class to prevent modifications to the method or field + lock_class = TRUE +) + +# Unlock the class to allow modifications of the method or field +Whale$unlock() + +#' Print the object +#' +#' @description +#' Print the object +#' +#' @export +Whale$set("public", "print", function(...) { + print(jsonlite::prettify(self$toJSONString())) + invisible(self) +}) + +# Lock the class to prevent modifications to the method or field +Whale$lock() + diff --git a/samples/client/petstore/R/R/zebra.R b/samples/client/petstore/R/R/zebra.R new file mode 100644 index 00000000000..d9d2788aca9 --- /dev/null +++ b/samples/client/petstore/R/R/zebra.R @@ -0,0 +1,200 @@ +#' Create a new Zebra +#' +#' @description +#' Zebra Class +#' +#' @docType class +#' @title Zebra +#' @description Zebra Class +#' @format An \code{R6Class} generator object +#' @field type character [optional] +#' @field className character +#' @importFrom R6 R6Class +#' @importFrom jsonlite fromJSON toJSON +#' @export +Zebra <- R6::R6Class( + "Zebra", + public = list( + `type` = NULL, + `className` = NULL, + #' Initialize a new Zebra class. + #' + #' @description + #' Initialize a new Zebra class. + #' + #' @param className className + #' @param type type + #' @param ... Other optional arguments. + #' @export + initialize = function( + `className`, `type` = NULL, ... + ) { + if (!missing(`className`)) { + stopifnot(is.character(`className`), length(`className`) == 1) + self$`className` <- `className` + } + if (!is.null(`type`)) { + stopifnot(is.character(`type`), length(`type`) == 1) + self$`type` <- `type` + } + }, + #' To JSON string + #' + #' @description + #' To JSON String + #' + #' @return Zebra in JSON format + #' @export + toJSON = function() { + ZebraObject <- list() + if (!is.null(self$`type`)) { + ZebraObject[["type"]] <- + self$`type` + } + if (!is.null(self$`className`)) { + ZebraObject[["className"]] <- + self$`className` + } + + ZebraObject + }, + #' Deserialize JSON string into an instance of Zebra + #' + #' @description + #' Deserialize JSON string into an instance of Zebra + #' + #' @param input_json the JSON input + #' @return the instance of Zebra + #' @export + fromJSON = function(input_json) { + this_object <- jsonlite::fromJSON(input_json) + if (!is.null(this_object$`type`)) { + self$`type` <- this_object$`type` + } + if (!is.null(this_object$`className`)) { + self$`className` <- this_object$`className` + } + self + }, + #' To JSON string + #' + #' @description + #' To JSON String + #' + #' @return Zebra in JSON format + #' @export + toJSONString = function() { + jsoncontent <- c( + if (!is.null(self$`type`)) { + sprintf( + '"type": + "%s" + ', + self$`type` + ) + }, + if (!is.null(self$`className`)) { + sprintf( + '"className": + "%s" + ', + self$`className` + ) + } + ) + jsoncontent <- paste(jsoncontent, collapse = ",") + as.character(jsonlite::minify(paste("{", jsoncontent, "}", sep = ""))) + }, + #' Deserialize JSON string into an instance of Zebra + #' + #' @description + #' Deserialize JSON string into an instance of Zebra + #' + #' @param input_json the JSON input + #' @return the instance of Zebra + #' @export + fromJSONString = function(input_json) { + this_object <- jsonlite::fromJSON(input_json) + self$`type` <- this_object$`type` + self$`className` <- this_object$`className` + self + }, + #' Validate JSON input with respect to Zebra + #' + #' @description + #' Validate JSON input with respect to Zebra and throw an exception if invalid + #' + #' @param input the JSON input + #' @export + validateJSON = function(input) { + input_json <- jsonlite::fromJSON(input) + # check the required field `className` + if (!is.null(input_json$`className`)) { + stopifnot(is.character(input_json$`className`), length(input_json$`className`) == 1) + } else { + stop(paste("The JSON input `", input, "` is invalid for Zebra: the required field `className` is missing.")) + } + }, + #' To string (JSON format) + #' + #' @description + #' To string (JSON format) + #' + #' @return String representation of Zebra + #' @export + toString = function() { + self$toJSONString() + }, + #' Return true if the values in all fields are valid. + #' + #' @description + #' Return true if the values in all fields are valid. + #' + #' @return true if the values in all fields are valid. + #' @export + isValid = function() { + # check if the required `className` is null + if (is.null(self$`className`)) { + return(FALSE) + } + + TRUE + }, + #' Return a list of invalid fields (if any). + #' + #' @description + #' Return a list of invalid fields (if any). + #' + #' @return A list of invalid fields (if any). + #' @export + getInvalidFields = function() { + invalid_fields <- list() + # check if the required `className` is null + if (is.null(self$`className`)) { + invalid_fields["className"] <- "Non-nullable required field `className` cannot be null." + } + + invalid_fields + } + ), + # Lock the class to prevent modifications to the method or field + lock_class = TRUE +) + +# Unlock the class to allow modifications of the method or field +Zebra$unlock() + +#' Print the object +#' +#' @description +#' Print the object +#' +#' @export +Zebra$set("public", "print", function(...) { + print(jsonlite::prettify(self$toJSONString())) + invisible(self) +}) + +# Lock the class to prevent modifications to the method or field +Zebra$lock() + diff --git a/samples/client/petstore/R/README.md b/samples/client/petstore/R/README.md index 31a85b19511..baf7bc69609 100644 --- a/samples/client/petstore/R/README.md +++ b/samples/client/petstore/R/README.md @@ -100,6 +100,7 @@ Class | Method | HTTP request | Description - [DanishPig](docs/DanishPig.md) - [Dog](docs/Dog.md) - [DogAllOf](docs/DogAllOf.md) + - [Mammal](docs/Mammal.md) - [ModelApiResponse](docs/ModelApiResponse.md) - [NestedOneOf](docs/NestedOneOf.md) - [OneOfPrimitiveTypeTest](docs/OneOfPrimitiveTypeTest.md) @@ -110,6 +111,8 @@ Class | Method | HTTP request | Description - [Tag](docs/Tag.md) - [UpdatePetRequest](docs/UpdatePetRequest.md) - [User](docs/User.md) + - [Whale](docs/Whale.md) + - [Zebra](docs/Zebra.md) ## Documentation for Authorization diff --git a/samples/client/petstore/R/docs/Mammal.md b/samples/client/petstore/R/docs/Mammal.md new file mode 100644 index 00000000000..d9746cd2df1 --- /dev/null +++ b/samples/client/petstore/R/docs/Mammal.md @@ -0,0 +1,12 @@ +# petstore::Mammal + + +## Properties +Name | Type | Description | Notes +------------ | ------------- | ------------- | ------------- +**hasBaleen** | **character** | | [optional] +**hasTeeth** | **character** | | [optional] +**className** | **character** | | +**type** | **character** | | [optional] + + diff --git a/samples/client/petstore/R/docs/Whale.md b/samples/client/petstore/R/docs/Whale.md new file mode 100644 index 00000000000..bd0d148686f --- /dev/null +++ b/samples/client/petstore/R/docs/Whale.md @@ -0,0 +1,11 @@ +# petstore::Whale + + +## Properties +Name | Type | Description | Notes +------------ | ------------- | ------------- | ------------- +**hasBaleen** | **character** | | [optional] +**hasTeeth** | **character** | | [optional] +**className** | **character** | | + + diff --git a/samples/client/petstore/R/docs/Zebra.md b/samples/client/petstore/R/docs/Zebra.md new file mode 100644 index 00000000000..84e4d20ce94 --- /dev/null +++ b/samples/client/petstore/R/docs/Zebra.md @@ -0,0 +1,10 @@ +# petstore::Zebra + + +## Properties +Name | Type | Description | Notes +------------ | ------------- | ------------- | ------------- +**type** | **character** | | [optional] +**className** | **character** | | + + diff --git a/samples/client/petstore/R/tests/testthat/test_mammal.R b/samples/client/petstore/R/tests/testthat/test_mammal.R new file mode 100644 index 00000000000..bc061ea799a --- /dev/null +++ b/samples/client/petstore/R/tests/testthat/test_mammal.R @@ -0,0 +1,34 @@ +# Automatically generated by openapi-generator (https://openapi-generator.tech) +# Please update as you see appropriate + +context("Test Mammal") + +model_instance <- Mammal$new() + +test_that("hasBaleen", { + # tests for the property `hasBaleen` (character) + + # uncomment below to test the property + #expect_equal(model.instance$`hasBaleen`, "EXPECTED_RESULT") +}) + +test_that("hasTeeth", { + # tests for the property `hasTeeth` (character) + + # uncomment below to test the property + #expect_equal(model.instance$`hasTeeth`, "EXPECTED_RESULT") +}) + +test_that("className", { + # tests for the property `className` (character) + + # uncomment below to test the property + #expect_equal(model.instance$`className`, "EXPECTED_RESULT") +}) + +test_that("type", { + # tests for the property `type` (character) + + # uncomment below to test the property + #expect_equal(model.instance$`type`, "EXPECTED_RESULT") +}) diff --git a/samples/client/petstore/R/tests/testthat/test_whale.R b/samples/client/petstore/R/tests/testthat/test_whale.R new file mode 100644 index 00000000000..b19c7b9eb3e --- /dev/null +++ b/samples/client/petstore/R/tests/testthat/test_whale.R @@ -0,0 +1,27 @@ +# Automatically generated by openapi-generator (https://openapi-generator.tech) +# Please update as you see appropriate + +context("Test Whale") + +model_instance <- Whale$new() + +test_that("hasBaleen", { + # tests for the property `hasBaleen` (character) + + # uncomment below to test the property + #expect_equal(model.instance$`hasBaleen`, "EXPECTED_RESULT") +}) + +test_that("hasTeeth", { + # tests for the property `hasTeeth` (character) + + # uncomment below to test the property + #expect_equal(model.instance$`hasTeeth`, "EXPECTED_RESULT") +}) + +test_that("className", { + # tests for the property `className` (character) + + # uncomment below to test the property + #expect_equal(model.instance$`className`, "EXPECTED_RESULT") +}) diff --git a/samples/client/petstore/R/tests/testthat/test_zebra.R b/samples/client/petstore/R/tests/testthat/test_zebra.R new file mode 100644 index 00000000000..444da2f8f77 --- /dev/null +++ b/samples/client/petstore/R/tests/testthat/test_zebra.R @@ -0,0 +1,20 @@ +# Automatically generated by openapi-generator (https://openapi-generator.tech) +# Please update as you see appropriate + +context("Test Zebra") + +model_instance <- Zebra$new() + +test_that("type", { + # tests for the property `type` (character) + + # uncomment below to test the property + #expect_equal(model.instance$`type`, "EXPECTED_RESULT") +}) + +test_that("className", { + # tests for the property `className` (character) + + # uncomment below to test the property + #expect_equal(model.instance$`className`, "EXPECTED_RESULT") +})