How to update REST API Client from a Swagger file in Visual Studio
Asked Answered
L

4

11

How do I update a REST API Client from a Swagger file in Visual Studio? I created my REST API Client via the "Add -> REST API Client" menu option when right-clicking on the project. See screenshot below. I now have an updated version of the Swagger file and need to also update the auto-generated client code. How do I do this in Visual Studio?

I have not been able to find a way to update the auto-generated code without completely deleting the code first and then re-adding the REST API Client. I am looking for something similar to the "Update Service Reference" functionality for a WSDL.

Add REST API Client menu option Add REST API Client popup

Largeminded answered 3/8, 2016 at 18:13 Comment(5)
That Rest API Client is a pain, the version they use is out of date and full of bugs, seems that Microsoft only updates it when there is a new version of Visual Studio, better use different swagger codegen tool.Starveling
I found and interesting post from the autorest team, "VS2017 integration uses a fixed version, so the best option is calling autorest directly" why they use a fixed version ? maybe a developer was in a rush : github.com/Azure/autorest/issues/2198Starveling
Note: With Visual Studio 2019 16.9.x, MS removed this generator and replaced it with an OpenAPI generator that doesn't even generate compileable code when having more than one controller class.Subclavius
You must use the template value (is also default value in NSwagStudio) for controller name specification: {controller}Client when there is more than one controller class.Pavia
which template value?Responsion
C
3

The menu option seems to be poorly named, but the act of Adding a REST API client actually overwrites/updates an existing client.

Confiscatory answered 15/8, 2016 at 14:52 Comment(2)
I tried that but it didn't overwrite/update the existing client. Instead it created a new client with "1" appended to the end.Largeminded
You're correct with regard to the "1" appended at the end of the client. I was able to replicate this issue using a locally-defined swagger file. If I add a REST API client using a swagger document url (download from the Azure portal) then new entry (with an appended "1") is not added to the project.Confiscatory
S
1

I was struggling with the same problem and this worked for me.

  1. Add -> REST API Client" menu option when right-clicking on the project.
  2. Copy and paste the swagger url into "metadata file Swagger Url"
  3. At this point I was expecting the OK button to be enabled but it was not
  4. Click on the button Select Azure Asset
  5. Click return back
  6. The OK button should be enabled
  7. Click OK

This should regenerate the existing client. This is just a play-around but I was able to regenerate it without adding an extra client.

Santosantonica answered 19/12, 2020 at 12:37 Comment(0)
A
0

The most convenient solution for me is to use the command line for this case.

  1. Install the nswag command line tool:
npm install nswag -g
  1. Generate C# client code from a Swagger specification:
nswag openapi2csclient /input:MyWebService.json 
                       /classname:MyServiceClient 
                       /namespace:MyNamespace
                       /output:MyServiceClient.cs

The latest help on this command can be found here: https://github.com/RicoSuter/NSwag/wiki/CommandLine#client-generators

Alda answered 8/1, 2024 at 6:45 Comment(0)
G
0

Go to Project -> Connected Services -> Manage Connected Services enter image description here

or right click on a project, Add -> Connected service enter image description here

In Service References panel discover your Swagger client, click ... and choose Refresh. Note, Refresh button from top level doesn't work. enter image description here

Starting from VS 17.11.1, sometimes it is not working, returning an error. In that case choose your swagger.json in Solution Explorer, then try to change its Class Name. If error occurs, ignore it. If name was changed, change it back. Then try to refresh the service by the methods described above.

Glottic answered 31/8, 2024 at 12:50 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.