Skip to content

CSharp language generator results in code with ambigious references #5577

Open
@mminns

Description

@mminns
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
  1. Run the generator
  2. Open the generated sln in Visual Studio
  3. 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

#5576

Suggest a Fix

In https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AbstractCSharpCodegen.java#L310

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);
    }


The in https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/resources/csharp/api.mustache#L15

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions