C# serialization fails on private setters

10 views
Skip to first unread message

Andrew Thomas

unread,
Jul 20, 2017, 2:10:45 PM7/20/17
to Swagger
I am trying to use Swagger to create an API in C#, using the nancyfx language option.

The Models are all generated with private setters, for example:

public sealed class ApiResponse:  IEquatable<ApiResponse>
   
{
       
public int? Code { get; private set; }
       
public string Type { get; private set; }
       
public string Message { get; private set; }
       
...
     
}


I want to serialize this object to XML.  Normally I would use the C# XmlSerializer class to do that, but it fails because it requires public getters and setters on all serialized members.

How can I resolve this?  
* Is there a recommended XML serializer that works with swagger-codegen output?
* Is there a way to tell swagger-codegen to create objects with public setters?

Andrew Thomas

unread,
Jul 20, 2017, 3:11:35 PM7/20/17
to Swagger
Looking at the mustache templates for nancyfx, I can see that swagger-codegen has a file called modelMutable.mustache that does what I want.  So, one more question:
* Is there a way to tell swagger-codegen that I want it to use the mutable model template instead of the immutable template?

Andrew Thomas

unread,
Jul 21, 2017, 6:08:10 AM7/21/17
to Swagger
I ended up unpacking the JAR and modifying modelGeneric.mustache.  I know that's not a great answer, but it looks like I will need to hack the mustache files anyway to get what I want.
Reply all
Reply to author
Forward
0 new messages