[R] add anyOf support (#12544)

* add anyOf support

* add anyOf support to r client generator
This commit is contained in:
William Cheng 2022-06-07 10:05:34 +08:00 committed by GitHub
parent a339123586
commit 4fbe1e9115
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 214 additions and 2 deletions

View 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
}
}
)
)

View File

@ -801,6 +801,10 @@ components:
allOf: allOf:
- $ref: '#/components/schemas/Tag' - $ref: '#/components/schemas/Tag'
- $ref: '#/components/schemas/ApiResponse' - $ref: '#/components/schemas/ApiResponse'
AnyOfPig:
anyOf:
- $ref: '#/components/schemas/BasquePig'
- $ref: '#/components/schemas/DanishPig'
Pig: Pig:
oneOf: oneOf:
- $ref: '#/components/schemas/BasquePig' - $ref: '#/components/schemas/BasquePig'

View File

@ -5,6 +5,7 @@ DESCRIPTION
NAMESPACE NAMESPACE
R/allof_tag_api_response.R R/allof_tag_api_response.R
R/animal.R R/animal.R
R/any_of_pig.R
R/api_client.R R/api_client.R
R/api_exception.R R/api_exception.R
R/api_response.R R/api_response.R
@ -28,6 +29,7 @@ R/user_api.R
README.md README.md
docs/AllofTagApiResponse.md docs/AllofTagApiResponse.md
docs/Animal.md docs/Animal.md
docs/AnyOfPig.md
docs/BasquePig.md docs/BasquePig.md
docs/Cat.md docs/Cat.md
docs/CatAllOf.md docs/CatAllOf.md

View File

@ -14,6 +14,7 @@ export(ApiException)
# Models # Models
export(AllofTagApiResponse) export(AllofTagApiResponse)
export(Animal) export(Animal)
export(AnyOfPig)
export(BasquePig) export(BasquePig)
export(Cat) export(Cat)
export(CatAllOf) export(CatAllOf)

View 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
}
}
)
)

View File

@ -83,6 +83,7 @@ Class | Method | HTTP request | Description
- [AllofTagApiResponse](docs/AllofTagApiResponse.md) - [AllofTagApiResponse](docs/AllofTagApiResponse.md)
- [Animal](docs/Animal.md) - [Animal](docs/Animal.md)
- [AnyOfPig](docs/AnyOfPig.md)
- [BasquePig](docs/BasquePig.md) - [BasquePig](docs/BasquePig.md)
- [Cat](docs/Cat.md) - [Cat](docs/Cat.md)
- [CatAllOf](docs/CatAllOf.md) - [CatAllOf](docs/CatAllOf.md)

View File

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

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

View File

@ -156,7 +156,7 @@ test_that("Tests oneOf", {
original_danish_pig = DanishPig$new()$fromJSON(danish_pig_json) original_danish_pig = DanishPig$new()$fromJSON(danish_pig_json)
original_basque_pig = BasquePig$new()$fromJSON(basque_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() pig <- Pig$new()
danish_pig <- pig$fromJSON(danish_pig_json) danish_pig <- pig$fromJSON(danish_pig_json)
expect_equal(danish_pig$actual_type, "DanishPig") expect_equal(danish_pig$actual_type, "DanishPig")
@ -168,7 +168,6 @@ test_that("Tests oneOf", {
expect_equal(pig$actual_instance$className, "DanishPig") expect_equal(pig$actual_instance$className, "DanishPig")
# test toJSON # test toJSON
#expect_equal(danish_pig$toJSON(), "")
expect_equal(danish_pig$toJSON(), original_danish_pig$toJSONString()) expect_equal(danish_pig$toJSON(), original_danish_pig$toJSONString())
basque_pig <- pig$fromJSON(basque_pig_json) 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\\.') 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", { #test_that("GetPetById", {