Description
Description
The CSharp api..mustache template creates API classes that reference Model objects. However if the Model contains classes with the same name as core C# classes the code will not compile without the references being explicitly specified.
Swagger-codegen version
Built from master @ commit ea16da8
Swagger declaration file content or url
https://api.bitbucket.org/swagger.json
Command line used for generation
java -jar C:\Users\mminn\Source\github.com\mminns\swagger-codegen\modules\swagger-codegen-cli\target\swagger-codegen-cli.jar generate -i swagger.json -l csharp -o client-mminns/csharp
Steps to reproduce
- Run the generator
- Open the generated sln in Visual Studio
- Try and compile
Results:
Compilation fails
Open Issue_trackerApi.cs
Listed with multiple errors, e.g.
Severity Code Description Project File Line Suppression State
Error CS0104 'Version' is an ambiguous reference between 'IO.Swagger.Model.Version' and 'System.Version' IO.Swagger C:\Users\mminn\Source\bitbucket.org\mminns\bitbuckit.net.swagger\client-mminnns\csharp-debug\src\IO.Swagger\Api\Issue_trackerApi.cs 562 Active
Related issues
Suggest a Fix
Add the following method:
+ private void processUsings(Map<String, Object> objs) {
+ List<Map<String, String>> imports = (List<Map<String, String>>)objs.get("imports");
+ Map<String,String> usings = new HashMap<String,String>();
+ for(Map<String, String> importMap: imports) {
+ for(Map.Entry<String, String> entry: importMap.entrySet()) {
+ String fullModelName = entry.getValue();
+ String[] parts = fullModelName.split("\\.");
+ usings.put(parts[1], fullModelName);
+ }
+ }
+ objs.put("usings", usings.entrySet());
+ }
And using it in:
@Override
public Map<String, Object> postProcessModels(Map<String, Object> objs) {
List<Object> models = (List<Object>) objs.get("models");
for (Object _mo : models) {
Map<String, Object> mo = (Map<String, Object>) _mo;
CodegenModel cm = (CodegenModel) mo.get("model");
for (CodegenProperty var : cm.vars) {
// check to see if model name is same as the property name
// which will result in compilation error
// if found, prepend with _ to workaround the limitation
if (var.name.equalsIgnoreCase(cm.name)) {
var.name = "_" + var.name;
}
}
}
+
+ processUsings(objs);
// process enum in models
return postProcessModelsEnum(objs);
}
Add the following to make use of the new "usings" data
{{>partial_header}}
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
{{#netStandard}}
using RestSharp.Portable;
{{/netStandard}}
{{^netStandard}}
using RestSharp;
{{/netStandard}}
using {{packageName}}.Client;
{{#hasImport}}using {{packageName}}.{{modelPackage}};
{{/hasImport}}
+{{#usings}}using {{key}} = {{packageName}}.{{value}};
+{{/usings}}
namespace {{packageName}}.{{apiPackage}}
{
{{#operations}}
/// <summary>
see also https://github.com/mminns/swagger-codegen/tree/issue/fix-csharp-for-bitbucket\
I'd like to submit a PR for the changes