Skip to content

Commit ab08dd1

Browse files
authored
Better handling of operationID starting with numbers (#691)
* add numeric operationid to test spec * better handling of operationId in more generators * fix go toOperationId * update samples * update java samples * update java samples (vertx, webclient) * update java google api client sample * fix typo, update OAS3 test spec, update php petstore (oas3) * defer camelize in operationid * remove duplicated sanitizeName
1 parent 21141f6 commit ab08dd1

113 files changed

Lines changed: 441 additions & 417 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,12 @@ public String toOperationId(String operationId) {
603603
operationId = "call_" + operationId;
604604
}
605605

606+
// operationId starts with a number
607+
if (operationId.matches("^\\d.*")) {
608+
LOGGER.warn(operationId + " (starting with a number) cannot be used as method name. Renamed to " + camelize(sanitizeName("call_" + operationId)));
609+
operationId = "call_" + operationId;
610+
}
611+
606612
return camelize(sanitizeName(operationId));
607613
}
608614

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,13 @@ public String toOperationId(String operationId) {
314314
// method name cannot use reserved keyword, e.g. return
315315
if (isReservedWord(sanitizedOperationId)) {
316316
LOGGER.warn(operationId + " (reserved word) cannot be used as method name. Renamed to "
317-
+ camelize("call_" + operationId));
317+
+ camelize("call_" + sanitizedOperationId));
318+
sanitizedOperationId = "call_" + sanitizedOperationId;
319+
}
320+
321+
// operationId starts with a number
322+
if (sanitizedOperationId.matches("^\\d.*")) {
323+
LOGGER.warn(operationId + " (starting with a number) cannot be used as method name. Renamed to " + camelize("call_" + sanitizedOperationId));
318324
sanitizedOperationId = "call_" + sanitizedOperationId;
319325
}
320326

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -884,6 +884,12 @@ public String toOperationId(String operationId) {
884884
return newOperationId;
885885
}
886886

887+
// operationId starts with a number
888+
if (operationId.matches("^\\d.*")) {
889+
LOGGER.warn(operationId + " (starting with a number) cannot be used as method sname. Renamed to " + camelize("call_" + operationId), true);
890+
operationId = camelize("call_" + operationId, true);
891+
}
892+
887893
return operationId;
888894
}
889895

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,8 +377,14 @@ public String toOperationId(String operationId) {
377377

378378
// method name cannot use reserved keyword, e.g. return
379379
if (isReservedWord(operationId)) {
380-
LOGGER.warn(operationId + " (reserved word) cannot be used as method name. Renamed to " + underscore("call_" + operationId));
381-
return underscore("call_" + operationId);
380+
LOGGER.warn(operationId + " (reserved word) cannot be used as method name. Renamed to " + underscore(sanitizeName("call_" + operationId)));
381+
return underscore(sanitizeName("call_" + operationId));
382+
}
383+
384+
// operationId starts with a number
385+
if (operationId.matches("^\\d.*")) {
386+
LOGGER.warn(operationId + " (starting with a number) cannot be used as method name. Renamed to " + underscore(sanitizeName("call_" + operationId)));
387+
operationId = "call_" + operationId;
382388
}
383389

384390
//return underscore(operationId).replaceAll("[^A-Za-z0-9_]", "");

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ public String toOperationId(String operationId) {
552552
operationId = "call_" + operationId;
553553
}
554554

555-
// model name starts with a number
555+
// operationId starts with a number
556556
if (operationId.matches("^\\d.*")) {
557557
LOGGER.warn(operationId + " (starting with a number) cannot be used as method name. Renamed to " + underscore(sanitizeName("call_" + operationId)));
558558
operationId = "call_" + operationId;

modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -929,8 +929,8 @@ paths:
929929
tags:
930930
- "$another-fake?"
931931
summary: To test special tags
932-
description: To test special tags
933-
operationId: test_special_tags
932+
description: To test special tags and operation ID starting with number
933+
operationId: 123_test_@#$%_special_tags
934934
consumes:
935935
- application/json
936936
produces:

modules/openapi-generator/src/test/resources/3_0/petstore-with-fake-endpoints-models-for-testing.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -898,8 +898,8 @@ paths:
898898
tags:
899899
- $another-fake?
900900
summary: To test special tags
901-
description: To test special tags
902-
operationId: test_special_tags
901+
description: To test special tags and operation ID starting with number
902+
operationId: 123_test_@#$%_special_tags
903903
responses:
904904
'200':
905905
description: successful operation
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.1.1-SNAPSHOT
1+
3.2.0-SNAPSHOT

samples/client/petstore/go/go-petstore/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ All URIs are relative to *http://petstore.swagger.io:80/v2*
3030

3131
Class | Method | HTTP request | Description
3232
------------ | ------------- | ------------- | -------------
33-
*AnotherFakeApi* | [**TestSpecialTags**](docs/AnotherFakeApi.md#testspecialtags) | **Patch** /another-fake/dummy | To test special tags
33+
*AnotherFakeApi* | [**Call123TestSpecialTags**](docs/AnotherFakeApi.md#call123testspecialtags) | **Patch** /another-fake/dummy | To test special tags
3434
*FakeApi* | [**FakeOuterBooleanSerialize**](docs/FakeApi.md#fakeouterbooleanserialize) | **Post** /fake/outer/boolean |
3535
*FakeApi* | [**FakeOuterCompositeSerialize**](docs/FakeApi.md#fakeoutercompositeserialize) | **Post** /fake/outer/composite |
3636
*FakeApi* | [**FakeOuterNumberSerialize**](docs/FakeApi.md#fakeouternumberserialize) | **Post** /fake/outer/number |

samples/client/petstore/go/go-petstore/api/openapi.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -954,8 +954,8 @@ paths:
954954
- fake
955955
/another-fake/dummy:
956956
patch:
957-
description: To test special tags
958-
operationId: test_special_tags
957+
description: To test special tags and operation ID starting with number
958+
operationId: 123_test_@#$%_special_tags
959959
requestBody:
960960
content:
961961
application/json:

0 commit comments

Comments
 (0)