I would like to use AutoRest generated client for a webapi service by using "Add"/"REST API Client..." in visual studio 2017. However, it gives the following error:
[Info]AutoRest Core 0.16.0.0
...
[Fatal]Error generating client model: Collection format "multi" is not supported (in parameter 'xxx').
The older version of AutoRest (0.16.0) does not support "multi" collection format. So I installed the latest version AutoRest 0.17.3. using Nuget. But when I try "Add"/"REST API Client..." in visual studio 2017, it still refers to 0.16.0 version AutoRest and gives me the same error. It seems visual studio 2017 has a built-in AutoRest version 0.16 assembly.
Question: How do I get the latest version of AutoRest and integrate it in Visual studio 2017?
AutoRest is no longer published on Nuget, but npm (see https://github.com/Azure/autorest#installing-autorest). Unfortunately, the VS2017 integration uses a fixed version, so the best option is calling autorest directly :-) That'll also give you waaaaay more customizability and options. Let us know if you need any help with that.
Thank you for your info. I have installed AutoRest by npm. and run it in cmd:
AutoRest -CodeGenerator CSharp -Modeler Swagger -Input http://xxxrest.user.service/swagger/docs/v1 -Namespace Services.UserServiceClient -OutputDirectory E:test -AddCrendentials true
Is this right?
Is there a way to change the generated file and the class name for "restservice", which inherited "ServiceClient"?
And there is a "XmlSerialization.cs" file in Models folder. What do we use it for?
the command looks good to me :-)
The name of the client class is determined from the title field in the Swagger's info section. But yes, you can also override that with -ClientName ...
Regarding the XmlSerialization.cs: It's used for XML (de-)serializing request/response bodies - I'm guessing you have a produces/consumes section in your Swagger that declares XML media types? The file should not be generated if you only have JSON there.
You are right. Thank you. It's helpful.
I have another question about autorest generated operations.
In my webapi service, I have something like follows:
[Route("books")]
[HttpGet]
[ResponseType(typeof(GetBooksResponse))]
public IHttpActionResult GetBooks([FromUri] GetBooksRequest request)
where GetBooksRequest is a complex object:
public int BookId
public string PublisherName
public string GenreName
public List<string> AuthorNames
In AutoRest generated operation, I got:
public async System.Threading.Tasks.Task
Question: is there any AutoRest extensions for Swagger, so that the generated operation takes in a single complex object instead of multiple parameters?
public async System.Threading.Tasks.Task
AutoRest would generate exactly what you expect if the GetBooksRequest was a body parameter. But no, unfortunately it won't do that for Uri parameters (I assume that's what [FromUri] means) and there is currently no way to enable that.
Thank you again for your help.
I encountered another problem here.
In my webapi service, the aforementioned GetBooksRequest inherited from another class ServiceRequest. ServiceRequest is defined in one of the assembly dependencies of webapi service project.
Previously, when I used "Add"/"REST API Client..." in visual studio 2017 or run AutoRest.exe (from Nuget package), I got ServiceRequest in Models of autorest generated code.
But when I run AutoRest -CodeGenerator CSharp -Modeler Swagger -Input http://xxxrest.user.service/swagger/docs/v1 -Namespace Services.UserServiceClient -OutputDirectory E:\test -AddCrendentials true there is no ServiceRequest in Models
How can I fix this problem?
/////////////////////////////////////
In Swagger.Json file,
-definitions
--- GetBooksRequest
---- type: object
---- allOf [2]
------- 0
---------- $ref: #/definitions/ServiceRequest
---------- x-ms-external: true
------- 1
---------- type: object
---------- properties
------------ BookIds
-------------- type: array
-------------- items:
---------------- format: int 32
---------------- type: integer
----------- PublisherName
-------------- type: string
...
And would you please take a look at the following question as well. I got errors when using list<int> in uri as querystring.
In my webapi service, I have:
```
[Route("books")]
[HttpGet]
[ResponseType(typeof(GetBooksResponse))]
public IHttpActionResult GetBooks([FromUri] GetBooksRequest request)
where `GetBooksRequest` is a complex object:
public List
public string PublisherName
public string GenreName
public List
In AutoRest generated operations
`public async System.Threading.Tasks.Task GetBooksWithHttpMessagesAsync(System.Collections.Generic.IList authorNames = default(System.Collections.Generic.IList), string publisherName= default(string), System.Collections.Generic.IList<int?> bookIds = default(System.Collections.Generic.IList<int?>), string GenreName= default(string), string accept = "application/json; version=1", System.Collections.Generic.Dictionary<string, System.Collections.Generic.List> customHeaders = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken))`
when constructing the URL:
if (bookIds != null)
{
if (bookIds.Count == 0)
{
_queryParameters.Add(string.Format("BookIds={0}", System.Uri.EscapeDataString(string.Empty)));
}
else
{
foreach (var _item in bookIds)
{
_queryParameters.Add(string.Format("BookIds={0}", System.Uri.EscapeDataString(_item ?? string.Empty)));
}
}
}
```
_item ??string.Empty throws an error: Operator '??' cannot be applied to operands of type 'int?' and 'string'
1) You set x-ms-external: true which explicitly asks AutoRest to not generate a class - hence I guess it is not generated :-) Furthermore, I'd personally not use allOf with 2 "items" here, but model the second case "directly", i.e. give the model those properties directly and then only allOf on ServiceRequest. That might also resolve some oddities.
2) Yes, unfortunately we currently only support lists of strings in URLs -- UPDATE: non-strings supported now!
Thank you for your reply.
I'll close this issue since the initial question was addressed, but feel free to post further questions! :-)
Has anyone brought this up to the Visual Studio team?
"VS2017 integration uses a fixed version"
Does that translates to no updates for the life of VS2017 ?
Its been a year now. Can i update Rest API Client now in VS?
NSwag looks very promising.
Most helpful comment
Has anyone brought this up to the Visual Studio team?
"VS2017 integration uses a fixed version"
Does that translates to no updates for the life of VS2017 ?