Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add set support to model properties in R client #13443

Merged
merged 2 commits into from
Sep 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,15 @@
{{/isPrimitiveType}}
{{/isContainer}}
{{#isContainer}}
{{#isPrimitiveType}}
stopifnot(is.vector(`{{name}}`), length(`{{name}}`) != 0)
sapply(`{{name}}`, function(x) stopifnot(is.character(x)))
{{/isPrimitiveType}}
{{^isPrimitiveType}}
stopifnot(is.vector(`{{name}}`), length(`{{name}}`) != 0)
sapply(`{{name}}`, function(x) stopifnot(R6::is.R6(x)))
{{/isPrimitiveType}}
sapply(`{{name}}`, function(x) stopifnot({{#isPrimitiveType}}is.character(x){{/isPrimitiveType}}{{^isPrimitiveType}}R6::is.R6(x){{/isPrimitiveType}}))
{{#isArray}}
{{#uniqueItems}}
if (!identical(`{{name}}`, unique(`{{name}}`))) {
stop("Error! Items in `{{name}}` are not unique.")
}
{{/uniqueItems}}
{{/isArray}}
{{/isContainer}}
self$`{{name}}` <- `{{name}}`
}
Expand Down Expand Up @@ -125,14 +126,15 @@
{{/isPrimitiveType}}
{{/isContainer}}
{{#isContainer}}
{{#isPrimitiveType}}
stopifnot(is.vector(`{{name}}`), length(`{{name}}`) != 0)
sapply(`{{name}}`, function(x) stopifnot(is.character(x)))
{{/isPrimitiveType}}
{{^isPrimitiveType}}
stopifnot(is.vector(`{{name}}`), length(`{{name}}`) != 0)
sapply(`{{name}}`, function(x) stopifnot(R6::is.R6(x)))
{{/isPrimitiveType}}
sapply(`{{name}}`, function(x) stopifnot({{#isPrimitiveType}}is.character(x){{/isPrimitiveType}}{{^isPrimitiveType}}R6::is.R6(x){{/isPrimitiveType}}))
{{#isArray}}
{{#uniqueItems}}
if (!identical(`{{name}}`, unique(`{{name}}`))) {
stop("Error! Items in `{{name}}` are not unique.")
}
{{/uniqueItems}}
{{/isArray}}
{{/isContainer}}
self$`{{name}}` <- `{{name}}`
}
Expand Down Expand Up @@ -207,6 +209,13 @@
if (!is.null(this_object$`{{baseName}}`)) {
{{#isContainer}}
self$`{{name}}` <- ApiClient$new()$deserializeObj(this_object$`{{baseName}}`, "{{dataType}}", loadNamespace("{{packageName}}"))
{{#isArray}}
{{#uniqueItems}}
if (!identical(self$`{{name}}`, unique(self$`{{name}}`))) {
stop("Error! Items in `{{name}}` are not unique.")
}
{{/uniqueItems}}
{{/isArray}}
{{/isContainer}}
{{^isContainer}}
{{#isPrimitiveType}}
Expand Down Expand Up @@ -321,6 +330,13 @@
{{! AAPI - added condition for handling container type of parameters, map and array}}
{{#isContainer}}
self$`{{name}}` <- ApiClient$new()$deserializeObj(this_object$`{{name}}`, "{{dataType}}", loadNamespace("{{packageName}}"))
{{#isArray}}
{{#uniqueItems}}
if (!identical(self$`{{name}}`, unique(self$`{{name}}`))) {
stop("Error! Items in `{{name}}` are not unique.")
}
{{/uniqueItems}}
{{/isArray}}
{{/isContainer}}
{{^isContainer}}
{{#isPrimitiveType}}
Expand Down Expand Up @@ -384,14 +400,15 @@
{{/isPrimitiveType}}
{{/isContainer}}
{{#isContainer}}
{{#isPrimitiveType}}
stopifnot(is.vector(input_json$`{{name}}`), length(input_json$`{{name}}`) != 0)
tmp <- sapply(input_json$`{{name}}`, function(x) stopifnot(is.character(x)))
{{/isPrimitiveType}}
{{^isPrimitiveType}}
stopifnot(is.vector(input_json$`{{name}}`), length(json_input$`{{name}}`) != 0)
tmp <- sapply(input_json$`{{name}}`, function(x) stopifnot(R6::is.R6(x)))
{{/isPrimitiveType}}
{{#isArray}}
{{#uniqueItems}}
if (!identical(input_json$`{{name}}`, unique(input_json$`{{name}}`))) {
stop("Error! Items in `{{name}}` are not unique.")
}
{{/uniqueItems}}
{{/isArray}}
tmp <- sapply(input_json$`{{name}}`, function(x) stopifnot({{#isPrimitiveType}}is.character(x){{/isPrimitiveType}}{{^isPrimitiveType}}R6::is.R6(x){{/isPrimitiveType}}))
{{/isContainer}}
} else {
stop(paste("The JSON input `", input, "` is invalid for {{classname}}: the required field `{{name}}` is missing."))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,11 @@ components:
description: Describes the result of uploading an image resource
type: object
properties:
set_test:
uniqueItems: true
type: array
items:
type: string
self:
type: integer
format: int32
Expand Down
39 changes: 37 additions & 2 deletions samples/client/petstore/R-httr2-wrapper/R/special.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#' @title Special
#' @description Special Class
#' @format An \code{R6Class} generator object
#' @field set_test list(character) [optional]
#' @field item_self integer [optional]
#' @field item_private character [optional]
#' @field item_super character [optional]
Expand All @@ -21,19 +22,21 @@
Special <- R6::R6Class(
"Special",
public = list(
`set_test` = NULL,
`item_self` = NULL,
`item_private` = NULL,
`item_super` = NULL,
`123_number` = NULL,
`array[test]` = NULL,
`empty_string` = NULL,
`_field_list` = c("item_self", "item_private", "item_super", "123_number", "array[test]", "empty_string"),
`_field_list` = c("set_test", "item_self", "item_private", "item_super", "123_number", "array[test]", "empty_string"),
`additional_properties` = list(),
#' Initialize a new Special class.
#'
#' @description
#' Initialize a new Special class.
#'
#' @param set_test set_test
#' @param item_self item_self
#' @param item_private item_private
#' @param item_super item_super
Expand All @@ -44,8 +47,16 @@ Special <- R6::R6Class(
#' @param ... Other optional arguments.
#' @export
initialize = function(
`item_self` = NULL, `item_private` = NULL, `item_super` = NULL, `123_number` = NULL, `array[test]` = NULL, `empty_string` = NULL, additional_properties = NULL, ...
`set_test` = NULL, `item_self` = NULL, `item_private` = NULL, `item_super` = NULL, `123_number` = NULL, `array[test]` = NULL, `empty_string` = NULL, additional_properties = NULL, ...
) {
if (!is.null(`set_test`)) {
stopifnot(is.vector(`set_test`), length(`set_test`) != 0)
sapply(`set_test`, function(x) stopifnot(is.character(x)))
if (!identical(`set_test`, unique(`set_test`))) {
stop("Error! Items in `set_test` are not unique.")
}
self$`set_test` <- `set_test`
}
if (!is.null(`item_self`)) {
stopifnot(is.numeric(`item_self`), length(`item_self`) == 1)
self$`item_self` <- `item_self`
Expand Down Expand Up @@ -85,6 +96,10 @@ Special <- R6::R6Class(
#' @export
toJSON = function() {
SpecialObject <- list()
if (!is.null(self$`set_test`)) {
SpecialObject[["set_test"]] <-
self$`set_test`
}
if (!is.null(self$`item_self`)) {
SpecialObject[["self"]] <-
self$`item_self`
Expand Down Expand Up @@ -125,6 +140,12 @@ Special <- R6::R6Class(
#' @export
fromJSON = function(input_json) {
this_object <- jsonlite::fromJSON(input_json)
if (!is.null(this_object$`set_test`)) {
self$`set_test` <- ApiClient$new()$deserializeObj(this_object$`set_test`, "set[character]", loadNamespace("petstore"))
if (!identical(self$`set_test`, unique(self$`set_test`))) {
stop("Error! Items in `set_test` are not unique.")
}
}
if (!is.null(this_object$`self`)) {
self$`item_self` <- this_object$`self`
}
Expand Down Expand Up @@ -161,6 +182,14 @@ Special <- R6::R6Class(
#' @export
toJSONString = function() {
jsoncontent <- c(
if (!is.null(self$`set_test`)) {
sprintf(
'"set_test":
[%s]
',
paste(unlist(lapply(self$`set_test`, function(x) paste0('"', x, '"'))), collapse = ",")
)
},
if (!is.null(self$`item_self`)) {
sprintf(
'"self":
Expand Down Expand Up @@ -228,6 +257,10 @@ Special <- R6::R6Class(
#' @export
fromJSONString = function(input_json) {
this_object <- jsonlite::fromJSON(input_json)
self$`set_test` <- ApiClient$new()$deserializeObj(this_object$`set_test`, "set[character]", loadNamespace("petstore"))
if (!identical(self$`set_test`, unique(self$`set_test`))) {
stop("Error! Items in `set_test` are not unique.")
}
self$`item_self` <- this_object$`item_self`
self$`item_private` <- this_object$`item_private`
self$`item_super` <- this_object$`item_super`
Expand Down Expand Up @@ -271,6 +304,7 @@ Special <- R6::R6Class(
#' @return true if the values in all fields are valid.
#' @export
isValid = function() {

TRUE
},
#' Return a list of invalid fields (if any).
Expand All @@ -282,6 +316,7 @@ Special <- R6::R6Class(
#' @export
getInvalidFields = function() {
invalid_fields <- list()

invalid_fields
},
#' Print the object
Expand Down
1 change: 1 addition & 0 deletions samples/client/petstore/R-httr2-wrapper/docs/Special.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Describes the result of uploading an image resource
## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**set_test** | **set[character]** | | [optional]
**item_self** | **integer** | | [optional]
**item_private** | **character** | | [optional]
**item_super** | **character** | | [optional]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,33 @@ test_that("Tests validateJSON", {

})

# test set in object
test_that("Tests set in object", {
invalid_set <-
'{"self": 123, "private": "red", "super": "something", "set_test": ["1","2","2","4"]}'
expect_error(Special$new()$fromJSON(invalid_set), "Error! Items in `set_test` are not unique")

special_json <-
'{"self": 123, "private": "red", "super": "something", "set_test": ["1","2","4"]}'
# test fromJSON
special <- Special$new()$fromJSON(special_json)
expect_equal(special$item_self, 123)
expect_equal(special$item_private, "red")
expect_equal(special$item_super, "something")

# test toJSONString
expect_true(grepl('"private"', special$toJSONString()))
expect_true(grepl('"self"', special$toJSONString()))
expect_true(grepl('"super"', special$toJSONString()))
expect_equal('{"set_test":["1","2","4"],"self":123,"private":"red","super":"something"}', special$toJSONString())

# round trip test
s1 <- Special$new()$fromJSONString(special_json)
s2 <- Special$new()$fromJSONString(s1$toJSONString())
expect_equal(s1, s2)

})

# test object with special item names: self, private, super
test_that("Tests special item names", {
special_json <-
Expand All @@ -343,7 +370,7 @@ test_that("Tests special item names", {
expect_equal(special$item_private, "red")
expect_equal(special$item_super, "something")

# test toJSONString
# test toJSONString
expect_true(grepl('"private"', special$toJSONString()))
expect_true(grepl('"self"', special$toJSONString()))
expect_true(grepl('"super"', special$toJSONString()))
Expand Down
37 changes: 36 additions & 1 deletion samples/client/petstore/R-httr2/R/special.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#' @title Special
#' @description Special Class
#' @format An \code{R6Class} generator object
#' @field set_test list(character) [optional]
#' @field item_self integer [optional]
#' @field item_private character [optional]
#' @field item_super character [optional]
Expand All @@ -19,6 +20,7 @@
Special <- R6::R6Class(
"Special",
public = list(
`set_test` = NULL,
`item_self` = NULL,
`item_private` = NULL,
`item_super` = NULL,
Expand All @@ -30,6 +32,7 @@ Special <- R6::R6Class(
#' @description
#' Initialize a new Special class.
#'
#' @param set_test set_test
#' @param item_self item_self
#' @param item_private item_private
#' @param item_super item_super
Expand All @@ -39,8 +42,16 @@ Special <- R6::R6Class(
#' @param ... Other optional arguments.
#' @export
initialize = function(
`item_self` = NULL, `item_private` = NULL, `item_super` = NULL, `123_number` = NULL, `array[test]` = NULL, `empty_string` = NULL, ...
`set_test` = NULL, `item_self` = NULL, `item_private` = NULL, `item_super` = NULL, `123_number` = NULL, `array[test]` = NULL, `empty_string` = NULL, ...
) {
if (!is.null(`set_test`)) {
stopifnot(is.vector(`set_test`), length(`set_test`) != 0)
sapply(`set_test`, function(x) stopifnot(is.character(x)))
if (!identical(`set_test`, unique(`set_test`))) {
stop("Error! Items in `set_test` are not unique.")
}
self$`set_test` <- `set_test`
}
if (!is.null(`item_self`)) {
stopifnot(is.numeric(`item_self`), length(`item_self`) == 1)
self$`item_self` <- `item_self`
Expand Down Expand Up @@ -75,6 +86,10 @@ Special <- R6::R6Class(
#' @export
toJSON = function() {
SpecialObject <- list()
if (!is.null(self$`set_test`)) {
SpecialObject[["set_test"]] <-
self$`set_test`
}
if (!is.null(self$`item_self`)) {
SpecialObject[["self"]] <-
self$`item_self`
Expand Down Expand Up @@ -111,6 +126,12 @@ Special <- R6::R6Class(
#' @export
fromJSON = function(input_json) {
this_object <- jsonlite::fromJSON(input_json)
if (!is.null(this_object$`set_test`)) {
self$`set_test` <- ApiClient$new()$deserializeObj(this_object$`set_test`, "set[character]", loadNamespace("petstore"))
if (!identical(self$`set_test`, unique(self$`set_test`))) {
stop("Error! Items in `set_test` are not unique.")
}
}
if (!is.null(this_object$`self`)) {
self$`item_self` <- this_object$`self`
}
Expand Down Expand Up @@ -140,6 +161,14 @@ Special <- R6::R6Class(
#' @export
toJSONString = function() {
jsoncontent <- c(
if (!is.null(self$`set_test`)) {
sprintf(
'"set_test":
[%s]
',
paste(unlist(lapply(self$`set_test`, function(x) paste0('"', x, '"'))), collapse = ",")
)
},
if (!is.null(self$`item_self`)) {
sprintf(
'"self":
Expand Down Expand Up @@ -202,6 +231,10 @@ Special <- R6::R6Class(
#' @export
fromJSONString = function(input_json) {
this_object <- jsonlite::fromJSON(input_json)
self$`set_test` <- ApiClient$new()$deserializeObj(this_object$`set_test`, "set[character]", loadNamespace("petstore"))
if (!identical(self$`set_test`, unique(self$`set_test`))) {
stop("Error! Items in `set_test` are not unique.")
}
self$`item_self` <- this_object$`item_self`
self$`item_private` <- this_object$`item_private`
self$`item_super` <- this_object$`item_super`
Expand Down Expand Up @@ -238,6 +271,7 @@ Special <- R6::R6Class(
#' @return true if the values in all fields are valid.
#' @export
isValid = function() {

TRUE
},
#' Return a list of invalid fields (if any).
Expand All @@ -249,6 +283,7 @@ Special <- R6::R6Class(
#' @export
getInvalidFields = function() {
invalid_fields <- list()

invalid_fields
},
#' Print the object
Expand Down
1 change: 1 addition & 0 deletions samples/client/petstore/R-httr2/docs/Special.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Describes the result of uploading an image resource
## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**set_test** | **set[character]** | | [optional]
**item_self** | **integer** | | [optional]
**item_private** | **character** | | [optional]
**item_super** | **character** | | [optional]
Expand Down
Loading