forked from OpenAPITools/openapi-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[R][Client] Fix api response, JSON for maps and let httr2 api client …
…handle empty response bodies (OpenAPITools#18049) * changed files from rebuilding project and updating samples * change default value of from_encoding to empty string - from_encoding = NULL is invalid and produces an error, as only strings are allowed, see https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/iconv - defaulting to an empty string fixes this issue * fix map being surrounded by quotes in json * allow httr2 client to deal with empty response body * changed files from rebuilding project and updating samples * added PetMap schema for test of correct map serialization in toJSONString and regenerated samples
- Loading branch information
Showing
28 changed files
with
663 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
#' Create a new PetMap | ||
#' | ||
#' @description | ||
#' A mock map of a pet and some properties | ||
#' | ||
#' @docType class | ||
#' @title PetMap | ||
#' @description PetMap Class | ||
#' @format An \code{R6Class} generator object | ||
#' @field pet named list(character) [optional] | ||
#' @field _field_list a list of fields list(character) | ||
#' @field additional_properties additional properties list(character) [optional] | ||
#' @importFrom R6 R6Class | ||
#' @importFrom jsonlite fromJSON toJSON | ||
#' @export | ||
PetMap <- R6::R6Class( | ||
"PetMap", | ||
public = list( | ||
`pet` = NULL, | ||
`_field_list` = c("pet"), | ||
`additional_properties` = list(), | ||
#' Initialize a new PetMap class. | ||
#' | ||
#' @description | ||
#' Initialize a new PetMap class. | ||
#' | ||
#' @param pet pet | ||
#' @param additional_properties additional properties (optional) | ||
#' @param ... Other optional arguments. | ||
#' @export | ||
initialize = function(`pet` = NULL, additional_properties = NULL, ...) { | ||
if (!is.null(`pet`)) { | ||
stopifnot(is.vector(`pet`), length(`pet`) != 0) | ||
sapply(`pet`, function(x) stopifnot(is.character(x))) | ||
self$`pet` <- `pet` | ||
} | ||
if (!is.null(additional_properties)) { | ||
for (key in names(additional_properties)) { | ||
self$additional_properties[[key]] <- additional_properties[[key]] | ||
} | ||
} | ||
}, | ||
#' To JSON string | ||
#' | ||
#' @description | ||
#' To JSON String | ||
#' | ||
#' @return PetMap in JSON format | ||
#' @export | ||
toJSON = function() { | ||
PetMapObject <- list() | ||
if (!is.null(self$`pet`)) { | ||
PetMapObject[["pet"]] <- | ||
self$`pet` | ||
} | ||
for (key in names(self$additional_properties)) { | ||
PetMapObject[[key]] <- self$additional_properties[[key]] | ||
} | ||
|
||
PetMapObject | ||
}, | ||
#' Deserialize JSON string into an instance of PetMap | ||
#' | ||
#' @description | ||
#' Deserialize JSON string into an instance of PetMap | ||
#' | ||
#' @param input_json the JSON input | ||
#' @return the instance of PetMap | ||
#' @export | ||
fromJSON = function(input_json) { | ||
this_object <- jsonlite::fromJSON(input_json) | ||
if (!is.null(this_object$`pet`)) { | ||
self$`pet` <- ApiClient$new()$deserializeObj(this_object$`pet`, "map(character)", loadNamespace("petstore")) | ||
} | ||
# process additional properties/fields in the payload | ||
for (key in names(this_object)) { | ||
if (!(key %in% self$`_field_list`)) { # json key not in list of fields | ||
self$additional_properties[[key]] <- this_object[[key]] | ||
} | ||
} | ||
|
||
self | ||
}, | ||
#' To JSON string | ||
#' | ||
#' @description | ||
#' To JSON String | ||
#' | ||
#' @return PetMap in JSON format | ||
#' @export | ||
toJSONString = function() { | ||
jsoncontent <- c( | ||
if (!is.null(self$`pet`)) { | ||
sprintf( | ||
'"pet": | ||
%s | ||
', | ||
jsonlite::toJSON(lapply(self$`pet`, function(x){ x }), auto_unbox = TRUE, digits = NA) | ||
) | ||
} | ||
) | ||
jsoncontent <- paste(jsoncontent, collapse = ",") | ||
json_string <- as.character(jsonlite::minify(paste("{", jsoncontent, "}", sep = ""))) | ||
json_obj <- jsonlite::fromJSON(json_string) | ||
for (key in names(self$additional_properties)) { | ||
json_obj[[key]] <- self$additional_properties[[key]] | ||
} | ||
json_string <- as.character(jsonlite::minify(jsonlite::toJSON(json_obj, auto_unbox = TRUE, digits = NA))) | ||
}, | ||
#' Deserialize JSON string into an instance of PetMap | ||
#' | ||
#' @description | ||
#' Deserialize JSON string into an instance of PetMap | ||
#' | ||
#' @param input_json the JSON input | ||
#' @return the instance of PetMap | ||
#' @export | ||
fromJSONString = function(input_json) { | ||
this_object <- jsonlite::fromJSON(input_json) | ||
self$`pet` <- ApiClient$new()$deserializeObj(this_object$`pet`, "map(character)", loadNamespace("petstore")) | ||
# process additional properties/fields in the payload | ||
for (key in names(this_object)) { | ||
if (!(key %in% self$`_field_list`)) { # json key not in list of fields | ||
self$additional_properties[[key]] <- this_object[[key]] | ||
} | ||
} | ||
|
||
self | ||
}, | ||
#' Validate JSON input with respect to PetMap | ||
#' | ||
#' @description | ||
#' Validate JSON input with respect to PetMap and throw an exception if invalid | ||
#' | ||
#' @param input the JSON input | ||
#' @export | ||
validateJSON = function(input) { | ||
input_json <- jsonlite::fromJSON(input) | ||
}, | ||
#' To string (JSON format) | ||
#' | ||
#' @description | ||
#' To string (JSON format) | ||
#' | ||
#' @return String representation of PetMap | ||
#' @export | ||
toString = function() { | ||
self$toJSONString() | ||
}, | ||
#' Return true if the values in all fields are valid. | ||
#' | ||
#' @description | ||
#' Return true if the values in all fields are valid. | ||
#' | ||
#' @return true if the values in all fields are valid. | ||
#' @export | ||
isValid = function() { | ||
TRUE | ||
}, | ||
#' Return a list of invalid fields (if any). | ||
#' | ||
#' @description | ||
#' Return a list of invalid fields (if any). | ||
#' | ||
#' @return A list of invalid fields (if any). | ||
#' @export | ||
getInvalidFields = function() { | ||
invalid_fields <- list() | ||
invalid_fields | ||
}, | ||
#' Print the object | ||
#' | ||
#' @description | ||
#' Print the object | ||
#' | ||
#' @export | ||
print = function() { | ||
print(jsonlite::prettify(self$toJSONString())) | ||
invisible(self) | ||
} | ||
), | ||
# Lock the class to prevent modifications to the method or field | ||
lock_class = TRUE | ||
) | ||
## Uncomment below to unlock the class to allow modifications of the method or field | ||
# PetMap$unlock() | ||
# | ||
## Below is an example to define the print function | ||
# PetMap$set("public", "print", function(...) { | ||
# print(jsonlite::prettify(self$toJSONString())) | ||
# invisible(self) | ||
# }) | ||
## Uncomment below to lock the class to prevent modifications to the method or field | ||
# PetMap$lock() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# petstore::PetMap | ||
|
||
A mock map of a pet and some properties | ||
|
||
## Properties | ||
Name | Type | Description | Notes | ||
------------ | ------------- | ------------- | ------------- | ||
**pet** | **map(character)** | | [optional] | ||
|
||
|
13 changes: 13 additions & 0 deletions
13
samples/client/petstore/R-httr2-wrapper/tests/testthat/test_pet_map.R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Automatically generated by openapi-generator (https://openapi-generator.tech) | ||
# Please update as you see appropriate | ||
|
||
context("Test PetMap") | ||
|
||
model_instance <- PetMap$new() | ||
|
||
test_that("pet", { | ||
# tests for the property `pet` (map(character)) | ||
|
||
# uncomment below to test the property | ||
#expect_equal(model.instance$`pet`, "EXPECTED_RESULT") | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.