From 247574aa0c16b77b04bb9a43fd6bd1bef4a04535 Mon Sep 17 00:00:00 2001 From: William Cheng Date: Fri, 5 Aug 2022 14:09:24 +0800 Subject: [PATCH] fix assignment, clean up tests, use !!! operator (#13075) --- .../r/libraries/httr2/api_client.mustache | 23 ++++++--------- .../client/petstore/R-httr2/R/api_client.R | 23 ++++++--------- .../R-httr2/tests/testthat/test_petstore.R | 29 +++---------------- 3 files changed, 22 insertions(+), 53 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/r/libraries/httr2/api_client.mustache b/modules/openapi-generator/src/main/resources/r/libraries/httr2/api_client.mustache index 035161994bc..1a76b56d198 100644 --- a/modules/openapi-generator/src/main/resources/r/libraries/httr2/api_client.mustache +++ b/modules/openapi-generator/src/main/resources/r/libraries/httr2/api_client.mustache @@ -189,34 +189,29 @@ ApiClient <- R6::R6Class( #' @export Execute = function(url, method, query_params, header_params, form_params, file_params, accepts, content_types, body, stream_callback = NULL, ...) { + # set the URL req <- request(url) - ## add headers and default headers - if (!is.null(header_params) && length(header_params) != 0) { - req <- req %>% req_headers(!!!header_params) - } - if (!is.null(self$default_headers) && length(self$default_headers) != 0) { - req <- req %>% req_headers(!!!self$default_headers) - } + ## add headers + req <- req %>% req_headers(!!!header_params) + + ## add default headers + req <- req %>% req_headers(!!!self$default_headers) # set HTTP accept header - accept = self$select_header(accepts) + accept <- self$select_header(accepts) if (!is.null(accept)) { req <- req %>% req_headers("Accept" = accept) } # set HTTP content-type header - content_type = self$select_header(content_types) + content_type <- self$select_header(content_types) if (!is.null(content_type)) { req <- req %>% req_headers("Content-Type" = content_type) } ## add query parameters - if (length(query_params) != 0) { - for (query_param in names(query_params)) { - req <- req %>% req_url_query(query_param = query_params[query_param]) - } - } + req <- req %>% req_url_query(!!!query_params) # has file upload? if (!is.null(file_params) && length(file_params) != 0) { diff --git a/samples/client/petstore/R-httr2/R/api_client.R b/samples/client/petstore/R-httr2/R/api_client.R index 8b52a1622d8..11ae43bd931 100644 --- a/samples/client/petstore/R-httr2/R/api_client.R +++ b/samples/client/petstore/R-httr2/R/api_client.R @@ -194,34 +194,29 @@ ApiClient <- R6::R6Class( #' @export Execute = function(url, method, query_params, header_params, form_params, file_params, accepts, content_types, body, stream_callback = NULL, ...) { + # set the URL req <- request(url) - ## add headers and default headers - if (!is.null(header_params) && length(header_params) != 0) { - req <- req %>% req_headers(!!!header_params) - } - if (!is.null(self$default_headers) && length(self$default_headers) != 0) { - req <- req %>% req_headers(!!!self$default_headers) - } + ## add headers + req <- req %>% req_headers(!!!header_params) + + ## add default headers + req <- req %>% req_headers(!!!self$default_headers) # set HTTP accept header - accept = self$select_header(accepts) + accept <- self$select_header(accepts) if (!is.null(accept)) { req <- req %>% req_headers("Accept" = accept) } # set HTTP content-type header - content_type = self$select_header(content_types) + content_type <- self$select_header(content_types) if (!is.null(content_type)) { req <- req %>% req_headers("Content-Type" = content_type) } ## add query parameters - if (length(query_params) != 0) { - for (query_param in names(query_params)) { - req <- req %>% req_url_query(query_param = query_params[query_param]) - } - } + req <- req %>% req_url_query(!!!query_params) # has file upload? if (!is.null(file_params) && length(file_params) != 0) { diff --git a/samples/client/petstore/R-httr2/tests/testthat/test_petstore.R b/samples/client/petstore/R-httr2/tests/testthat/test_petstore.R index 9b94ebe64a3..d0e7088c464 100644 --- a/samples/client/petstore/R-httr2/tests/testthat/test_petstore.R +++ b/samples/client/petstore/R-httr2/tests/testthat/test_petstore.R @@ -1,5 +1,6 @@ context("basic functionality") +## create a new pet and add to petstore server pet_api <- PetApi$new() pet_id <- 123321 pet <- Pet$new("name_test", @@ -15,12 +16,12 @@ pet_api$api_client$username <- "username123" pet_api$api_client$password <- "password123" result <- pet_api$add_pet(pet) -test_that("add_pet", { +test_that("Test toJSON toJSONString fromJSON fromJSONString", { + # test pet expect_equal(pet_id, 123321) expect_equal(pet$toJSONString(), '{"id":123321,"category":{"id":450,"name":"test_cat"},"name":"name_test","photoUrls":["photo_test","second test"],"tags":[{"id":123,"name":"tag_test"},{"id":456,"name":"unknown"}],"status":"available"}') -}) -test_that("Test toJSON toJSONString fromJSON fromJSONString", { + # tests for other pet objects pet0 <- Pet$new() jsonpet <- pet0$toJSON() pet2 <- pet0$fromJSON( @@ -350,25 +351,3 @@ test_that("Tests anyOf", { expect_error(pig$validateJSON('{}'), 'No match found when deserializing the payload into AnyOfPig with anyOf schemas BasquePig, DanishPig. Details: The JSON input ` \\{\\} ` is invalid for BasquePig: the required field `className` is missing\\., The JSON input ` \\{\\} ` is invalid for DanishPig: the required field `className` is missing\\.') }) - -#test_that("GetPetById", { -# pet.id <- pet.id -# pet <- Pet$new(pet.id, NULL, "name_test2", -# list("photo_test2", "second test2"), -# NULL, NULL) -# result <-pet_api$AddPet(pet) -# -# response <- pet_api$GetPetById(pet.id) -# -# expect_equal(response$id, pet.id) -# expect_equal(response$name, "name_test2") -# #expect_equal(response$category, Category$new(450,"test_cat")) -# expect_equal(response$photoUrls, list("photo_test2", "second test2")) -# expect_equal(response$status, NULL) -# #expect_equal(response$tags, list(Tag$new(123, "tag_test"), Tag$new(456, "unknown"))) -#}) - -#test_that("updatePetWithForm", { -# response <- pet_api$updatePetWithForm(pet_id, "test", "sold") -# expect_equal(response, "Pet updated") -#})