Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate C# Client from OpenApi.json

Tags:

c#

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!

like image 653
Trevor Daniel Avatar asked Apr 17 '26 17:04

Trevor Daniel


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

The generator supports different .net framework and Http libraries (e.g. restsharp, httpclient, etc). Please refer to the documentation for various option to customize the output: https://openapi-generator.tech/docs/generators/csharp/

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.

UPDATE: csharp-netcore has been renamed to csharp in v7.0.0 release.

like image 198
William Cheng Avatar answered Apr 20 '26 07:04

William Cheng


TL;DR: Able to solve this by using NSwagStudio and set the

"operationGenerationMode": "SingleClientFromOperationId"

I faced similar issue while generating C# client from API that have operationId with name_[number] format.

I do believe it's a bug on the generator as it generates invalid method name. Though I much prefer to rename the operationId with more meaningful name instead of using number; though it requires to have control on the API that you're trying to consume / generate client.

credit: https://github.com/RicoSuter/NSwag/issues/934

like image 45
tsuryadi Avatar answered Apr 20 '26 08:04

tsuryadi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!