swagger-api / swagger-api/swagger-codegen

CSharp language generator results in code with ambigious references

Open
#5,577 3 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: C-Sharp Issue: Bug
Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

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
  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

https://github.com/swagger-api/swagger-codegen/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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AbstractCSharpCodegen.java and modules/swagger-codegen/src/main/resources/csharp/api.mustache. Run the supplied generation command with the Bitbucket Swagger definition, then inspect Issue_trackerApi.cs for the reported ambiguous references. Done means the generated C# solution compiles without CS0104 errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, java
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.