Generate C# Client from OpenApi.json

Viewed 8277

I have a .net core 2.2 Class Library.

I have installed the VS Studio 2017 "OpenAPI (Swagger) Connected Service" extension.

enter image description here

I have attempted to use this extension to generate a c# client for the following API:

https://skybox.vividseats.com/services/openapi.json

The extension runs and builds a number of files:

enter image description here

But, when I build the project I have 1640 errors:

enter image description here

It appears to have generated all the functions and named then as 1Async, 2Async etc....

enter image description here

Can anyone see anything wrong that I am doing? Or suggest another method to generate a client from the url?

Any help would be greatly appreciated!

2 Answers

You may want to try OpenAPI Generator to generate C# .NET Core client:

Download latest stable version v4.1.3: http://central.maven.org/maven2/org/openapitools/openapi-generator-cli/4.1.3/openapi-generator-cli-4.1.3.jar, and rename it as openapi-generator-cli.jar

java -jar openapi-generator-cli.jar generate -g csharp-netcore -i https://skybox.vividseats.com/services/openapi.json -o /var/tmp/ --skip-validate-spec

I can build the project without issue. Please test it to see if it works for you.

There are spec validation errors so I use --skip-validate-spec to skip those errors. Please review those errors when you've time.

There are other ways to install OpenAPI Generator: https://github.com/OpenAPITools/openapi-generator#1---installation

UPDATE: we've added a csharp-netcore client generator. Please check out the latest master of v5.0.0 release to give it a try.

I've finally found a solution to this problem.

The solution is to define OperationId for every endpoint. You can do this in different ways, I think it's the best described here.

I do this in the SwaggerDefaultValues:

var controllerActionDescriptor = (ControllerActionDescriptor)apiDescription.ActionDescriptor;
var controllerName = controllerActionDescriptor.ControllerName;
var actionName = controllerActionDescriptor.ActionName;
operation.OperationId ??= $"{controllerName}{actionName}";

It's important to note that operation id SHOULD NOT contain underscores.

Related