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

[R][Client] catch enum classes in toJSON #18194

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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 @@ -238,7 +238,11 @@
self$`{{name}}`
{{/isPrimitiveType}}
{{^isPrimitiveType}}
self$`{{name}}`$toJSON()
if (length(names(self$`{{name}}`$toJSON())) == 0L && is.character(jsonlite::fromJSON(self$`{{name}}`$toJSON()))) {
Copy link
Member

Choose a reason for hiding this comment

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

i saw you using is.character check

Does it also cover numeric values such as 13, 5.6 ?

Copy link
Member

Choose a reason for hiding this comment

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

i saw you using is.character check

Does it also cover numeric values such as 13, 5.6 ?

Copy link
Contributor Author

@joXemMx joXemMx Mar 22, 2024

Choose a reason for hiding this comment

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

Currently it does not, as I intended. I only want to catch enum classes (as generated by modelEnum.mustache) and did not see how anything other than character would make much sense. If you want to include numeric values, I could check for jsonlite::fromJSON(self$`{{name}}`$toJSON()) %in$ c("character", "numeric", "integer", "logical", "complex") instead of using is.character.

jsonlite::fromJSON(self$`{{name}}`$toJSON())
} else {
self$`{{name}}`$toJSON()
}
{{/isPrimitiveType}}
{{/isContainer}}
}
Expand Down
6 changes: 5 additions & 1 deletion samples/client/echo_api/r/R/pet.R
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ Pet <- R6::R6Class(
}
if (!is.null(self$`category`)) {
PetObject[["category"]] <-
self$`category`$toJSON()
if (length(names(self$`category`$toJSON())) == 0L && is.character(jsonlite::fromJSON(self$`category`$toJSON()))) {
jsonlite::fromJSON(self$`category`$toJSON())
} else {
self$`category`$toJSON()
}
}
if (!is.null(self$`photoUrls`)) {
PetObject[["photoUrls"]] <-
Expand Down
6 changes: 5 additions & 1 deletion samples/client/petstore/R-httr2-wrapper/R/nested_one_of.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ NestedOneOf <- R6::R6Class(
}
if (!is.null(self$`nested_pig`)) {
NestedOneOfObject[["nested_pig"]] <-
self$`nested_pig`$toJSON()
if (length(names(self$`nested_pig`$toJSON())) == 0L && is.character(jsonlite::fromJSON(self$`nested_pig`$toJSON()))) {
jsonlite::fromJSON(self$`nested_pig`$toJSON())
} else {
self$`nested_pig`$toJSON()
}
}
for (key in names(self$additional_properties)) {
NestedOneOfObject[[key]] <- self$additional_properties[[key]]
Expand Down
6 changes: 5 additions & 1 deletion samples/client/petstore/R-httr2-wrapper/R/pet.R
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,11 @@ Pet <- R6::R6Class(
}
if (!is.null(self$`category`)) {
PetObject[["category"]] <-
self$`category`$toJSON()
if (length(names(self$`category`$toJSON())) == 0L && is.character(jsonlite::fromJSON(self$`category`$toJSON()))) {
jsonlite::fromJSON(self$`category`$toJSON())
} else {
self$`category`$toJSON()
}
}
if (!is.null(self$`name`)) {
PetObject[["name"]] <-
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ UpdatePetRequest <- R6::R6Class(
UpdatePetRequestObject <- list()
if (!is.null(self$`jsonData`)) {
UpdatePetRequestObject[["jsonData"]] <-
self$`jsonData`$toJSON()
if (length(names(self$`jsonData`$toJSON())) == 0L && is.character(jsonlite::fromJSON(self$`jsonData`$toJSON()))) {
jsonlite::fromJSON(self$`jsonData`$toJSON())
} else {
self$`jsonData`$toJSON()
}
}
if (!is.null(self$`binaryDataN2Information`)) {
UpdatePetRequestObject[["binaryDataN2Information"]] <-
Expand Down
6 changes: 5 additions & 1 deletion samples/client/petstore/R-httr2/R/nested_one_of.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ NestedOneOf <- R6::R6Class(
}
if (!is.null(self$`nested_pig`)) {
NestedOneOfObject[["nested_pig"]] <-
self$`nested_pig`$toJSON()
if (length(names(self$`nested_pig`$toJSON())) == 0L && is.character(jsonlite::fromJSON(self$`nested_pig`$toJSON()))) {
jsonlite::fromJSON(self$`nested_pig`$toJSON())
} else {
self$`nested_pig`$toJSON()
}
}
NestedOneOfObject
},
Expand Down
6 changes: 5 additions & 1 deletion samples/client/petstore/R-httr2/R/pet.R
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ Pet <- R6::R6Class(
}
if (!is.null(self$`category`)) {
PetObject[["category"]] <-
self$`category`$toJSON()
if (length(names(self$`category`$toJSON())) == 0L && is.character(jsonlite::fromJSON(self$`category`$toJSON()))) {
jsonlite::fromJSON(self$`category`$toJSON())
} else {
self$`category`$toJSON()
}
}
if (!is.null(self$`name`)) {
PetObject[["name"]] <-
Expand Down
6 changes: 5 additions & 1 deletion samples/client/petstore/R-httr2/R/update_pet_request.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ UpdatePetRequest <- R6::R6Class(
UpdatePetRequestObject <- list()
if (!is.null(self$`jsonData`)) {
UpdatePetRequestObject[["jsonData"]] <-
self$`jsonData`$toJSON()
if (length(names(self$`jsonData`$toJSON())) == 0L && is.character(jsonlite::fromJSON(self$`jsonData`$toJSON()))) {
jsonlite::fromJSON(self$`jsonData`$toJSON())
} else {
self$`jsonData`$toJSON()
}
}
if (!is.null(self$`binaryDataN2Information`)) {
UpdatePetRequestObject[["binaryDataN2Information"]] <-
Expand Down
6 changes: 5 additions & 1 deletion samples/client/petstore/R/R/nested_one_of.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ NestedOneOf <- R6::R6Class(
}
if (!is.null(self$`nested_pig`)) {
NestedOneOfObject[["nested_pig"]] <-
self$`nested_pig`$toJSON()
if (length(names(self$`nested_pig`$toJSON())) == 0L && is.character(jsonlite::fromJSON(self$`nested_pig`$toJSON()))) {
jsonlite::fromJSON(self$`nested_pig`$toJSON())
} else {
self$`nested_pig`$toJSON()
}
}
for (key in names(self$additional_properties)) {
NestedOneOfObject[[key]] <- self$additional_properties[[key]]
Expand Down
6 changes: 5 additions & 1 deletion samples/client/petstore/R/R/pet.R
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,11 @@ Pet <- R6::R6Class(
}
if (!is.null(self$`category`)) {
PetObject[["category"]] <-
self$`category`$toJSON()
if (length(names(self$`category`$toJSON())) == 0L && is.character(jsonlite::fromJSON(self$`category`$toJSON()))) {
jsonlite::fromJSON(self$`category`$toJSON())
} else {
self$`category`$toJSON()
}
}
if (!is.null(self$`name`)) {
PetObject[["name"]] <-
Expand Down
6 changes: 5 additions & 1 deletion samples/client/petstore/R/R/update_pet_request.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ UpdatePetRequest <- R6::R6Class(
UpdatePetRequestObject <- list()
if (!is.null(self$`jsonData`)) {
UpdatePetRequestObject[["jsonData"]] <-
self$`jsonData`$toJSON()
if (length(names(self$`jsonData`$toJSON())) == 0L && is.character(jsonlite::fromJSON(self$`jsonData`$toJSON()))) {
jsonlite::fromJSON(self$`jsonData`$toJSON())
} else {
self$`jsonData`$toJSON()
}
}
if (!is.null(self$`binaryDataN2Information`)) {
UpdatePetRequestObject[["binaryDataN2Information"]] <-
Expand Down
Loading