Skip to content

Commit b3a1bed

Browse files
committed
feat(gdscript): support reserved keywords
Also adding some more assertions, and using our own OAS file now. /spend 3h
1 parent ec7edfc commit b3a1bed

File tree

19 files changed

+2033
-53
lines changed

19 files changed

+2033
-53
lines changed

bin/configs/gdscript-petstore.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ outputDir: samples/client/petstore/gdscript/addons/oas.petstore.client
1515
# Exception: Could not process model 'Bird'. Please make sure that your schema is correct!
1616
# Caused by: java.lang.RuntimeException: reserved word color not allowed
1717
# Perhaps try this again later, using another config file like gdscript-echo.yaml
18+
# > Later: this has been solved, we should now be able to use echo as well
1819
#inputSpec: modules/openapi-generator/src/test/resources/3_0/echo_api.yaml
1920

2021
# B: Older (legacy, deprecated) petstore server OAS
21-
inputSpec: modules/openapi-generator/src/test/resources/3_0/petstore.yaml
22+
inputSpec: modules/openapi-generator/src/test/resources/3_0/gdscript/petstore.yaml
2223

2324
templateDir: modules/openapi-generator/src/main/resources/gdscript
2425

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

Lines changed: 59 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,16 @@
1818

1919
public class GdscriptClientCodegen extends DefaultCodegen implements CodegenConfig {
2020

21-
// All generated GDScript classes (including core) will use this prefix both in class_name and file name.
21+
// All generated core classes will use this affixes both in class_name and file name.
2222
public static final String CORE_NAME_PREFIX = "coreNamePrefix";
23+
public static final String CORE_NAME_PREFIX_VALUE = "";
2324
public static final String CORE_NAME_SUFFIX = "coreNameSuffix";
24-
// Perhaps for the README's title ? Any chars allowed ?
25-
public static final String PROJECT_NAME = "projectName";
25+
public static final String CORE_NAME_SUFFIX_VALUE = "";
26+
// Affixes added to reserved words ; do include the _ for snake_case.
27+
public static final String ANTICOLLISION_PREFIX = "anticollisionPrefix";
28+
public static final String ANTICOLLISION_PREFIX_VALUE = "some_";
29+
public static final String ANTICOLLISION_SUFFIX = "anticollisionSuffix";
30+
public static final String ANTICOLLISION_SUFFIX_VALUE = "";
2631

2732
@SuppressWarnings("FieldCanBeLocal")
2833
private final Logger LOGGER = LoggerFactory.getLogger(GdscriptClientCodegen.class);
@@ -32,6 +37,9 @@ public class GdscriptClientCodegen extends DefaultCodegen implements CodegenConf
3237
protected String coreNamePrefix = "";
3338
protected String coreNameSuffix = "";
3439

40+
protected String anticollisionPrefix = ANTICOLLISION_PREFIX_VALUE;
41+
protected String anticollisionSuffix = ANTICOLLISION_SUFFIX_VALUE;
42+
3543
// We're putting the doc files right next their target code file
3644
protected String apiDocPath = "apis/";
3745
protected String modelDocPath = "models/";
@@ -44,12 +52,28 @@ public String getCoreNamePrefix() {
4452
return coreNamePrefix;
4553
}
4654

55+
public void setCoreNameSuffix(String coreNameSuffix) {
56+
this.coreNameSuffix = coreNameSuffix;
57+
}
58+
4759
public String getCoreNameSuffix() {
4860
return coreNameSuffix;
4961
}
5062

51-
public void setCoreNameSuffix(String coreNameSuffix) {
52-
this.coreNameSuffix = coreNameSuffix;
63+
public void setAnticollisionPrefix(String anticollisionPrefix) {
64+
this.anticollisionPrefix = anticollisionPrefix;
65+
}
66+
67+
public String getAnticollisionPrefix() {
68+
return anticollisionPrefix;
69+
}
70+
71+
public void setAnticollisionSuffix(String anticollisionSuffix) {
72+
this.anticollisionSuffix = anticollisionSuffix;
73+
}
74+
75+
public String getAnticollisionSuffix() {
76+
return anticollisionSuffix;
5377
}
5478

5579
public CodegenType getTag() {
@@ -214,8 +238,14 @@ public GdscriptClientCodegen() {
214238
//typeMapping.put("Error", "?");
215239
//typeMapping.put("AnyType", "Variant");
216240

217-
cliOptions.add(new CliOption(CORE_NAME_PREFIX, "PascalCase prefix added to all core classes"));
218-
cliOptions.add(new CliOption(CORE_NAME_SUFFIX, "PascalCase suffix added to all core classes"));
241+
cliOptions.add(new CliOption(CORE_NAME_PREFIX, "PascalCase prefix added to all core classes")
242+
.defaultValue(CORE_NAME_PREFIX_VALUE));
243+
cliOptions.add(new CliOption(CORE_NAME_SUFFIX, "PascalCase suffix added to all core classes")
244+
.defaultValue(CORE_NAME_SUFFIX_VALUE));
245+
cliOptions.add(new CliOption(ANTICOLLISION_PREFIX, "Prefix added at the beginning of reserved words")
246+
.defaultValue(ANTICOLLISION_PREFIX_VALUE));
247+
cliOptions.add(new CliOption(ANTICOLLISION_SUFFIX, "Suffix added at the ending of reserved words")
248+
.defaultValue(ANTICOLLISION_SUFFIX_VALUE));
219249

220250
// Also, I have not taken care of escaping things properly in the templates.
221251
// I'm not sure how to handle the different escaping strategies required.
@@ -235,14 +265,23 @@ public void processAdditionalProperties() {
235265

236266
if (additionalProperties.containsKey(CORE_NAME_PREFIX)) {
237267
setCoreNamePrefix((String) additionalProperties.get(CORE_NAME_PREFIX));
238-
} else {
239-
additionalProperties.put(CORE_NAME_PREFIX, "");
240268
}
269+
writePropertyBack(CORE_NAME_PREFIX, getCoreNamePrefix());
270+
241271
if (additionalProperties.containsKey(CORE_NAME_SUFFIX)) {
242272
setCoreNameSuffix((String) additionalProperties.get(CORE_NAME_SUFFIX));
243-
} else {
244-
additionalProperties.put(CORE_NAME_SUFFIX, "");
245273
}
274+
writePropertyBack(CORE_NAME_SUFFIX, getCoreNameSuffix());
275+
276+
if (additionalProperties.containsKey(ANTICOLLISION_PREFIX)) {
277+
setAnticollisionPrefix((String) additionalProperties.get(ANTICOLLISION_PREFIX));
278+
}
279+
writePropertyBack(ANTICOLLISION_PREFIX, getAnticollisionPrefix());
280+
281+
if (additionalProperties.containsKey(ANTICOLLISION_SUFFIX)) {
282+
setAnticollisionSuffix((String) additionalProperties.get(ANTICOLLISION_SUFFIX));
283+
}
284+
writePropertyBack(ANTICOLLISION_SUFFIX, getAnticollisionSuffix());
246285
}
247286

248287
@Override
@@ -297,10 +336,16 @@ public String escapeQuotationMark(String input) {
297336
;
298337
}
299338

339+
@Override
340+
public String escapeReservedWord(String name) {
341+
return getAnticollisionPrefix() + name + getAnticollisionSuffix();
342+
}
343+
300344
public String escapeStringLiteral(String input) {
301345
return input
302-
.replace("\"", "\\\"")
303-
.replaceAll("[\\\\]+$", "")
346+
.replace("\"", "\\\"") // escape double quotes
347+
.replaceAll("[\\\\]+$", "") // remove trailing backslash(es)
348+
// issue: "foo\<control char>" will perhaps still wreak havoc
304349
;
305350
}
306351

@@ -379,7 +424,7 @@ public Map<String, ModelsMap> updateAllModels(Map<String, ModelsMap> objs) {
379424
protected List<String> getReservedWords() {
380425
return Arrays.asList(
381426
// Local method names used in base API class
382-
"_bzz_connect_client_if_needed", "bzz_request", "_bzz_request_text", "_bzz_do_request_text",
427+
"_bzz_connect_client_if_needed", "_bzz_request", "_bzz_request_text", "_bzz_do_request_text",
383428
"_bzz_convert_http_method", "_bzz_urlize_path_param", "_bzz_escape_path_param",
384429
"_bzz_next_loop_iteration", "_bzz_get_content_type", "_bzz_format_error_response",
385430
// Local properties used in base API class

modules/openapi-generator/src/test/java/org/openapitools/codegen/options/GdscriptClientCodegenOptionsProvider.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
99

1010
public class GdscriptClientCodegenOptionsProvider implements OptionsProvider {
1111

12-
public static final String PROJECT_NAME_VALUE = "GDScript OAS Client";
13-
public static final String CORE_NAME_PREFIX_VALUE = "";
14-
public static final String CORE_NAME_SUFFIX_VALUE = "";
15-
1612
// public static final String MODEL_PACKAGE_VALUE = "package";
1713
// public static final String API_PACKAGE_VALUE = "apiPackage";
1814
// public static final String VARIABLE_NAMING_CONVENTION_VALUE = "snake_case";
@@ -40,9 +36,10 @@ public Map<String, String> createOptions() {
4036
ImmutableMap.Builder<String, String> builder = new ImmutableMap.Builder<String, String>();
4137
return builder
4238

43-
.put(GdscriptClientCodegen.PROJECT_NAME, PROJECT_NAME_VALUE)
44-
.put(GdscriptClientCodegen.CORE_NAME_PREFIX, CORE_NAME_PREFIX_VALUE)
45-
.put(GdscriptClientCodegen.CORE_NAME_SUFFIX, CORE_NAME_SUFFIX_VALUE)
39+
.put(GdscriptClientCodegen.CORE_NAME_PREFIX, GdscriptClientCodegen.CORE_NAME_PREFIX_VALUE)
40+
.put(GdscriptClientCodegen.CORE_NAME_SUFFIX, GdscriptClientCodegen.CORE_NAME_SUFFIX_VALUE)
41+
.put(GdscriptClientCodegen.ANTICOLLISION_PREFIX, GdscriptClientCodegen.ANTICOLLISION_PREFIX_VALUE)
42+
.put(GdscriptClientCodegen.ANTICOLLISION_SUFFIX, GdscriptClientCodegen.ANTICOLLISION_SUFFIX_VALUE)
4643

4744
// > "le remblai, c'était mieux avant"
4845

0 commit comments

Comments
 (0)