1

I'm using NSwagStudio to generate a client for my WebAPI (.NET Core 2.0). Unfortunately the generated client is useless.

For example:

[Route("v1/documentsets")]
.
.
.
[Route("")]
[HttpGet]
[Authorize(Policy = AuthorizationPolicyKeys.ReadOnly)]
[ProducesResponseType(typeof(ResponseContainer<PagingInfo, IList<GetDocumentSetsResponseDto>>), 200)]
public async Task<IActionResult> GetAll(...)

becomes:

public System.Threading.Tasks.Task V1DocumentsetsGetAsync(...)

This is because - swagger.json (what I use for the generation) is also invalid (I think):

"responses": {
  "200": {
    "description": "Success",
    "schema": {
      "$ref": "#/definitions/Comp.Shared.Helpers.ResponseContainer`2[[Comp.Shared.Helpers.PagingInfo, Comp.Shared, Version=2.0.16.0, Culture=neutral, PublicKeyToken=null],[System.Collections.Generic.IList`1[[Comp.Documents.Private.DataTransferObjects.Documents.GetDocumentsResponseDto, Comp.Documents.Private, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]"
    }
  }
}

$ref seems to be invalid here... PagingInfo and ResponseContainer types are from a Nuget package.

The generated method name - V1DocumentsetsGetAsync - seems strange as well. The operationId from swagger.json: "operationId": "V1DocumentsetsGet" - this is strange too. Why is the version number is in the generated id?

Swagger settings:

    services.AddSwaggerGen(c =>
    {
        c.SwaggerDoc("v1", new Swashbuckle.AspNetCore.Swagger.Info { Title = "Documents API", Version = "v1" });                
        var xmlPath = $@"{AppDomain.CurrentDomain.BaseDirectory}\bin\{Assembly.GetExecutingAssembly().GetName().Name}.XML";
        if (File.Exists(xmlPath))
        {
            c.IncludeXmlComments(xmlPath);
        }
        c.DescribeAllEnumsAsStrings();
        c.CustomSchemaIds((type) => type.FullName);
        c.MapType<RequestedFieldDictionary>(() => new Schema { Type = "string" });
    });

What should I change?

Wiggin77
  • 11
  • 2
  • Just FYI: You are using Swashbuckle and not NSwag to generate the spec, you need to check Swashbuckle's docs about how to change the operation id to the form Controller_MethodName... – Rico Suter Sep 04 '18 at 22:39
  • Thank you Rico. Yes, I know that NSwag generates the client, Swashbuckle generates swagger.json. I'm going to check the docs regarding the operation id. But it is a minor problem compared to the main problem with the response in swagger.json. Do you have any idea what could the reason for this? – Wiggin77 Sep 05 '18 at 08:17
  • The way Swashbuckle handles generic types seems to be broken sometimes (here it even adds assembly versions, etc.), maybe you can implement an own class `public class MyResponse : ResponseContainer> { }` to fix that? NSwag would transform the this type name to something like `ResponseContainerOfPagingInfoOfArrayOfGetDocumentSetsResponseDto`... – Rico Suter Sep 05 '18 at 09:02
  • In the end I used your idea as a workaround, I did not find any other solution. Thank you for your help! – Wiggin77 Sep 07 '18 at 08:12
  • I was having the same problem. Removing `c.CustomSchemaIds(x => x.FullName);` did the trick for me. It's something that occurs also on the swagger webpage – Ole EH Dufour Apr 10 '20 at 16:05

0 Answers0