我试图使用autorest来生成我的web api客户端,但返回datacontract类型总是作为对象生成 [英] I am trying to use autorest to generate my web api client, but the return datacontract types are always generated as object

查看:85
本文介绍了我试图使用autorest来生成我的web api客户端,但返回datacontract类型总是作为对象生成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图调用自己的Web API调用来获取一个我定义为数据契约的对象。我使用Swagger来装饰调用,在客户端我使用AutoREST生成客户端。但是,当我在[DataContract]对象中传递的调用工作正常时,返回值总是在生成的客户端代码中设置为对象,而不是我定义的相应[DataContract]类。



我一直在寻找有关Swagger / Swashbuckle和AutoREST的更多信息以及我需要做什么,但没有太多帮助。这是VS 2015 Pro使用.Net 4.5.2。



当我浏览并查看实际的HttpResponse数据时,我将我的对象序列化为JSON。但是,生成的客户端只想反序列化为Object,当然返回null。我可以破解生成的代码,但是每当它重新生成时我都必须这样做。



我将不胜感激任何见解...



我的尝试:



这是DataContracts之一:

I am attempting to call my own Web API call to get a an object that I define as a Data Contract. I am using Swagger to decorate the call, and in the client I am generating the client with AutoREST. However while my calls that pass in a [DataContract] object works fine, the return values are always set as object in the generated client code, rather than the appropriate [DataContract] class I have defined.

I have been searching for more info on Swagger/Swashbuckle and AutoREST and what I need to do, but not a lot of help. This is with VS 2015 Pro using .Net 4.5.2.

When I trace through and look at the actual HttpResponse data, I am getting my object serialized as JSON. However the generated client only wants to deserialize as Object, which of course returns a null. I could hack the generated code, but I would have to do this whenever it gets regenerated.

I would appreciate any insights on this...

What I have tried:

Here is one of the DataContracts:

[DataContract]
public class Company
{
    [DataMember(Name = "id")]
    public int Id { get; set; }

    [DataMember(Name = "companyName")]
    public string CompanyName { get; set; }

    [DataMember(Name = "website")]
    public string Website { get; set; }

    [DataMember(Name = "description")]
    public string CompanyDescription { get; set; }
}





这是一个GET实例调用:



And here is a call to GET an instance:

[SwaggerOperation("GetCompanyById")]
[SwaggerResponse(HttpStatusCode.OK)]
[SwaggerResponse(HttpStatusCode.NotFound)]
public DataContract.Company Get(int id)
{
    //*** work to retrieve the data, validate etc. ***
    DataContract.Company company = ...
	
    return company;
}

推荐答案

想出来。似乎SwaggerResponse属性上还有其他参数我不知道(由于Swashbuckle包上没有任何明确的文档)。我通过使用ObjectBrowser找到了这些,发现还有一组可选参数:



Figured it out. It seems that there are additional parameters on the SwaggerResponse attributes that I was not aware of (due to a lack of any clear documentation on the Swashbuckle package). I found these by using the ObjectBrowser and found that there is an additional set of optional parameters:

[SwaggerResponse(HttpStatusCode.OK, Description = "Get a Company by ID", Type = typeof(DataContract.Company))]





无论如何,有了这个,REST客户端就是生成正确。我希望这有助于任何人自己尝试...



Anyways, with that in place, the REST client is generated correctly. I hope this helps anyone trying this themselves...


这篇关于我试图使用autorest来生成我的web api客户端,但返回datacontract类型总是作为对象生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆