Skip to content

Commit 042f717

Browse files
authored
[R] escape item reserved words in model items (#12653)
* fix item reserved words * add comment * add log
1 parent 1f7a495 commit 042f717

9 files changed

Lines changed: 256 additions & 72 deletions

File tree

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RClientCodegen.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public class RClientCodegen extends DefaultCodegen implements CodegenConfig {
5656
protected boolean returnExceptionOnFailure = false;
5757
protected String exceptionPackage = "default";
5858
protected Map<String, String> exceptionPackages = new LinkedHashMap<String, String>();
59+
protected Set<String> itemReservedWords = new TreeSet<String>();
5960

6061
public static final String EXCEPTION_PACKAGE = "exceptionPackage";
6162
public static final String USE_DEFAULT_EXCEPTION = "useDefaultExceptionHandling";
@@ -130,6 +131,11 @@ public RClientCodegen() {
130131
)
131132
);
132133

134+
// these are reserved words in items: https://github.com/r-lib/R6/blob/main/R/r6_class.R#L484
135+
itemReservedWords.add("self");
136+
itemReservedWords.add("private");
137+
itemReservedWords.add("super");
138+
133139
languageSpecificPrimitives.clear();
134140
languageSpecificPrimitives.add("integer");
135141
languageSpecificPrimitives.add("numeric");
@@ -305,6 +311,12 @@ public String toParamName(String name) {
305311

306312
@Override
307313
public String toVarName(String name) {
314+
// escape item reserved words with "item_" prefix
315+
if (itemReservedWords.contains(name)) {
316+
LOGGER.info("The item `{}` has been renamed to `item_{}` as it's a reserved word.", name, name);
317+
return "item_" + name;
318+
}
319+
308320
// don't do anything as we'll put property name inside ` `, e.g. `date-time`
309321
return name;
310322
}

modules/openapi-generator/src/main/resources/r/modelGeneric.mustache

Lines changed: 72 additions & 72 deletions
Large diffs are not rendered by default.

modules/openapi-generator/src/test/resources/3_0/r/petstore.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,18 @@ components:
784784
type: string
785785
message:
786786
type: string
787+
Special:
788+
title: An uploaded response
789+
description: Describes the result of uploading an image resource
790+
type: object
791+
properties:
792+
self:
793+
type: integer
794+
format: int32
795+
private:
796+
type: string
797+
super:
798+
type: string
787799
Dog:
788800
allOf:
789801
- $ref: '#/components/schemas/Animal'

samples/client/petstore/R/.openapi-generator/FILES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ R/order.R
2222
R/pet.R
2323
R/pet_api.R
2424
R/pig.R
25+
R/special.R
2526
R/store_api.R
2627
R/tag.R
2728
R/update_pet_request.R
@@ -44,6 +45,7 @@ docs/Order.md
4445
docs/Pet.md
4546
docs/PetApi.md
4647
docs/Pig.md
48+
docs/Special.md
4749
docs/StoreApi.md
4850
docs/Tag.md
4951
docs/UpdatePetRequest.md

samples/client/petstore/R/NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export(ModelApiResponse)
2626
export(Order)
2727
export(Pet)
2828
export(Pig)
29+
export(Special)
2930
export(Tag)
3031
export(UpdatePetRequest)
3132
export(User)
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
#' OpenAPI Petstore
2+
#'
3+
#' This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
4+
#'
5+
#' The version of the OpenAPI document: 1.0.0
6+
#' Generated by: https://openapi-generator.tech
7+
#'
8+
9+
#' @docType class
10+
#' @title Special
11+
#'
12+
#' @description Special Class
13+
#'
14+
#' @format An \code{R6Class} generator object
15+
#'
16+
#' @field item_self integer [optional]
17+
#'
18+
#' @field item_private character [optional]
19+
#'
20+
#' @field item_super character [optional]
21+
#'
22+
#' @importFrom R6 R6Class
23+
#' @importFrom jsonlite fromJSON toJSON
24+
#' @export
25+
Special <- R6::R6Class(
26+
'Special',
27+
public = list(
28+
`item_self` = NULL,
29+
`item_private` = NULL,
30+
`item_super` = NULL,
31+
initialize = function(
32+
`item_self`=NULL, `item_private`=NULL, `item_super`=NULL, ...
33+
) {
34+
if (!is.null(`item_self`)) {
35+
stopifnot(is.numeric(`item_self`), length(`item_self`) == 1)
36+
self$`item_self` <- `item_self`
37+
}
38+
if (!is.null(`item_private`)) {
39+
stopifnot(is.character(`item_private`), length(`item_private`) == 1)
40+
self$`item_private` <- `item_private`
41+
}
42+
if (!is.null(`item_super`)) {
43+
stopifnot(is.character(`item_super`), length(`item_super`) == 1)
44+
self$`item_super` <- `item_super`
45+
}
46+
},
47+
toJSON = function() {
48+
SpecialObject <- list()
49+
if (!is.null(self$`item_self`)) {
50+
SpecialObject[['self']] <-
51+
self$`item_self`
52+
}
53+
if (!is.null(self$`item_private`)) {
54+
SpecialObject[['private']] <-
55+
self$`item_private`
56+
}
57+
if (!is.null(self$`item_super`)) {
58+
SpecialObject[['super']] <-
59+
self$`item_super`
60+
}
61+
62+
SpecialObject
63+
},
64+
fromJSON = function(SpecialJson) {
65+
SpecialObject <- jsonlite::fromJSON(SpecialJson)
66+
if (!is.null(SpecialObject$`item_self`)) {
67+
self$`item_self` <- SpecialObject$`item_self`
68+
}
69+
if (!is.null(SpecialObject$`item_private`)) {
70+
self$`item_private` <- SpecialObject$`item_private`
71+
}
72+
if (!is.null(SpecialObject$`item_super`)) {
73+
self$`item_super` <- SpecialObject$`item_super`
74+
}
75+
self
76+
},
77+
toJSONString = function() {
78+
jsoncontent <- c(
79+
if (!is.null(self$`item_self`)) {
80+
sprintf(
81+
'"self":
82+
%d
83+
',
84+
self$`item_self`
85+
)},
86+
if (!is.null(self$`item_private`)) {
87+
sprintf(
88+
'"private":
89+
"%s"
90+
',
91+
self$`item_private`
92+
)},
93+
if (!is.null(self$`item_super`)) {
94+
sprintf(
95+
'"super":
96+
"%s"
97+
',
98+
self$`item_super`
99+
)}
100+
)
101+
jsoncontent <- paste(jsoncontent, collapse = ",")
102+
paste('{', jsoncontent, '}', sep = "")
103+
},
104+
fromJSONString = function(SpecialJson) {
105+
SpecialObject <- jsonlite::fromJSON(SpecialJson)
106+
self$`item_self` <- SpecialObject$`item_self`
107+
self$`item_private` <- SpecialObject$`item_private`
108+
self$`item_super` <- SpecialObject$`item_super`
109+
self
110+
},
111+
validateJSON = function(input) {
112+
input_json <- jsonlite::fromJSON(input)
113+
}
114+
115+
)
116+
)
117+

samples/client/petstore/R/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ Class | Method | HTTP request | Description
9595
- [Order](docs/Order.md)
9696
- [Pet](docs/Pet.md)
9797
- [Pig](docs/Pig.md)
98+
- [Special](docs/Special.md)
9899
- [Tag](docs/Tag.md)
99100
- [UpdatePetRequest](docs/UpdatePetRequest.md)
100101
- [User](docs/User.md)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# petstore::Special
2+
3+
Describes the result of uploading an image resource
4+
5+
## Properties
6+
Name | Type | Description | Notes
7+
------------ | ------------- | ------------- | -------------
8+
**item_self** | **integer** | | [optional]
9+
**item_private** | **character** | | [optional]
10+
**item_super** | **character** | | [optional]
11+
12+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Automatically generated by openapi-generator (https://openapi-generator.tech)
2+
# Please update as you see appropriate
3+
4+
context("Test Special")
5+
6+
model_instance <- Special$new()
7+
8+
test_that("self", {
9+
# tests for the property `self` (integer)
10+
11+
# uncomment below to test the property
12+
#expect_equal(model.instance$`self`, "EXPECTED_RESULT")
13+
})
14+
15+
test_that("private", {
16+
# tests for the property `private` (character)
17+
18+
# uncomment below to test the property
19+
#expect_equal(model.instance$`private`, "EXPECTED_RESULT")
20+
})
21+
22+
test_that("super", {
23+
# tests for the property `super` (character)
24+
25+
# uncomment below to test the property
26+
#expect_equal(model.instance$`super`, "EXPECTED_RESULT")
27+
})

0 commit comments

Comments
 (0)