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] Fix api response, JSON for maps and let httr2 api client handle empty response bodies #18049

Merged
merged 6 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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 @@ -49,7 +49,7 @@ ApiResponse <- R6::R6Class(
#' @param from_encoding The encoding of the raw response.
#' @param to_encoding The target encoding of the return value.
#' @export
response_as_text = function(from_encoding = NULL, to_encoding = "UTF-8") {
response_as_text = function(from_encoding = "", to_encoding = "UTF-8") {
text_response <- iconv(readBin(self$response, character()), from = from_encoding, to = to_encoding)
if (is.na(text_response)) {
warning("The response is binary and will not be converted to text.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,11 @@ ApiClient <- R6::R6Class(
api_response <- ApiResponse$new()
api_response$status_code <- resp %>% resp_status()
api_response$status_code_desc <- resp %>% resp_status_desc()
api_response$response <- resp %>% resp_body_raw()
if (length(resp$body) == 0) {
api_response$response <- NULL
} else {
api_response$response <- resp %>% resp_body_raw()
}
api_response$headers <- resp %>% resp_headers()

api_response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@
{{/isArray}}
{{#isMap}}
{{#isPrimitiveType}}
{{#isNumeric}}%d{{/isNumeric}}{{^isNumeric}}{{^isBoolean}}"{{/isBoolean}}%s{{^isBoolean}}"{{/isBoolean}}{{/isNumeric}}
{{#isNumeric}}%d{{/isNumeric}}{{^isNumeric}}{{^isBoolean}}{{/isBoolean}}%s{{^isBoolean}}{{/isBoolean}}{{/isNumeric}}
Copy link
Member

Choose a reason for hiding this comment

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

thanks for the PR. for this change, can we add a test model in the test spec to ensure it's tested moving forward?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please clarify what you need, or link a PR that did something similar. I would be glad to contribute some testing!

Copy link
Member

Choose a reason for hiding this comment

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

please update modules/openapi-generator/src/test/resources/3_0/r/petstore.yaml with test schemas (models) to ensure the above change works as expected (and then regenerate the samples)

Copy link
Contributor Author

@joXemMx joXemMx Mar 12, 2024

Choose a reason for hiding this comment

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

I added a minimal map schema that leads to the generation of the toJSONString behavior I mentioned above. Please see if this is what you wanted.

{{/isPrimitiveType}}
{{^isPrimitiveType}}%s
{{/isPrimitiveType}}
Expand Down
2 changes: 1 addition & 1 deletion samples/client/echo_api/r/R/api_response.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ ApiResponse <- R6::R6Class(
#' @param from_encoding The encoding of the raw response.
#' @param to_encoding The target encoding of the return value.
#' @export
response_as_text = function(from_encoding = NULL, to_encoding = "UTF-8") {
response_as_text = function(from_encoding = "", to_encoding = "UTF-8") {
text_response <- iconv(readBin(self$response, character()), from = from_encoding, to = to_encoding)
if (is.na(text_response)) {
warning("The response is binary and will not be converted to text.")
Expand Down
6 changes: 5 additions & 1 deletion samples/client/petstore/R-httr2-wrapper/R/api_client.R
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,11 @@ ApiClient <- R6::R6Class(
api_response <- ApiResponse$new()
api_response$status_code <- resp %>% resp_status()
api_response$status_code_desc <- resp %>% resp_status_desc()
api_response$response <- resp %>% resp_body_raw()
if (length(resp$body) == 0) {
api_response$response <- NULL
} else {
api_response$response <- resp %>% resp_body_raw()
}
api_response$headers <- resp %>% resp_headers()

api_response
Expand Down
2 changes: 1 addition & 1 deletion samples/client/petstore/R-httr2-wrapper/R/api_response.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ ApiResponse <- R6::R6Class(
#' @param from_encoding The encoding of the raw response.
#' @param to_encoding The target encoding of the return value.
#' @export
response_as_text = function(from_encoding = NULL, to_encoding = "UTF-8") {
response_as_text = function(from_encoding = "", to_encoding = "UTF-8") {
text_response <- iconv(readBin(self$response, character()), from = from_encoding, to = to_encoding)
if (is.na(text_response)) {
warning("The response is binary and will not be converted to text.")
Expand Down
6 changes: 5 additions & 1 deletion samples/client/petstore/R-httr2/R/api_client.R
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,11 @@ ApiClient <- R6::R6Class(
api_response <- ApiResponse$new()
api_response$status_code <- resp %>% resp_status()
api_response$status_code_desc <- resp %>% resp_status_desc()
api_response$response <- resp %>% resp_body_raw()
if (length(resp$body) == 0) {
api_response$response <- NULL
} else {
api_response$response <- resp %>% resp_body_raw()
}
api_response$headers <- resp %>% resp_headers()

api_response
Expand Down
2 changes: 1 addition & 1 deletion samples/client/petstore/R-httr2/R/api_response.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ ApiResponse <- R6::R6Class(
#' @param from_encoding The encoding of the raw response.
#' @param to_encoding The target encoding of the return value.
#' @export
response_as_text = function(from_encoding = NULL, to_encoding = "UTF-8") {
response_as_text = function(from_encoding = "", to_encoding = "UTF-8") {
text_response <- iconv(readBin(self$response, character()), from = from_encoding, to = to_encoding)
if (is.na(text_response)) {
warning("The response is binary and will not be converted to text.")
Expand Down
2 changes: 1 addition & 1 deletion samples/client/petstore/R/R/api_response.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ ApiResponse <- R6::R6Class(
#' @param from_encoding The encoding of the raw response.
#' @param to_encoding The target encoding of the return value.
#' @export
response_as_text = function(from_encoding = NULL, to_encoding = "UTF-8") {
response_as_text = function(from_encoding = "", to_encoding = "UTF-8") {
text_response <- iconv(readBin(self$response, character()), from = from_encoding, to = to_encoding)
if (is.na(text_response)) {
warning("The response is binary and will not be converted to text.")
Expand Down
Loading