Skip to content

Commit

Permalink
[R] escape item reserved words in model items (#12653)
Browse files Browse the repository at this point in the history
* fix item reserved words

* add comment

* add log
  • Loading branch information
wing328 authored Jun 21, 2022
1 parent 1f7a495 commit 042f717
Show file tree
Hide file tree
Showing 9 changed files with 256 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public class RClientCodegen extends DefaultCodegen implements CodegenConfig {
protected boolean returnExceptionOnFailure = false;
protected String exceptionPackage = "default";
protected Map<String, String> exceptionPackages = new LinkedHashMap<String, String>();
protected Set<String> itemReservedWords = new TreeSet<String>();

public static final String EXCEPTION_PACKAGE = "exceptionPackage";
public static final String USE_DEFAULT_EXCEPTION = "useDefaultExceptionHandling";
Expand Down Expand Up @@ -130,6 +131,11 @@ public RClientCodegen() {
)
);

// these are reserved words in items: https://github.com/r-lib/R6/blob/main/R/r6_class.R#L484
itemReservedWords.add("self");
itemReservedWords.add("private");
itemReservedWords.add("super");

languageSpecificPrimitives.clear();
languageSpecificPrimitives.add("integer");
languageSpecificPrimitives.add("numeric");
Expand Down Expand Up @@ -305,6 +311,12 @@ public String toParamName(String name) {

@Override
public String toVarName(String name) {
// escape item reserved words with "item_" prefix
if (itemReservedWords.contains(name)) {
LOGGER.info("The item `{}` has been renamed to `item_{}` as it's a reserved word.", name, name);
return "item_" + name;
}

// don't do anything as we'll put property name inside ` `, e.g. `date-time`
return name;
}
Expand Down
144 changes: 72 additions & 72 deletions modules/openapi-generator/src/main/resources/r/modelGeneric.mustache

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions modules/openapi-generator/src/test/resources/3_0/r/petstore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,18 @@ components:
type: string
message:
type: string
Special:
title: An uploaded response
description: Describes the result of uploading an image resource
type: object
properties:
self:
type: integer
format: int32
private:
type: string
super:
type: string
Dog:
allOf:
- $ref: '#/components/schemas/Animal'
Expand Down
2 changes: 2 additions & 0 deletions samples/client/petstore/R/.openapi-generator/FILES
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ R/order.R
R/pet.R
R/pet_api.R
R/pig.R
R/special.R
R/store_api.R
R/tag.R
R/update_pet_request.R
Expand All @@ -44,6 +45,7 @@ docs/Order.md
docs/Pet.md
docs/PetApi.md
docs/Pig.md
docs/Special.md
docs/StoreApi.md
docs/Tag.md
docs/UpdatePetRequest.md
Expand Down
1 change: 1 addition & 0 deletions samples/client/petstore/R/NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export(ModelApiResponse)
export(Order)
export(Pet)
export(Pig)
export(Special)
export(Tag)
export(UpdatePetRequest)
export(User)
Expand Down
117 changes: 117 additions & 0 deletions samples/client/petstore/R/R/special.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#' OpenAPI Petstore
#'
#' This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
#'
#' The version of the OpenAPI document: 1.0.0
#' Generated by: https://openapi-generator.tech
#'

#' @docType class
#' @title Special
#'
#' @description Special Class
#'
#' @format An \code{R6Class} generator object
#'
#' @field item_self integer [optional]
#'
#' @field item_private character [optional]
#'
#' @field item_super character [optional]
#'
#' @importFrom R6 R6Class
#' @importFrom jsonlite fromJSON toJSON
#' @export
Special <- R6::R6Class(
'Special',
public = list(
`item_self` = NULL,
`item_private` = NULL,
`item_super` = NULL,
initialize = function(
`item_self`=NULL, `item_private`=NULL, `item_super`=NULL, ...
) {
if (!is.null(`item_self`)) {
stopifnot(is.numeric(`item_self`), length(`item_self`) == 1)
self$`item_self` <- `item_self`
}
if (!is.null(`item_private`)) {
stopifnot(is.character(`item_private`), length(`item_private`) == 1)
self$`item_private` <- `item_private`
}
if (!is.null(`item_super`)) {
stopifnot(is.character(`item_super`), length(`item_super`) == 1)
self$`item_super` <- `item_super`
}
},
toJSON = function() {
SpecialObject <- list()
if (!is.null(self$`item_self`)) {
SpecialObject[['self']] <-
self$`item_self`
}
if (!is.null(self$`item_private`)) {
SpecialObject[['private']] <-
self$`item_private`
}
if (!is.null(self$`item_super`)) {
SpecialObject[['super']] <-
self$`item_super`
}

SpecialObject
},
fromJSON = function(SpecialJson) {
SpecialObject <- jsonlite::fromJSON(SpecialJson)
if (!is.null(SpecialObject$`item_self`)) {
self$`item_self` <- SpecialObject$`item_self`
}
if (!is.null(SpecialObject$`item_private`)) {
self$`item_private` <- SpecialObject$`item_private`
}
if (!is.null(SpecialObject$`item_super`)) {
self$`item_super` <- SpecialObject$`item_super`
}
self
},
toJSONString = function() {
jsoncontent <- c(
if (!is.null(self$`item_self`)) {
sprintf(
'"self":
%d
',
self$`item_self`
)},
if (!is.null(self$`item_private`)) {
sprintf(
'"private":
"%s"
',
self$`item_private`
)},
if (!is.null(self$`item_super`)) {
sprintf(
'"super":
"%s"
',
self$`item_super`
)}
)
jsoncontent <- paste(jsoncontent, collapse = ",")
paste('{', jsoncontent, '}', sep = "")
},
fromJSONString = function(SpecialJson) {
SpecialObject <- jsonlite::fromJSON(SpecialJson)
self$`item_self` <- SpecialObject$`item_self`
self$`item_private` <- SpecialObject$`item_private`
self$`item_super` <- SpecialObject$`item_super`
self
},
validateJSON = function(input) {
input_json <- jsonlite::fromJSON(input)
}

)
)

1 change: 1 addition & 0 deletions samples/client/petstore/R/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ Class | Method | HTTP request | Description
- [Order](docs/Order.md)
- [Pet](docs/Pet.md)
- [Pig](docs/Pig.md)
- [Special](docs/Special.md)
- [Tag](docs/Tag.md)
- [UpdatePetRequest](docs/UpdatePetRequest.md)
- [User](docs/User.md)
Expand Down
12 changes: 12 additions & 0 deletions samples/client/petstore/R/docs/Special.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# petstore::Special

Describes the result of uploading an image resource

## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**item_self** | **integer** | | [optional]
**item_private** | **character** | | [optional]
**item_super** | **character** | | [optional]


27 changes: 27 additions & 0 deletions samples/client/petstore/R/tests/testthat/test_special.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Automatically generated by openapi-generator (https://openapi-generator.tech)
# Please update as you see appropriate

context("Test Special")

model_instance <- Special$new()

test_that("self", {
# tests for the property `self` (integer)

# uncomment below to test the property
#expect_equal(model.instance$`self`, "EXPECTED_RESULT")
})

test_that("private", {
# tests for the property `private` (character)

# uncomment below to test the property
#expect_equal(model.instance$`private`, "EXPECTED_RESULT")
})

test_that("super", {
# tests for the property `super` (character)

# uncomment below to test the property
#expect_equal(model.instance$`super`, "EXPECTED_RESULT")
})

0 comments on commit 042f717

Please sign in to comment.