Reputation: 18188
I am using Autorest to generate code to call my api.
I want to use the following in my unit tests
private JobHeaderRequestObject MakeRequestFromResponse(JobHeaderResponseObject InResponseObject)
{
// make use of the fact that in the server side code we know JobHeaderResponseObject inherits from JobHeaderRequestObject
return MySerializer.DeepConvert<JobHeaderResponseObject,JobHeaderRequestObject>(InResponseObject);
}
internal class MySerializer
{
public static T2 DeepConvert<T1,T2>(T1 obj)
{
using (var ms = new MemoryStream())
{
var formatter = new BinaryFormatter();
formatter.Serialize(ms, obj);
ms.Position = 0;
return (T2)formatter.Deserialize(ms);
}
}
}
However when I try to run this code I get an error mentioning
System.Runtime.Serialization.SerializationException
indicating that the type
is not marked as serializable
I don't want to go through the generated code adding the attribute
[Serializable]
to each type, because the code will get overwritten the next time I generate.
I looked at this issue on the Git Hub project and it mentions
If the swagger spec has a model definition like this
{
"Foo": {
"properties": {
"name": {
"type": "string"
},
"age": {
"type": "number"
}
},
"additionalProperties": {
"type": "string"
}
}
}
then Autorest generated SDKs should be able to serialize and deserialize (round trip) the unknown properties correctly.
Generated C# clients support this today by having a Dictionary AdditionalProperties.
So now I am trying to understand what an Autorest spec is and how I set it up.
In startup.cs I have in Configure
app.UseSwagger(c =>
{
c.RouteTemplate =
"api-docs/{documentName}/swagger.json";
});
app.UseSwaggerUI(c =>
{
//Include virtual directory if site is configured so
c.RoutePrefix = "api-docs";
c.SwaggerEndpoint("./v1/swagger.json", "Api v1");
});
In ConfigureServices I have
services.AddSwaggerGen(c =>
{
c.OperationFilter<AuthorizationHeaderParameterOperationFilter>();
c.SwaggerDoc("v1", new Info
{
Version = "v1",
Title = "My API",
Description = "ASP.NET Core Web API",
TermsOfService = "None",
Contact = new Swashbuckle.AspNetCore.Swagger.Contact
{
Name = "MyName",
Email = "[email protected]"
}
});
c.EnableAnnotations();
c.DescribeAllEnumsAsStrings();
c.ParameterFilter<SwaggerEnumParameterFilter>();
c.SchemaFilter<SwaggerEnumFilter>();
});
Upvotes: 0
Views: 220