fix nested oneof, fix toStringJSON (#12723)

This commit is contained in:
William Cheng 2022-06-28 23:43:56 +08:00 committed by GitHub
parent 05ae49698f
commit 3feb11acbb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
30 changed files with 292 additions and 32 deletions

View File

@ -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

View File

@ -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}}}
#'

View File

@ -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

View File

@ -863,3 +863,10 @@ components:
required:
- className
- size
NestedOneOf:
type: object
properties:
size:
type: integer
nested_pig:
$ref: '#/components/schemas/Pig'

View File

@ -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

View File

@ -23,6 +23,7 @@ export(DanishPig)
export(Dog)
export(DogAllOf)
export(ModelApiResponse)
export(NestedOneOf)
export(Order)
export(Pet)
export(Pig)

View File

@ -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
#'

View File

@ -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
#'

View File

@ -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

View File

@ -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
#'

View File

@ -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
#'

View File

@ -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
#'

View File

@ -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
#'

View File

@ -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
#'

View File

@ -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
#'

View File

@ -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
#'

View File

@ -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
#'

View File

@ -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()
}
)
)

View File

@ -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
#'

View File

@ -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
#'

View File

@ -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

View File

@ -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
#'

View File

@ -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
#'

View File

@ -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
#'

View File

@ -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
#'

View File

@ -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)

View File

@ -0,0 +1,10 @@
# petstore::NestedOneOf
## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**size** | **integer** | | [optional]
**nested_pig** | [**Pig**](Pig.md) | | [optional]

View File

@ -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())

View File

@ -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")
})

View File

@ -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\\.')