diff --git a/modules/openapi-generator/src/main/resources/r/ApiResponse.mustache b/modules/openapi-generator/src/main/resources/r/ApiResponse.mustache index 9afb8828d555..058bdb5ca280 100644 --- a/modules/openapi-generator/src/main/resources/r/ApiResponse.mustache +++ b/modules/openapi-generator/src/main/resources/r/ApiResponse.mustache @@ -49,7 +49,7 @@ ApiResponse <- R6::R6Class( #' @param from_encoding The encoding of the raw response. #' @param to_encoding The target encoding of the return value. #' @export - response_as_text = function(from_encoding = NULL, to_encoding = "UTF-8") { + response_as_text = function(from_encoding = "", to_encoding = "UTF-8") { text_response <- iconv(readBin(self$response, character()), from = from_encoding, to = to_encoding) if (is.na(text_response)) { warning("The response is binary and will not be converted to text.") diff --git a/modules/openapi-generator/src/main/resources/r/libraries/httr2/api_client.mustache b/modules/openapi-generator/src/main/resources/r/libraries/httr2/api_client.mustache index 54148bca6533..e888008f4597 100644 --- a/modules/openapi-generator/src/main/resources/r/libraries/httr2/api_client.mustache +++ b/modules/openapi-generator/src/main/resources/r/libraries/httr2/api_client.mustache @@ -323,7 +323,11 @@ ApiClient <- R6::R6Class( api_response <- ApiResponse$new() api_response$status_code <- resp %>% resp_status() api_response$status_code_desc <- resp %>% resp_status_desc() - api_response$response <- resp %>% resp_body_raw() + if (length(resp$body) == 0) { + api_response$response <- NULL + } else { + api_response$response <- resp %>% resp_body_raw() + } api_response$headers <- resp %>% resp_headers() api_response diff --git a/modules/openapi-generator/src/main/resources/r/modelGeneric.mustache b/modules/openapi-generator/src/main/resources/r/modelGeneric.mustache index f967537e2944..cdafe398e0d8 100644 --- a/modules/openapi-generator/src/main/resources/r/modelGeneric.mustache +++ b/modules/openapi-generator/src/main/resources/r/modelGeneric.mustache @@ -330,7 +330,7 @@ {{/isArray}} {{#isMap}} {{#isPrimitiveType}} - {{#isNumeric}}%d{{/isNumeric}}{{^isNumeric}}{{^isBoolean}}"{{/isBoolean}}%s{{^isBoolean}}"{{/isBoolean}}{{/isNumeric}} + {{#isNumeric}}%d{{/isNumeric}}{{^isNumeric}}{{^isBoolean}}{{/isBoolean}}%s{{^isBoolean}}{{/isBoolean}}{{/isNumeric}} {{/isPrimitiveType}} {{^isPrimitiveType}}%s {{/isPrimitiveType}} 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 2fd09bffbf84..70497d526ed5 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 @@ -958,6 +958,15 @@ components: - sold xml: name: Pet + PetMap: + title: a PetMap + description: A mock map of a pet and some properties + type: object + properties: + pet: + type: object + additionalProperties: + type: string ApiResponse: title: An uploaded response description: Describes the result of uploading an image resource diff --git a/samples/client/echo_api/r/R/api_response.R b/samples/client/echo_api/r/R/api_response.R index c98bb5122750..404f2ec8a9e8 100644 --- a/samples/client/echo_api/r/R/api_response.R +++ b/samples/client/echo_api/r/R/api_response.R @@ -56,7 +56,7 @@ ApiResponse <- R6::R6Class( #' @param from_encoding The encoding of the raw response. #' @param to_encoding The target encoding of the return value. #' @export - response_as_text = function(from_encoding = NULL, to_encoding = "UTF-8") { + response_as_text = function(from_encoding = "", to_encoding = "UTF-8") { text_response <- iconv(readBin(self$response, character()), from = from_encoding, to = to_encoding) if (is.na(text_response)) { warning("The response is binary and will not be converted to text.") diff --git a/samples/client/petstore/R-httr2-wrapper/.openapi-generator/FILES b/samples/client/petstore/R-httr2-wrapper/.openapi-generator/FILES index 5023966da14b..379998b2a2b8 100644 --- a/samples/client/petstore/R-httr2-wrapper/.openapi-generator/FILES +++ b/samples/client/petstore/R-httr2-wrapper/.openapi-generator/FILES @@ -28,6 +28,7 @@ R/one_of_primitive_type_test.R R/order.R R/pet.R R/pet_api.R +R/pet_map.R R/petstore_api.R R/pig.R R/special.R @@ -59,6 +60,7 @@ docs/OneOfPrimitiveTypeTest.md docs/Order.md docs/Pet.md docs/PetApi.md +docs/PetMap.md docs/Pig.md docs/Special.md docs/StoreApi.md diff --git a/samples/client/petstore/R-httr2-wrapper/NAMESPACE b/samples/client/petstore/R-httr2-wrapper/NAMESPACE index fd062786db13..fd66fd4ef7e8 100644 --- a/samples/client/petstore/R-httr2-wrapper/NAMESPACE +++ b/samples/client/petstore/R-httr2-wrapper/NAMESPACE @@ -34,6 +34,7 @@ export(NestedOneOf) export(OneOfPrimitiveTypeTest) export(Order) export(Pet) +export(PetMap) export(Pig) export(Special) export(Tag) diff --git a/samples/client/petstore/R-httr2-wrapper/R/api_client.R b/samples/client/petstore/R-httr2-wrapper/R/api_client.R index 5e51a57b302b..32aa262f5545 100644 --- a/samples/client/petstore/R-httr2-wrapper/R/api_client.R +++ b/samples/client/petstore/R-httr2-wrapper/R/api_client.R @@ -312,7 +312,11 @@ ApiClient <- R6::R6Class( api_response <- ApiResponse$new() api_response$status_code <- resp %>% resp_status() api_response$status_code_desc <- resp %>% resp_status_desc() - api_response$response <- resp %>% resp_body_raw() + if (length(resp$body) == 0) { + api_response$response <- NULL + } else { + api_response$response <- resp %>% resp_body_raw() + } api_response$headers <- resp %>% resp_headers() api_response diff --git a/samples/client/petstore/R-httr2-wrapper/R/api_response.R b/samples/client/petstore/R-httr2-wrapper/R/api_response.R index f76d20d3f769..b84d66f6ee0f 100644 --- a/samples/client/petstore/R-httr2-wrapper/R/api_response.R +++ b/samples/client/petstore/R-httr2-wrapper/R/api_response.R @@ -55,7 +55,7 @@ ApiResponse <- R6::R6Class( #' @param from_encoding The encoding of the raw response. #' @param to_encoding The target encoding of the return value. #' @export - response_as_text = function(from_encoding = NULL, to_encoding = "UTF-8") { + response_as_text = function(from_encoding = "", to_encoding = "UTF-8") { text_response <- iconv(readBin(self$response, character()), from = from_encoding, to = to_encoding) if (is.na(text_response)) { warning("The response is binary and will not be converted to text.") diff --git a/samples/client/petstore/R-httr2-wrapper/R/pet_map.R b/samples/client/petstore/R-httr2-wrapper/R/pet_map.R new file mode 100644 index 000000000000..80d9eb3abf89 --- /dev/null +++ b/samples/client/petstore/R-httr2-wrapper/R/pet_map.R @@ -0,0 +1,195 @@ +#' Create a new PetMap +#' +#' @description +#' A mock map of a pet and some properties +#' +#' @docType class +#' @title PetMap +#' @description PetMap Class +#' @format An \code{R6Class} generator object +#' @field pet named list(character) [optional] +#' @field _field_list a list of fields list(character) +#' @field additional_properties additional properties list(character) [optional] +#' @importFrom R6 R6Class +#' @importFrom jsonlite fromJSON toJSON +#' @export +PetMap <- R6::R6Class( + "PetMap", + public = list( + `pet` = NULL, + `_field_list` = c("pet"), + `additional_properties` = list(), + #' Initialize a new PetMap class. + #' + #' @description + #' Initialize a new PetMap class. + #' + #' @param pet pet + #' @param additional_properties additional properties (optional) + #' @param ... Other optional arguments. + #' @export + initialize = function(`pet` = NULL, additional_properties = NULL, ...) { + if (!is.null(`pet`)) { + stopifnot(is.vector(`pet`), length(`pet`) != 0) + sapply(`pet`, function(x) stopifnot(is.character(x))) + self$`pet` <- `pet` + } + if (!is.null(additional_properties)) { + for (key in names(additional_properties)) { + self$additional_properties[[key]] <- additional_properties[[key]] + } + } + }, + #' To JSON string + #' + #' @description + #' To JSON String + #' + #' @return PetMap in JSON format + #' @export + toJSON = function() { + PetMapObject <- list() + if (!is.null(self$`pet`)) { + PetMapObject[["pet"]] <- + self$`pet` + } + for (key in names(self$additional_properties)) { + PetMapObject[[key]] <- self$additional_properties[[key]] + } + + PetMapObject + }, + #' Deserialize JSON string into an instance of PetMap + #' + #' @description + #' Deserialize JSON string into an instance of PetMap + #' + #' @param input_json the JSON input + #' @return the instance of PetMap + #' @export + fromJSON = function(input_json) { + this_object <- jsonlite::fromJSON(input_json) + if (!is.null(this_object$`pet`)) { + self$`pet` <- ApiClient$new()$deserializeObj(this_object$`pet`, "map(character)", loadNamespace("petstore")) + } + # process additional properties/fields in the payload + for (key in names(this_object)) { + if (!(key %in% self$`_field_list`)) { # json key not in list of fields + self$additional_properties[[key]] <- this_object[[key]] + } + } + + self + }, + #' To JSON string + #' + #' @description + #' To JSON String + #' + #' @return PetMap in JSON format + #' @export + toJSONString = function() { + jsoncontent <- c( + if (!is.null(self$`pet`)) { + sprintf( + '"pet": + %s + ', + jsonlite::toJSON(lapply(self$`pet`, function(x){ x }), auto_unbox = TRUE, digits = NA) + ) + } + ) + jsoncontent <- paste(jsoncontent, collapse = ",") + json_string <- as.character(jsonlite::minify(paste("{", jsoncontent, "}", sep = ""))) + json_obj <- jsonlite::fromJSON(json_string) + for (key in names(self$additional_properties)) { + json_obj[[key]] <- self$additional_properties[[key]] + } + json_string <- as.character(jsonlite::minify(jsonlite::toJSON(json_obj, auto_unbox = TRUE, digits = NA))) + }, + #' Deserialize JSON string into an instance of PetMap + #' + #' @description + #' Deserialize JSON string into an instance of PetMap + #' + #' @param input_json the JSON input + #' @return the instance of PetMap + #' @export + fromJSONString = function(input_json) { + this_object <- jsonlite::fromJSON(input_json) + self$`pet` <- ApiClient$new()$deserializeObj(this_object$`pet`, "map(character)", loadNamespace("petstore")) + # process additional properties/fields in the payload + for (key in names(this_object)) { + if (!(key %in% self$`_field_list`)) { # json key not in list of fields + self$additional_properties[[key]] <- this_object[[key]] + } + } + + self + }, + #' Validate JSON input with respect to PetMap + #' + #' @description + #' Validate JSON input with respect to PetMap and throw an exception if invalid + #' + #' @param input the JSON input + #' @export + validateJSON = function(input) { + input_json <- jsonlite::fromJSON(input) + }, + #' To string (JSON format) + #' + #' @description + #' To string (JSON format) + #' + #' @return String representation of PetMap + #' @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() { + 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() + invalid_fields + }, + #' Print the object + #' + #' @description + #' Print the object + #' + #' @export + print = function() { + print(jsonlite::prettify(self$toJSONString())) + invisible(self) + } + ), + # Lock the class to prevent modifications to the method or field + lock_class = TRUE +) +## Uncomment below to unlock the class to allow modifications of the method or field +# PetMap$unlock() +# +## Below is an example to define the print function +# PetMap$set("public", "print", function(...) { +# print(jsonlite::prettify(self$toJSONString())) +# invisible(self) +# }) +## Uncomment below to lock the class to prevent modifications to the method or field +# PetMap$lock() + diff --git a/samples/client/petstore/R-httr2-wrapper/README.md b/samples/client/petstore/R-httr2-wrapper/README.md index 888ff773a75f..0f142b8291bd 100644 --- a/samples/client/petstore/R-httr2-wrapper/README.md +++ b/samples/client/petstore/R-httr2-wrapper/README.md @@ -122,6 +122,7 @@ Class | Method | HTTP request | Description - [OneOfPrimitiveTypeTest](docs/OneOfPrimitiveTypeTest.md) - [Order](docs/Order.md) - [Pet](docs/Pet.md) + - [PetMap](docs/PetMap.md) - [Pig](docs/Pig.md) - [Special](docs/Special.md) - [Tag](docs/Tag.md) diff --git a/samples/client/petstore/R-httr2-wrapper/docs/PetMap.md b/samples/client/petstore/R-httr2-wrapper/docs/PetMap.md new file mode 100644 index 000000000000..ba53785e1167 --- /dev/null +++ b/samples/client/petstore/R-httr2-wrapper/docs/PetMap.md @@ -0,0 +1,10 @@ +# petstore::PetMap + +A mock map of a pet and some properties + +## Properties +Name | Type | Description | Notes +------------ | ------------- | ------------- | ------------- +**pet** | **map(character)** | | [optional] + + diff --git a/samples/client/petstore/R-httr2-wrapper/tests/testthat/test_pet_map.R b/samples/client/petstore/R-httr2-wrapper/tests/testthat/test_pet_map.R new file mode 100644 index 000000000000..e5ee9ecf9df6 --- /dev/null +++ b/samples/client/petstore/R-httr2-wrapper/tests/testthat/test_pet_map.R @@ -0,0 +1,13 @@ +# Automatically generated by openapi-generator (https://openapi-generator.tech) +# Please update as you see appropriate + +context("Test PetMap") + +model_instance <- PetMap$new() + +test_that("pet", { + # tests for the property `pet` (map(character)) + + # uncomment below to test the property + #expect_equal(model.instance$`pet`, "EXPECTED_RESULT") +}) diff --git a/samples/client/petstore/R-httr2/.openapi-generator/FILES b/samples/client/petstore/R-httr2/.openapi-generator/FILES index e3ca4f60cbc2..07243c898d78 100644 --- a/samples/client/petstore/R-httr2/.openapi-generator/FILES +++ b/samples/client/petstore/R-httr2/.openapi-generator/FILES @@ -28,6 +28,7 @@ R/one_of_primitive_type_test.R R/order.R R/pet.R R/pet_api.R +R/pet_map.R R/pig.R R/special.R R/store_api.R @@ -58,6 +59,7 @@ docs/OneOfPrimitiveTypeTest.md docs/Order.md docs/Pet.md docs/PetApi.md +docs/PetMap.md docs/Pig.md docs/Special.md docs/StoreApi.md diff --git a/samples/client/petstore/R-httr2/NAMESPACE b/samples/client/petstore/R-httr2/NAMESPACE index e196ce103b39..29a7f89e22fd 100644 --- a/samples/client/petstore/R-httr2/NAMESPACE +++ b/samples/client/petstore/R-httr2/NAMESPACE @@ -32,6 +32,7 @@ export(NestedOneOf) export(OneOfPrimitiveTypeTest) export(Order) export(Pet) +export(PetMap) export(Pig) export(Special) export(Tag) diff --git a/samples/client/petstore/R-httr2/R/api_client.R b/samples/client/petstore/R-httr2/R/api_client.R index 5e51a57b302b..32aa262f5545 100644 --- a/samples/client/petstore/R-httr2/R/api_client.R +++ b/samples/client/petstore/R-httr2/R/api_client.R @@ -312,7 +312,11 @@ ApiClient <- R6::R6Class( api_response <- ApiResponse$new() api_response$status_code <- resp %>% resp_status() api_response$status_code_desc <- resp %>% resp_status_desc() - api_response$response <- resp %>% resp_body_raw() + if (length(resp$body) == 0) { + api_response$response <- NULL + } else { + api_response$response <- resp %>% resp_body_raw() + } api_response$headers <- resp %>% resp_headers() api_response diff --git a/samples/client/petstore/R-httr2/R/api_response.R b/samples/client/petstore/R-httr2/R/api_response.R index f76d20d3f769..b84d66f6ee0f 100644 --- a/samples/client/petstore/R-httr2/R/api_response.R +++ b/samples/client/petstore/R-httr2/R/api_response.R @@ -55,7 +55,7 @@ ApiResponse <- R6::R6Class( #' @param from_encoding The encoding of the raw response. #' @param to_encoding The target encoding of the return value. #' @export - response_as_text = function(from_encoding = NULL, to_encoding = "UTF-8") { + response_as_text = function(from_encoding = "", to_encoding = "UTF-8") { text_response <- iconv(readBin(self$response, character()), from = from_encoding, to = to_encoding) if (is.na(text_response)) { warning("The response is binary and will not be converted to text.") diff --git a/samples/client/petstore/R-httr2/R/pet_map.R b/samples/client/petstore/R-httr2/R/pet_map.R new file mode 100644 index 000000000000..5fe81b5dd05e --- /dev/null +++ b/samples/client/petstore/R-httr2/R/pet_map.R @@ -0,0 +1,162 @@ +#' Create a new PetMap +#' +#' @description +#' A mock map of a pet and some properties +#' +#' @docType class +#' @title PetMap +#' @description PetMap Class +#' @format An \code{R6Class} generator object +#' @field pet named list(character) [optional] +#' @importFrom R6 R6Class +#' @importFrom jsonlite fromJSON toJSON +#' @export +PetMap <- R6::R6Class( + "PetMap", + public = list( + `pet` = NULL, + #' Initialize a new PetMap class. + #' + #' @description + #' Initialize a new PetMap class. + #' + #' @param pet pet + #' @param ... Other optional arguments. + #' @export + initialize = function(`pet` = NULL, ...) { + if (!is.null(`pet`)) { + stopifnot(is.vector(`pet`), length(`pet`) != 0) + sapply(`pet`, function(x) stopifnot(is.character(x))) + self$`pet` <- `pet` + } + }, + #' To JSON string + #' + #' @description + #' To JSON String + #' + #' @return PetMap in JSON format + #' @export + toJSON = function() { + PetMapObject <- list() + if (!is.null(self$`pet`)) { + PetMapObject[["pet"]] <- + self$`pet` + } + PetMapObject + }, + #' Deserialize JSON string into an instance of PetMap + #' + #' @description + #' Deserialize JSON string into an instance of PetMap + #' + #' @param input_json the JSON input + #' @return the instance of PetMap + #' @export + fromJSON = function(input_json) { + this_object <- jsonlite::fromJSON(input_json) + if (!is.null(this_object$`pet`)) { + self$`pet` <- ApiClient$new()$deserializeObj(this_object$`pet`, "map(character)", loadNamespace("petstore")) + } + self + }, + #' To JSON string + #' + #' @description + #' To JSON String + #' + #' @return PetMap in JSON format + #' @export + toJSONString = function() { + jsoncontent <- c( + if (!is.null(self$`pet`)) { + sprintf( + '"pet": + %s + ', + jsonlite::toJSON(lapply(self$`pet`, function(x){ x }), auto_unbox = TRUE, digits = NA) + ) + } + ) + jsoncontent <- paste(jsoncontent, collapse = ",") + json_string <- as.character(jsonlite::minify(paste("{", jsoncontent, "}", sep = ""))) + }, + #' Deserialize JSON string into an instance of PetMap + #' + #' @description + #' Deserialize JSON string into an instance of PetMap + #' + #' @param input_json the JSON input + #' @return the instance of PetMap + #' @export + fromJSONString = function(input_json) { + this_object <- jsonlite::fromJSON(input_json) + self$`pet` <- ApiClient$new()$deserializeObj(this_object$`pet`, "map(character)", loadNamespace("petstore")) + self + }, + #' Validate JSON input with respect to PetMap + #' + #' @description + #' Validate JSON input with respect to PetMap and throw an exception if invalid + #' + #' @param input the JSON input + #' @export + validateJSON = function(input) { + input_json <- jsonlite::fromJSON(input) + }, + #' To string (JSON format) + #' + #' @description + #' To string (JSON format) + #' + #' @return String representation of PetMap + #' @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() { + 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() + invalid_fields + }, + #' Print the object + #' + #' @description + #' Print the object + #' + #' @export + print = function() { + print(jsonlite::prettify(self$toJSONString())) + invisible(self) + } + ), + # Lock the class to prevent modifications to the method or field + lock_class = TRUE +) +## Uncomment below to unlock the class to allow modifications of the method or field +# PetMap$unlock() +# +## Below is an example to define the print function +# PetMap$set("public", "print", function(...) { +# print(jsonlite::prettify(self$toJSONString())) +# invisible(self) +# }) +## Uncomment below to lock the class to prevent modifications to the method or field +# PetMap$lock() + diff --git a/samples/client/petstore/R-httr2/README.md b/samples/client/petstore/R-httr2/README.md index 47e4370616c9..d2107f853f6b 100644 --- a/samples/client/petstore/R-httr2/README.md +++ b/samples/client/petstore/R-httr2/README.md @@ -122,6 +122,7 @@ Class | Method | HTTP request | Description - [OneOfPrimitiveTypeTest](docs/OneOfPrimitiveTypeTest.md) - [Order](docs/Order.md) - [Pet](docs/Pet.md) + - [PetMap](docs/PetMap.md) - [Pig](docs/Pig.md) - [Special](docs/Special.md) - [Tag](docs/Tag.md) diff --git a/samples/client/petstore/R-httr2/docs/PetMap.md b/samples/client/petstore/R-httr2/docs/PetMap.md new file mode 100644 index 000000000000..ba53785e1167 --- /dev/null +++ b/samples/client/petstore/R-httr2/docs/PetMap.md @@ -0,0 +1,10 @@ +# petstore::PetMap + +A mock map of a pet and some properties + +## Properties +Name | Type | Description | Notes +------------ | ------------- | ------------- | ------------- +**pet** | **map(character)** | | [optional] + + diff --git a/samples/client/petstore/R-httr2/tests/testthat/test_pet_map.R b/samples/client/petstore/R-httr2/tests/testthat/test_pet_map.R new file mode 100644 index 000000000000..e5ee9ecf9df6 --- /dev/null +++ b/samples/client/petstore/R-httr2/tests/testthat/test_pet_map.R @@ -0,0 +1,13 @@ +# Automatically generated by openapi-generator (https://openapi-generator.tech) +# Please update as you see appropriate + +context("Test PetMap") + +model_instance <- PetMap$new() + +test_that("pet", { + # tests for the property `pet` (map(character)) + + # uncomment below to test the property + #expect_equal(model.instance$`pet`, "EXPECTED_RESULT") +}) diff --git a/samples/client/petstore/R/.openapi-generator/FILES b/samples/client/petstore/R/.openapi-generator/FILES index 9a7a9f21724e..43681d7ddb2c 100644 --- a/samples/client/petstore/R/.openapi-generator/FILES +++ b/samples/client/petstore/R/.openapi-generator/FILES @@ -28,6 +28,7 @@ R/one_of_primitive_type_test.R R/order.R R/pet.R R/pet_api.R +R/pet_map.R R/pig.R R/special.R R/store_api.R @@ -58,6 +59,7 @@ docs/OneOfPrimitiveTypeTest.md docs/Order.md docs/Pet.md docs/PetApi.md +docs/PetMap.md docs/Pig.md docs/Special.md docs/StoreApi.md diff --git a/samples/client/petstore/R/NAMESPACE b/samples/client/petstore/R/NAMESPACE index afcc404eca78..0b48a9205360 100644 --- a/samples/client/petstore/R/NAMESPACE +++ b/samples/client/petstore/R/NAMESPACE @@ -32,6 +32,7 @@ export(NestedOneOf) export(OneOfPrimitiveTypeTest) export(Order) export(Pet) +export(PetMap) export(Pig) export(Special) export(Tag) diff --git a/samples/client/petstore/R/R/api_response.R b/samples/client/petstore/R/R/api_response.R index f76d20d3f769..b84d66f6ee0f 100644 --- a/samples/client/petstore/R/R/api_response.R +++ b/samples/client/petstore/R/R/api_response.R @@ -55,7 +55,7 @@ ApiResponse <- R6::R6Class( #' @param from_encoding The encoding of the raw response. #' @param to_encoding The target encoding of the return value. #' @export - response_as_text = function(from_encoding = NULL, to_encoding = "UTF-8") { + response_as_text = function(from_encoding = "", to_encoding = "UTF-8") { text_response <- iconv(readBin(self$response, character()), from = from_encoding, to = to_encoding) if (is.na(text_response)) { warning("The response is binary and will not be converted to text.") diff --git a/samples/client/petstore/R/R/pet_map.R b/samples/client/petstore/R/R/pet_map.R new file mode 100644 index 000000000000..80d9eb3abf89 --- /dev/null +++ b/samples/client/petstore/R/R/pet_map.R @@ -0,0 +1,195 @@ +#' Create a new PetMap +#' +#' @description +#' A mock map of a pet and some properties +#' +#' @docType class +#' @title PetMap +#' @description PetMap Class +#' @format An \code{R6Class} generator object +#' @field pet named list(character) [optional] +#' @field _field_list a list of fields list(character) +#' @field additional_properties additional properties list(character) [optional] +#' @importFrom R6 R6Class +#' @importFrom jsonlite fromJSON toJSON +#' @export +PetMap <- R6::R6Class( + "PetMap", + public = list( + `pet` = NULL, + `_field_list` = c("pet"), + `additional_properties` = list(), + #' Initialize a new PetMap class. + #' + #' @description + #' Initialize a new PetMap class. + #' + #' @param pet pet + #' @param additional_properties additional properties (optional) + #' @param ... Other optional arguments. + #' @export + initialize = function(`pet` = NULL, additional_properties = NULL, ...) { + if (!is.null(`pet`)) { + stopifnot(is.vector(`pet`), length(`pet`) != 0) + sapply(`pet`, function(x) stopifnot(is.character(x))) + self$`pet` <- `pet` + } + if (!is.null(additional_properties)) { + for (key in names(additional_properties)) { + self$additional_properties[[key]] <- additional_properties[[key]] + } + } + }, + #' To JSON string + #' + #' @description + #' To JSON String + #' + #' @return PetMap in JSON format + #' @export + toJSON = function() { + PetMapObject <- list() + if (!is.null(self$`pet`)) { + PetMapObject[["pet"]] <- + self$`pet` + } + for (key in names(self$additional_properties)) { + PetMapObject[[key]] <- self$additional_properties[[key]] + } + + PetMapObject + }, + #' Deserialize JSON string into an instance of PetMap + #' + #' @description + #' Deserialize JSON string into an instance of PetMap + #' + #' @param input_json the JSON input + #' @return the instance of PetMap + #' @export + fromJSON = function(input_json) { + this_object <- jsonlite::fromJSON(input_json) + if (!is.null(this_object$`pet`)) { + self$`pet` <- ApiClient$new()$deserializeObj(this_object$`pet`, "map(character)", loadNamespace("petstore")) + } + # process additional properties/fields in the payload + for (key in names(this_object)) { + if (!(key %in% self$`_field_list`)) { # json key not in list of fields + self$additional_properties[[key]] <- this_object[[key]] + } + } + + self + }, + #' To JSON string + #' + #' @description + #' To JSON String + #' + #' @return PetMap in JSON format + #' @export + toJSONString = function() { + jsoncontent <- c( + if (!is.null(self$`pet`)) { + sprintf( + '"pet": + %s + ', + jsonlite::toJSON(lapply(self$`pet`, function(x){ x }), auto_unbox = TRUE, digits = NA) + ) + } + ) + jsoncontent <- paste(jsoncontent, collapse = ",") + json_string <- as.character(jsonlite::minify(paste("{", jsoncontent, "}", sep = ""))) + json_obj <- jsonlite::fromJSON(json_string) + for (key in names(self$additional_properties)) { + json_obj[[key]] <- self$additional_properties[[key]] + } + json_string <- as.character(jsonlite::minify(jsonlite::toJSON(json_obj, auto_unbox = TRUE, digits = NA))) + }, + #' Deserialize JSON string into an instance of PetMap + #' + #' @description + #' Deserialize JSON string into an instance of PetMap + #' + #' @param input_json the JSON input + #' @return the instance of PetMap + #' @export + fromJSONString = function(input_json) { + this_object <- jsonlite::fromJSON(input_json) + self$`pet` <- ApiClient$new()$deserializeObj(this_object$`pet`, "map(character)", loadNamespace("petstore")) + # process additional properties/fields in the payload + for (key in names(this_object)) { + if (!(key %in% self$`_field_list`)) { # json key not in list of fields + self$additional_properties[[key]] <- this_object[[key]] + } + } + + self + }, + #' Validate JSON input with respect to PetMap + #' + #' @description + #' Validate JSON input with respect to PetMap and throw an exception if invalid + #' + #' @param input the JSON input + #' @export + validateJSON = function(input) { + input_json <- jsonlite::fromJSON(input) + }, + #' To string (JSON format) + #' + #' @description + #' To string (JSON format) + #' + #' @return String representation of PetMap + #' @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() { + 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() + invalid_fields + }, + #' Print the object + #' + #' @description + #' Print the object + #' + #' @export + print = function() { + print(jsonlite::prettify(self$toJSONString())) + invisible(self) + } + ), + # Lock the class to prevent modifications to the method or field + lock_class = TRUE +) +## Uncomment below to unlock the class to allow modifications of the method or field +# PetMap$unlock() +# +## Below is an example to define the print function +# PetMap$set("public", "print", function(...) { +# print(jsonlite::prettify(self$toJSONString())) +# invisible(self) +# }) +## Uncomment below to lock the class to prevent modifications to the method or field +# PetMap$lock() + diff --git a/samples/client/petstore/R/README.md b/samples/client/petstore/R/README.md index 3ccba825c793..04327fa38766 100644 --- a/samples/client/petstore/R/README.md +++ b/samples/client/petstore/R/README.md @@ -122,6 +122,7 @@ Class | Method | HTTP request | Description - [OneOfPrimitiveTypeTest](docs/OneOfPrimitiveTypeTest.md) - [Order](docs/Order.md) - [Pet](docs/Pet.md) + - [PetMap](docs/PetMap.md) - [Pig](docs/Pig.md) - [Special](docs/Special.md) - [Tag](docs/Tag.md) diff --git a/samples/client/petstore/R/docs/PetMap.md b/samples/client/petstore/R/docs/PetMap.md new file mode 100644 index 000000000000..ba53785e1167 --- /dev/null +++ b/samples/client/petstore/R/docs/PetMap.md @@ -0,0 +1,10 @@ +# petstore::PetMap + +A mock map of a pet and some properties + +## Properties +Name | Type | Description | Notes +------------ | ------------- | ------------- | ------------- +**pet** | **map(character)** | | [optional] + + diff --git a/samples/client/petstore/R/tests/testthat/test_pet_map.R b/samples/client/petstore/R/tests/testthat/test_pet_map.R new file mode 100644 index 000000000000..e5ee9ecf9df6 --- /dev/null +++ b/samples/client/petstore/R/tests/testthat/test_pet_map.R @@ -0,0 +1,13 @@ +# Automatically generated by openapi-generator (https://openapi-generator.tech) +# Please update as you see appropriate + +context("Test PetMap") + +model_instance <- PetMap$new() + +test_that("pet", { + # tests for the property `pet` (map(character)) + + # uncomment below to test the property + #expect_equal(model.instance$`pet`, "EXPECTED_RESULT") +})