-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
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] fix to-list and to-json functionality #20132
base: master
Are you sure you want to change the base?
Conversation
toJSON = function() { | ||
.Deprecated(new = "toList", msg = "Use the '$toList()' method instead since that is more learly named. Use '$toJSONstring()' to get a JSON string") | ||
return(self$toList()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I deprecated toJSON()
because it doesn't actually convert the R6 object to a JSON structure, just an R list
type. The functionality is still there in the renamed toList()
function.
jsoncontent <- paste(jsoncontent, collapse = ",") | ||
json_string <- as.character(jsonlite::minify(paste("{", jsoncontent, "}", sep = ""))) | ||
toJSONString = function(minify = TRUE, ...) { | ||
json_obj <- self$toList() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In local testing I was getting some errors from $toJSONString()
. It also seemed pretty redundant with the content in toJSON()
/toList()
. I refactored this method to use $toList()
internally and then convert the list-type to JSON using the jsonlite
library.
thanks for the PR I ran the integration tests locally but got errors:
can you pleasea take a look? to run the test, please run you will need to run the petstore server locally: https://github.com/OpenAPITools/openapi-generator/wiki/Integration-Tests |
Thanks @wing328. I had some trouble with the integration tests (my hosts file is locked down) but I think I fixed the issue. |
#' @description | ||
#' Serialize {{{classname}}} to JSON string. | ||
#' | ||
#' | ||
#' @param ... Parameters passed to `jsonlite::toJSON` | ||
#' @return JSON string representation of the {{{classname}}}. | ||
toJSONString = function() { | ||
toJSONString = function(...) { | ||
simple <- self$toSimpleType() | ||
if (!is.null(self$actual_instance)) { | ||
as.character(jsonlite::minify(self$actual_instance$toJSONString())) | ||
json <- jsonlite::toJSON(simple, auto_unbox = TRUE, ...) | ||
return(as.character(jsonlite::minify(json))) | ||
} else { | ||
NULL | ||
return(NULL) | ||
} | ||
}, | ||
#' @description | ||
#' Serialize {{{classname}}} to JSON. | ||
#' | ||
#' @return JSON representation of the {{{classname}}}. | ||
#' Convert to an R object. This method is deprecated. Use `toSimpleType()` instead. | ||
toJSON = function() { | ||
.Deprecated(new = "toSimpleType", msg = "Use the '$toSimpleType()' method instead since that is more learly named. Use '$toJSONString()' to get a JSON string") | ||
return(self$toSimpleType()) | ||
}, | ||
#' @description | ||
#' Convert {{{classname}}} to a base R type | ||
#' | ||
#' @return A base R type, e.g. a list or numeric/character array. | ||
toSimpleType = function() { | ||
if (!is.null(self$actual_instance)) { | ||
self$actual_instance$toJSON() | ||
return(self$actual_instance$toSimpleType()) | ||
} else { | ||
NULL | ||
return(NULL) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I refactored this PR a bit since a realized that not all model types necessarily get transformed to R list types enroute to JSON.
The gist is that
- I depreciated
toJSON
in favor oftoSimpleType
and made the behavior consistent (i.e., it returns an R list, or character vector, etc.). PreviouslytoJSON
might return a list or a JSON string, depending on the model type. - I added
toSimpleType
to all models - I modified
toJSONString
to usetoSimpleType()
to get the R object and then invoke thejsonlite
transformations on that object (toJSON
andminifiy
).
#' @description | ||
#' Convert to a List | ||
#' | ||
#' Convert the R6 object to a list to work more easily with other tooling. | ||
#' | ||
#' @return {{{classname}}} as a base R list. | ||
#' @examples | ||
#' # convert array of {{{classname}}} (x) to a data frame | ||
#' \dontrun{ | ||
#' df <- x |> purrr::map_dfr(\(y)y$toList()) | ||
#' df | ||
#' } | ||
toList = function() { | ||
return(self$toSimpleType()) | ||
}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for the generic model only, I also added a toList
method which just wraps toSimpleType
but
- is more clear with respect to return type
- is documented to support converting an array of R6 objects to a
data.frame
, which is how most R users prefer to operate
0e78426
to
6b9cedd
Compare
thank you. let me test and see if i've any questions. |
toJSON = function() { | ||
.Deprecated(new = "toSimpleType", msg = "Use the '$toSimpleType()' method instead since that is more learly named. Use '$toJSONString()' to get a JSON string") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mattpollock should it be "clearly named" instead of "learly named"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks - pushed a fix.
FYI. Filed follow-up PR #20196 as it fails to run under WSL (Windows). (was running fine previously on Mac) |
I'm running/testing on a Mac too - thanks for handling the windows issue. |
@Ramanth, @saigiridhar21, @wing328
This PR fixes an issue with converting R6 objects to JSON and R list types.
PR checklist
Commit all changed files.
This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
These must match the expectations made by your contribution.
You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example
./bin/generate-samples.sh bin/configs/java*
.IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
master
(upcoming7.x.0
minor release - breaking changes with fallbacks),8.0.x
(breaking changes without fallbacks)