diff --git a/modules/openapi-generator/src/main/resources/r/modelAnyOf.mustache b/modules/openapi-generator/src/main/resources/r/modelAnyOf.mustache index 5b0e047fbaf..d27c5cedbb5 100644 --- a/modules/openapi-generator/src/main/resources/r/modelAnyOf.mustache +++ b/modules/openapi-generator/src/main/resources/r/modelAnyOf.mustache @@ -71,13 +71,27 @@ #' #' @return JSON string reprenation of the {{{classname}}}. #' @export - toJSON = function() { + toJSONString = function() { if (!is.null(self$actual_instance)) { self$actual_instance$toJSONString() } else { NULL } }, + #' Serialize {{{classname}}} to JSON. + #' + #' @description + #' Serialize {{{classname}}} to JSON. + #' + #' @return JSON reprenation of the {{{classname}}}. + #' @export + toJSON = function() { + if (!is.null(self$actual_instance)) { + self$actual_instance$toJSON() + } else { + NULL + } + }, #' Validate the input JSON with respect to {{{classname}}}. #' #' @description diff --git a/modules/openapi-generator/src/main/resources/r/modelGeneric.mustache b/modules/openapi-generator/src/main/resources/r/modelGeneric.mustache index 539025096d9..d55150d0f73 100644 --- a/modules/openapi-generator/src/main/resources/r/modelGeneric.mustache +++ b/modules/openapi-generator/src/main/resources/r/modelGeneric.mustache @@ -266,7 +266,7 @@ {{/vars}} ) jsoncontent <- paste(jsoncontent, collapse = ",") - jsonlite::minify(paste('{', jsoncontent, '}', sep = "")) + as.character(jsonlite::minify(paste('{', jsoncontent, '}', sep = ""))) }, #' Deserialize JSON string into an instance of {{{classname}}} #' diff --git a/modules/openapi-generator/src/main/resources/r/modelOneOf.mustache b/modules/openapi-generator/src/main/resources/r/modelOneOf.mustache index 2e67fd5e68b..d53f32f59fa 100644 --- a/modules/openapi-generator/src/main/resources/r/modelOneOf.mustache +++ b/modules/openapi-generator/src/main/resources/r/modelOneOf.mustache @@ -86,13 +86,27 @@ #' #' @return JSON string reprenation of the {{{classname}}}. #' @export - toJSON = function() { + toJSONString = function() { if (!is.null(self$actual_instance)) { self$actual_instance$toJSONString() } else { NULL } }, + #' Serialize {{{classname}}} to JSON. + #' + #' @description + #' Serialize {{{classname}}} to JSON. + #' + #' @return JSON reprenation of the {{{classname}}}. + #' @export + toJSON = function() { + if (!is.null(self$actual_instance)) { + self$actual_instance$toJSON() + } else { + NULL + } + }, #' Validate the input JSON with respect to {{{classname}}}. #' #' @description 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 22a80bb3148..b6c2731285d 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 @@ -863,3 +863,10 @@ components: required: - className - size + NestedOneOf: + type: object + properties: + size: + type: integer + nested_pig: + $ref: '#/components/schemas/Pig' diff --git a/samples/client/petstore/R/.openapi-generator/FILES b/samples/client/petstore/R/.openapi-generator/FILES index 61126f669d8..3046316ea30 100644 --- a/samples/client/petstore/R/.openapi-generator/FILES +++ b/samples/client/petstore/R/.openapi-generator/FILES @@ -18,6 +18,7 @@ R/dog.R R/dog_all_of.R R/fake_api.R R/model_api_response.R +R/nested_one_of.R R/order.R R/pet.R R/pet_api.R @@ -41,6 +42,7 @@ docs/Dog.md docs/DogAllOf.md docs/FakeApi.md docs/ModelApiResponse.md +docs/NestedOneOf.md docs/Order.md docs/Pet.md docs/PetApi.md diff --git a/samples/client/petstore/R/NAMESPACE b/samples/client/petstore/R/NAMESPACE index 2f72275d495..d8fc867efa7 100644 --- a/samples/client/petstore/R/NAMESPACE +++ b/samples/client/petstore/R/NAMESPACE @@ -23,6 +23,7 @@ export(DanishPig) export(Dog) export(DogAllOf) export(ModelApiResponse) +export(NestedOneOf) export(Order) export(Pet) export(Pig) diff --git a/samples/client/petstore/R/R/allof_tag_api_response.R b/samples/client/petstore/R/R/allof_tag_api_response.R index 49d7c3872cb..1b7a67a078e 100644 --- a/samples/client/petstore/R/R/allof_tag_api_response.R +++ b/samples/client/petstore/R/R/allof_tag_api_response.R @@ -167,7 +167,7 @@ AllofTagApiResponse <- R6::R6Class( )} ) jsoncontent <- paste(jsoncontent, collapse = ",") - jsonlite::minify(paste('{', jsoncontent, '}', sep = "")) + as.character(jsonlite::minify(paste('{', jsoncontent, '}', sep = ""))) }, #' Deserialize JSON string into an instance of AllofTagApiResponse #' diff --git a/samples/client/petstore/R/R/animal.R b/samples/client/petstore/R/R/animal.R index 860f5284901..043e6f50431 100644 --- a/samples/client/petstore/R/R/animal.R +++ b/samples/client/petstore/R/R/animal.R @@ -104,7 +104,7 @@ Animal <- R6::R6Class( )} ) jsoncontent <- paste(jsoncontent, collapse = ",") - jsonlite::minify(paste('{', jsoncontent, '}', sep = "")) + as.character(jsonlite::minify(paste('{', jsoncontent, '}', sep = ""))) }, #' Deserialize JSON string into an instance of Animal #' diff --git a/samples/client/petstore/R/R/any_of_pig.R b/samples/client/petstore/R/R/any_of_pig.R index 23c24102381..73b8e89c6f9 100644 --- a/samples/client/petstore/R/R/any_of_pig.R +++ b/samples/client/petstore/R/R/any_of_pig.R @@ -94,13 +94,27 @@ AnyOfPig <- R6::R6Class( #' #' @return JSON string reprenation of the AnyOfPig. #' @export - toJSON = function() { + toJSONString = function() { if (!is.null(self$actual_instance)) { self$actual_instance$toJSONString() } else { NULL } }, + #' Serialize AnyOfPig to JSON. + #' + #' @description + #' Serialize AnyOfPig to JSON. + #' + #' @return JSON reprenation of the AnyOfPig. + #' @export + toJSON = function() { + if (!is.null(self$actual_instance)) { + self$actual_instance$toJSON() + } else { + NULL + } + }, #' Validate the input JSON with respect to AnyOfPig. #' #' @description diff --git a/samples/client/petstore/R/R/basque_pig.R b/samples/client/petstore/R/R/basque_pig.R index 04d568cd9c0..ad3a3d8b84d 100644 --- a/samples/client/petstore/R/R/basque_pig.R +++ b/samples/client/petstore/R/R/basque_pig.R @@ -104,7 +104,7 @@ BasquePig <- R6::R6Class( )} ) jsoncontent <- paste(jsoncontent, collapse = ",") - jsonlite::minify(paste('{', jsoncontent, '}', sep = "")) + as.character(jsonlite::minify(paste('{', jsoncontent, '}', sep = ""))) }, #' Deserialize JSON string into an instance of BasquePig #' diff --git a/samples/client/petstore/R/R/cat.R b/samples/client/petstore/R/R/cat.R index 08d4ff1b9ab..9e70f50a0c1 100644 --- a/samples/client/petstore/R/R/cat.R +++ b/samples/client/petstore/R/R/cat.R @@ -126,7 +126,7 @@ Cat <- R6::R6Class( )} ) jsoncontent <- paste(jsoncontent, collapse = ",") - jsonlite::minify(paste('{', jsoncontent, '}', sep = "")) + as.character(jsonlite::minify(paste('{', jsoncontent, '}', sep = ""))) }, #' Deserialize JSON string into an instance of Cat #' diff --git a/samples/client/petstore/R/R/cat_all_of.R b/samples/client/petstore/R/R/cat_all_of.R index 1dabfdff72b..cbdad46b9dd 100644 --- a/samples/client/petstore/R/R/cat_all_of.R +++ b/samples/client/petstore/R/R/cat_all_of.R @@ -83,7 +83,7 @@ CatAllOf <- R6::R6Class( )} ) jsoncontent <- paste(jsoncontent, collapse = ",") - jsonlite::minify(paste('{', jsoncontent, '}', sep = "")) + as.character(jsonlite::minify(paste('{', jsoncontent, '}', sep = ""))) }, #' Deserialize JSON string into an instance of CatAllOf #' diff --git a/samples/client/petstore/R/R/category.R b/samples/client/petstore/R/R/category.R index f0b33419e4a..986a5065142 100644 --- a/samples/client/petstore/R/R/category.R +++ b/samples/client/petstore/R/R/category.R @@ -104,7 +104,7 @@ Category <- R6::R6Class( )} ) jsoncontent <- paste(jsoncontent, collapse = ",") - jsonlite::minify(paste('{', jsoncontent, '}', sep = "")) + as.character(jsonlite::minify(paste('{', jsoncontent, '}', sep = ""))) }, #' Deserialize JSON string into an instance of Category #' diff --git a/samples/client/petstore/R/R/danish_pig.R b/samples/client/petstore/R/R/danish_pig.R index 716479eb2a3..00caeda1ef9 100644 --- a/samples/client/petstore/R/R/danish_pig.R +++ b/samples/client/petstore/R/R/danish_pig.R @@ -104,7 +104,7 @@ DanishPig <- R6::R6Class( )} ) jsoncontent <- paste(jsoncontent, collapse = ",") - jsonlite::minify(paste('{', jsoncontent, '}', sep = "")) + as.character(jsonlite::minify(paste('{', jsoncontent, '}', sep = ""))) }, #' Deserialize JSON string into an instance of DanishPig #' diff --git a/samples/client/petstore/R/R/dog.R b/samples/client/petstore/R/R/dog.R index 5ce00cd5593..596e3e74a14 100644 --- a/samples/client/petstore/R/R/dog.R +++ b/samples/client/petstore/R/R/dog.R @@ -126,7 +126,7 @@ Dog <- R6::R6Class( )} ) jsoncontent <- paste(jsoncontent, collapse = ",") - jsonlite::minify(paste('{', jsoncontent, '}', sep = "")) + as.character(jsonlite::minify(paste('{', jsoncontent, '}', sep = ""))) }, #' Deserialize JSON string into an instance of Dog #' diff --git a/samples/client/petstore/R/R/dog_all_of.R b/samples/client/petstore/R/R/dog_all_of.R index e34354cde44..b7e1b355f49 100644 --- a/samples/client/petstore/R/R/dog_all_of.R +++ b/samples/client/petstore/R/R/dog_all_of.R @@ -83,7 +83,7 @@ DogAllOf <- R6::R6Class( )} ) jsoncontent <- paste(jsoncontent, collapse = ",") - jsonlite::minify(paste('{', jsoncontent, '}', sep = "")) + as.character(jsonlite::minify(paste('{', jsoncontent, '}', sep = ""))) }, #' Deserialize JSON string into an instance of DogAllOf #' diff --git a/samples/client/petstore/R/R/model_api_response.R b/samples/client/petstore/R/R/model_api_response.R index 88ec1289afe..28477200f62 100644 --- a/samples/client/petstore/R/R/model_api_response.R +++ b/samples/client/petstore/R/R/model_api_response.R @@ -125,7 +125,7 @@ ModelApiResponse <- R6::R6Class( )} ) jsoncontent <- paste(jsoncontent, collapse = ",") - jsonlite::minify(paste('{', jsoncontent, '}', sep = "")) + as.character(jsonlite::minify(paste('{', jsoncontent, '}', sep = ""))) }, #' Deserialize JSON string into an instance of ModelApiResponse #' diff --git a/samples/client/petstore/R/R/nested_one_of.R b/samples/client/petstore/R/R/nested_one_of.R new file mode 100644 index 00000000000..f40db2629ac --- /dev/null +++ b/samples/client/petstore/R/R/nested_one_of.R @@ -0,0 +1,147 @@ +#' OpenAPI Petstore +#' +#' This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. +#' +#' The version of the OpenAPI document: 1.0.0 +#' Generated by: https://openapi-generator.tech +#' + +#' @docType class +#' @title NestedOneOf +#' @description NestedOneOf Class +#' @format An \code{R6Class} generator object +#' @field size integer [optional] +#' @field nested_pig \link{Pig} [optional] +#' @importFrom R6 R6Class +#' @importFrom jsonlite fromJSON toJSON +#' @export +NestedOneOf <- R6::R6Class( + 'NestedOneOf', + public = list( + `size` = NULL, + `nested_pig` = NULL, + #' Initialize a new NestedOneOf class. + #' + #' @description + #' Initialize a new NestedOneOf class. + #' + #' @param size size + #' @param nested_pig nested_pig + #' @param ... Other optional arguments. + #' @export + initialize = function( + `size`=NULL, `nested_pig`=NULL, ... + ) { + if (!is.null(`size`)) { + stopifnot(is.numeric(`size`), length(`size`) == 1) + self$`size` <- `size` + } + if (!is.null(`nested_pig`)) { + stopifnot(R6::is.R6(`nested_pig`)) + self$`nested_pig` <- `nested_pig` + } + }, + #' To JSON string + #' + #' @description + #' To JSON String + #' + #' @return NestedOneOf in JSON format + #' @export + toJSON = function() { + NestedOneOfObject <- list() + if (!is.null(self$`size`)) { + NestedOneOfObject[['size']] <- + self$`size` + } + if (!is.null(self$`nested_pig`)) { + NestedOneOfObject[['nested_pig']] <- + self$`nested_pig`$toJSON() + } + + NestedOneOfObject + }, + #' Deserialize JSON string into an instance of NestedOneOf + #' + #' @description + #' Deserialize JSON string into an instance of NestedOneOf + #' + #' @param input_json the JSON input + #' @return the instance of NestedOneOf + #' @export + fromJSON = function(input_json) { + this_object <- jsonlite::fromJSON(input_json) + if (!is.null(this_object$`size`)) { + self$`size` <- this_object$`size` + } + if (!is.null(this_object$`nested_pig`)) { + nested_pig_object <- Pig$new() + nested_pig_object$fromJSON(jsonlite::toJSON(this_object$nested_pig, auto_unbox = TRUE, digits = NA)) + self$`nested_pig` <- nested_pig_object + } + self + }, + #' To JSON string + #' + #' @description + #' To JSON String + #' + #' @return NestedOneOf in JSON format + #' @export + toJSONString = function() { + jsoncontent <- c( + if (!is.null(self$`size`)) { + sprintf( + '"size": + %d + ', + self$`size` + )}, + if (!is.null(self$`nested_pig`)) { + sprintf( + '"nested_pig": + %s + ', + jsonlite::toJSON(self$`nested_pig`$toJSON(), auto_unbox=TRUE, digits = NA) + )} + ) + jsoncontent <- paste(jsoncontent, collapse = ",") + as.character(jsonlite::minify(paste('{', jsoncontent, '}', sep = ""))) + }, + #' Deserialize JSON string into an instance of NestedOneOf + #' + #' @description + #' Deserialize JSON string into an instance of NestedOneOf + #' + #' @param input_json the JSON input + #' @return the instance of NestedOneOf + #' @export + fromJSONString = function(input_json) { + this_object <- jsonlite::fromJSON(input_json) + self$`size` <- this_object$`size` + self$`nested_pig` <- Pig$new()$fromJSON(jsonlite::toJSON(this_object$nested_pig, auto_unbox = TRUE, digits = NA)) + self + }, + #' Validate JSON input with respect to NestedOneOf + #' + #' @description + #' Validate JSON input with respect to NestedOneOf 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 NestedOneOf + #' @export + toString = function() { + self$toJSONString() + } + ) +) + diff --git a/samples/client/petstore/R/R/order.R b/samples/client/petstore/R/R/order.R index e676946ba2b..f7ce4aa1cd2 100644 --- a/samples/client/petstore/R/R/order.R +++ b/samples/client/petstore/R/R/order.R @@ -188,7 +188,7 @@ Order <- R6::R6Class( )} ) jsoncontent <- paste(jsoncontent, collapse = ",") - jsonlite::minify(paste('{', jsoncontent, '}', sep = "")) + as.character(jsonlite::minify(paste('{', jsoncontent, '}', sep = ""))) }, #' Deserialize JSON string into an instance of Order #' diff --git a/samples/client/petstore/R/R/pet.R b/samples/client/petstore/R/R/pet.R index fce32f92e52..f3345adc046 100644 --- a/samples/client/petstore/R/R/pet.R +++ b/samples/client/petstore/R/R/pet.R @@ -192,7 +192,7 @@ Pet <- R6::R6Class( )} ) jsoncontent <- paste(jsoncontent, collapse = ",") - jsonlite::minify(paste('{', jsoncontent, '}', sep = "")) + as.character(jsonlite::minify(paste('{', jsoncontent, '}', sep = ""))) }, #' Deserialize JSON string into an instance of Pet #' diff --git a/samples/client/petstore/R/R/pig.R b/samples/client/petstore/R/R/pig.R index 4887ec3d26c..565df078209 100644 --- a/samples/client/petstore/R/R/pig.R +++ b/samples/client/petstore/R/R/pig.R @@ -110,13 +110,27 @@ Pig <- R6::R6Class( #' #' @return JSON string reprenation of the Pig. #' @export - toJSON = function() { + toJSONString = function() { if (!is.null(self$actual_instance)) { self$actual_instance$toJSONString() } else { NULL } }, + #' Serialize Pig to JSON. + #' + #' @description + #' Serialize Pig to JSON. + #' + #' @return JSON reprenation of the Pig. + #' @export + toJSON = function() { + if (!is.null(self$actual_instance)) { + self$actual_instance$toJSON() + } else { + NULL + } + }, #' Validate the input JSON with respect to Pig. #' #' @description diff --git a/samples/client/petstore/R/R/special.R b/samples/client/petstore/R/R/special.R index 2f14118b02f..7d34c26a9af 100644 --- a/samples/client/petstore/R/R/special.R +++ b/samples/client/petstore/R/R/special.R @@ -146,7 +146,7 @@ Special <- R6::R6Class( )} ) jsoncontent <- paste(jsoncontent, collapse = ",") - jsonlite::minify(paste('{', jsoncontent, '}', sep = "")) + as.character(jsonlite::minify(paste('{', jsoncontent, '}', sep = ""))) }, #' Deserialize JSON string into an instance of Special #' diff --git a/samples/client/petstore/R/R/tag.R b/samples/client/petstore/R/R/tag.R index 6b0d17a2d0c..e58c9899a10 100644 --- a/samples/client/petstore/R/R/tag.R +++ b/samples/client/petstore/R/R/tag.R @@ -104,7 +104,7 @@ Tag <- R6::R6Class( )} ) jsoncontent <- paste(jsoncontent, collapse = ",") - jsonlite::minify(paste('{', jsoncontent, '}', sep = "")) + as.character(jsonlite::minify(paste('{', jsoncontent, '}', sep = ""))) }, #' Deserialize JSON string into an instance of Tag #' diff --git a/samples/client/petstore/R/R/update_pet_request.R b/samples/client/petstore/R/R/update_pet_request.R index 7a7f6026479..2756c970f54 100644 --- a/samples/client/petstore/R/R/update_pet_request.R +++ b/samples/client/petstore/R/R/update_pet_request.R @@ -105,7 +105,7 @@ UpdatePetRequest <- R6::R6Class( )} ) jsoncontent <- paste(jsoncontent, collapse = ",") - jsonlite::minify(paste('{', jsoncontent, '}', sep = "")) + as.character(jsonlite::minify(paste('{', jsoncontent, '}', sep = ""))) }, #' Deserialize JSON string into an instance of UpdatePetRequest #' diff --git a/samples/client/petstore/R/R/user.R b/samples/client/petstore/R/R/user.R index 2016a2dce29..3379f7cfa75 100644 --- a/samples/client/petstore/R/R/user.R +++ b/samples/client/petstore/R/R/user.R @@ -230,7 +230,7 @@ User <- R6::R6Class( )} ) jsoncontent <- paste(jsoncontent, collapse = ",") - jsonlite::minify(paste('{', jsoncontent, '}', sep = "")) + as.character(jsonlite::minify(paste('{', jsoncontent, '}', sep = ""))) }, #' Deserialize JSON string into an instance of User #' diff --git a/samples/client/petstore/R/README.md b/samples/client/petstore/R/README.md index eb829f1e420..79c2d3f042d 100644 --- a/samples/client/petstore/R/README.md +++ b/samples/client/petstore/R/README.md @@ -97,6 +97,7 @@ Class | Method | HTTP request | Description - [Dog](docs/Dog.md) - [DogAllOf](docs/DogAllOf.md) - [ModelApiResponse](docs/ModelApiResponse.md) + - [NestedOneOf](docs/NestedOneOf.md) - [Order](docs/Order.md) - [Pet](docs/Pet.md) - [Pig](docs/Pig.md) diff --git a/samples/client/petstore/R/docs/NestedOneOf.md b/samples/client/petstore/R/docs/NestedOneOf.md new file mode 100644 index 00000000000..ecc2ed89144 --- /dev/null +++ b/samples/client/petstore/R/docs/NestedOneOf.md @@ -0,0 +1,10 @@ +# petstore::NestedOneOf + + +## Properties +Name | Type | Description | Notes +------------ | ------------- | ------------- | ------------- +**size** | **integer** | | [optional] +**nested_pig** | [**Pig**](Pig.md) | | [optional] + + diff --git a/samples/client/petstore/R/test_petstore.R b/samples/client/petstore/R/test_petstore.R index e75bf247140..ff95be9432d 100644 --- a/samples/client/petstore/R/test_petstore.R +++ b/samples/client/petstore/R/test_petstore.R @@ -67,8 +67,8 @@ toString(pet$toString()) ##Pet$my_static_method <- function(x) { x + 2} ##Pet$public_methods$my_static_method(1) # -# basque_pig_json <- -# '{"className2": "BasquePig", "color": "red"}' + basque_pig_json <- + '{"className2": "BasquePig", "color": "red"}' # # danish_pig_json <- # '{"className2": "DanishPig", "size": 7}' @@ -81,8 +81,8 @@ toString(pet$toString()) # ]' # # print("==========") -# pig <- Pig$new() -# basque_pig <- pig$fromJSON(basque_pig_json) + pig <- Pig$new() + basque_pig <- pig$fromJSON(basque_pig_json) # #print(basque_pig$actual_instance$color) # #expect_equal(basque_pig$actual_type, "BasquePig") # pig$fromJSON(danish_pig_json) @@ -93,3 +93,11 @@ toString(pet$toString()) # #dp <- d$validateJSON(danish_pig_json) # # + +# test nested oneOf +nested_oneof <- NestedOneOf$new() +nested_oneof$nested_pig <- pig +nested_oneof$size <- 15 + +cat(nested_oneof$toJSONString()) + diff --git a/samples/client/petstore/R/tests/testthat/test_nested_one_of.R b/samples/client/petstore/R/tests/testthat/test_nested_one_of.R new file mode 100644 index 00000000000..940a7fba2a6 --- /dev/null +++ b/samples/client/petstore/R/tests/testthat/test_nested_one_of.R @@ -0,0 +1,20 @@ +# Automatically generated by openapi-generator (https://openapi-generator.tech) +# Please update as you see appropriate + +context("Test NestedOneOf") + +model_instance <- NestedOneOf$new() + +test_that("size", { + # tests for the property `size` (integer) + + # uncomment below to test the property + #expect_equal(model.instance$`size`, "EXPECTED_RESULT") +}) + +test_that("nested_pig", { + # tests for the property `nested_pig` (Pig) + + # uncomment below to test the property + #expect_equal(model.instance$`nested_pig`, "EXPECTED_RESULT") +}) diff --git a/samples/client/petstore/R/tests/testthat/test_petstore.R b/samples/client/petstore/R/tests/testthat/test_petstore.R index 8c5e1e39441..424fc5c291a 100644 --- a/samples/client/petstore/R/tests/testthat/test_petstore.R +++ b/samples/client/petstore/R/tests/testthat/test_petstore.R @@ -16,7 +16,7 @@ result <- pet_api$AddPet(pet) test_that("AddPet", { expect_equal(pet_id, 123321) #expect_equal(result, NULL) - #expect_equal(pet$toJSONString(), '{"id":123321,"category":{"id":450,"name":"test_cat"},"name":"name_test","photoUrls":["photo_test","second test"],"tags":[{"id":123,"name":"tag_test"},{"id":456,"name":"unknown"}],"status":"available"}') + expect_equal(pet$toJSONString(), '{"id":123321,"category":{"id":450,"name":"test_cat"},"name":"name_test","photoUrls":["photo_test","second test"],"tags":[{"id":123,"name":"tag_test"},{"id":456,"name":"unknown"}],"status":"available"}') }) test_that("Test toJSON toJSONString fromJSON fromJSONString", { @@ -154,7 +154,7 @@ test_that("Tests validateJSON", { }) # test object with special item names: self, private, super -test_that("Tests oneOf", { +test_that("Tests special item names", { special_json <- '{"self": 123, "private": "red", "super": "something"}' @@ -168,6 +168,7 @@ test_that("Tests oneOf", { expect_true(grepl('"private"', special$toJSONString())) expect_true(grepl('"self"', special$toJSONString())) expect_true(grepl('"super"', special$toJSONString())) + expect_equal('{"self":123,"private":"red","super":"something"}', special$toJSONString()) # round trip test s1 <- Special$new()$fromJSONString(special_json) @@ -206,13 +207,13 @@ test_that("Tests oneOf", { expect_equal(pig$actual_instance$className, "DanishPig") # test toJSON - expect_equal(danish_pig$toJSON(), original_danish_pig$toJSONString()) + expect_equal(danish_pig$toJSONString(), original_danish_pig$toJSONString()) basque_pig <- pig$fromJSON(basque_pig_json) expect_equal(basque_pig$actual_type, "BasquePig") expect_equal(basque_pig$actual_instance$color, "red") expect_equal(basque_pig$actual_instance$className, "BasquePig") - expect_equal(basque_pig$toJSON(), original_basque_pig$toJSONString()) + 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\\.') @@ -226,9 +227,16 @@ test_that("Tests oneOf", { expect_equal(pig2$actual_type, "BasquePig") expect_equal(pig2$actual_instance$color, "red") expect_equal(pig2$actual_instance$className, "BasquePig") - expect_equal(pig2$toJSON(), original_basque_pig$toJSONString()) + expect_equal(pig2$toJSONString(), original_basque_pig$toJSONString()) expect_error(Pig$new(instance = basque_pig), 'Failed to initialize Pig with oneOf schemas BasquePig, DanishPig. Provided class name: Pig') + + # test nested oneOf + nested_oneof <- NestedOneOf$new() + nested_oneof$nested_pig <- pig + nested_oneof$size <- 15 + expect_equal(nested_oneof$toJSONString(), '{"size":15,"nested_pig":{"className":"BasquePig","color":"red"}}') + }) test_that("Tests anyOf", { @@ -260,13 +268,13 @@ test_that("Tests anyOf", { expect_equal(pig$actual_instance$className, "DanishPig") # test toJSON - expect_equal(danish_pig$toJSON(), original_danish_pig$toJSONString()) + expect_equal(danish_pig$toJSONString(), original_danish_pig$toJSONString()) basque_pig <- pig$fromJSON(basque_pig_json) expect_equal(basque_pig$actual_type, "BasquePig") expect_equal(basque_pig$actual_instance$color, "red") expect_equal(basque_pig$actual_instance$className, "BasquePig") - expect_equal(basque_pig$toJSON(), original_basque_pig$toJSONString()) + 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 AnyOfPig with anyOf schemas BasquePig, DanishPig. Details: The JSON input ` \\{\\} ` is invalid for BasquePig: the required field `className` is missing\\., The JSON input ` \\{\\} ` is invalid for DanishPig: the required field `className` is missing\\.')