Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RestSharp serialization not serializing class name correctly

RestSharp doesn't seem to be paying attention to the "SerializeAs" attribute that I've decorated one of my class names with:

Person

[Serializable, SerializeAs(Name = "person")]
public class Person
{
    [SerializeAs(Name = "first-name")]
    public string FirstName { get; set; }

    [SerializeAs(Name = "contact-data")]
    public ContactData ContactData { get; set; }
}

ContactData

public class ContactData
{
    [SerializeAs(Name = "email-addresses")]
    public List<EmailAddress> EmailAddresses { get; set; }
}

EmailAddress

[SerializeAs(Name = "email-address")]
public class EmailAddress
{
    [SerializeAs(Name = "address")]
    public string Address { get; set; }

    [SerializeAs(Name = "location")]
    public string Location { get; set; }
}

I'm using the following code to serialize the XML:

var request = new RestRequest("people/{id}.xml", Method.PUT);
request.AddParameter("id", person.Id, ParameterType.UrlSegment);
request.XmlSerializer = new XmlSerializer();
request.AddBody(person);

However, the resulting XML looks like this:

<person>
    <first-name>Scott</first-name> 
    <contact-data>
        <email-adresses>
            <EmailAddress>
                <address>[email protected]</address> 
                <location>Work</location> 
             </EmailAddress>
        </email-adresses>
    </contact-data>
</person>

You can see that the <EmailAddress> element appears to be ignoring the SerializeAs attribute and is not serialized to "email-address" as I would expect it to be while all of the others work just fine. Does anyone know why this might be happening or how to fix it?

like image 584
Scott Avatar asked Apr 15 '26 22:04

Scott


1 Answers

After poking around the RestSharp source, I realized this is a bug in the XmlSerializer built into RestSharp. I've fixed it and submitted a pull request.

like image 89
Scott Avatar answered Apr 17 '26 11:04

Scott