forked from loafle/openapi-generator-original
[R] add anyOf support (#12544)
* add anyOf support * add anyOf support to r client generator
This commit is contained in:
parent
a339123586
commit
4fbe1e9115
52
modules/openapi-generator/src/main/resources/r/modelAnyOf.mustache
vendored
Normal file
52
modules/openapi-generator/src/main/resources/r/modelAnyOf.mustache
vendored
Normal file
@ -0,0 +1,52 @@
|
||||
#' @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,
|
||||
any_of = list({{#anyOf}}"{{{.}}}"{{^-last}}, {{/-last}}{{/anyOf}}),
|
||||
initialize = function(
|
||||
{{#requiredVars}}`{{baseName}}`, {{/requiredVars}}{{#optionalVars}}`{{baseName}}`={{{defaultValue}}}{{^defaultValue}}NULL{{/defaultValue}}, {{/optionalVars}}...
|
||||
) {
|
||||
local_optional_var <- list(...)
|
||||
},
|
||||
fromJSON = function(input) {
|
||||
error_messages <- list()
|
||||
|
||||
{{#anyOf}}
|
||||
{{{.}}}_result <- tryCatch({
|
||||
{{{.}}}$public_methods$validateJSON(input)
|
||||
{{{.}}}_instance <- {{{.}}}$new()
|
||||
self$actual_instance <- {{{.}}}_instance$fromJSON(input)
|
||||
self$actual_type <- "{{{.}}}"
|
||||
return(self)
|
||||
},
|
||||
error = function(err) err
|
||||
)
|
||||
|
||||
if (!is.null({{{.}}}_result['error'])) {
|
||||
error_messages <- append(error_messages, {{{.}}}_result['message'])
|
||||
}
|
||||
|
||||
{{/anyOf}}
|
||||
# no match
|
||||
stop(paste("No match found when deserializing the payload into {{{classname}}} with anyOf schemas {{#anyOf}}{{{.}}}{{^-last}}, {{/-last}}{{/anyOf}}. Details: ", paste(error_messages, collapse = ', ')))
|
||||
},
|
||||
toJSON = function() {
|
||||
if (!is.null(self$actual_instance)) {
|
||||
self$actual_instance$toJSONString()
|
||||
} else {
|
||||
NULL
|
||||
}
|
||||
}
|
||||
)
|
||||
)
|
@ -801,6 +801,10 @@ components:
|
||||
allOf:
|
||||
- $ref: '#/components/schemas/Tag'
|
||||
- $ref: '#/components/schemas/ApiResponse'
|
||||
AnyOfPig:
|
||||
anyOf:
|
||||
- $ref: '#/components/schemas/BasquePig'
|
||||
- $ref: '#/components/schemas/DanishPig'
|
||||
Pig:
|
||||
oneOf:
|
||||
- $ref: '#/components/schemas/BasquePig'
|
||||
|
@ -5,6 +5,7 @@ DESCRIPTION
|
||||
NAMESPACE
|
||||
R/allof_tag_api_response.R
|
||||
R/animal.R
|
||||
R/any_of_pig.R
|
||||
R/api_client.R
|
||||
R/api_exception.R
|
||||
R/api_response.R
|
||||
@ -28,6 +29,7 @@ R/user_api.R
|
||||
README.md
|
||||
docs/AllofTagApiResponse.md
|
||||
docs/Animal.md
|
||||
docs/AnyOfPig.md
|
||||
docs/BasquePig.md
|
||||
docs/Cat.md
|
||||
docs/CatAllOf.md
|
||||
|
@ -14,6 +14,7 @@ export(ApiException)
|
||||
# Models
|
||||
export(AllofTagApiResponse)
|
||||
export(Animal)
|
||||
export(AnyOfPig)
|
||||
export(BasquePig)
|
||||
export(Cat)
|
||||
export(CatAllOf)
|
||||
|
72
samples/client/petstore/R/R/any_of_pig.R
Normal file
72
samples/client/petstore/R/R/any_of_pig.R
Normal file
@ -0,0 +1,72 @@
|
||||
# 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 AnyOfPig
|
||||
#'
|
||||
#' @description AnyOfPig Class
|
||||
#'
|
||||
#' @format An \code{R6Class} generator object
|
||||
#'
|
||||
#' @importFrom R6 R6Class
|
||||
#' @importFrom jsonlite fromJSON toJSON
|
||||
#' @export
|
||||
AnyOfPig <- R6::R6Class(
|
||||
'AnyOfPig',
|
||||
public = list(
|
||||
actual_instance = NULL,
|
||||
actual_type = NULL,
|
||||
any_of = list("BasquePig", "DanishPig"),
|
||||
initialize = function(
|
||||
`className`, `color`, `size`, ...
|
||||
) {
|
||||
local_optional_var <- list(...)
|
||||
},
|
||||
fromJSON = function(input) {
|
||||
error_messages <- list()
|
||||
|
||||
BasquePig_result <- tryCatch({
|
||||
BasquePig$public_methods$validateJSON(input)
|
||||
BasquePig_instance <- BasquePig$new()
|
||||
self$actual_instance <- BasquePig_instance$fromJSON(input)
|
||||
self$actual_type <- "BasquePig"
|
||||
return(self)
|
||||
},
|
||||
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()
|
||||
self$actual_instance <- DanishPig_instance$fromJSON(input)
|
||||
self$actual_type <- "DanishPig"
|
||||
return(self)
|
||||
},
|
||||
error = function(err) err
|
||||
)
|
||||
|
||||
if (!is.null(DanishPig_result['error'])) {
|
||||
error_messages <- append(error_messages, DanishPig_result['message'])
|
||||
}
|
||||
|
||||
# no match
|
||||
stop(paste("No match found when deserializing the payload into AnyOfPig with anyOf schemas BasquePig, DanishPig. Details: ", paste(error_messages, collapse = ', ')))
|
||||
},
|
||||
toJSON = function() {
|
||||
if (!is.null(self$actual_instance)) {
|
||||
self$actual_instance$toJSONString()
|
||||
} else {
|
||||
NULL
|
||||
}
|
||||
}
|
||||
)
|
||||
)
|
||||
|
@ -83,6 +83,7 @@ Class | Method | HTTP request | Description
|
||||
|
||||
- [AllofTagApiResponse](docs/AllofTagApiResponse.md)
|
||||
- [Animal](docs/Animal.md)
|
||||
- [AnyOfPig](docs/AnyOfPig.md)
|
||||
- [BasquePig](docs/BasquePig.md)
|
||||
- [Cat](docs/Cat.md)
|
||||
- [CatAllOf](docs/CatAllOf.md)
|
||||
|
11
samples/client/petstore/R/docs/AnyOfPig.md
Normal file
11
samples/client/petstore/R/docs/AnyOfPig.md
Normal file
@ -0,0 +1,11 @@
|
||||
# petstore::AnyOfPig
|
||||
|
||||
|
||||
## Properties
|
||||
Name | Type | Description | Notes
|
||||
------------ | ------------- | ------------- | -------------
|
||||
**className** | **character** | |
|
||||
**color** | **character** | |
|
||||
**size** | **integer** | |
|
||||
|
||||
|
27
samples/client/petstore/R/tests/testthat/test_any_of_pig.R
Normal file
27
samples/client/petstore/R/tests/testthat/test_any_of_pig.R
Normal file
@ -0,0 +1,27 @@
|
||||
# Automatically generated by openapi-generator (https://openapi-generator.tech)
|
||||
# Please update as you see appropriate
|
||||
|
||||
context("Test AnyOfPig")
|
||||
|
||||
model_instance <- AnyOfPig$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")
|
||||
})
|
@ -156,7 +156,7 @@ test_that("Tests oneOf", {
|
||||
original_danish_pig = DanishPig$new()$fromJSON(danish_pig_json)
|
||||
original_basque_pig = BasquePig$new()$fromJSON(basque_pig_json)
|
||||
|
||||
# test fromJSNO, actual_tpye, actual_instance
|
||||
# test fromJSON, actual_tpye, actual_instance
|
||||
pig <- Pig$new()
|
||||
danish_pig <- pig$fromJSON(danish_pig_json)
|
||||
expect_equal(danish_pig$actual_type, "DanishPig")
|
||||
@ -168,7 +168,6 @@ test_that("Tests oneOf", {
|
||||
expect_equal(pig$actual_instance$className, "DanishPig")
|
||||
|
||||
# test toJSON
|
||||
#expect_equal(danish_pig$toJSON(), "")
|
||||
expect_equal(danish_pig$toJSON(), original_danish_pig$toJSONString())
|
||||
|
||||
basque_pig <- pig$fromJSON(basque_pig_json)
|
||||
@ -181,6 +180,49 @@ test_that("Tests oneOf", {
|
||||
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("Tests anyOf", {
|
||||
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"}
|
||||
]'
|
||||
|
||||
original_danish_pig = DanishPig$new()$fromJSON(danish_pig_json)
|
||||
original_basque_pig = BasquePig$new()$fromJSON(basque_pig_json)
|
||||
|
||||
# test fromJSON, actual_tpye, actual_instance
|
||||
pig <- AnyOfPig$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")
|
||||
|
||||
expect_equal(pig$actual_type, "DanishPig")
|
||||
expect_equal(pig$actual_instance$size, 7)
|
||||
expect_equal(pig$actual_instance$className, "DanishPig")
|
||||
|
||||
# test toJSON
|
||||
expect_equal(danish_pig$toJSON(), 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())
|
||||
|
||||
# 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\\.')
|
||||
|
||||
|
||||
})
|
||||
|
||||
#test_that("GetPetById", {
|
||||
|
Loading…
x
Reference in New Issue
Block a user