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

Conversation

joXemMx
Copy link
Contributor

@joXemMx joXemMx commented Mar 7, 2024

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package 
    ./bin/generate-samples.sh ./bin/configs/*.yaml
    ./bin/utils/export_docs_generators.sh
    
    (For Windows users, please run the script in Git BASH)
    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.
  • File the PR against the correct branch: master (upcoming 7.1.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

ApiResponse fix:
The current code for response_to_text is:

response_as_text = function(from_encoding = NULL, 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.")
      }
      return(text_response)
    }

But from_encoding = NULL is invalid and produces an error, as only strings are allowed, see iconv docs https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/iconv. I changed it to an empty string, which works as intended:

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.")
      }
      return(text_response)
    }

JSON for maps fix:
Currently maps are generated like

"supportedILPSolvers": "{
        "CLP": "2.10.8.2",
        "GUROBI": "9.1.1",
        "CPLEX": "12.7.1"
    }"

by code following this pattern (actual snippet of my generated code):

if (!is.null(self$`supportedILPSolvers`)) {
          sprintf(
          '"supportedILPSolvers":
            "%s"
          ',
          jsonlite::toJSON(lapply(self$`supportedILPSolvers`, function(x){ x }), auto_unbox = TRUE, digits = NA)
          )
        }

which is wrong JSON and produces an error in the classes toJSONString function.
I simply changed the template to produce %s instead of "%s" for maps, which results in perfect JSON and removes the error. Resulting JSONs look like

"supportedILPSolvers": {
        "CLP": "2.10.8.2",
        "GUROBI": "9.1.1",
        "CPLEX": "12.7.1"
    }

httr2 api_client handling empty response bodies:
As it is currently, an empty response body will lead to

Backtrace:
    ▆
 1. ├─api_instance$CloseProjectSpace(project_id)
 2. │ └─self$CloseProjectSpaceWithHttpInfo(project_id, ...) at generated/R/projects_api.R:492:6
 3. │   └─self$api_client$CallApi(...) at generated/R/projects_api.R:539:6
 4. │     └─self$Execute(...) at generated/R/api_client.R:153:6
 5. │       └─resp %>% resp_body_raw() at generated/R/api_client.R:263:8
 6. └─httr2::resp_body_raw(.)
 7.   └─rlang::abort("Can not retrieve empty body")

in the httr2 api_client.R. I simply rewrote this to take the value of NULL instead of calling resp %>% resp_body_raw() when the response body is empty.

Mentioning technical staff: @Ramanth @saigiridhar21

@joXemMx joXemMx changed the title [R][Client] Fix api response and JSON for maps [R][Client] Fix api response, JSON for maps and let httr2 api client handle empty response bodies Mar 8, 2024
@@ -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.

@wing328 wing328 added this to the 7.5.0 milestone Mar 13, 2024
@wing328
Copy link
Member

wing328 commented Mar 13, 2024

lgtm. lets give it a try

@wing328 wing328 merged commit 6075b8a into OpenAPITools:master Mar 13, 2024
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants