[R] add oneOf support (#12491)

* add oneof support

* add validateJson method

* add validteJSON and tests

* add tests, add oneOf logic

* better error message in validateJSON

* fix tests

* add test for error message

* remove unused code
This commit is contained in:
William Cheng 2022-06-05 18:03:34 +08:00 committed by GitHub
parent f52ee28215
commit 6cd246931e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
33 changed files with 1021 additions and 294 deletions

View File

@ -36,7 +36,7 @@ git clone https://{{gitHost}}/{{{gitUserId}}}/{{{gitRepoId}}}
cd {{{gitRepoId}}}
R CMD build .
R CMD check {{{packageName}}}_{{{packageVersion}}}.tar.gz --no-manual
R CMD INSTALL {{{packageName}}}_{{{packageVersion}}}.tar.gz
R CMD INSTALL --preclean {{{packageName}}}_{{{packageVersion}}}.tar.gz
```
### Install the package

View File

@ -1,264 +1,6 @@
{{>partial_header}}
{{#models}}
{{#model}}
{{>partial_header}}
#' @docType class
#' @title {{classname}}
#'
#' @description {{classname}} Class
#'
#' @format An \code{R6Class} generator object
#'
{{#vars}}
#' @field {{baseName}} {{title}} {{#isContainer}}{{#isArray}}list( {{/isArray}}{{#isMap}}named list( {{/isMap}}{{/isContainer}}{{^isPrimitiveType}}\link{{=<% %>=}}{<%/isPrimitiveType%><%={{ }}=%>{{#isContainer}}{{#items}}{{dataType}}{{/items}}{{/isContainer}}{{^isContainer}}{{dataType}}{{/isContainer}}{{=<% %>=}}<%^isPrimitiveType%>}<%={{ }}=%>{{/isPrimitiveType}}{{#isContainer}}{{#isArray}} ){{/isArray}}{{#isMap}} ){{/isMap}}{{/isContainer}} {{^required}}[optional]{{/required}}
#'
{{/vars}}
#' @importFrom R6 R6Class
#' @importFrom jsonlite fromJSON toJSON
{{#isEnum}}
{{>modelEnum}}
{{/isEnum}}
{{^isEnum}}
#' @export
{{classname}} <- R6::R6Class(
'{{classname}}',
{{#parent}}
inherit = {{{.}}},
{{/parent}}
public = list(
{{#vars}}
`{{{baseName}}}` = NULL,
{{/vars}}
initialize = function(
{{#requiredVars}}`{{baseName}}`, {{/requiredVars}}{{#optionalVars}}`{{baseName}}`={{{defaultValue}}}{{^defaultValue}}NULL{{/defaultValue}}, {{/optionalVars}}...
) {
local.optional.var <- list(...)
{{#requiredVars}}
if (!missing(`{{baseName}}`)) {
{{^isContainer}}
{{#isInteger}}
stopifnot(is.numeric(`{{baseName}}`), length(`{{baseName}}`) == 1)
{{/isInteger}}
{{#isLong}}
stopifnot(is.numeric(`{{baseName}}`), length(`{{baseName}}`) == 1)
{{/isLong}}
{{#isFloat}}
stopifnot(is.numeric(`{{baseName}}`), length(`{{baseName}}`) == 1)
{{/isFloat}}
{{#isDouble}}
stopifnot(is.numeric(`{{baseName}}`), length(`{{baseName}}`) == 1)
{{/isDouble}}
{{#isString}}
stopifnot(is.character(`{{baseName}}`), length(`{{baseName}}`) == 1)
{{/isString}}
{{#isBoolean}}
stopifnot(is.logical(`{{baseName}}`), length(`{{baseName}}`) == 1)
{{/isBoolean}}
{{#isDate}}
stopifnot(is.character(`{{baseName}}`), length(`{{baseName}}`) == 1)
{{/isDate}}
{{#isDateTime}}
stopifnot(is.character(`{{baseName}}`), length(`{{baseName}}`) == 1)
{{/isDateTime}}
{{^isPrimitiveType}}
stopifnot(R6::is.R6(`{{baseName}}`))
{{/isPrimitiveType}}
{{/isContainer}}
{{#isContainer}}
{{#isPrimitiveType}}
stopifnot(is.vector(`{{baseName}}`), length(`{{baseName}}`) != 0)
sapply(`{{baseName}}`, function(x) stopifnot(is.character(x)))
{{/isPrimitiveType}}
{{^isPrimitiveType}}
stopifnot(is.vector(`{{baseName}}`), length(`{{baseName}}`) != 0)
sapply(`{{baseName}}`, function(x) stopifnot(R6::is.R6(x)))
{{/isPrimitiveType}}
{{/isContainer}}
self$`{{baseName}}` <- `{{baseName}}`
}
{{/requiredVars}}
{{#optionalVars}}
if (!is.null(`{{baseName}}`)) {
{{^isContainer}}
{{#isInteger}}
stopifnot(is.numeric(`{{baseName}}`), length(`{{baseName}}`) == 1)
{{/isInteger}}
{{#isLong}}
stopifnot(is.numeric(`{{baseName}}`), length(`{{baseName}}`) == 1)
{{/isLong}}
{{#isFloat}}
stopifnot(is.numeric(`{{baseName}}`), length(`{{baseName}}`) == 1)
{{/isFloat}}
{{#isDouble}}
stopifnot(is.numeric(`{{baseName}}`), length(`{{baseName}}`) == 1)
{{/isDouble}}
{{#isString}}
stopifnot(is.character(`{{baseName}}`), length(`{{baseName}}`) == 1)
{{/isString}}
{{#isBoolean}}
stopifnot(is.logical(`{{baseName}}`), length(`{{baseName}}`) == 1)
{{/isBoolean}}
{{#isDate}}
stopifnot(is.character(`{{baseName}}`), length(`{{baseName}}`) == 1)
{{/isDate}}
{{#isDateTime}}
stopifnot(is.character(`{{baseName}}`), length(`{{baseName}}`) == 1)
{{/isDateTime}}
{{^isPrimitiveType}}
stopifnot(R6::is.R6(`{{baseName}}`))
{{/isPrimitiveType}}
{{/isContainer}}
{{#isContainer}}
{{#isPrimitiveType}}
stopifnot(is.vector(`{{baseName}}`), length(`{{baseName}}`) != 0)
sapply(`{{baseName}}`, function(x) stopifnot(is.character(x)))
{{/isPrimitiveType}}
{{^isPrimitiveType}}
stopifnot(is.vector(`{{baseName}}`), length(`{{baseName}}`) != 0)
sapply(`{{baseName}}`, function(x) stopifnot(R6::is.R6(x)))
{{/isPrimitiveType}}
{{/isContainer}}
self$`{{baseName}}` <- `{{baseName}}`
}
{{/optionalVars}}
},
toJSON = function() {
{{classname}}Object <- list()
{{#vars}}
if (!is.null(self$`{{baseName}}`)) {
{{classname}}Object[['{{baseName}}']] <-
{{#isContainer}}
{{#isArray}}
{{#isPrimitiveType}}
self$`{{baseName}}`
{{/isPrimitiveType}}
{{^isPrimitiveType}}
lapply(self$`{{baseName}}`, function(x) x$toJSON())
{{/isPrimitiveType}}
{{/isArray}}
{{#isMap}}
{{#isPrimitiveType}}
self$`{{baseName}}`
{{/isPrimitiveType}}
{{^isPrimitiveType}}
lapply(self$`{{baseName}}`, function(x) x$toJSON())
{{/isPrimitiveType}}
{{/isMap}}
{{/isContainer}}
{{^isContainer}}
{{#isPrimitiveType}}
self$`{{baseName}}`
{{/isPrimitiveType}}
{{^isPrimitiveType}}
self$`{{baseName}}`$toJSON()
{{/isPrimitiveType}}
{{/isContainer}}
}
{{/vars}}
{{classname}}Object
},
fromJSON = function({{classname}}Json) {
{{classname}}Object <- jsonlite::fromJSON({{classname}}Json)
{{#vars}}
if (!is.null({{classname}}Object$`{{baseName}}`)) {
{{#isContainer}}
self$`{{baseName}}` <- ApiClient$new()$deserializeObj({{classname}}Object$`{{baseName}}`, "{{dataType}}", loadNamespace("{{packageName}}"))
{{/isContainer}}
{{^isContainer}}
{{#isPrimitiveType}}
self$`{{baseName}}` <- {{classname}}Object$`{{baseName}}`
{{/isPrimitiveType}}
{{^isPrimitiveType}}
{{baseName}}Object <- {{dataType}}$new()
{{baseName}}Object$fromJSON(jsonlite::toJSON({{classname}}Object${{baseName}}, auto_unbox = TRUE, digits = NA))
self$`{{baseName}}` <- {{baseName}}Object
{{/isPrimitiveType}}
{{/isContainer}}
}
{{/vars}}
self
},
toJSONString = function() {
jsoncontent <- c(
{{#vars}}
if (!is.null(self$`{{baseName}}`)) {
sprintf(
'"{{baseName}}":
{{#isContainer}}
{{#isArray}}
{{#isPrimitiveType}}
{{#isNumeric}}[%d]{{/isNumeric}}{{^isNumeric}}[%s]{{/isNumeric}}
{{/isPrimitiveType}}
{{^isPrimitiveType}}[%s]
{{/isPrimitiveType}}
{{/isArray}}
{{#isMap}}
{{#isPrimitiveType}}
{{#isNumeric}}%d{{/isNumeric}}{{^isNumeric}}{{^isBoolean}}"{{/isBoolean}}%s{{^isBoolean}}"{{/isBoolean}}{{/isNumeric}}
{{/isPrimitiveType}}
{{^isPrimitiveType}}%s
{{/isPrimitiveType}}
{{/isMap}}
{{/isContainer}}
{{^isContainer}}
{{#isPrimitiveType}}
{{#isNumeric}}%d{{/isNumeric}}{{^isNumeric}}{{^isBoolean}}"{{/isBoolean}}%s{{^isBoolean}}"{{/isBoolean}}{{/isNumeric}}
{{/isPrimitiveType}}
{{^isPrimitiveType}}%s
{{/isPrimitiveType}}
{{/isContainer}}',
{{#isContainer}}
{{#isArray}}
{{#isPrimitiveType}}
paste(unlist(lapply(self$`{{{baseName}}}`, function(x) paste0('"', x, '"'))), collapse=",")
{{/isPrimitiveType}}
{{^isPrimitiveType}}
paste(sapply(self$`{{{baseName}}}`, function(x) jsonlite::toJSON(x$toJSON(), auto_unbox=TRUE, digits = NA)), collapse=",")
{{/isPrimitiveType}}
{{/isArray}}
{{#isMap}}
{{#isPrimitiveType}}
jsonlite::toJSON(lapply(self$`{{{baseName}}}`, function(x){ x }), auto_unbox = TRUE, digits=NA)
{{/isPrimitiveType}}
{{^isPrimitiveType}}
jsonlite::toJSON(lapply(self$`{{{baseName}}}`, function(x){ x$toJSON() }), auto_unbox = TRUE, digits=NA)
{{/isPrimitiveType}}
{{/isMap}}
{{/isContainer}}
{{^isContainer}}
{{#isPrimitiveType}}
{{#isBoolean}}tolower({{/isBoolean}}self$`{{baseName}}`{{#isBoolean}}){{/isBoolean}}
{{/isPrimitiveType}}
{{^isPrimitiveType}}
jsonlite::toJSON(self$`{{baseName}}`$toJSON(), auto_unbox=TRUE, digits = NA)
{{/isPrimitiveType}}
{{/isContainer}}
)}{{^-last}},{{/-last}}
{{/vars}}
)
jsoncontent <- paste(jsoncontent, collapse = ",")
paste('{', jsoncontent, '}', sep = "")
},
fromJSONString = function({{classname}}Json) {
{{classname}}Object <- jsonlite::fromJSON({{classname}}Json)
{{#vars}}
{{! AAPI - added condition for handling container type of parameters, map and array}}
{{#isContainer}}
self$`{{baseName}}` <- ApiClient$new()$deserializeObj({{classname}}Object$`{{baseName}}`, "{{dataType}}", loadNamespace("{{packageName}}"))
{{/isContainer}}
{{^isContainer}}
{{#isPrimitiveType}}
self$`{{baseName}}` <- {{classname}}Object$`{{baseName}}`
{{/isPrimitiveType}}
{{^isPrimitiveType}}
self$`{{baseName}}` <- {{dataType}}$new()$fromJSON(jsonlite::toJSON({{classname}}Object${{baseName}}, auto_unbox = TRUE, digits = NA))
{{/isPrimitiveType}}
{{/isContainer}}
{{/vars}}
self
}
)
)
{{/isEnum}}
{{#isEnum}}{{>modelEnum}}{{/isEnum}}{{^isEnum}}{{#oneOf}}{{#-first}}{{>modelOneOf}}{{/-first}}{{/oneOf}}{{^oneOf}}{{#anyOf}}{{#-first}}{{>modelAnyOf}}{{/-first}}{{/anyOf}}{{^anyOf}}{{>modelGeneric}}{{/anyOf}}{{/oneOf}}{{/isEnum}}
{{/model}}
{{/models}}

View File

@ -0,0 +1,312 @@
{{#models}}
{{#model}}
#' @docType class
#' @title {{classname}}
#'
#' @description {{classname}} Class
#'
#' @format An \code{R6Class} generator object
#'
{{#vars}}
#' @field {{baseName}} {{title}} {{#isContainer}}{{#isArray}}list( {{/isArray}}{{#isMap}}named list( {{/isMap}}{{/isContainer}}{{^isPrimitiveType}}\link{{=<% %>=}}{<%/isPrimitiveType%><%={{ }}=%>{{#isContainer}}{{#items}}{{dataType}}{{/items}}{{/isContainer}}{{^isContainer}}{{dataType}}{{/isContainer}}{{=<% %>=}}<%^isPrimitiveType%>}<%={{ }}=%>{{/isPrimitiveType}}{{#isContainer}}{{#isArray}} ){{/isArray}}{{#isMap}} ){{/isMap}}{{/isContainer}} {{^required}}[optional]{{/required}}
#'
{{/vars}}
#' @importFrom R6 R6Class
#' @importFrom jsonlite fromJSON toJSON
{{#isEnum}}
{{>modelEnum}}
{{/isEnum}}
{{^isEnum}}
#' @export
{{classname}} <- R6::R6Class(
'{{classname}}',
{{#parent}}
inherit = {{{.}}},
{{/parent}}
public = list(
{{#vars}}
`{{{baseName}}}` = NULL,
{{/vars}}
initialize = function(
{{#requiredVars}}`{{baseName}}`, {{/requiredVars}}{{#optionalVars}}`{{baseName}}`={{{defaultValue}}}{{^defaultValue}}NULL{{/defaultValue}}, {{/optionalVars}}...
) {
{{#requiredVars}}
if (!missing(`{{baseName}}`)) {
{{^isContainer}}
{{#isInteger}}
stopifnot(is.numeric(`{{baseName}}`), length(`{{baseName}}`) == 1)
{{/isInteger}}
{{#isLong}}
stopifnot(is.numeric(`{{baseName}}`), length(`{{baseName}}`) == 1)
{{/isLong}}
{{#isFloat}}
stopifnot(is.numeric(`{{baseName}}`), length(`{{baseName}}`) == 1)
{{/isFloat}}
{{#isDouble}}
stopifnot(is.numeric(`{{baseName}}`), length(`{{baseName}}`) == 1)
{{/isDouble}}
{{#isString}}
stopifnot(is.character(`{{baseName}}`), length(`{{baseName}}`) == 1)
{{/isString}}
{{#isBoolean}}
stopifnot(is.logical(`{{baseName}}`), length(`{{baseName}}`) == 1)
{{/isBoolean}}
{{#isDate}}
stopifnot(is.character(`{{baseName}}`), length(`{{baseName}}`) == 1)
{{/isDate}}
{{#isDateTime}}
stopifnot(is.character(`{{baseName}}`), length(`{{baseName}}`) == 1)
{{/isDateTime}}
{{^isPrimitiveType}}
stopifnot(R6::is.R6(`{{baseName}}`))
{{/isPrimitiveType}}
{{/isContainer}}
{{#isContainer}}
{{#isPrimitiveType}}
stopifnot(is.vector(`{{baseName}}`), length(`{{baseName}}`) != 0)
sapply(`{{baseName}}`, function(x) stopifnot(is.character(x)))
{{/isPrimitiveType}}
{{^isPrimitiveType}}
stopifnot(is.vector(`{{baseName}}`), length(`{{baseName}}`) != 0)
sapply(`{{baseName}}`, function(x) stopifnot(R6::is.R6(x)))
{{/isPrimitiveType}}
{{/isContainer}}
self$`{{baseName}}` <- `{{baseName}}`
}
{{/requiredVars}}
{{#optionalVars}}
if (!is.null(`{{baseName}}`)) {
{{^isContainer}}
{{#isInteger}}
stopifnot(is.numeric(`{{baseName}}`), length(`{{baseName}}`) == 1)
{{/isInteger}}
{{#isLong}}
stopifnot(is.numeric(`{{baseName}}`), length(`{{baseName}}`) == 1)
{{/isLong}}
{{#isFloat}}
stopifnot(is.numeric(`{{baseName}}`), length(`{{baseName}}`) == 1)
{{/isFloat}}
{{#isDouble}}
stopifnot(is.numeric(`{{baseName}}`), length(`{{baseName}}`) == 1)
{{/isDouble}}
{{#isString}}
stopifnot(is.character(`{{baseName}}`), length(`{{baseName}}`) == 1)
{{/isString}}
{{#isBoolean}}
stopifnot(is.logical(`{{baseName}}`), length(`{{baseName}}`) == 1)
{{/isBoolean}}
{{#isDate}}
stopifnot(is.character(`{{baseName}}`), length(`{{baseName}}`) == 1)
{{/isDate}}
{{#isDateTime}}
stopifnot(is.character(`{{baseName}}`), length(`{{baseName}}`) == 1)
{{/isDateTime}}
{{^isPrimitiveType}}
stopifnot(R6::is.R6(`{{baseName}}`))
{{/isPrimitiveType}}
{{/isContainer}}
{{#isContainer}}
{{#isPrimitiveType}}
stopifnot(is.vector(`{{baseName}}`), length(`{{baseName}}`) != 0)
sapply(`{{baseName}}`, function(x) stopifnot(is.character(x)))
{{/isPrimitiveType}}
{{^isPrimitiveType}}
stopifnot(is.vector(`{{baseName}}`), length(`{{baseName}}`) != 0)
sapply(`{{baseName}}`, function(x) stopifnot(R6::is.R6(x)))
{{/isPrimitiveType}}
{{/isContainer}}
self$`{{baseName}}` <- `{{baseName}}`
}
{{/optionalVars}}
},
toJSON = function() {
{{classname}}Object <- list()
{{#vars}}
if (!is.null(self$`{{baseName}}`)) {
{{classname}}Object[['{{baseName}}']] <-
{{#isContainer}}
{{#isArray}}
{{#isPrimitiveType}}
self$`{{baseName}}`
{{/isPrimitiveType}}
{{^isPrimitiveType}}
lapply(self$`{{baseName}}`, function(x) x$toJSON())
{{/isPrimitiveType}}
{{/isArray}}
{{#isMap}}
{{#isPrimitiveType}}
self$`{{baseName}}`
{{/isPrimitiveType}}
{{^isPrimitiveType}}
lapply(self$`{{baseName}}`, function(x) x$toJSON())
{{/isPrimitiveType}}
{{/isMap}}
{{/isContainer}}
{{^isContainer}}
{{#isPrimitiveType}}
self$`{{baseName}}`
{{/isPrimitiveType}}
{{^isPrimitiveType}}
self$`{{baseName}}`$toJSON()
{{/isPrimitiveType}}
{{/isContainer}}
}
{{/vars}}
{{classname}}Object
},
fromJSON = function({{classname}}Json) {
{{classname}}Object <- jsonlite::fromJSON({{classname}}Json)
{{#vars}}
if (!is.null({{classname}}Object$`{{baseName}}`)) {
{{#isContainer}}
self$`{{baseName}}` <- ApiClient$new()$deserializeObj({{classname}}Object$`{{baseName}}`, "{{dataType}}", loadNamespace("{{packageName}}"))
{{/isContainer}}
{{^isContainer}}
{{#isPrimitiveType}}
self$`{{baseName}}` <- {{classname}}Object$`{{baseName}}`
{{/isPrimitiveType}}
{{^isPrimitiveType}}
{{baseName}}Object <- {{dataType}}$new()
{{baseName}}Object$fromJSON(jsonlite::toJSON({{classname}}Object${{baseName}}, auto_unbox = TRUE, digits = NA))
self$`{{baseName}}` <- {{baseName}}Object
{{/isPrimitiveType}}
{{/isContainer}}
}
{{/vars}}
self
},
toJSONString = function() {
jsoncontent <- c(
{{#vars}}
if (!is.null(self$`{{baseName}}`)) {
sprintf(
'"{{baseName}}":
{{#isContainer}}
{{#isArray}}
{{#isPrimitiveType}}
{{#isNumeric}}[%d]{{/isNumeric}}{{^isNumeric}}[%s]{{/isNumeric}}
{{/isPrimitiveType}}
{{^isPrimitiveType}}[%s]
{{/isPrimitiveType}}
{{/isArray}}
{{#isMap}}
{{#isPrimitiveType}}
{{#isNumeric}}%d{{/isNumeric}}{{^isNumeric}}{{^isBoolean}}"{{/isBoolean}}%s{{^isBoolean}}"{{/isBoolean}}{{/isNumeric}}
{{/isPrimitiveType}}
{{^isPrimitiveType}}%s
{{/isPrimitiveType}}
{{/isMap}}
{{/isContainer}}
{{^isContainer}}
{{#isPrimitiveType}}
{{#isNumeric}}%d{{/isNumeric}}{{^isNumeric}}{{^isBoolean}}"{{/isBoolean}}%s{{^isBoolean}}"{{/isBoolean}}{{/isNumeric}}
{{/isPrimitiveType}}
{{^isPrimitiveType}}%s
{{/isPrimitiveType}}
{{/isContainer}}',
{{#isContainer}}
{{#isArray}}
{{#isPrimitiveType}}
paste(unlist(lapply(self$`{{{baseName}}}`, function(x) paste0('"', x, '"'))), collapse=",")
{{/isPrimitiveType}}
{{^isPrimitiveType}}
paste(sapply(self$`{{{baseName}}}`, function(x) jsonlite::toJSON(x$toJSON(), auto_unbox=TRUE, digits = NA)), collapse=",")
{{/isPrimitiveType}}
{{/isArray}}
{{#isMap}}
{{#isPrimitiveType}}
jsonlite::toJSON(lapply(self$`{{{baseName}}}`, function(x){ x }), auto_unbox = TRUE, digits=NA)
{{/isPrimitiveType}}
{{^isPrimitiveType}}
jsonlite::toJSON(lapply(self$`{{{baseName}}}`, function(x){ x$toJSON() }), auto_unbox = TRUE, digits=NA)
{{/isPrimitiveType}}
{{/isMap}}
{{/isContainer}}
{{^isContainer}}
{{#isPrimitiveType}}
{{#isBoolean}}tolower({{/isBoolean}}self$`{{baseName}}`{{#isBoolean}}){{/isBoolean}}
{{/isPrimitiveType}}
{{^isPrimitiveType}}
jsonlite::toJSON(self$`{{baseName}}`$toJSON(), auto_unbox=TRUE, digits = NA)
{{/isPrimitiveType}}
{{/isContainer}}
)}{{^-last}},{{/-last}}
{{/vars}}
)
jsoncontent <- paste(jsoncontent, collapse = ",")
paste('{', jsoncontent, '}', sep = "")
},
fromJSONString = function({{classname}}Json) {
{{classname}}Object <- jsonlite::fromJSON({{classname}}Json)
{{#vars}}
{{! AAPI - added condition for handling container type of parameters, map and array}}
{{#isContainer}}
self$`{{baseName}}` <- ApiClient$new()$deserializeObj({{classname}}Object$`{{baseName}}`, "{{dataType}}", loadNamespace("{{packageName}}"))
{{/isContainer}}
{{^isContainer}}
{{#isPrimitiveType}}
self$`{{baseName}}` <- {{classname}}Object$`{{baseName}}`
{{/isPrimitiveType}}
{{^isPrimitiveType}}
self$`{{baseName}}` <- {{dataType}}$new()$fromJSON(jsonlite::toJSON({{classname}}Object${{baseName}}, auto_unbox = TRUE, digits = NA))
{{/isPrimitiveType}}
{{/isContainer}}
{{/vars}}
self
},
validateJSON = function(input) {
input_json <- jsonlite::fromJSON(input)
{{#requiredVars}}
# check the required field `{{baseName}}`
if (!is.null(input_json$`{{baseName}}`)) {
{{^isContainer}}
{{#isInteger}}
stopifnot(is.numeric(input_json$`{{baseName}}`), length(input_json$`{{baseName}}`) == 1)
{{/isInteger}}
{{#isLong}}
stopifnot(is.numeric(input_json$`{{baseName}}`), length(input_json$`{{baseName}}`) == 1)
{{/isLong}}
{{#isFloat}}
stopifnot(is.numeric(input_json$`{{baseName}}`), length(input_json$`{{baseName}}`) == 1)
{{/isFloat}}
{{#isDouble}}
stopifnot(is.numeric(input_json$`{{baseName}}`), length(input_json$`{{baseName}}`) == 1)
{{/isDouble}}
{{#isString}}
stopifnot(is.character(input_json$`{{baseName}}`), length(input_json$`{{baseName}}`) == 1)
{{/isString}}
{{#isBoolean}}
stopifnot(is.logical(input_json$`{{baseName}}`), length(input_json$`{{baseName}}`) == 1)
{{/isBoolean}}
{{#isDate}}
stopifnot(is.character(input_json$`{{baseName}}`), length(input_json$`{{baseName}}`) == 1)
{{/isDate}}
{{#isDateTime}}
stopifnot(is.character(input_json$`{{baseName}}`), length(input_json$`{{baseName}}`) == 1)
{{/isDateTime}}
{{^isPrimitiveType}}
stopifnot(R6::is.R6(input_json$`{{baseName}}`))
{{/isPrimitiveType}}
{{/isContainer}}
{{#isContainer}}
{{#isPrimitiveType}}
stopifnot(is.vector(input_json$`{{baseName}}`), length(input_json$`{{baseName}}`) != 0)
tmp <- sapply(input_json$`{{baseName}}`, function(x) stopifnot(is.character(x)))
{{/isPrimitiveType}}
{{^isPrimitiveType}}
stopifnot(is.vector(input_json$`{{baseName}}`), length(json_input$`{{baseName}}`) != 0)
tmp <- sapply(input_json$`{{baseName}}`, function(x) stopifnot(R6::is.R6(x)))
{{/isPrimitiveType}}
{{/isContainer}}
} else {
stop(paste("The JSON input `", input, "` is invalid for {{classname}}: the required field `{{baseName}}` is missing."))
}
{{/requiredVars}}
}
)
)
{{/isEnum}}
{{/model}}
{{/models}}

View File

@ -0,0 +1,64 @@
#' @docType class
#' @title {{classname}}
#'
#' @description {{classname}} Class
#'
#' @format An \code{R6Class} generator object
#'
#' @importFrom R6 R6Class
#' @importFrom jsonlite fromJSON toJSON
#' @export
{{classname}} <- R6::R6Class(
'{{classname}}',
public = list(
actual_instance = NULL,
actual_type = NULL,
one_of = list({{#oneOf}}"{{{.}}}"{{^-last}}, {{/-last}}{{/oneOf}}),
initialize = function(
{{#requiredVars}}`{{baseName}}`, {{/requiredVars}}{{#optionalVars}}`{{baseName}}`={{{defaultValue}}}{{^defaultValue}}NULL{{/defaultValue}}, {{/optionalVars}}...
) {
local_optional_var <- list(...)
},
toJSON = function() {
{{classname}}_object <- list()
{{classname}}_object
},
fromJSON = function(input) {
matched <- 0 # match counter
matched_schemas <- list() #names of matched schemas
error_messages <- list()
instance <- NULL
{{#oneOf}}
{{{.}}}_result <- tryCatch({
{{{.}}}$public_methods$validateJSON(input)
{{{.}}}_instance <- {{{.}}}$new()
instance <- {{{.}}}_instance$fromJSON(input)
instance_type <- "{{{.}}}"
matched_schemas <- append(matched_schemas, "{{{.}}}")
matched <- matched + 1
},
error = function(err) err
)
if (!is.null({{{.}}}_result['error'])) {
error_messages <- append(error_messages, {{{.}}}_result['message'])
}
{{/oneOf}}
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 {{{classname}}} with oneOf schemas {{#oneOf}}{{{.}}}{{^-last}}, {{/-last}}{{/oneOf}}. ")
} else {
# no match
stop(paste("No match found when deserializing the payload into {{{classname}}} with oneOf schemas {{#oneOf}}{{{.}}}{{^-last}}, {{/-last}}{{/oneOf}}. Details: ", paste(error_messages, collapse = ', ')))
}
self
}
)
)

View File

@ -801,3 +801,29 @@ components:
allOf:
- $ref: '#/components/schemas/Tag'
- $ref: '#/components/schemas/ApiResponse'
Pig:
oneOf:
- $ref: '#/components/schemas/BasquePig'
- $ref: '#/components/schemas/DanishPig'
discriminator:
propertyName: className
BasquePig:
type: object
properties:
className:
type: string
color:
type: string
required:
- className
- color
DanishPig:
type: object
properties:
className:
type: string
size:
type: integer
required:
- className
- size

View File

@ -8,9 +8,11 @@ R/animal.R
R/api_client.R
R/api_exception.R
R/api_response.R
R/basque_pig.R
R/cat.R
R/cat_all_of.R
R/category.R
R/danish_pig.R
R/dog.R
R/dog_all_of.R
R/fake_api.R
@ -18,6 +20,7 @@ R/model_api_response.R
R/order.R
R/pet.R
R/pet_api.R
R/pig.R
R/store_api.R
R/tag.R
R/user.R
@ -25,9 +28,11 @@ R/user_api.R
README.md
docs/AllofTagApiResponse.md
docs/Animal.md
docs/BasquePig.md
docs/Cat.md
docs/CatAllOf.md
docs/Category.md
docs/DanishPig.md
docs/Dog.md
docs/DogAllOf.md
docs/FakeApi.md
@ -35,6 +40,7 @@ docs/ModelApiResponse.md
docs/Order.md
docs/Pet.md
docs/PetApi.md
docs/Pig.md
docs/StoreApi.md
docs/Tag.md
docs/User.md

View File

@ -14,14 +14,17 @@ export(ApiException)
# Models
export(AllofTagApiResponse)
export(Animal)
export(BasquePig)
export(Cat)
export(CatAllOf)
export(Category)
export(DanishPig)
export(Dog)
export(DogAllOf)
export(ModelApiResponse)
export(Order)
export(Pet)
export(Pig)
export(Tag)
export(User)

View File

@ -36,7 +36,6 @@ AllofTagApiResponse <- R6::R6Class(
initialize = function(
`id`=NULL, `name`=NULL, `code`=NULL, `type`=NULL, `message`=NULL, ...
) {
local.optional.var <- list(...)
if (!is.null(`id`)) {
stopifnot(is.numeric(`id`), length(`id`) == 1)
self$`id` <- `id`
@ -151,6 +150,11 @@ AllofTagApiResponse <- R6::R6Class(
self$`type` <- AllofTagApiResponseObject$`type`
self$`message` <- AllofTagApiResponseObject$`message`
self
},
validateJSON = function(input) {
input_json <- jsonlite::fromJSON(input)
}
)
)

View File

@ -27,7 +27,6 @@ Animal <- R6::R6Class(
initialize = function(
`className`, `color`='red', ...
) {
local.optional.var <- list(...)
if (!missing(`className`)) {
stopifnot(is.character(`className`), length(`className`) == 1)
self$`className` <- `className`
@ -85,6 +84,17 @@ Animal <- R6::R6Class(
self$`className` <- AnimalObject$`className`
self$`color` <- AnimalObject$`color`
self
},
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 Animal: the required field `className` is missing."))
}
}
)
)

View File

@ -0,0 +1,106 @@
# 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 BasquePig
#'
#' @description BasquePig Class
#'
#' @format An \code{R6Class} generator object
#'
#' @field className character
#'
#' @field color character
#'
#' @importFrom R6 R6Class
#' @importFrom jsonlite fromJSON toJSON
#' @export
BasquePig <- R6::R6Class(
'BasquePig',
public = list(
`className` = NULL,
`color` = NULL,
initialize = function(
`className`, `color`, ...
) {
if (!missing(`className`)) {
stopifnot(is.character(`className`), length(`className`) == 1)
self$`className` <- `className`
}
if (!missing(`color`)) {
stopifnot(is.character(`color`), length(`color`) == 1)
self$`color` <- `color`
}
},
toJSON = function() {
BasquePigObject <- list()
if (!is.null(self$`className`)) {
BasquePigObject[['className']] <-
self$`className`
}
if (!is.null(self$`color`)) {
BasquePigObject[['color']] <-
self$`color`
}
BasquePigObject
},
fromJSON = function(BasquePigJson) {
BasquePigObject <- jsonlite::fromJSON(BasquePigJson)
if (!is.null(BasquePigObject$`className`)) {
self$`className` <- BasquePigObject$`className`
}
if (!is.null(BasquePigObject$`color`)) {
self$`color` <- BasquePigObject$`color`
}
self
},
toJSONString = function() {
jsoncontent <- c(
if (!is.null(self$`className`)) {
sprintf(
'"className":
"%s"
',
self$`className`
)},
if (!is.null(self$`color`)) {
sprintf(
'"color":
"%s"
',
self$`color`
)}
)
jsoncontent <- paste(jsoncontent, collapse = ",")
paste('{', jsoncontent, '}', sep = "")
},
fromJSONString = function(BasquePigJson) {
BasquePigObject <- jsonlite::fromJSON(BasquePigJson)
self$`className` <- BasquePigObject$`className`
self$`color` <- BasquePigObject$`color`
self
},
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 BasquePig: the required field `className` is missing."))
}
# check the required field `color`
if (!is.null(input_json$`color`)) {
stopifnot(is.character(input_json$`color`), length(input_json$`color`) == 1)
} else {
stop(paste("The JSON input `", input, "` is invalid for BasquePig: the required field `color` is missing."))
}
}
)
)

View File

@ -31,7 +31,6 @@ Cat <- R6::R6Class(
initialize = function(
`className`, `color`='red', `declawed`=NULL, ...
) {
local.optional.var <- list(...)
if (!missing(`className`)) {
stopifnot(is.character(`className`), length(`className`) == 1)
self$`className` <- `className`
@ -108,6 +107,17 @@ Cat <- R6::R6Class(
self$`color` <- CatObject$`color`
self$`declawed` <- CatObject$`declawed`
self
},
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 Cat: the required field `className` is missing."))
}
}
)
)

View File

@ -24,7 +24,6 @@ CatAllOf <- R6::R6Class(
initialize = function(
`declawed`=NULL, ...
) {
local.optional.var <- list(...)
if (!is.null(`declawed`)) {
stopifnot(is.logical(`declawed`), length(`declawed`) == 1)
self$`declawed` <- `declawed`
@ -63,6 +62,11 @@ CatAllOf <- R6::R6Class(
CatAllOfObject <- jsonlite::fromJSON(CatAllOfJson)
self$`declawed` <- CatAllOfObject$`declawed`
self
},
validateJSON = function(input) {
input_json <- jsonlite::fromJSON(input)
}
)
)

View File

@ -27,7 +27,6 @@ Category <- R6::R6Class(
initialize = function(
`id`=NULL, `name`=NULL, ...
) {
local.optional.var <- list(...)
if (!is.null(`id`)) {
stopifnot(is.numeric(`id`), length(`id`) == 1)
self$`id` <- `id`
@ -85,6 +84,11 @@ Category <- R6::R6Class(
self$`id` <- CategoryObject$`id`
self$`name` <- CategoryObject$`name`
self
},
validateJSON = function(input) {
input_json <- jsonlite::fromJSON(input)
}
)
)

View File

@ -0,0 +1,106 @@
# 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 DanishPig
#'
#' @description DanishPig Class
#'
#' @format An \code{R6Class} generator object
#'
#' @field className character
#'
#' @field size integer
#'
#' @importFrom R6 R6Class
#' @importFrom jsonlite fromJSON toJSON
#' @export
DanishPig <- R6::R6Class(
'DanishPig',
public = list(
`className` = NULL,
`size` = NULL,
initialize = function(
`className`, `size`, ...
) {
if (!missing(`className`)) {
stopifnot(is.character(`className`), length(`className`) == 1)
self$`className` <- `className`
}
if (!missing(`size`)) {
stopifnot(is.numeric(`size`), length(`size`) == 1)
self$`size` <- `size`
}
},
toJSON = function() {
DanishPigObject <- list()
if (!is.null(self$`className`)) {
DanishPigObject[['className']] <-
self$`className`
}
if (!is.null(self$`size`)) {
DanishPigObject[['size']] <-
self$`size`
}
DanishPigObject
},
fromJSON = function(DanishPigJson) {
DanishPigObject <- jsonlite::fromJSON(DanishPigJson)
if (!is.null(DanishPigObject$`className`)) {
self$`className` <- DanishPigObject$`className`
}
if (!is.null(DanishPigObject$`size`)) {
self$`size` <- DanishPigObject$`size`
}
self
},
toJSONString = function() {
jsoncontent <- c(
if (!is.null(self$`className`)) {
sprintf(
'"className":
"%s"
',
self$`className`
)},
if (!is.null(self$`size`)) {
sprintf(
'"size":
%d
',
self$`size`
)}
)
jsoncontent <- paste(jsoncontent, collapse = ",")
paste('{', jsoncontent, '}', sep = "")
},
fromJSONString = function(DanishPigJson) {
DanishPigObject <- jsonlite::fromJSON(DanishPigJson)
self$`className` <- DanishPigObject$`className`
self$`size` <- DanishPigObject$`size`
self
},
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 DanishPig: the required field `className` is missing."))
}
# check the required field `size`
if (!is.null(input_json$`size`)) {
stopifnot(is.numeric(input_json$`size`), length(input_json$`size`) == 1)
} else {
stop(paste("The JSON input `", input, "` is invalid for DanishPig: the required field `size` is missing."))
}
}
)
)

View File

@ -31,7 +31,6 @@ Dog <- R6::R6Class(
initialize = function(
`className`, `color`='red', `breed`=NULL, ...
) {
local.optional.var <- list(...)
if (!missing(`className`)) {
stopifnot(is.character(`className`), length(`className`) == 1)
self$`className` <- `className`
@ -108,6 +107,17 @@ Dog <- R6::R6Class(
self$`color` <- DogObject$`color`
self$`breed` <- DogObject$`breed`
self
},
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 Dog: the required field `className` is missing."))
}
}
)
)

View File

@ -24,7 +24,6 @@ DogAllOf <- R6::R6Class(
initialize = function(
`breed`=NULL, ...
) {
local.optional.var <- list(...)
if (!is.null(`breed`)) {
stopifnot(is.character(`breed`), length(`breed`) == 1)
self$`breed` <- `breed`
@ -63,6 +62,11 @@ DogAllOf <- R6::R6Class(
DogAllOfObject <- jsonlite::fromJSON(DogAllOfJson)
self$`breed` <- DogAllOfObject$`breed`
self
},
validateJSON = function(input) {
input_json <- jsonlite::fromJSON(input)
}
)
)

View File

@ -30,7 +30,6 @@ ModelApiResponse <- R6::R6Class(
initialize = function(
`code`=NULL, `type`=NULL, `message`=NULL, ...
) {
local.optional.var <- list(...)
if (!is.null(`code`)) {
stopifnot(is.numeric(`code`), length(`code`) == 1)
self$`code` <- `code`
@ -107,6 +106,11 @@ ModelApiResponse <- R6::R6Class(
self$`type` <- ModelApiResponseObject$`type`
self$`message` <- ModelApiResponseObject$`message`
self
},
validateJSON = function(input) {
input_json <- jsonlite::fromJSON(input)
}
)
)

View File

@ -39,7 +39,6 @@ Order <- R6::R6Class(
initialize = function(
`id`=NULL, `petId`=NULL, `quantity`=NULL, `shipDate`=NULL, `status`=NULL, `complete`=FALSE, ...
) {
local.optional.var <- list(...)
if (!is.null(`id`)) {
stopifnot(is.numeric(`id`), length(`id`) == 1)
self$`id` <- `id`
@ -173,6 +172,11 @@ Order <- R6::R6Class(
self$`status` <- OrderObject$`status`
self$`complete` <- OrderObject$`complete`
self
},
validateJSON = function(input) {
input_json <- jsonlite::fromJSON(input)
}
)
)

View File

@ -39,7 +39,6 @@ Pet <- R6::R6Class(
initialize = function(
`name`, `photoUrls`, `id`=NULL, `category`=NULL, `tags`=NULL, `status`=NULL, ...
) {
local.optional.var <- list(...)
if (!missing(`name`)) {
stopifnot(is.character(`name`), length(`name`) == 1)
self$`name` <- `name`
@ -177,6 +176,24 @@ Pet <- R6::R6Class(
self$`tags` <- ApiClient$new()$deserializeObj(PetObject$`tags`, "array[Tag]", loadNamespace("petstore"))
self$`status` <- PetObject$`status`
self
},
validateJSON = function(input) {
input_json <- jsonlite::fromJSON(input)
# check the required field `name`
if (!is.null(input_json$`name`)) {
stopifnot(is.character(input_json$`name`), length(input_json$`name`) == 1)
} else {
stop(paste("The JSON input `", input, "` is invalid for Pet: the required field `name` is missing."))
}
# check the required field `photoUrls`
if (!is.null(input_json$`photoUrls`)) {
stopifnot(is.vector(input_json$`photoUrls`), length(input_json$`photoUrls`) != 0)
tmp <- sapply(input_json$`photoUrls`, function(x) stopifnot(is.character(x)))
} else {
stop(paste("The JSON input `", input, "` is invalid for Pet: the required field `photoUrls` is missing."))
}
}
)
)

View File

@ -0,0 +1,85 @@
# 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 Pig
#'
#' @description Pig Class
#'
#' @format An \code{R6Class} generator object
#'
#' @importFrom R6 R6Class
#' @importFrom jsonlite fromJSON toJSON
#' @export
Pig <- R6::R6Class(
'Pig',
public = list(
actual_instance = NULL,
actual_type = NULL,
one_of = list("BasquePig", "DanishPig"),
initialize = function(
`className`, `color`, `size`, ...
) {
local_optional_var <- list(...)
},
toJSON = function() {
Pig_object <- list()
Pig_object
},
fromJSON = function(input) {
matched <- 0 # match counter
matched_schemas <- list() #names of matched schemas
error_messages <- list()
instance <- NULL
BasquePig_result <- tryCatch({
BasquePig$public_methods$validateJSON(input)
BasquePig_instance <- BasquePig$new()
instance <- BasquePig_instance$fromJSON(input)
instance_type <- "BasquePig"
matched_schemas <- append(matched_schemas, "BasquePig")
matched <- matched + 1
},
error = function(err) err
)
if (!is.null(BasquePig_result['error'])) {
error_messages <- append(error_messages, BasquePig_result['message'])
}
DanishPig_result <- tryCatch({
DanishPig$public_methods$validateJSON(input)
DanishPig_instance <- DanishPig$new()
instance <- DanishPig_instance$fromJSON(input)
instance_type <- "DanishPig"
matched_schemas <- append(matched_schemas, "DanishPig")
matched <- matched + 1
},
error = function(err) err
)
if (!is.null(DanishPig_result['error'])) {
error_messages <- append(error_messages, DanishPig_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 Pig with oneOf schemas BasquePig, DanishPig. ")
} else {
# no match
stop(paste("No match found when deserializing the payload into Pig with oneOf schemas BasquePig, DanishPig. Details: ", paste(error_messages, collapse = ', ')))
}
self
}
)
)

View File

@ -27,7 +27,6 @@ Tag <- R6::R6Class(
initialize = function(
`id`=NULL, `name`=NULL, ...
) {
local.optional.var <- list(...)
if (!is.null(`id`)) {
stopifnot(is.numeric(`id`), length(`id`) == 1)
self$`id` <- `id`
@ -85,6 +84,11 @@ Tag <- R6::R6Class(
self$`id` <- TagObject$`id`
self$`name` <- TagObject$`name`
self
},
validateJSON = function(input) {
input_json <- jsonlite::fromJSON(input)
}
)
)

View File

@ -45,7 +45,6 @@ User <- R6::R6Class(
initialize = function(
`id`=NULL, `username`=NULL, `firstName`=NULL, `lastName`=NULL, `email`=NULL, `password`=NULL, `phone`=NULL, `userStatus`=NULL, ...
) {
local.optional.var <- list(...)
if (!is.null(`id`)) {
stopifnot(is.numeric(`id`), length(`id`) == 1)
self$`id` <- `id`
@ -217,6 +216,11 @@ User <- R6::R6Class(
self$`phone` <- UserObject$`phone`
self$`userStatus` <- UserObject$`userStatus`
self
},
validateJSON = function(input) {
input_json <- jsonlite::fromJSON(input)
}
)
)

View File

@ -28,7 +28,7 @@ git clone https://github.com/GIT_USER_ID/GIT_REPO_ID
cd GIT_REPO_ID
R CMD build .
R CMD check petstore_1.0.0.tar.gz --no-manual
R CMD INSTALL petstore_1.0.0.tar.gz
R CMD INSTALL --preclean petstore_1.0.0.tar.gz
```
### Install the package
@ -83,14 +83,17 @@ Class | Method | HTTP request | Description
- [AllofTagApiResponse](docs/AllofTagApiResponse.md)
- [Animal](docs/Animal.md)
- [BasquePig](docs/BasquePig.md)
- [Cat](docs/Cat.md)
- [CatAllOf](docs/CatAllOf.md)
- [Category](docs/Category.md)
- [DanishPig](docs/DanishPig.md)
- [Dog](docs/Dog.md)
- [DogAllOf](docs/DogAllOf.md)
- [ModelApiResponse](docs/ModelApiResponse.md)
- [Order](docs/Order.md)
- [Pet](docs/Pet.md)
- [Pig](docs/Pig.md)
- [Tag](docs/Tag.md)
- [User](docs/User.md)

View File

@ -18,6 +18,8 @@ Rscript -e "install.packages('base64enc', repos='$REPO', lib='$R_LIBS_USER')"
Rscript -e "install.packages('rlang', repos='$REPO', lib='$R_LIBS_USER')"
Rscript -e "install.packages('rjson', repos='$REPO', lib='$R_LIBS_USER')"
rm petstore_1.0.0.tar.gz || true
R CMD build .
R CMD check *tar.gz --no-manual
R CMD INSTALL *tar.gz
R CMD install --preclean *tar.gz

View File

@ -0,0 +1,10 @@
# petstore::BasquePig
## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**className** | **character** | |
**color** | **character** | |

View File

@ -0,0 +1,10 @@
# petstore::DanishPig
## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**className** | **character** | |
**size** | **integer** | |

View File

@ -0,0 +1,11 @@
# petstore::Pig
## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**className** | **character** | |
**color** | **character** | |
**size** | **integer** | |

View File

@ -35,7 +35,7 @@
<configuration>
<executable>bash</executable>
<arguments>
<argument>test_petstore.bash</argument>
<argument>build_and_test.bash</argument>
</arguments>
</configuration>
</execution>

View File

@ -1,24 +1,70 @@
install.packages("petstore_1.0.0.tar.gz",repos=NULL, type="source")
library(petstore)
var_pet_id <- 56 # integer | ID of pet to return
#Find pet by ID
api_instance <- PetApi$new()
# Configure API key authorization: api_key
api_instance$api_client$api_keys['api_key'] <- 'TODO_YOUR_API_KEY';
result <- tryCatch(
api_instance$GetPetById(var_pet_id),
ApiException = function(ex) ex
)
# In case of error, print the error object
if(!is.null(result$ApiException)) {
cat(result$ApiException$toString())
} else {
# deserialized response object
response.object <- result$content
# response headers
response.headers <- result$response$headers
# response status code
response.status.code <- result$response$status_code
}
#api_instance <- PetApi$new()
## Configure API key authorization: api_key
#api_instance$api_client$api_keys['api_key'] <- 'TODO_YOUR_API_KEY';
#result <- tryCatch(
# api_instance$GetPetById(var_pet_id),
# ApiException = function(ex) ex
# )
## In case of error, print the error object
#if(!is.null(result$ApiException)) {
# cat(result$ApiException$toString())
#} else {
# # deserialized response object
# response.object <- result$content
# # response headers
# response.headers <- result$response$headers
# # response status code
# response.status.code <- result$response$status_code
#}
json2 <-
'{"name": "pet", "photoUrls" : ["http://a.com", "http://b.com"]}'
json <-
'[
{"Name" : "Mario", "Age" : 32, "Occupation" : "Plumber"},
{"Name" : "Peach", "Age" : 21, "Occupation" : "Princess"},
{},
{"Name" : "Bowser", "Occupation" : "Koopa"}
]'
#Pet$public_methods
#Pet$public_methods$fromJSON(json)
#Pet$public_methods$toJson()
#Pet$public_methods$validateJSON(json2)
#Pet$public_methods$validateJson(json)
#Pet$my_static_method <- function(x) { x + 2}
#Pet$public_methods$my_static_method(1)
basque_pig_json <-
'{"className2": "BasquePig", "color": "red"}'
danish_pig_json <-
'{"className2": "DanishPig", "size": 7}'
wrong_json <-
'[
{"Name" : "Tom", "Age" : 32, "Occupation" : "Consultant"},
{},
{"Name" : "Ada", "Occupation" : "Engineer"}
]'
pig <- Pig$new()
basque_pig <- pig$fromJSON(basque_pig_json)
#print(basque_pig$actual_instance$color)
#expect_equal(basque_pig$actual_type, "BasquePig")
i <- pig$fromJSON(danish_pig_json)
#i <- pig$fromJSON(wrong_json)
d <- DanishPig$new()
dp <- d$validateJSON(danish_pig_json)

View File

@ -0,0 +1,20 @@
# Automatically generated by openapi-generator (https://openapi-generator.tech)
# Please update as you see appropriate
context("Test BasquePig")
model_instance <- BasquePig$new()
test_that("className", {
# tests for the property `className` (character)
# uncomment below to test the property
#expect_equal(model.instance$`className`, "EXPECTED_RESULT")
})
test_that("color", {
# tests for the property `color` (character)
# uncomment below to test the property
#expect_equal(model.instance$`color`, "EXPECTED_RESULT")
})

View File

@ -0,0 +1,20 @@
# Automatically generated by openapi-generator (https://openapi-generator.tech)
# Please update as you see appropriate
context("Test DanishPig")
model_instance <- DanishPig$new()
test_that("className", {
# tests for the property `className` (character)
# uncomment below to test the property
#expect_equal(model.instance$`className`, "EXPECTED_RESULT")
})
test_that("size", {
# tests for the property `size` (integer)
# uncomment below to test the property
#expect_equal(model.instance$`size`, "EXPECTED_RESULT")
})

View File

@ -119,6 +119,55 @@ test_that("Tests allOf with discriminator", {
expect_true(c1$declawed)
})
test_that("Tests validateJSON", {
json <-
'{"name": "pet", "photoUrls" : ["http://a.com", "http://b.com"]}'
json2 <-
'[
{"Name" : "Tom", "Age" : 32, "Occupation" : "Consultant"},
{},
{"Name" : "Ada", "Occupation" : "Engineer"}
]'
# validate `json` and no error throw
Pet$public_methods$validateJSON(json)
# validate `json2` and should throw an error due to missing required fields
#expect_error(Pet$public_methods$validateJSON(json2), 'The JSON input ` [\n {\"Name\" : \"Tom\", \"Age\" : 32, \"Occupation\" : \"Consultant\"}, \n {},\n {\"Name\" : \"Ada\", \"Occupation\" : \"Engineer\"}\n ] ` is invalid for Pet: the required field `name` is missing.')
})
test_that("Tests oneOf", {
basque_pig_json <-
'{"className": "BasquePig", "color": "red"}'
danish_pig_json <-
'{"className": "DanishPig", "size": 7}'
wrong_json <-
'[
{"Name" : "Tom", "Age" : 32, "Occupation" : "Consultant"},
{},
{"Name" : "Ada", "Occupation" : "Engineer"}
]'
pig <- Pig$new()
danish_pig <- pig$fromJSON(danish_pig_json)
expect_equal(danish_pig$actual_type, "DanishPig")
expect_equal(danish_pig$actual_instance$size, 7)
expect_equal(danish_pig$actual_instance$className, "DanishPig")
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_error(pig$fromJSON(wrong_json), "No match found when deserializing the payload into Pig with oneOf schemas BasquePig, DanishPig. Details: The JSON input ` [\n {\"Name\" : \"Tom\", \"Age\" : 32, \"Occupation\" : \"Consultant\"}, \n {},\n {\"Name\" : \"Ada\", \"Occupation\" : \"Engineer\"}\n ] ` is invalid for BasquePig: the required field `className` is missing., The JSON input ` [\n {\"Name\" : \"Tom\", \"Age\" : 32, \"Occupation\" : \"Consultant\"}, \n {},\n {\"Name\" : \"Ada\", \"Occupation\" : \"Engineer\"}\n ] ` 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: 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\\.')
})
#test_that("GetPetById", {
# pet.id <- pet.id
# pet <- Pet$new(pet.id, NULL, "name_test2",

View File

@ -0,0 +1,27 @@
# Automatically generated by openapi-generator (https://openapi-generator.tech)
# Please update as you see appropriate
context("Test Pig")
model_instance <- Pig$new()
test_that("className", {
# tests for the property `className` (character)
# uncomment below to test the property
#expect_equal(model.instance$`className`, "EXPECTED_RESULT")
})
test_that("color", {
# tests for the property `color` (character)
# uncomment below to test the property
#expect_equal(model.instance$`color`, "EXPECTED_RESULT")
})
test_that("size", {
# tests for the property `size` (integer)
# uncomment below to test the property
#expect_equal(model.instance$`size`, "EXPECTED_RESULT")
})