Skip to content

Commit 943d8bf

Browse files
authored
Add set support to model properties in R client (#13443)
* add set support to model properties in r client * update samples
1 parent 26ebde4 commit 943d8bf

File tree

9 files changed

+184
-27
lines changed

9 files changed

+184
-27
lines changed

modules/openapi-generator/src/main/resources/r/modelGeneric.mustache

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,15 @@
8181
{{/isPrimitiveType}}
8282
{{/isContainer}}
8383
{{#isContainer}}
84-
{{#isPrimitiveType}}
8584
stopifnot(is.vector(`{{name}}`), length(`{{name}}`) != 0)
86-
sapply(`{{name}}`, function(x) stopifnot(is.character(x)))
87-
{{/isPrimitiveType}}
88-
{{^isPrimitiveType}}
89-
stopifnot(is.vector(`{{name}}`), length(`{{name}}`) != 0)
90-
sapply(`{{name}}`, function(x) stopifnot(R6::is.R6(x)))
91-
{{/isPrimitiveType}}
85+
sapply(`{{name}}`, function(x) stopifnot({{#isPrimitiveType}}is.character(x){{/isPrimitiveType}}{{^isPrimitiveType}}R6::is.R6(x){{/isPrimitiveType}}))
86+
{{#isArray}}
87+
{{#uniqueItems}}
88+
if (!identical(`{{name}}`, unique(`{{name}}`))) {
89+
stop("Error! Items in `{{name}}` are not unique.")
90+
}
91+
{{/uniqueItems}}
92+
{{/isArray}}
9293
{{/isContainer}}
9394
self$`{{name}}` <- `{{name}}`
9495
}
@@ -125,14 +126,15 @@
125126
{{/isPrimitiveType}}
126127
{{/isContainer}}
127128
{{#isContainer}}
128-
{{#isPrimitiveType}}
129-
stopifnot(is.vector(`{{name}}`), length(`{{name}}`) != 0)
130-
sapply(`{{name}}`, function(x) stopifnot(is.character(x)))
131-
{{/isPrimitiveType}}
132-
{{^isPrimitiveType}}
133129
stopifnot(is.vector(`{{name}}`), length(`{{name}}`) != 0)
134-
sapply(`{{name}}`, function(x) stopifnot(R6::is.R6(x)))
135-
{{/isPrimitiveType}}
130+
sapply(`{{name}}`, function(x) stopifnot({{#isPrimitiveType}}is.character(x){{/isPrimitiveType}}{{^isPrimitiveType}}R6::is.R6(x){{/isPrimitiveType}}))
131+
{{#isArray}}
132+
{{#uniqueItems}}
133+
if (!identical(`{{name}}`, unique(`{{name}}`))) {
134+
stop("Error! Items in `{{name}}` are not unique.")
135+
}
136+
{{/uniqueItems}}
137+
{{/isArray}}
136138
{{/isContainer}}
137139
self$`{{name}}` <- `{{name}}`
138140
}
@@ -207,6 +209,13 @@
207209
if (!is.null(this_object$`{{baseName}}`)) {
208210
{{#isContainer}}
209211
self$`{{name}}` <- ApiClient$new()$deserializeObj(this_object$`{{baseName}}`, "{{dataType}}", loadNamespace("{{packageName}}"))
212+
{{#isArray}}
213+
{{#uniqueItems}}
214+
if (!identical(self$`{{name}}`, unique(self$`{{name}}`))) {
215+
stop("Error! Items in `{{name}}` are not unique.")
216+
}
217+
{{/uniqueItems}}
218+
{{/isArray}}
210219
{{/isContainer}}
211220
{{^isContainer}}
212221
{{#isPrimitiveType}}
@@ -321,6 +330,13 @@
321330
{{! AAPI - added condition for handling container type of parameters, map and array}}
322331
{{#isContainer}}
323332
self$`{{name}}` <- ApiClient$new()$deserializeObj(this_object$`{{name}}`, "{{dataType}}", loadNamespace("{{packageName}}"))
333+
{{#isArray}}
334+
{{#uniqueItems}}
335+
if (!identical(self$`{{name}}`, unique(self$`{{name}}`))) {
336+
stop("Error! Items in `{{name}}` are not unique.")
337+
}
338+
{{/uniqueItems}}
339+
{{/isArray}}
324340
{{/isContainer}}
325341
{{^isContainer}}
326342
{{#isPrimitiveType}}
@@ -384,14 +400,15 @@
384400
{{/isPrimitiveType}}
385401
{{/isContainer}}
386402
{{#isContainer}}
387-
{{#isPrimitiveType}}
388403
stopifnot(is.vector(input_json$`{{name}}`), length(input_json$`{{name}}`) != 0)
389-
tmp <- sapply(input_json$`{{name}}`, function(x) stopifnot(is.character(x)))
390-
{{/isPrimitiveType}}
391-
{{^isPrimitiveType}}
392-
stopifnot(is.vector(input_json$`{{name}}`), length(json_input$`{{name}}`) != 0)
393-
tmp <- sapply(input_json$`{{name}}`, function(x) stopifnot(R6::is.R6(x)))
394-
{{/isPrimitiveType}}
404+
{{#isArray}}
405+
{{#uniqueItems}}
406+
if (!identical(input_json$`{{name}}`, unique(input_json$`{{name}}`))) {
407+
stop("Error! Items in `{{name}}` are not unique.")
408+
}
409+
{{/uniqueItems}}
410+
{{/isArray}}
411+
tmp <- sapply(input_json$`{{name}}`, function(x) stopifnot({{#isPrimitiveType}}is.character(x){{/isPrimitiveType}}{{^isPrimitiveType}}R6::is.R6(x){{/isPrimitiveType}}))
395412
{{/isContainer}}
396413
} else {
397414
stop(paste("The JSON input `", input, "` is invalid for {{classname}}: the required field `{{name}}` is missing."))

modules/openapi-generator/src/test/resources/3_0/r/petstore.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -905,6 +905,11 @@ components:
905905
description: Describes the result of uploading an image resource
906906
type: object
907907
properties:
908+
set_test:
909+
uniqueItems: true
910+
type: array
911+
items:
912+
type: string
908913
self:
909914
type: integer
910915
format: int32

samples/client/petstore/R-httr2-wrapper/R/special.R

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#' @title Special
88
#' @description Special Class
99
#' @format An \code{R6Class} generator object
10+
#' @field set_test list(character) [optional]
1011
#' @field item_self integer [optional]
1112
#' @field item_private character [optional]
1213
#' @field item_super character [optional]
@@ -21,19 +22,21 @@
2122
Special <- R6::R6Class(
2223
"Special",
2324
public = list(
25+
`set_test` = NULL,
2426
`item_self` = NULL,
2527
`item_private` = NULL,
2628
`item_super` = NULL,
2729
`123_number` = NULL,
2830
`array[test]` = NULL,
2931
`empty_string` = NULL,
30-
`_field_list` = c("item_self", "item_private", "item_super", "123_number", "array[test]", "empty_string"),
32+
`_field_list` = c("set_test", "item_self", "item_private", "item_super", "123_number", "array[test]", "empty_string"),
3133
`additional_properties` = list(),
3234
#' Initialize a new Special class.
3335
#'
3436
#' @description
3537
#' Initialize a new Special class.
3638
#'
39+
#' @param set_test set_test
3740
#' @param item_self item_self
3841
#' @param item_private item_private
3942
#' @param item_super item_super
@@ -44,8 +47,16 @@ Special <- R6::R6Class(
4447
#' @param ... Other optional arguments.
4548
#' @export
4649
initialize = function(
47-
`item_self` = NULL, `item_private` = NULL, `item_super` = NULL, `123_number` = NULL, `array[test]` = NULL, `empty_string` = NULL, additional_properties = NULL, ...
50+
`set_test` = NULL, `item_self` = NULL, `item_private` = NULL, `item_super` = NULL, `123_number` = NULL, `array[test]` = NULL, `empty_string` = NULL, additional_properties = NULL, ...
4851
) {
52+
if (!is.null(`set_test`)) {
53+
stopifnot(is.vector(`set_test`), length(`set_test`) != 0)
54+
sapply(`set_test`, function(x) stopifnot(is.character(x)))
55+
if (!identical(`set_test`, unique(`set_test`))) {
56+
stop("Error! Items in `set_test` are not unique.")
57+
}
58+
self$`set_test` <- `set_test`
59+
}
4960
if (!is.null(`item_self`)) {
5061
stopifnot(is.numeric(`item_self`), length(`item_self`) == 1)
5162
self$`item_self` <- `item_self`
@@ -85,6 +96,10 @@ Special <- R6::R6Class(
8596
#' @export
8697
toJSON = function() {
8798
SpecialObject <- list()
99+
if (!is.null(self$`set_test`)) {
100+
SpecialObject[["set_test"]] <-
101+
self$`set_test`
102+
}
88103
if (!is.null(self$`item_self`)) {
89104
SpecialObject[["self"]] <-
90105
self$`item_self`
@@ -125,6 +140,12 @@ Special <- R6::R6Class(
125140
#' @export
126141
fromJSON = function(input_json) {
127142
this_object <- jsonlite::fromJSON(input_json)
143+
if (!is.null(this_object$`set_test`)) {
144+
self$`set_test` <- ApiClient$new()$deserializeObj(this_object$`set_test`, "set[character]", loadNamespace("petstore"))
145+
if (!identical(self$`set_test`, unique(self$`set_test`))) {
146+
stop("Error! Items in `set_test` are not unique.")
147+
}
148+
}
128149
if (!is.null(this_object$`self`)) {
129150
self$`item_self` <- this_object$`self`
130151
}
@@ -161,6 +182,14 @@ Special <- R6::R6Class(
161182
#' @export
162183
toJSONString = function() {
163184
jsoncontent <- c(
185+
if (!is.null(self$`set_test`)) {
186+
sprintf(
187+
'"set_test":
188+
[%s]
189+
',
190+
paste(unlist(lapply(self$`set_test`, function(x) paste0('"', x, '"'))), collapse = ",")
191+
)
192+
},
164193
if (!is.null(self$`item_self`)) {
165194
sprintf(
166195
'"self":
@@ -228,6 +257,10 @@ Special <- R6::R6Class(
228257
#' @export
229258
fromJSONString = function(input_json) {
230259
this_object <- jsonlite::fromJSON(input_json)
260+
self$`set_test` <- ApiClient$new()$deserializeObj(this_object$`set_test`, "set[character]", loadNamespace("petstore"))
261+
if (!identical(self$`set_test`, unique(self$`set_test`))) {
262+
stop("Error! Items in `set_test` are not unique.")
263+
}
231264
self$`item_self` <- this_object$`item_self`
232265
self$`item_private` <- this_object$`item_private`
233266
self$`item_super` <- this_object$`item_super`
@@ -271,6 +304,7 @@ Special <- R6::R6Class(
271304
#' @return true if the values in all fields are valid.
272305
#' @export
273306
isValid = function() {
307+
274308
TRUE
275309
},
276310
#' Return a list of invalid fields (if any).
@@ -282,6 +316,7 @@ Special <- R6::R6Class(
282316
#' @export
283317
getInvalidFields = function() {
284318
invalid_fields <- list()
319+
285320
invalid_fields
286321
},
287322
#' Print the object

samples/client/petstore/R-httr2-wrapper/docs/Special.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Describes the result of uploading an image resource
55
## Properties
66
Name | Type | Description | Notes
77
------------ | ------------- | ------------- | -------------
8+
**set_test** | **set[character]** | | [optional]
89
**item_self** | **integer** | | [optional]
910
**item_private** | **character** | | [optional]
1011
**item_super** | **character** | | [optional]

samples/client/petstore/R-httr2-wrapper/tests/testthat/test_petstore.R

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,33 @@ test_that("Tests validateJSON", {
332332

333333
})
334334

335+
# test set in object
336+
test_that("Tests set in object", {
337+
invalid_set <-
338+
'{"self": 123, "private": "red", "super": "something", "set_test": ["1","2","2","4"]}'
339+
expect_error(Special$new()$fromJSON(invalid_set), "Error! Items in `set_test` are not unique")
340+
341+
special_json <-
342+
'{"self": 123, "private": "red", "super": "something", "set_test": ["1","2","4"]}'
343+
# test fromJSON
344+
special <- Special$new()$fromJSON(special_json)
345+
expect_equal(special$item_self, 123)
346+
expect_equal(special$item_private, "red")
347+
expect_equal(special$item_super, "something")
348+
349+
# test toJSONString
350+
expect_true(grepl('"private"', special$toJSONString()))
351+
expect_true(grepl('"self"', special$toJSONString()))
352+
expect_true(grepl('"super"', special$toJSONString()))
353+
expect_equal('{"set_test":["1","2","4"],"self":123,"private":"red","super":"something"}', special$toJSONString())
354+
355+
# round trip test
356+
s1 <- Special$new()$fromJSONString(special_json)
357+
s2 <- Special$new()$fromJSONString(s1$toJSONString())
358+
expect_equal(s1, s2)
359+
360+
})
361+
335362
# test object with special item names: self, private, super
336363
test_that("Tests special item names", {
337364
special_json <-
@@ -343,7 +370,7 @@ test_that("Tests special item names", {
343370
expect_equal(special$item_private, "red")
344371
expect_equal(special$item_super, "something")
345372

346-
# test toJSONString
373+
# test toJSONString
347374
expect_true(grepl('"private"', special$toJSONString()))
348375
expect_true(grepl('"self"', special$toJSONString()))
349376
expect_true(grepl('"super"', special$toJSONString()))

samples/client/petstore/R-httr2/R/special.R

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#' @title Special
88
#' @description Special Class
99
#' @format An \code{R6Class} generator object
10+
#' @field set_test list(character) [optional]
1011
#' @field item_self integer [optional]
1112
#' @field item_private character [optional]
1213
#' @field item_super character [optional]
@@ -19,6 +20,7 @@
1920
Special <- R6::R6Class(
2021
"Special",
2122
public = list(
23+
`set_test` = NULL,
2224
`item_self` = NULL,
2325
`item_private` = NULL,
2426
`item_super` = NULL,
@@ -30,6 +32,7 @@ Special <- R6::R6Class(
3032
#' @description
3133
#' Initialize a new Special class.
3234
#'
35+
#' @param set_test set_test
3336
#' @param item_self item_self
3437
#' @param item_private item_private
3538
#' @param item_super item_super
@@ -39,8 +42,16 @@ Special <- R6::R6Class(
3942
#' @param ... Other optional arguments.
4043
#' @export
4144
initialize = function(
42-
`item_self` = NULL, `item_private` = NULL, `item_super` = NULL, `123_number` = NULL, `array[test]` = NULL, `empty_string` = NULL, ...
45+
`set_test` = NULL, `item_self` = NULL, `item_private` = NULL, `item_super` = NULL, `123_number` = NULL, `array[test]` = NULL, `empty_string` = NULL, ...
4346
) {
47+
if (!is.null(`set_test`)) {
48+
stopifnot(is.vector(`set_test`), length(`set_test`) != 0)
49+
sapply(`set_test`, function(x) stopifnot(is.character(x)))
50+
if (!identical(`set_test`, unique(`set_test`))) {
51+
stop("Error! Items in `set_test` are not unique.")
52+
}
53+
self$`set_test` <- `set_test`
54+
}
4455
if (!is.null(`item_self`)) {
4556
stopifnot(is.numeric(`item_self`), length(`item_self`) == 1)
4657
self$`item_self` <- `item_self`
@@ -75,6 +86,10 @@ Special <- R6::R6Class(
7586
#' @export
7687
toJSON = function() {
7788
SpecialObject <- list()
89+
if (!is.null(self$`set_test`)) {
90+
SpecialObject[["set_test"]] <-
91+
self$`set_test`
92+
}
7893
if (!is.null(self$`item_self`)) {
7994
SpecialObject[["self"]] <-
8095
self$`item_self`
@@ -111,6 +126,12 @@ Special <- R6::R6Class(
111126
#' @export
112127
fromJSON = function(input_json) {
113128
this_object <- jsonlite::fromJSON(input_json)
129+
if (!is.null(this_object$`set_test`)) {
130+
self$`set_test` <- ApiClient$new()$deserializeObj(this_object$`set_test`, "set[character]", loadNamespace("petstore"))
131+
if (!identical(self$`set_test`, unique(self$`set_test`))) {
132+
stop("Error! Items in `set_test` are not unique.")
133+
}
134+
}
114135
if (!is.null(this_object$`self`)) {
115136
self$`item_self` <- this_object$`self`
116137
}
@@ -140,6 +161,14 @@ Special <- R6::R6Class(
140161
#' @export
141162
toJSONString = function() {
142163
jsoncontent <- c(
164+
if (!is.null(self$`set_test`)) {
165+
sprintf(
166+
'"set_test":
167+
[%s]
168+
',
169+
paste(unlist(lapply(self$`set_test`, function(x) paste0('"', x, '"'))), collapse = ",")
170+
)
171+
},
143172
if (!is.null(self$`item_self`)) {
144173
sprintf(
145174
'"self":
@@ -202,6 +231,10 @@ Special <- R6::R6Class(
202231
#' @export
203232
fromJSONString = function(input_json) {
204233
this_object <- jsonlite::fromJSON(input_json)
234+
self$`set_test` <- ApiClient$new()$deserializeObj(this_object$`set_test`, "set[character]", loadNamespace("petstore"))
235+
if (!identical(self$`set_test`, unique(self$`set_test`))) {
236+
stop("Error! Items in `set_test` are not unique.")
237+
}
205238
self$`item_self` <- this_object$`item_self`
206239
self$`item_private` <- this_object$`item_private`
207240
self$`item_super` <- this_object$`item_super`
@@ -238,6 +271,7 @@ Special <- R6::R6Class(
238271
#' @return true if the values in all fields are valid.
239272
#' @export
240273
isValid = function() {
274+
241275
TRUE
242276
},
243277
#' Return a list of invalid fields (if any).
@@ -249,6 +283,7 @@ Special <- R6::R6Class(
249283
#' @export
250284
getInvalidFields = function() {
251285
invalid_fields <- list()
286+
252287
invalid_fields
253288
},
254289
#' Print the object

samples/client/petstore/R-httr2/docs/Special.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Describes the result of uploading an image resource
55
## Properties
66
Name | Type | Description | Notes
77
------------ | ------------- | ------------- | -------------
8+
**set_test** | **set[character]** | | [optional]
89
**item_self** | **integer** | | [optional]
910
**item_private** | **character** | | [optional]
1011
**item_super** | **character** | | [optional]

0 commit comments

Comments
 (0)