Fix fields serialization

This commit is contained in:
Zoe Roux 2023-10-27 20:45:03 +02:00
parent c7db07f7ba
commit 86427cf6ef
7 changed files with 23 additions and 20 deletions

View File

@ -17,12 +17,11 @@
// along with Kyoo. If not, see <https://www.gnu.org/licenses/>. // along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
using System; using System;
using Kyoo.Abstractions.Controllers;
namespace Kyoo.Abstractions.Models.Attributes namespace Kyoo.Abstractions.Models.Attributes
{ {
/// <summary> /// <summary>
/// The targeted relation can be loaded via a call to <see cref="ILibraryManager.Load"/>. /// The targeted relation can be loaded.
/// </summary> /// </summary>
[AttributeUsage(AttributeTargets.Property)] [AttributeUsage(AttributeTargets.Property)]
public class LoadableRelationAttribute : Attribute public class LoadableRelationAttribute : Attribute

View File

@ -84,7 +84,7 @@ namespace Kyoo.Abstractions.Models
[SerializeIgnore] public int ShowId { get; set; } [SerializeIgnore] public int ShowId { get; set; }
/// <summary> /// <summary>
/// The show that contains this episode. This must be explicitly loaded via a call to <see cref="ILibraryManager.Load"/>. /// The show that contains this episode.
/// </summary> /// </summary>
[LoadableRelation(nameof(ShowId))] public Show? Show { get; set; } [LoadableRelation(nameof(ShowId))] public Show? Show { get; set; }
@ -95,7 +95,6 @@ namespace Kyoo.Abstractions.Models
/// <summary> /// <summary>
/// The season that contains this episode. /// The season that contains this episode.
/// This must be explicitly loaded via a call to <see cref="ILibraryManager.Load"/>.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This can be null if the season is unknown and the episode is only identified /// This can be null if the season is unknown and the episode is only identified

View File

@ -25,8 +25,15 @@ using Kyoo.Abstractions.Models.Attributes;
namespace Kyoo.Abstractions.Models.Utils; namespace Kyoo.Abstractions.Models.Utils;
/// <summary>
/// The aditional fields to include in the result.
/// </summary>
/// <typeparam name="T">The type related to the new fields</typeparam>
public class Include<T> public class Include<T>
{ {
/// <summary>
/// The aditional fields to include in the result.
/// </summary>
public ICollection<string> Fields { get; private init; } = ArraySegment<string>.Empty; public ICollection<string> Fields { get; private init; } = ArraySegment<string>.Empty;
public static Include<T> From(string? fields) public static Include<T> From(string? fields)

View File

@ -129,18 +129,18 @@ namespace Kyoo.Core.Controllers
// Sort<PeopleRole>? sort = default, // Sort<PeopleRole>? sort = default,
// Pagination? limit = default) // Pagination? limit = default)
// { // {
// ICollection<PeopleRole> people = await ApplyFilters(_database.PeopleRoles // ICollection<PeopleRole> people = await ApplyFilters(_database.PeopleRoles
// .Where(x => x.ShowID == showID) // .Where(x => x.ShowID == showID)
// .Include(x => x.People), // .Include(x => x.People),
// x => x.People.Name, // x => x.People.Name,
// where, // where,
// sort, // sort,
// limit); // limit);
// if (!people.Any() && await _shows.Value.GetOrDefault(showID) == null) // if (!people.Any() && await _shows.Value.GetOrDefault(showID) == null)
// throw new ItemNotFoundException(); // throw new ItemNotFoundException();
// foreach (PeopleRole role in people) // foreach (PeopleRole role in people)
// role.ForPeople = true; // role.ForPeople = true;
// return people; // return people;
// } // }
// /// <inheritdoc /> // /// <inheritdoc />

View File

@ -37,6 +37,7 @@ public class IncludeBinder : IModelBinder
object include = bindingContext.ModelType.GetMethod(nameof(Include<object>.From))! object include = bindingContext.ModelType.GetMethod(nameof(Include<object>.From))!
.Invoke(null, new object?[] { fields.FirstValue })!; .Invoke(null, new object?[] { fields.FirstValue })!;
bindingContext.Result = ModelBindingResult.Success(include); bindingContext.Result = ModelBindingResult.Success(include);
bindingContext.HttpContext.Items["fields"] = ((dynamic)include).Fields;
return Task.CompletedTask; return Task.CompletedTask;
} }
catch (TargetInvocationException ex) catch (TargetInvocationException ex)

View File

@ -58,9 +58,6 @@ namespace Kyoo.Core.Api
{ {
property.ShouldSerialize = _ => property.ShouldSerialize = _ =>
{ {
string resType = (string)_httpContextAccessor.HttpContext!.Items["ResourceType"]!;
if (member.DeclaringType!.Name != resType)
return false;
ICollection<string> fields = (ICollection<string>)_httpContextAccessor.HttpContext!.Items["fields"]!; ICollection<string> fields = (ICollection<string>)_httpContextAccessor.HttpContext!.Items["fields"]!;
return fields.Contains(member.Name); return fields.Contains(member.Name);
}; };

View File

@ -82,7 +82,7 @@ namespace Kyoo.Tests.Database
} }
public IRepository<T> GetRepository<T>() public IRepository<T> GetRepository<T>()
where T: class, IResource where T : class, IResource
{ {
return _repositories.First(x => x.RepositoryType == typeof(T)) as IRepository<T>; return _repositories.First(x => x.RepositoryType == typeof(T)) as IRepository<T>;
} }