0

I have a get method on a .net Core 3.1 Web API controller that returns an expando object which is generated from a model class.

The model contains an enum value:

public class MyModel 
{
    public int Id { get; set; }
    public string Name { get; set; }
    public Gender Gender { get; set; }
}

public enum Gender
{
    Male,
    Female
}

[HttpGet("{id}")]
public async Task<IActionResult> GetAsync(int id)
{
    var recordFromDB = await dbService.GetAsync(id);

    if (recordFromDB == null)
        return NotFound();

    var returnModel = mapper.Map<MyModel>(recordFromDB).ShapeData(null);

    return Ok(returnModel);
}

public static ExpandoObject ShapeData<TSource>(this TSource source, string fields)
{
    var dataShapedObject = new ExpandoObject();

    if (string.IsNullOrWhiteSpace(fields))
    {
        var propertyInfos = typeof(TSource).GetProperties(BindingFlags.Public | BindingFlags.Instance);

        foreach (var propertyInfo in propertyInfos)
        {
            var propertyValue = propertyInfo.GetValue(source);

            ((IDictionary<string, object>)dataShapedObject).Add(propertyInfo.Name, propertyValue);
        }

        return dataShapedObject;
    }

    ... more of the method here but it's never hit so I've removed the code
}

If I do a get request for this record with the accept header application/json it all works fine.

if I change the accept header to application/xml I receive the following error:

System.Runtime.Serialization.SerializationException: Type 'Gender' with data contract name 'Gender' is not expected. Add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.

If I remove the enum value from the model the XML is generated fine.

Where do I add the KnownTypeAttribute or how do I get around this error?

Thanks in advance

Sun
  • 4,458
  • 14
  • 66
  • 108
  • How does your Get method look like? – Iztoksson Aug 24 '20 at 18:07
  • @Iztoksson I've updated the question to include the method – Sun Aug 24 '20 at 18:49
  • I don't see anything strange, apart from trying to map to an abstract class - does that work without errors? However this shouldn't be a reason for the error you are getting. – Iztoksson Aug 24 '20 at 19:17
  • Hi @Iztoksson. I need to apologize. I missed out a bit, I didn't think it was an issue but it looks like it is. I actually return an Expando object as I allow the consumer to shape the data. But for this example, I haven't passed the fields in. – Sun Aug 24 '20 at 19:50
  • I made a replica project and I don't get an error, I did notice however that with these last changes my "application/xml" is not honored anymore, I always get a JSON response in Postman at least. I found this [question](https://stackoverflow.com/questions/52698929/returning-a-dynamic-object-is-throwing-runtime-error), maybe it would be of some help. If that won't help I would try to get the Accept header from request and make my own serialization before returning a string (Json or Xml). – Iztoksson Aug 25 '20 at 06:18

0 Answers0