Skip to content
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
14 changes: 11 additions & 3 deletions modules/openapi-generator/src/main/resources/r/api_client.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,11 @@ ApiClient <- R6::R6Class(
return_obj <- NULL
primitive_types <- c("character", "numeric", "integer", "logical", "complex")

# for deserialization, uniqueness requirements do not matter
return_type <- gsub(pattern = "^(set|array)\\[",
replacement = "collection\\[",
x = return_type)

# To handle the "map" type
if (startsWith(return_type, "map(")) {
inner_return_type <- regmatches(return_type,
Expand All @@ -359,10 +364,10 @@ ApiClient <- R6::R6Class(
self$deserializeObj(obj[[name]], inner_return_type, pkg_env)
})
names(return_obj) <- names(obj)
} else if (startsWith(return_type, "array[")) {
# To handle the "array" type
} else if (startsWith(return_type, "collection[")) {
# To handle the "array" and "set" types
inner_return_type <- regmatches(return_type,
regexec(pattern = "array\\[(.*)\\]", return_type))[[1]][2]
regexec(pattern = "collection\\[(.*)\\]", return_type))[[1]][2]
if (c(inner_return_type) %in% primitive_types) {
return_obj <- vector("list", length = length(obj))
if (length(obj) > 0) {
Expand All @@ -371,6 +376,9 @@ ApiClient <- R6::R6Class(
}
}
} else {
if (is.list(obj) && length(obj) == 1 && is.data.frame(obj[[1]])) {
obj <- obj[[1]]
}
Comment on lines +379 to +381
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This situation arises when parsing responses that have fields that are arrays of other response models. E.g. consider a Person response with a nested family field populated with an array of Relationship objects

txt <- '[{"name": "bob", "family":[{"name":"joe", "relationship":"dad"},{"name":"sue", "relationship":"mom"}]}]'
json <- jsonlite::fromJSON(txt)
class(json$family)
#> [1] "list"
class(json$family[[1]])
#> [1] "data.frame"

Here the family field is initially parsed as a list of length 1, populated with a data.frame. This is not handled in the current code because nrow(json$family) is NULL. This is why we get some unexpected data.frame return values where we should get a list of R6 objects.

if (!is.null(nrow(obj))) {
return_obj <- vector("list", length = nrow(obj))
if (nrow(obj) > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,11 @@ ApiClient <- R6::R6Class(
return_obj <- NULL
primitive_types <- c("character", "numeric", "integer", "logical", "complex")

# for deserialization, uniqueness requirements do not matter
return_type <- gsub(pattern = "^(set|array)\\[",
replacement = "collection\\[",
x = return_type)

# To handle the "map" type
if (startsWith(return_type, "map(")) {
inner_return_type <- regmatches(return_type,
Expand All @@ -371,10 +376,10 @@ ApiClient <- R6::R6Class(
self$deserializeObj(obj[[name]], inner_return_type, pkg_env)
})
names(return_obj) <- names(obj)
} else if (startsWith(return_type, "array[")) {
# To handle the "array" type
} else if (startsWith(return_type, "collection[")) {
# To handle the "array" and "set" types
inner_return_type <- regmatches(return_type,
regexec(pattern = "array\\[(.*)\\]", return_type))[[1]][2]
regexec(pattern = "collection\\[(.*)\\]", return_type))[[1]][2]
if (c(inner_return_type) %in% primitive_types) {
return_obj <- vector("list", length = length(obj))
if (length(obj) > 0) {
Expand All @@ -383,6 +388,9 @@ ApiClient <- R6::R6Class(
}
}
} else {
if (is.list(obj) && length(obj) == 1 && is.data.frame(obj[[1]])) {
obj <- obj[[1]]
}
if (!is.null(nrow(obj))) {
return_obj <- vector("list", length = nrow(obj))
if (nrow(obj) > 0) {
Expand Down
14 changes: 11 additions & 3 deletions samples/client/echo_api/r/R/api_client.R
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,11 @@ ApiClient <- R6::R6Class(
return_obj <- NULL
primitive_types <- c("character", "numeric", "integer", "logical", "complex")

# for deserialization, uniqueness requirements do not matter
return_type <- gsub(pattern = "^(set|array)\\[",
replacement = "collection\\[",
x = return_type)

# To handle the "map" type
if (startsWith(return_type, "map(")) {
inner_return_type <- regmatches(return_type,
Expand All @@ -311,10 +316,10 @@ ApiClient <- R6::R6Class(
self$deserializeObj(obj[[name]], inner_return_type, pkg_env)
})
names(return_obj) <- names(obj)
} else if (startsWith(return_type, "array[")) {
# To handle the "array" type
} else if (startsWith(return_type, "collection[")) {
# To handle the "array" and "set" types
inner_return_type <- regmatches(return_type,
regexec(pattern = "array\\[(.*)\\]", return_type))[[1]][2]
regexec(pattern = "collection\\[(.*)\\]", return_type))[[1]][2]
if (c(inner_return_type) %in% primitive_types) {
return_obj <- vector("list", length = length(obj))
if (length(obj) > 0) {
Expand All @@ -323,6 +328,9 @@ ApiClient <- R6::R6Class(
}
}
} else {
if (is.list(obj) && length(obj) == 1 && is.data.frame(obj[[1]])) {
obj <- obj[[1]]
}
if (!is.null(nrow(obj))) {
return_obj <- vector("list", length = nrow(obj))
if (nrow(obj) > 0) {
Expand Down
14 changes: 11 additions & 3 deletions samples/client/petstore/R-httr2-wrapper/R/api_client.R
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,11 @@ ApiClient <- R6::R6Class(
return_obj <- NULL
primitive_types <- c("character", "numeric", "integer", "logical", "complex")

# for deserialization, uniqueness requirements do not matter
return_type <- gsub(pattern = "^(set|array)\\[",
replacement = "collection\\[",
x = return_type)

# To handle the "map" type
if (startsWith(return_type, "map(")) {
inner_return_type <- regmatches(return_type,
Expand All @@ -360,10 +365,10 @@ ApiClient <- R6::R6Class(
self$deserializeObj(obj[[name]], inner_return_type, pkg_env)
})
names(return_obj) <- names(obj)
} else if (startsWith(return_type, "array[")) {
# To handle the "array" type
} else if (startsWith(return_type, "collection[")) {
# To handle the "array" and "set" types
inner_return_type <- regmatches(return_type,
regexec(pattern = "array\\[(.*)\\]", return_type))[[1]][2]
regexec(pattern = "collection\\[(.*)\\]", return_type))[[1]][2]
if (c(inner_return_type) %in% primitive_types) {
return_obj <- vector("list", length = length(obj))
if (length(obj) > 0) {
Expand All @@ -372,6 +377,9 @@ ApiClient <- R6::R6Class(
}
}
} else {
if (is.list(obj) && length(obj) == 1 && is.data.frame(obj[[1]])) {
obj <- obj[[1]]
}
if (!is.null(nrow(obj))) {
return_obj <- vector("list", length = nrow(obj))
if (nrow(obj) > 0) {
Expand Down
14 changes: 11 additions & 3 deletions samples/client/petstore/R-httr2/R/api_client.R
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,11 @@ ApiClient <- R6::R6Class(
return_obj <- NULL
primitive_types <- c("character", "numeric", "integer", "logical", "complex")

# for deserialization, uniqueness requirements do not matter
return_type <- gsub(pattern = "^(set|array)\\[",
replacement = "collection\\[",
x = return_type)

# To handle the "map" type
if (startsWith(return_type, "map(")) {
inner_return_type <- regmatches(return_type,
Expand All @@ -360,10 +365,10 @@ ApiClient <- R6::R6Class(
self$deserializeObj(obj[[name]], inner_return_type, pkg_env)
})
names(return_obj) <- names(obj)
} else if (startsWith(return_type, "array[")) {
# To handle the "array" type
} else if (startsWith(return_type, "collection[")) {
# To handle the "array" and "set" types
inner_return_type <- regmatches(return_type,
regexec(pattern = "array\\[(.*)\\]", return_type))[[1]][2]
regexec(pattern = "collection\\[(.*)\\]", return_type))[[1]][2]
if (c(inner_return_type) %in% primitive_types) {
return_obj <- vector("list", length = length(obj))
if (length(obj) > 0) {
Expand All @@ -372,6 +377,9 @@ ApiClient <- R6::R6Class(
}
}
} else {
if (is.list(obj) && length(obj) == 1 && is.data.frame(obj[[1]])) {
obj <- obj[[1]]
}
if (!is.null(nrow(obj))) {
return_obj <- vector("list", length = nrow(obj))
if (nrow(obj) > 0) {
Expand Down
14 changes: 11 additions & 3 deletions samples/client/petstore/R/R/api_client.R
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,11 @@ ApiClient <- R6::R6Class(
return_obj <- NULL
primitive_types <- c("character", "numeric", "integer", "logical", "complex")

# for deserialization, uniqueness requirements do not matter
return_type <- gsub(pattern = "^(set|array)\\[",
replacement = "collection\\[",
x = return_type)

# To handle the "map" type
if (startsWith(return_type, "map(")) {
inner_return_type <- regmatches(return_type,
Expand All @@ -340,10 +345,10 @@ ApiClient <- R6::R6Class(
self$deserializeObj(obj[[name]], inner_return_type, pkg_env)
})
names(return_obj) <- names(obj)
} else if (startsWith(return_type, "array[")) {
# To handle the "array" type
} else if (startsWith(return_type, "collection[")) {
# To handle the "array" and "set" types
inner_return_type <- regmatches(return_type,
regexec(pattern = "array\\[(.*)\\]", return_type))[[1]][2]
regexec(pattern = "collection\\[(.*)\\]", return_type))[[1]][2]
if (c(inner_return_type) %in% primitive_types) {
return_obj <- vector("list", length = length(obj))
if (length(obj) > 0) {
Expand All @@ -352,6 +357,9 @@ ApiClient <- R6::R6Class(
}
}
} else {
if (is.list(obj) && length(obj) == 1 && is.data.frame(obj[[1]])) {
obj <- obj[[1]]
}
if (!is.null(nrow(obj))) {
return_obj <- vector("list", length = nrow(obj))
if (nrow(obj) > 0) {
Expand Down
Loading