diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RClientCodegen.java index d588e42b0995..188436a75eb5 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RClientCodegen.java @@ -601,6 +601,7 @@ public String toOperationId(String operationId) { public ModelsMap postProcessModels(ModelsMap objs) { for (ModelMap mo : objs.getModels()) { CodegenModel cm = mo.getModel(); + boolean needsExtractSimpleType = false; for (CodegenProperty var : cm.vars) { // check to see if base name is an empty string if ("".equals(var.baseName)) { @@ -608,10 +609,17 @@ public ModelsMap postProcessModels(ModelsMap objs) { var.baseName = "empty_string"; } + if (!var.isPrimitiveType) { + needsExtractSimpleType = true; + } + // create extension x-r-doc-type to store the data type in r doc format var.vendorExtensions.put("x-r-doc-type", constructRdocType(var)); } + // create extension x-r-has-non-primitive-field to indicate whether generated models need special handling for complex types + cm.vendorExtensions.put("x-r-has-non-primitive-field", needsExtractSimpleType); + // apply the same fix, enhancement for allVars for (CodegenProperty var : cm.allVars) { // check to see if base name is an empty string diff --git a/modules/openapi-generator/src/main/resources/r/modelGeneric.mustache b/modules/openapi-generator/src/main/resources/r/modelGeneric.mustache index 0da6607925aa..ed5873bd9305 100644 --- a/modules/openapi-generator/src/main/resources/r/modelGeneric.mustache +++ b/modules/openapi-generator/src/main/resources/r/modelGeneric.mustache @@ -242,7 +242,7 @@ self$`{{name}}` {{/isPrimitiveType}} {{^isPrimitiveType}} - lapply(self$`{{name}}`, function(x) x$toSimpleType()) + self$extractSimpleType(self$`{{name}}`) {{/isPrimitiveType}} {{/isArray}} {{#isMap}} @@ -250,7 +250,7 @@ self$`{{name}}` {{/isPrimitiveType}} {{^isPrimitiveType}} - lapply(self$`{{name}}`, function(x) x$toSimpleType()) + self$extractSimpleType(self$`{{name}}`) {{/isPrimitiveType}} {{/isMap}} {{/isContainer}} @@ -259,7 +259,7 @@ self$`{{name}}` {{/isPrimitiveType}} {{^isPrimitiveType}} - self$`{{name}}`$toSimpleType() + self$extractSimpleType(self$`{{name}}`) {{/isPrimitiveType}} {{/isContainer}} } @@ -272,6 +272,31 @@ {{/isAdditionalPropertiesTrue}} return({{classname}}Object) }, + {{#vendorExtensions.x-r-has-non-primitive-field}} + + extractSimpleType = function(x) { + if (R6::is.R6(x)) { + return(x$toSimpleType()) + } else if (!self$hasNestedR6(x)) { + return(x) + } + lapply(x, self$extractSimpleType) + }, + + hasNestedR6 = function(x) { + if (R6::is.R6(x)) { + return(TRUE) + } + if (is.list(x)) { + for (item in x) { + if (self$hasNestedR6(item)) { + return(TRUE) + } + } + } + FALSE + }, + {{/vendorExtensions.x-r-has-non-primitive-field}} #' @description #' Deserialize JSON string into an instance of {{{classname}}} diff --git a/samples/client/echo_api/r/R/default_value.R b/samples/client/echo_api/r/R/default_value.R index 2a3a83f32655..e6a089514fc0 100644 --- a/samples/client/echo_api/r/R/default_value.R +++ b/samples/client/echo_api/r/R/default_value.R @@ -119,7 +119,7 @@ DefaultValue <- R6::R6Class( DefaultValueObject <- list() if (!is.null(self$`array_string_enum_ref_default`)) { DefaultValueObject[["array_string_enum_ref_default"]] <- - lapply(self$`array_string_enum_ref_default`, function(x) x$toSimpleType()) + self$extractSimpleType(self$`array_string_enum_ref_default`) } if (!is.null(self$`array_string_enum_default`)) { DefaultValueObject[["array_string_enum_default"]] <- @@ -152,6 +152,29 @@ DefaultValue <- R6::R6Class( return(DefaultValueObject) }, + extractSimpleType = function(x) { + if (R6::is.R6(x)) { + return(x$toSimpleType()) + } else if (!self$hasNestedR6(x)) { + return(x) + } + lapply(x, self$extractSimpleType) + }, + + hasNestedR6 = function(x) { + if (R6::is.R6(x)) { + return(TRUE) + } + if (is.list(x)) { + for (item in x) { + if (self$hasNestedR6(item)) { + return(TRUE) + } + } + } + FALSE + }, + #' @description #' Deserialize JSON string into an instance of DefaultValue #' diff --git a/samples/client/echo_api/r/R/pet.R b/samples/client/echo_api/r/R/pet.R index 6f59d7ea1f6c..b4cae7506eec 100644 --- a/samples/client/echo_api/r/R/pet.R +++ b/samples/client/echo_api/r/R/pet.R @@ -115,7 +115,7 @@ Pet <- R6::R6Class( } if (!is.null(self$`category`)) { PetObject[["category"]] <- - self$`category`$toSimpleType() + self$extractSimpleType(self$`category`) } if (!is.null(self$`photoUrls`)) { PetObject[["photoUrls"]] <- @@ -123,7 +123,7 @@ Pet <- R6::R6Class( } if (!is.null(self$`tags`)) { PetObject[["tags"]] <- - lapply(self$`tags`, function(x) x$toSimpleType()) + self$extractSimpleType(self$`tags`) } if (!is.null(self$`status`)) { PetObject[["status"]] <- @@ -132,6 +132,29 @@ Pet <- R6::R6Class( return(PetObject) }, + extractSimpleType = function(x) { + if (R6::is.R6(x)) { + return(x$toSimpleType()) + } else if (!self$hasNestedR6(x)) { + return(x) + } + lapply(x, self$extractSimpleType) + }, + + hasNestedR6 = function(x) { + if (R6::is.R6(x)) { + return(TRUE) + } + if (is.list(x)) { + for (item in x) { + if (self$hasNestedR6(item)) { + return(TRUE) + } + } + } + FALSE + }, + #' @description #' Deserialize JSON string into an instance of Pet #' diff --git a/samples/client/petstore/R-httr2-wrapper/R/nested_one_of.R b/samples/client/petstore/R-httr2-wrapper/R/nested_one_of.R index 31dcc2f10d01..fe396f11faa4 100644 --- a/samples/client/petstore/R-httr2-wrapper/R/nested_one_of.R +++ b/samples/client/petstore/R-httr2-wrapper/R/nested_one_of.R @@ -84,7 +84,7 @@ NestedOneOf <- R6::R6Class( } if (!is.null(self$`nested_pig`)) { NestedOneOfObject[["nested_pig"]] <- - self$`nested_pig`$toSimpleType() + self$extractSimpleType(self$`nested_pig`) } for (key in names(self$additional_properties)) { NestedOneOfObject[[key]] <- self$additional_properties[[key]] @@ -93,6 +93,29 @@ NestedOneOf <- R6::R6Class( return(NestedOneOfObject) }, + extractSimpleType = function(x) { + if (R6::is.R6(x)) { + return(x$toSimpleType()) + } else if (!self$hasNestedR6(x)) { + return(x) + } + lapply(x, self$extractSimpleType) + }, + + hasNestedR6 = function(x) { + if (R6::is.R6(x)) { + return(TRUE) + } + if (is.list(x)) { + for (item in x) { + if (self$hasNestedR6(item)) { + return(TRUE) + } + } + } + FALSE + }, + #' @description #' Deserialize JSON string into an instance of NestedOneOf #' diff --git a/samples/client/petstore/R-httr2-wrapper/R/pet.R b/samples/client/petstore/R-httr2-wrapper/R/pet.R index 27db68d8fdbd..37d2db91adb7 100644 --- a/samples/client/petstore/R-httr2-wrapper/R/pet.R +++ b/samples/client/petstore/R-httr2-wrapper/R/pet.R @@ -121,7 +121,7 @@ Pet <- R6::R6Class( } if (!is.null(self$`category`)) { PetObject[["category"]] <- - self$`category`$toSimpleType() + self$extractSimpleType(self$`category`) } if (!is.null(self$`name`)) { PetObject[["name"]] <- @@ -133,7 +133,7 @@ Pet <- R6::R6Class( } if (!is.null(self$`tags`)) { PetObject[["tags"]] <- - lapply(self$`tags`, function(x) x$toSimpleType()) + self$extractSimpleType(self$`tags`) } if (!is.null(self$`status`)) { PetObject[["status"]] <- @@ -146,6 +146,29 @@ Pet <- R6::R6Class( return(PetObject) }, + extractSimpleType = function(x) { + if (R6::is.R6(x)) { + return(x$toSimpleType()) + } else if (!self$hasNestedR6(x)) { + return(x) + } + lapply(x, self$extractSimpleType) + }, + + hasNestedR6 = function(x) { + if (R6::is.R6(x)) { + return(TRUE) + } + if (is.list(x)) { + for (item in x) { + if (self$hasNestedR6(item)) { + return(TRUE) + } + } + } + FALSE + }, + #' @description #' Deserialize JSON string into an instance of Pet #' diff --git a/samples/client/petstore/R-httr2-wrapper/R/update_pet_request.R b/samples/client/petstore/R-httr2-wrapper/R/update_pet_request.R index 3559d3c4d838..7a4fb1112f75 100644 --- a/samples/client/petstore/R-httr2-wrapper/R/update_pet_request.R +++ b/samples/client/petstore/R-httr2-wrapper/R/update_pet_request.R @@ -77,7 +77,7 @@ UpdatePetRequest <- R6::R6Class( UpdatePetRequestObject <- list() if (!is.null(self$`jsonData`)) { UpdatePetRequestObject[["jsonData"]] <- - self$`jsonData`$toSimpleType() + self$extractSimpleType(self$`jsonData`) } if (!is.null(self$`binaryDataN2Information`)) { UpdatePetRequestObject[["binaryDataN2Information"]] <- @@ -90,6 +90,29 @@ UpdatePetRequest <- R6::R6Class( return(UpdatePetRequestObject) }, + extractSimpleType = function(x) { + if (R6::is.R6(x)) { + return(x$toSimpleType()) + } else if (!self$hasNestedR6(x)) { + return(x) + } + lapply(x, self$extractSimpleType) + }, + + hasNestedR6 = function(x) { + if (R6::is.R6(x)) { + return(TRUE) + } + if (is.list(x)) { + for (item in x) { + if (self$hasNestedR6(item)) { + return(TRUE) + } + } + } + FALSE + }, + #' @description #' Deserialize JSON string into an instance of UpdatePetRequest #' diff --git a/samples/client/petstore/R-httr2/R/nested_one_of.R b/samples/client/petstore/R-httr2/R/nested_one_of.R index d0ef1f9b67a5..dc43a39e5f41 100644 --- a/samples/client/petstore/R-httr2/R/nested_one_of.R +++ b/samples/client/petstore/R-httr2/R/nested_one_of.R @@ -74,11 +74,34 @@ NestedOneOf <- R6::R6Class( } if (!is.null(self$`nested_pig`)) { NestedOneOfObject[["nested_pig"]] <- - self$`nested_pig`$toSimpleType() + self$extractSimpleType(self$`nested_pig`) } return(NestedOneOfObject) }, + extractSimpleType = function(x) { + if (R6::is.R6(x)) { + return(x$toSimpleType()) + } else if (!self$hasNestedR6(x)) { + return(x) + } + lapply(x, self$extractSimpleType) + }, + + hasNestedR6 = function(x) { + if (R6::is.R6(x)) { + return(TRUE) + } + if (is.list(x)) { + for (item in x) { + if (self$hasNestedR6(item)) { + return(TRUE) + } + } + } + FALSE + }, + #' @description #' Deserialize JSON string into an instance of NestedOneOf #' diff --git a/samples/client/petstore/R-httr2/R/pet.R b/samples/client/petstore/R-httr2/R/pet.R index c3d0bc32fd61..93e6b64c3e7b 100644 --- a/samples/client/petstore/R-httr2/R/pet.R +++ b/samples/client/petstore/R-httr2/R/pet.R @@ -111,7 +111,7 @@ Pet <- R6::R6Class( } if (!is.null(self$`category`)) { PetObject[["category"]] <- - self$`category`$toSimpleType() + self$extractSimpleType(self$`category`) } if (!is.null(self$`name`)) { PetObject[["name"]] <- @@ -123,7 +123,7 @@ Pet <- R6::R6Class( } if (!is.null(self$`tags`)) { PetObject[["tags"]] <- - lapply(self$`tags`, function(x) x$toSimpleType()) + self$extractSimpleType(self$`tags`) } if (!is.null(self$`status`)) { PetObject[["status"]] <- @@ -132,6 +132,29 @@ Pet <- R6::R6Class( return(PetObject) }, + extractSimpleType = function(x) { + if (R6::is.R6(x)) { + return(x$toSimpleType()) + } else if (!self$hasNestedR6(x)) { + return(x) + } + lapply(x, self$extractSimpleType) + }, + + hasNestedR6 = function(x) { + if (R6::is.R6(x)) { + return(TRUE) + } + if (is.list(x)) { + for (item in x) { + if (self$hasNestedR6(item)) { + return(TRUE) + } + } + } + FALSE + }, + #' @description #' Deserialize JSON string into an instance of Pet #' diff --git a/samples/client/petstore/R-httr2/R/update_pet_request.R b/samples/client/petstore/R-httr2/R/update_pet_request.R index e42c9a114546..dc2ede0a8e16 100644 --- a/samples/client/petstore/R-httr2/R/update_pet_request.R +++ b/samples/client/petstore/R-httr2/R/update_pet_request.R @@ -67,7 +67,7 @@ UpdatePetRequest <- R6::R6Class( UpdatePetRequestObject <- list() if (!is.null(self$`jsonData`)) { UpdatePetRequestObject[["jsonData"]] <- - self$`jsonData`$toSimpleType() + self$extractSimpleType(self$`jsonData`) } if (!is.null(self$`binaryDataN2Information`)) { UpdatePetRequestObject[["binaryDataN2Information"]] <- @@ -76,6 +76,29 @@ UpdatePetRequest <- R6::R6Class( return(UpdatePetRequestObject) }, + extractSimpleType = function(x) { + if (R6::is.R6(x)) { + return(x$toSimpleType()) + } else if (!self$hasNestedR6(x)) { + return(x) + } + lapply(x, self$extractSimpleType) + }, + + hasNestedR6 = function(x) { + if (R6::is.R6(x)) { + return(TRUE) + } + if (is.list(x)) { + for (item in x) { + if (self$hasNestedR6(item)) { + return(TRUE) + } + } + } + FALSE + }, + #' @description #' Deserialize JSON string into an instance of UpdatePetRequest #' diff --git a/samples/client/petstore/R/R/nested_one_of.R b/samples/client/petstore/R/R/nested_one_of.R index 31dcc2f10d01..fe396f11faa4 100644 --- a/samples/client/petstore/R/R/nested_one_of.R +++ b/samples/client/petstore/R/R/nested_one_of.R @@ -84,7 +84,7 @@ NestedOneOf <- R6::R6Class( } if (!is.null(self$`nested_pig`)) { NestedOneOfObject[["nested_pig"]] <- - self$`nested_pig`$toSimpleType() + self$extractSimpleType(self$`nested_pig`) } for (key in names(self$additional_properties)) { NestedOneOfObject[[key]] <- self$additional_properties[[key]] @@ -93,6 +93,29 @@ NestedOneOf <- R6::R6Class( return(NestedOneOfObject) }, + extractSimpleType = function(x) { + if (R6::is.R6(x)) { + return(x$toSimpleType()) + } else if (!self$hasNestedR6(x)) { + return(x) + } + lapply(x, self$extractSimpleType) + }, + + hasNestedR6 = function(x) { + if (R6::is.R6(x)) { + return(TRUE) + } + if (is.list(x)) { + for (item in x) { + if (self$hasNestedR6(item)) { + return(TRUE) + } + } + } + FALSE + }, + #' @description #' Deserialize JSON string into an instance of NestedOneOf #' diff --git a/samples/client/petstore/R/R/pet.R b/samples/client/petstore/R/R/pet.R index 27db68d8fdbd..37d2db91adb7 100644 --- a/samples/client/petstore/R/R/pet.R +++ b/samples/client/petstore/R/R/pet.R @@ -121,7 +121,7 @@ Pet <- R6::R6Class( } if (!is.null(self$`category`)) { PetObject[["category"]] <- - self$`category`$toSimpleType() + self$extractSimpleType(self$`category`) } if (!is.null(self$`name`)) { PetObject[["name"]] <- @@ -133,7 +133,7 @@ Pet <- R6::R6Class( } if (!is.null(self$`tags`)) { PetObject[["tags"]] <- - lapply(self$`tags`, function(x) x$toSimpleType()) + self$extractSimpleType(self$`tags`) } if (!is.null(self$`status`)) { PetObject[["status"]] <- @@ -146,6 +146,29 @@ Pet <- R6::R6Class( return(PetObject) }, + extractSimpleType = function(x) { + if (R6::is.R6(x)) { + return(x$toSimpleType()) + } else if (!self$hasNestedR6(x)) { + return(x) + } + lapply(x, self$extractSimpleType) + }, + + hasNestedR6 = function(x) { + if (R6::is.R6(x)) { + return(TRUE) + } + if (is.list(x)) { + for (item in x) { + if (self$hasNestedR6(item)) { + return(TRUE) + } + } + } + FALSE + }, + #' @description #' Deserialize JSON string into an instance of Pet #' diff --git a/samples/client/petstore/R/R/update_pet_request.R b/samples/client/petstore/R/R/update_pet_request.R index 3559d3c4d838..7a4fb1112f75 100644 --- a/samples/client/petstore/R/R/update_pet_request.R +++ b/samples/client/petstore/R/R/update_pet_request.R @@ -77,7 +77,7 @@ UpdatePetRequest <- R6::R6Class( UpdatePetRequestObject <- list() if (!is.null(self$`jsonData`)) { UpdatePetRequestObject[["jsonData"]] <- - self$`jsonData`$toSimpleType() + self$extractSimpleType(self$`jsonData`) } if (!is.null(self$`binaryDataN2Information`)) { UpdatePetRequestObject[["binaryDataN2Information"]] <- @@ -90,6 +90,29 @@ UpdatePetRequest <- R6::R6Class( return(UpdatePetRequestObject) }, + extractSimpleType = function(x) { + if (R6::is.R6(x)) { + return(x$toSimpleType()) + } else if (!self$hasNestedR6(x)) { + return(x) + } + lapply(x, self$extractSimpleType) + }, + + hasNestedR6 = function(x) { + if (R6::is.R6(x)) { + return(TRUE) + } + if (is.list(x)) { + for (item in x) { + if (self$hasNestedR6(item)) { + return(TRUE) + } + } + } + FALSE + }, + #' @description #' Deserialize JSON string into an instance of UpdatePetRequest #'