Merge pull request #2240 from wing328/csharp_better_reserved_word

[C#] add better reserved keyword handling for c#
This commit is contained in:
wing328 2016-02-25 17:04:44 +08:00
commit 005584cf2a
6 changed files with 37 additions and 35 deletions

View File

@ -276,14 +276,15 @@ public abstract class AbstractCSharpCodegen extends DefaultCodegen implements Co
@Override @Override
public String toOperationId(String operationId) { public String toOperationId(String operationId) {
// throw exception if method name is empty // throw exception if method name is empty (should not occur as an auto-generated method name will be used)
if (StringUtils.isEmpty(operationId)) { if (StringUtils.isEmpty(operationId)) {
throw new RuntimeException("Empty method name (operationId) not allowed"); throw new RuntimeException("Empty method name (operationId) not allowed");
} }
// method name cannot use reserved keyword, e.g. return // method name cannot use reserved keyword, e.g. return
if (reservedWords.contains(operationId)) { if (reservedWords.contains(operationId)) {
throw new RuntimeException(operationId + " (reserved word) cannot be used as method name"); LOGGER.warn(operationId + " (reserved word) cannot be used as method name. Renamed to " + camelize(sanitizeName("call_" + operationId)));
operationId = "call_" + operationId;
} }
return camelize(sanitizeName(operationId)); return camelize(sanitizeName(operationId));
@ -471,7 +472,8 @@ public abstract class AbstractCSharpCodegen extends DefaultCodegen implements Co
// model name cannot use reserved keyword, e.g. return // model name cannot use reserved keyword, e.g. return
if (reservedWords.contains(name)) { if (reservedWords.contains(name)) {
throw new RuntimeException(name + " (reserved word) cannot be used as a model name"); LOGGER.warn(name + " (reserved word) cannot be used as model name. Renamed to " + camelize("object_" + name));
name = "object_" + name; // e.g. return => ObjectReturn (after camelize)
} }
// camelize the model name // camelize the model name

View File

@ -156,15 +156,15 @@ namespace IO.Swagger.Test
} }
/// <summary> /// <summary>
/// Test GetPetByIdWithByteArray /// Test PetPetIdtestingByteArraytrueGet
/// </summary> /// </summary>
[Test] [Test]
public void GetPetByIdWithByteArrayTest() public void PetPetIdtestingByteArraytrueGetTest()
{ {
// TODO: add unit test for the method 'GetPetByIdWithByteArray' // TODO: add unit test for the method 'PetPetIdtestingByteArraytrueGet'
long? petId = null; // TODO: replace null with proper value long? petId = null; // TODO: replace null with proper value
var response = instance.GetPetByIdWithByteArray(petId); var response = instance.PetPetIdtestingByteArraytrueGet(petId);
Assert.IsInstanceOf<byte[]> (response, "response is byte[]"); Assert.IsInstanceOf<byte[]> (response, "response is byte[]");
} }

View File

@ -212,7 +212,7 @@ namespace IO.Swagger.Api
/// <exception cref="IO.Swagger.Client.ApiException">Thrown when fails to make API call</exception> /// <exception cref="IO.Swagger.Client.ApiException">Thrown when fails to make API call</exception>
/// <param name="petId">ID of pet that needs to be fetched</param> /// <param name="petId">ID of pet that needs to be fetched</param>
/// <returns>byte[]</returns> /// <returns>byte[]</returns>
byte[] GetPetByIdWithByteArray (long? petId); byte[] PetPetIdtestingByteArraytrueGet (long? petId);
/// <summary> /// <summary>
/// Fake endpoint to test byte array return by &#39;Find pet by ID&#39; /// Fake endpoint to test byte array return by &#39;Find pet by ID&#39;
@ -223,7 +223,7 @@ namespace IO.Swagger.Api
/// <exception cref="IO.Swagger.Client.ApiException">Thrown when fails to make API call</exception> /// <exception cref="IO.Swagger.Client.ApiException">Thrown when fails to make API call</exception>
/// <param name="petId">ID of pet that needs to be fetched</param> /// <param name="petId">ID of pet that needs to be fetched</param>
/// <returns>ApiResponse of byte[]</returns> /// <returns>ApiResponse of byte[]</returns>
ApiResponse<byte[]> GetPetByIdWithByteArrayWithHttpInfo (long? petId); ApiResponse<byte[]> PetPetIdtestingByteArraytrueGetWithHttpInfo (long? petId);
/// <summary> /// <summary>
/// Fake endpoint to test byte array in body parameter for adding a new pet to the store /// Fake endpoint to test byte array in body parameter for adding a new pet to the store
@ -446,7 +446,7 @@ namespace IO.Swagger.Api
/// <exception cref="IO.Swagger.Client.ApiException">Thrown when fails to make API call</exception> /// <exception cref="IO.Swagger.Client.ApiException">Thrown when fails to make API call</exception>
/// <param name="petId">ID of pet that needs to be fetched</param> /// <param name="petId">ID of pet that needs to be fetched</param>
/// <returns>Task of byte[]</returns> /// <returns>Task of byte[]</returns>
System.Threading.Tasks.Task<byte[]> GetPetByIdWithByteArrayAsync (long? petId); System.Threading.Tasks.Task<byte[]> PetPetIdtestingByteArraytrueGetAsync (long? petId);
/// <summary> /// <summary>
/// Fake endpoint to test byte array return by &#39;Find pet by ID&#39; /// Fake endpoint to test byte array return by &#39;Find pet by ID&#39;
@ -457,7 +457,7 @@ namespace IO.Swagger.Api
/// <exception cref="IO.Swagger.Client.ApiException">Thrown when fails to make API call</exception> /// <exception cref="IO.Swagger.Client.ApiException">Thrown when fails to make API call</exception>
/// <param name="petId">ID of pet that needs to be fetched</param> /// <param name="petId">ID of pet that needs to be fetched</param>
/// <returns>Task of ApiResponse (byte[])</returns> /// <returns>Task of ApiResponse (byte[])</returns>
System.Threading.Tasks.Task<ApiResponse<byte[]>> GetPetByIdWithByteArrayAsyncWithHttpInfo (long? petId); System.Threading.Tasks.Task<ApiResponse<byte[]>> PetPetIdtestingByteArraytrueGetAsyncWithHttpInfo (long? petId);
/// <summary> /// <summary>
/// Fake endpoint to test byte array in body parameter for adding a new pet to the store /// Fake endpoint to test byte array in body parameter for adding a new pet to the store
@ -1972,9 +1972,9 @@ namespace IO.Swagger.Api
/// <exception cref="IO.Swagger.Client.ApiException">Thrown when fails to make API call</exception> /// <exception cref="IO.Swagger.Client.ApiException">Thrown when fails to make API call</exception>
/// <param name="petId">ID of pet that needs to be fetched</param> /// <param name="petId">ID of pet that needs to be fetched</param>
/// <returns>byte[]</returns> /// <returns>byte[]</returns>
public byte[] GetPetByIdWithByteArray (long? petId) public byte[] PetPetIdtestingByteArraytrueGet (long? petId)
{ {
ApiResponse<byte[]> response = GetPetByIdWithByteArrayWithHttpInfo(petId); ApiResponse<byte[]> response = PetPetIdtestingByteArraytrueGetWithHttpInfo(petId);
return response.Data; return response.Data;
} }
@ -1984,12 +1984,12 @@ namespace IO.Swagger.Api
/// <exception cref="IO.Swagger.Client.ApiException">Thrown when fails to make API call</exception> /// <exception cref="IO.Swagger.Client.ApiException">Thrown when fails to make API call</exception>
/// <param name="petId">ID of pet that needs to be fetched</param> /// <param name="petId">ID of pet that needs to be fetched</param>
/// <returns>ApiResponse of byte[]</returns> /// <returns>ApiResponse of byte[]</returns>
public ApiResponse< byte[] > GetPetByIdWithByteArrayWithHttpInfo (long? petId) public ApiResponse< byte[] > PetPetIdtestingByteArraytrueGetWithHttpInfo (long? petId)
{ {
// verify the required parameter 'petId' is set // verify the required parameter 'petId' is set
if (petId == null) if (petId == null)
throw new ApiException(400, "Missing required parameter 'petId' when calling PetApi->GetPetByIdWithByteArray"); throw new ApiException(400, "Missing required parameter 'petId' when calling PetApi->PetPetIdtestingByteArraytrueGet");
var path_ = "/pet/{petId}?testing_byte_array=true"; var path_ = "/pet/{petId}?testing_byte_array=true";
@ -2048,9 +2048,9 @@ namespace IO.Swagger.Api
int statusCode = (int) response.StatusCode; int statusCode = (int) response.StatusCode;
if (statusCode >= 400) if (statusCode >= 400)
throw new ApiException (statusCode, "Error calling GetPetByIdWithByteArray: " + response.Content, response.Content); throw new ApiException (statusCode, "Error calling PetPetIdtestingByteArraytrueGet: " + response.Content, response.Content);
else if (statusCode == 0) else if (statusCode == 0)
throw new ApiException (statusCode, "Error calling GetPetByIdWithByteArray: " + response.ErrorMessage, response.ErrorMessage); throw new ApiException (statusCode, "Error calling PetPetIdtestingByteArraytrueGet: " + response.ErrorMessage, response.ErrorMessage);
return new ApiResponse<byte[]>(statusCode, return new ApiResponse<byte[]>(statusCode,
response.Headers.ToDictionary(x => x.Name, x => x.Value.ToString()), response.Headers.ToDictionary(x => x.Name, x => x.Value.ToString()),
@ -2065,9 +2065,9 @@ namespace IO.Swagger.Api
/// <exception cref="IO.Swagger.Client.ApiException">Thrown when fails to make API call</exception> /// <exception cref="IO.Swagger.Client.ApiException">Thrown when fails to make API call</exception>
/// <param name="petId">ID of pet that needs to be fetched</param> /// <param name="petId">ID of pet that needs to be fetched</param>
/// <returns>Task of byte[]</returns> /// <returns>Task of byte[]</returns>
public async System.Threading.Tasks.Task<byte[]> GetPetByIdWithByteArrayAsync (long? petId) public async System.Threading.Tasks.Task<byte[]> PetPetIdtestingByteArraytrueGetAsync (long? petId)
{ {
ApiResponse<byte[]> response = await GetPetByIdWithByteArrayAsyncWithHttpInfo(petId); ApiResponse<byte[]> response = await PetPetIdtestingByteArraytrueGetAsyncWithHttpInfo(petId);
return response.Data; return response.Data;
} }
@ -2078,10 +2078,10 @@ namespace IO.Swagger.Api
/// <exception cref="IO.Swagger.Client.ApiException">Thrown when fails to make API call</exception> /// <exception cref="IO.Swagger.Client.ApiException">Thrown when fails to make API call</exception>
/// <param name="petId">ID of pet that needs to be fetched</param> /// <param name="petId">ID of pet that needs to be fetched</param>
/// <returns>Task of ApiResponse (byte[])</returns> /// <returns>Task of ApiResponse (byte[])</returns>
public async System.Threading.Tasks.Task<ApiResponse<byte[]>> GetPetByIdWithByteArrayAsyncWithHttpInfo (long? petId) public async System.Threading.Tasks.Task<ApiResponse<byte[]>> PetPetIdtestingByteArraytrueGetAsyncWithHttpInfo (long? petId)
{ {
// verify the required parameter 'petId' is set // verify the required parameter 'petId' is set
if (petId == null) throw new ApiException(400, "Missing required parameter 'petId' when calling GetPetByIdWithByteArray"); if (petId == null) throw new ApiException(400, "Missing required parameter 'petId' when calling PetPetIdtestingByteArraytrueGet");
var path_ = "/pet/{petId}?testing_byte_array=true"; var path_ = "/pet/{petId}?testing_byte_array=true";
@ -2142,9 +2142,9 @@ namespace IO.Swagger.Api
int statusCode = (int) response.StatusCode; int statusCode = (int) response.StatusCode;
if (statusCode >= 400) if (statusCode >= 400)
throw new ApiException (statusCode, "Error calling GetPetByIdWithByteArray: " + response.Content, response.Content); throw new ApiException (statusCode, "Error calling PetPetIdtestingByteArraytrueGet: " + response.Content, response.Content);
else if (statusCode == 0) else if (statusCode == 0)
throw new ApiException (statusCode, "Error calling GetPetByIdWithByteArray: " + response.ErrorMessage, response.ErrorMessage); throw new ApiException (statusCode, "Error calling PetPetIdtestingByteArraytrueGet: " + response.ErrorMessage, response.ErrorMessage);
return new ApiResponse<byte[]>(statusCode, return new ApiResponse<byte[]>(statusCode,
response.Headers.ToDictionary(x => x.Name, x => x.Value.ToString()), response.Headers.ToDictionary(x => x.Name, x => x.Value.ToString()),

View File

@ -1,8 +1,8 @@
<Properties StartupItem="SwaggerClientTest.csproj"> <Properties StartupItem="SwaggerClientTest.csproj">
<MonoDevelop.Ide.Workspace ActiveConfiguration="Debug" /> <MonoDevelop.Ide.Workspace ActiveConfiguration="Debug" />
<MonoDevelop.Ide.Workbench ActiveDocument="TestOrder.cs"> <MonoDevelop.Ide.Workbench ActiveDocument="TestPet.cs">
<Files> <Files>
<File FileName="TestPet.cs" Line="84" Column="10" /> <File FileName="TestPet.cs" Line="162" Column="35" />
<File FileName="TestOrder.cs" Line="42" Column="12" /> <File FileName="TestOrder.cs" Line="42" Column="12" />
</Files> </Files>
</MonoDevelop.Ide.Workbench> </MonoDevelop.Ide.Workbench>

View File

@ -168,7 +168,7 @@ namespace SwaggerClientTest.TestPet
Configuration c1 = new Configuration (timeout: 10000); Configuration c1 = new Configuration (timeout: 10000);
PetApi petApi = new PetApi (c1); PetApi petApi = new PetApi (c1);
byte[] response = petApi.GetPetByIdWithByteArray (petId); byte[] response = petApi.PetPetIdtestingByteArraytrueGet (petId);
Assert.IsInstanceOf<byte[]> (response, "Response is byte array"); Assert.IsInstanceOf<byte[]> (response, "Response is byte array");
} }

View File

@ -1,9 +1,9 @@
/Users/williamcheng/Code/wing328/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/obj/Debug/.NETFramework,Version=v4.5.AssemblyAttribute.cs /Users/williamcheng/Code/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/obj/Debug/.NETFramework,Version=v4.5.AssemblyAttribute.cs
/Users/williamcheng/Code/wing328/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/obj/Debug/SwaggerClientTest.swagger-logo.png /Users/williamcheng/Code/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/obj/Debug/SwaggerClientTest.swagger-logo.png
/Users/williamcheng/Code/wing328/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/SwaggerClientTest.dll.mdb /Users/williamcheng/Code/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/SwaggerClientTest.dll.mdb
/Users/williamcheng/Code/wing328/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/SwaggerClientTest.dll /Users/williamcheng/Code/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/SwaggerClientTest.dll
/Users/williamcheng/Code/wing328/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/obj/Debug/SwaggerClientTest.dll /Users/williamcheng/Code/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/obj/Debug/SwaggerClientTest.dll
/Users/williamcheng/Code/wing328/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/obj/Debug/SwaggerClientTest.dll.mdb /Users/williamcheng/Code/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/obj/Debug/SwaggerClientTest.dll.mdb
/Users/williamcheng/Code/wing328/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/Newtonsoft.Json.dll /Users/williamcheng/Code/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/Newtonsoft.Json.dll
/Users/williamcheng/Code/wing328/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/nunit.framework.dll /Users/williamcheng/Code/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/nunit.framework.dll
/Users/williamcheng/Code/wing328/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/RestSharp.dll /Users/williamcheng/Code/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/RestSharp.dll