forked from loafle/openapi-generator-original
[R] add discriminator and mappings support (#13529)
* add discriminator property, mapping * add discriminator, mapping support to R client generator * add discriminator, mapping support to R client genrator httr library
This commit is contained in:
parent
4ba43f7577
commit
6a631b4cc0
@ -388,6 +388,20 @@ ApiClient <- R6::R6Class(
|
||||
# To handle model objects which are not array or map containers. Ex:"Pet"
|
||||
return_type <- get(return_type, envir = as.environment(pkg_env))
|
||||
return_obj <- return_type$new()
|
||||
# check if discriminator is defined
|
||||
if (!is.null(return_obj$`_discriminator_property_name`)) {
|
||||
data_type <- return_obj$`_discriminator_property_name`
|
||||
# use discriminator mapping if provided
|
||||
if (!is.null(return_obj$`_discriminator_mapping_name`)) {
|
||||
data_type <- (return_obj$`_discriminator_mapping_name`)[[obj[[data_type]]]]
|
||||
} else {
|
||||
# no mapping provided, use the value directly
|
||||
data_type <- obj[[data_type]]
|
||||
}
|
||||
# create an object of the mapped type (e.g. Cat)
|
||||
return_type <- get(data_type, envir = as.environment(pkg_env))
|
||||
return_obj <- return_type$new()
|
||||
}
|
||||
return_obj$fromJSON(
|
||||
jsonlite::toJSON(obj, digits = NA, auto_unbox = TRUE)
|
||||
)
|
||||
|
@ -390,9 +390,23 @@ ApiClient <- R6::R6Class(
|
||||
}
|
||||
}
|
||||
} else if (exists(return_type, pkg_env) && !(c(return_type) %in% primitive_types)) {
|
||||
# To handle model objects which are not array or map containers. Ex:"Pet"
|
||||
# To handle model objects which are not array or map containers (e.g. Pet)
|
||||
return_type <- get(return_type, envir = as.environment(pkg_env))
|
||||
return_obj <- return_type$new()
|
||||
# check if discriminator is defined
|
||||
if (!is.null(return_obj$`_discriminator_property_name`)) {
|
||||
data_type <- return_obj$`_discriminator_property_name`
|
||||
# use discriminator mapping if provided
|
||||
if (!is.null(return_obj$`_discriminator_mapping_name`)) {
|
||||
data_type <- (return_obj$`_discriminator_mapping_name`)[[obj[[data_type]]]]
|
||||
} else {
|
||||
# no mapping provided, use the value directly
|
||||
data_type <- obj[[data_type]]
|
||||
}
|
||||
# create an object of the mapped type (e.g. Cat)
|
||||
return_type <- get(data_type, envir = as.environment(pkg_env))
|
||||
return_obj <- return_type$new()
|
||||
}
|
||||
return_obj$fromJSON(
|
||||
jsonlite::toJSON(obj, digits = NA, auto_unbox = TRUE)
|
||||
)
|
||||
|
@ -30,6 +30,10 @@
|
||||
`_field_list` = c({{#vars}}"{{{name}}}"{{^-last}}, {{/-last}}{{/vars}}),
|
||||
`additional_properties` = list(),
|
||||
{{/isAdditionalPropertiesTrue}}
|
||||
{{#discriminator}}
|
||||
`_discriminator_property_name` = '{{discriminator.propertyName}}',
|
||||
{{#discriminator.mappedModels}}{{#-first}}`_discriminator_mapping_name` = c({{/-first}}'{{mappingName}}' = '{{modelName}}'{{^-last}},{{/-last}}{{#-last}}),{{/-last}}{{/discriminator.mappedModels}}
|
||||
{{/discriminator}}
|
||||
#' Initialize a new {{{classname}}} class.
|
||||
#'
|
||||
#' @description
|
||||
|
@ -21,6 +21,8 @@ Animal <- R6::R6Class(
|
||||
`color` = NULL,
|
||||
`_field_list` = c("className", "color"),
|
||||
`additional_properties` = list(),
|
||||
`_discriminator_property_name` = 'className',
|
||||
`_discriminator_mapping_name` = c('Cat' = 'Cat','Dog' = 'Dog'),
|
||||
#' Initialize a new Animal class.
|
||||
#'
|
||||
#' @description
|
||||
|
@ -379,9 +379,23 @@ ApiClient <- R6::R6Class(
|
||||
}
|
||||
}
|
||||
} else if (exists(return_type, pkg_env) && !(c(return_type) %in% primitive_types)) {
|
||||
# To handle model objects which are not array or map containers. Ex:"Pet"
|
||||
# To handle model objects which are not array or map containers (e.g. Pet)
|
||||
return_type <- get(return_type, envir = as.environment(pkg_env))
|
||||
return_obj <- return_type$new()
|
||||
# check if discriminator is defined
|
||||
if (!is.null(return_obj$`_discriminator_property_name`)) {
|
||||
data_type <- return_obj$`_discriminator_property_name`
|
||||
# use discriminator mapping if provided
|
||||
if (!is.null(return_obj$`_discriminator_mapping_name`)) {
|
||||
data_type <- (return_obj$`_discriminator_mapping_name`)[[obj[[data_type]]]]
|
||||
} else {
|
||||
# no mapping provided, use the value directly
|
||||
data_type <- obj[[data_type]]
|
||||
}
|
||||
# create an object of the mapped type (e.g. Cat)
|
||||
return_type <- get(data_type, envir = as.environment(pkg_env))
|
||||
return_obj <- return_type$new()
|
||||
}
|
||||
return_obj$fromJSON(
|
||||
jsonlite::toJSON(obj, digits = NA, auto_unbox = TRUE)
|
||||
)
|
||||
|
@ -3,6 +3,8 @@ install.packages("petstore_1.0.0.tar.gz",repos=NULL, type="source")
|
||||
library(petstore)
|
||||
library(jsonlite)
|
||||
|
||||
|
||||
|
||||
var_status <- list("something inside", "explode please", "123") # array[character] | Status values that need to be considered for filter
|
||||
|
||||
# Finds Pets by status
|
||||
|
@ -21,6 +21,15 @@ pet_api$api_client$username <- "username123"
|
||||
pet_api$api_client$password <- "password123"
|
||||
result <- pet_api$add_pet(pet)
|
||||
|
||||
test_that("Test discriminator and mapping", {
|
||||
d <- '{"breed": "bulldog","color":"white","className":"Dog"}'
|
||||
dog <- ApiClient$new()$deserialize(d, "Animal", loadNamespace("petstore"))
|
||||
expect_equal(class(dog)[1], "Dog")
|
||||
expect_equal(dog$breed, "bulldog")
|
||||
expect_equal(dog$color, "white")
|
||||
expect_equal(dog$className, "Dog")
|
||||
})
|
||||
|
||||
test_that("Invalid enum value test", {
|
||||
expect_error(Pet$new("name_test",
|
||||
photoUrls = list("photo_test", "second test"),
|
||||
|
@ -17,6 +17,8 @@ Animal <- R6::R6Class(
|
||||
public = list(
|
||||
`className` = NULL,
|
||||
`color` = NULL,
|
||||
`_discriminator_property_name` = 'className',
|
||||
`_discriminator_mapping_name` = c('Cat' = 'Cat','Dog' = 'Dog'),
|
||||
#' Initialize a new Animal class.
|
||||
#'
|
||||
#' @description
|
||||
|
@ -379,9 +379,23 @@ ApiClient <- R6::R6Class(
|
||||
}
|
||||
}
|
||||
} else if (exists(return_type, pkg_env) && !(c(return_type) %in% primitive_types)) {
|
||||
# To handle model objects which are not array or map containers. Ex:"Pet"
|
||||
# To handle model objects which are not array or map containers (e.g. Pet)
|
||||
return_type <- get(return_type, envir = as.environment(pkg_env))
|
||||
return_obj <- return_type$new()
|
||||
# check if discriminator is defined
|
||||
if (!is.null(return_obj$`_discriminator_property_name`)) {
|
||||
data_type <- return_obj$`_discriminator_property_name`
|
||||
# use discriminator mapping if provided
|
||||
if (!is.null(return_obj$`_discriminator_mapping_name`)) {
|
||||
data_type <- (return_obj$`_discriminator_mapping_name`)[[obj[[data_type]]]]
|
||||
} else {
|
||||
# no mapping provided, use the value directly
|
||||
data_type <- obj[[data_type]]
|
||||
}
|
||||
# create an object of the mapped type (e.g. Cat)
|
||||
return_type <- get(data_type, envir = as.environment(pkg_env))
|
||||
return_obj <- return_type$new()
|
||||
}
|
||||
return_obj$fromJSON(
|
||||
jsonlite::toJSON(obj, digits = NA, auto_unbox = TRUE)
|
||||
)
|
||||
|
@ -21,6 +21,8 @@ Animal <- R6::R6Class(
|
||||
`color` = NULL,
|
||||
`_field_list` = c("className", "color"),
|
||||
`additional_properties` = list(),
|
||||
`_discriminator_property_name` = 'className',
|
||||
`_discriminator_mapping_name` = c('Cat' = 'Cat','Dog' = 'Dog'),
|
||||
#' Initialize a new Animal class.
|
||||
#'
|
||||
#' @description
|
||||
|
@ -369,6 +369,20 @@ ApiClient <- R6::R6Class(
|
||||
# To handle model objects which are not array or map containers. Ex:"Pet"
|
||||
return_type <- get(return_type, envir = as.environment(pkg_env))
|
||||
return_obj <- return_type$new()
|
||||
# check if discriminator is defined
|
||||
if (!is.null(return_obj$`_discriminator_property_name`)) {
|
||||
data_type <- return_obj$`_discriminator_property_name`
|
||||
# use discriminator mapping if provided
|
||||
if (!is.null(return_obj$`_discriminator_mapping_name`)) {
|
||||
data_type <- (return_obj$`_discriminator_mapping_name`)[[obj[[data_type]]]]
|
||||
} else {
|
||||
# no mapping provided, use the value directly
|
||||
data_type <- obj[[data_type]]
|
||||
}
|
||||
# create an object of the mapped type (e.g. Cat)
|
||||
return_type <- get(data_type, envir = as.environment(pkg_env))
|
||||
return_obj <- return_type$new()
|
||||
}
|
||||
return_obj$fromJSON(
|
||||
jsonlite::toJSON(obj, digits = NA, auto_unbox = TRUE)
|
||||
)
|
||||
|
@ -18,6 +18,15 @@ pet <- Pet$new("name_test",
|
||||
#pet_api$api_client$password <- ""
|
||||
result <- pet_api$AddPet(pet)
|
||||
|
||||
test_that("Test discriminator and mapping", {
|
||||
d <- '{"breed": "bulldog","color":"white","className":"Dog"}'
|
||||
dog <- ApiClient$new()$deserialize(d, "Animal", loadNamespace("petstore"))
|
||||
expect_equal(class(dog)[1], "Dog")
|
||||
expect_equal(dog$breed, "bulldog")
|
||||
expect_equal(dog$color, "white")
|
||||
expect_equal(dog$className, "Dog")
|
||||
})
|
||||
|
||||
test_that("Test toJSONString", {
|
||||
expect_equal(pet_id, 123321)
|
||||
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"}')
|
||||
|
Loading…
x
Reference in New Issue
Block a user