Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Visual Studio 2017 fail to generate REST API client for Swagger Petstore?

I'm playing around with using Swagger / OpenAPI docs/specs to generate REST API client code in C#, but I'm running into several problems.

Most notably - when trying to use the Swagger.io Petstore example as a starting point:

enter image description here

Using the VS 2017 Add > REST API client option in VS 2017, I don't get any code produced - instead an error is shown:

enter image description here

Generating client code and adding to project started
Generate client code for REST API with following parameters:
REST API Name: OpenApiClientClient, Base namespace: OpenApiClient, Metadata file path: C:\Users\Marc\AppData\Local\Temp\WebToolsAutoRest\OpenApiClientClient\201807162213351660\swagger.json
[Info]AutoRest Core 0.16.0.0
[Info]Initializing modeler.
[Info]Initializing modeler.
[Info]Parsing swagger json file.
[Info]Generating client model from swagger model.
[Fatal]Error generating client model: Collection format "multi" is not supported (in parameter 'status').
Exception: There was an error during code generation when trying to add a client for the REST API
Generating client code and adding to project failed
Adding REST API client for failed

So if the Swagger sample app isn't compatible - how will others be?? (and I tried a few others, all with the same results, unfortunately).

What's wrong here? Am I missing something, do I need to add some extra tooling to VS 2017 to make this work?

UPDATE:

OK, so I now tried to install Autorest using npm directly:

npm install -g autorest

This appears to work - no errors shown or anything.

But trying to run Autorest - with a set of command line parameters, or even just on its own - results in an error:

AutoRest -CodeGenerator CSharp -Modeler Swagger 
         -Input https://petstore.swagger.io/v2/swagger.json 
         -Namespace Services.UserServiceClient -OutputDirectory d:\projects 
         -AddCrendentials true

or just

AutoRest <kbd>Enter</kbd>

results in:

AutoRest code generation utility [version: 2.0.4280; node: v9.9.0]
(C) 2018 Microsoft Corporation.
https://aka.ms/autorest
Failure:
Error: Unable to start AutoRest Core from C:\Users\Marc.autorest\@[email protected]\node_modules\@microsoft.azure\autorest-core
Error: Unable to start AutoRest Core from C:\Users\Marc.autorest\@[email protected]\node_modules\@microsoft.azure\autorest-core
at main (C:\Users\Marc\AppData\Roaming\npm\node_modules\autorest \dist\app.js:232:19) at

Any further ideas?

like image 449
marc_s Avatar asked Jul 16 '18 20:07

marc_s


1 Answers

Visual Studio 2017 is using a very old version of AutoRest. The issue you are seeing is this one which was fixed in AutoRest v. 1.0. As explained in the comments in that issue:

Are you referring to the autorest version that's built-into visual studio? -- That's incredibly old, and we didn't update that (we've changed the whole way autorest works).

You are going to need to install node and use autorest from the command line.

Looks like it's not possible to update AutoRest used by Visual Studio 2017, so you'll need to call AutoRest directly.

like image 149
Helen Avatar answered Nov 15 '22 05:11

Helen