Fixing the json serializer for multiple level inheritance

This commit is contained in:
Zoe Roux 2020-08-30 18:46:20 +02:00
parent ff1be5c145
commit 8430f0f1d8
2 changed files with 21 additions and 15 deletions

View File

@ -40,11 +40,11 @@ namespace Kyoo.CommonApi
[JsonDetailed] [JsonDetailed]
public virtual async Task<ActionResult<T>> Get(string slug) public virtual async Task<ActionResult<T>> Get(string slug)
{ {
T ressource = await _repository.Get(slug); T resource = await _repository.Get(slug);
if (ressource == null) if (resource == null)
return NotFound(); return NotFound();
return ressource; return resource;
} }
[HttpGet] [HttpGet]

View File

@ -32,20 +32,26 @@ namespace Kyoo.Controllers
private bool IsIgnored(Type type, string propertyName) private bool IsIgnored(Type type, string propertyName)
{ {
if (_ignored.ContainsKey(type) && _ignored[type].Contains(propertyName)) while (type != null)
return true; {
if (type.BaseType == null) if (_ignored.ContainsKey(type) && _ignored[type].Contains(propertyName))
return false; return true;
return _ignored.ContainsKey(type.BaseType) && _ignored[type.BaseType].Contains(propertyName); type = type.BaseType;
}
return false;
} }
private bool IsSerializationForced(Type type, string propertyName) private bool IsSerializationForced(Type type, string propertyName)
{ {
if (_forceSerialize.ContainsKey(type) && _forceSerialize[type].Contains(propertyName)) while (type != null)
return true; {
if (type.BaseType == null) if (_forceSerialize.ContainsKey(type) && _forceSerialize[type].Contains(propertyName))
return false; return true;
return _forceSerialize.ContainsKey(type.BaseType) && _forceSerialize[type.BaseType].Contains(propertyName); type = type.BaseType;
}
return false;
} }
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization) protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
@ -75,7 +81,7 @@ namespace Kyoo.Controllers
result.Formatters.Add(new NewtonsoftJsonOutputFormatter( result.Formatters.Add(new NewtonsoftJsonOutputFormatter(
new JsonSerializerSettings new JsonSerializerSettings
{ {
ContractResolver = new JsonPropertySelector(null, new Dictionary<Type, HashSet<string>>() ContractResolver = new JsonPropertySelector(null, new Dictionary<Type, HashSet<string>>
{ {
{typeof(Show), new HashSet<string> {"genres", "studio"}}, {typeof(Show), new HashSet<string> {"genres", "studio"}},
{typeof(Episode), new HashSet<string> {"tracks"}}, {typeof(Episode), new HashSet<string> {"tracks"}},