Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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; }
}


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;
}
Posted
Updated 25-Jun-16 9:53am
v2

1 solution

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:

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


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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900