Включенное свойство возвращает объект, к которому оно относится - PullRequest
0 голосов
/ 15 февраля 2019

Я работаю над сайтом, использующим WebAPI в ASP.NET Core 2.1.Я использую Entity Framework Core 2.1.

У меня есть пользователь:

public class User : Account
{
    public UInt64 UserEmail { get; protected set; }
    public UserImage UserImage { get; set; }
    public virtual ICollection<Recipe> Recipes { get; set; }

    public User() { }

    public User(string nick, UInt64 login, byte[] salt, byte[] passwordHash,
        string restoreKey, UInt64 userEmail) : base(nick, login, salt, passwordHash, restoreKey)
    {
        UserEmail = userEmail;
        Role = "user";
    }

    public void Update(UInt64 login, UInt64 userEmail)
    {
        Login = login;
        UserEmail = userEmail;
        UpdatedAt = DateTime.UtcNow;
    }

    public void UpdatePassword(byte[] newPassword)
    {
        PasswordHash = newPassword;
        UpdatedAt = DateTime.UtcNow;
    }
}

и UserImage:

public class UserImage : Image
{
    public int? UserRef { get; set; }

    public virtual User User { get; set; }

    public UserImage() : base() { }

    public UserImage(string content) : base(content) { }
}

В моем DbContext у меня есть что-то вроде этого:

modelBuilder.Entity<User>()
            .HasOne(x => x.UserImage)
            .WithOne(y => y.User)
            .HasForeignKey<UserImage>(y => y.UserRef)
            .IsRequired(false);

У меня в службе есть метод, который возвращает пользователя с его изображением:

public async Task<User> GetAsync(int id)
    {
        var user = await _context.Users.GetById(id)
            .Include(x => x.UserImage)
            .SingleOrDefaultAsync();

        if (user == null)
            throw new CorruptedOperationException("Invalid id");

        return user;
    }

Проблема в том, что когда я использую Почтальона для получения пользователя, я получаю такой видответа:

{
"userEmail": 2606810040825320252,
"userImage": {
    "userRef": 16,
    "user": {
        "userEmail": 2606810040825320252,
        "recipes": null,
        "nick": "MadBear123",
        "login": 10458175107962595193,
        "salt": "Fv/S1pnpu1u0RA6RxE1wfwCmqhbkb0Fu0W2sOuFgv//PHyizyPtmuaX8OtYkCgSJPlKMGmE2qFgg2rgs70Ee9bbMU26iVhtIApqV/Zxac54P9EXBvgkAXede3YHzSPzHkvGz3WchUUDIQqHF+EmdvPT9KuYR1Djgywxh0bDbSJk=",
        "passwordHash": "Yk5S3jutaKJpwSQoRH0nk2At3nYL/Wzi+8QGRFOZuByi54o+YJHuhPYRMMSG3Vmimv1UMRWe+VA8ym2xQxoEJA==",
        "role": "user",
        "restoreKey": "Vub!@g17@kcP",
        "id": 16,
        "createdAt": "2018-10-14T11:11:43.9902857",
        "updatedAt": "2018-10-14T11:11:43.990384"
    },
    "imageContent": "/9j/4AAQSkZJRgABAQEASABIAAD/4QAiRXhpZgAATU0AKgAAAAgAAQESAAMAAAABAAEAAAAAAAD/2wBDAAIBAQIBAQICAgICAgICAwUDAwMDAwYEBAMFBwYHBwcGBwcICQsJCAgKCAcHCg0KCgsMDAwMBwkODw0MDgsMDAz/2wBDAQICAgMDAwYDAwYMCAcIDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAz/wAARCAAgACADASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6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id": 10,
    "createdAt": "2018-10-16T05:45:42.4644513",
    "updatedAt": "2018-10-16T05:45:42.4645292"
},
"recipes": null,
"nick": "MadBear123",
"login": 10458175107962595193,
"salt": "Fv/S1pnpu1u0RA6RxE1wfwCmqhbkb0Fu0W2sOuFgv//PHyizyPtmuaX8OtYkCgSJPlKMGmE2qFgg2rgs70Ee9bbMU26iVhtIApqV/Zxac54P9EXBvgkAXede3YHzSPzHkvGz3WchUUDIQqHF+EmdvPT9KuYR1Djgywxh0bDbSJk=",
"passwordHash": "Yk5S3jutaKJpwSQoRH0nk2At3nYL/Wzi+8QGRFOZuByi54o+YJHuhPYRMMSG3Vmimv1UMRWe+VA8ym2xQxoEJA==",
"role": "user",
"restoreKey": "Vub!@g17@kcP",
"id": 16,
"createdAt": "2018-10-14T11:11:43.9902857",
"updatedAt": "2018-10-14T11:11:43.990384"

}

Как вы можете видеть, я получаю пользователя с его изображением, и на этом изображении я получаю (снова) этого пользователя.Я хотел бы получить ответ как это:

{
"userEmail": 2606810040825320252,
"userImage": {
    "userRef": 16,
    "user": null,
    "imageContent": "/9j/4AAQSkZJRgABAQEASABIAAD/4QAiRXhpZgAATU0AKgAAAAgAAQESAAMAAAABAAEAAAAAAAD/2wBDAAIBAQIBAQICAgICAgICAwUDAwMDAwYEBAMFBwYHBwcGBwcICQsJCAgKCAcHCg0KCgsMDAwMBwkODw0MDgsMDAz/2wBDAQICAgMDAwYDAwYMCAcIDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAz/wAARCAAgACADASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKT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dHe0voPLdoZI3QFJIw6vACXkDkD550T4a61qHmC3jhZmwCd3T9K0JPgT4vZvlsvM3DGVfj9a6oymlaK179TCXI37z07dD6k+B3/BT/VPF+ixt8dvGnxw+JMvhXVDqnhfQtP8AEUVnZ3FxPb/Zp5r69niuLhsQBo4lEbGP7ROysm+RJfIf21v2pNU/a++KknjTVJLiBZ4I9IhtL3xFNq1zp0NuCY4fOu5ZLuaNVkAEk5JJ3ruYoSeb8IfslfEDxEFa2t7W3C95p9nqewNaXxW/Z48caV4b0e21OOzm/s8zKskV2824OwY/KVG0jpkZJGAeAMaSjVlG8l87av5mcalGMrRa9L6L0Wx//9k=",
    "id": 10,
    "createdAt": "2018-10-16T05:45:42.4644513",
    "updatedAt": "2018-10-16T05:45:42.4645292"
},
"recipes": null,
"nick": "MadBear123",
"login": 10458175107962595193,
"salt": "Fv/S1pnpu1u0RA6RxE1wfwCmqhbkb0Fu0W2sOuFgv//PHyizyPtmuaX8OtYkCgSJPlKMGmE2qFgg2rgs70Ee9bbMU26iVhtIApqV/Zxac54P9EXBvgkAXede3YHzSPzHkvGz3WchUUDIQqHF+EmdvPT9KuYR1Djgywxh0bDbSJk=",
"passwordHash": "Yk5S3jutaKJpwSQoRH0nk2At3nYL/Wzi+8QGRFOZuByi54o+YJHuhPYRMMSG3Vmimv1UMRWe+VA8ym2xQxoEJA==",
"role": "user",
"restoreKey": "Vub!@g17@kcP",
"id": 16,
"createdAt": "2018-10-14T11:11:43.9902857",
"updatedAt": "2018-10-14T11:11:43.990384"

}

Или что-то подобное.Я попытался по-своему работать с Include () и (или без) виртуальными, но я закончил с этим.

UPDATE

Определение для GetById ():

public static IQueryable<User> GetById(this IQueryable<User> value,int id)
     => value.Where(x => x.Id == id);

Полный проект можно найти на GitHub .Я работаю на ветке MB#20.

Ответы [ 2 ]

0 голосов
/ 19 февраля 2019

Мне удалось исправить мою проблему.Я изменил все частные сеттеры в свойствах моделей на общедоступный оригинальный ответ Дао Чжоу.

Мой метод ShapeData:

public static ExpandoObject ShapeData<TSource>(this TSource source, string fields)
    {
        if (source == null)
        {
            throw new ArgumentNullException("source");
        }

        var result = JsonConvert.SerializeObject(source, new JsonSerializerSettings
        {
            ReferenceLoopHandling = ReferenceLoopHandling.Ignore
        });
        source = JsonConvert.DeserializeObject<TSource>(result);
        var dataShapedObject = new ExpandoObject();
        // rest of my code
    }

Пользователь:

public class User : Account
{
    public UInt64 UserEmail { get; set; }
    public UserImage UserImage { get; set; }
    public virtual ICollection<Recipe> Recipes { get; set; }
    // rest of the code
}

UserImage:

public class UserImage : Image
{
    public int? UserRef { get; set; }

    public virtual User User { get; set; }

    public UserImage() : base() { }

    public UserImage(string content) : base(content) { }
}

Пример возвращаемых данных:

{
"userEmail": 2606810040825320252,
"userImage": {
    "userRef": 16,
    "user": null,
    "imageContent": "/9j/4AAQSkZJRgABAQEASABIAAD/4QAiRXhpZgAATU0AKgAAAAgAAQESAAMAAAABAAEAAAAAAAD/2wBDAAIBAQIBAQICAgICAgICAwUDAwMDAwYEBAMFBwYHBwcGBwcICQsJCAgKCAcHCg0KCgsMDAwMBwkODw0MDgsMDAz/2wBDAQICAgMDAwYDAwYMCAcIDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAz/wAARCAAgACADASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDB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5r69niuLhsQBo4lEbGP7ROysm+RJfIf21v2pNU/a++KknjTVJLiBZ4I9IhtL3xFNq1zp0NuCY4fOu5ZLuaNVkAEk5JJ3ruYoSeb8IfslfEDxEFa2t7W3C95p9nqewNaXxW/Z48caV4b0e21OOzm/s8zKskV2824OwY/KVG0jpkZJGAeAMaSjVlG8l87av5mcalGMrRa9L6L0Wx//9k=",
    "id": 10,
    "createdAt": "2018-10-16T05:45:42.4644513",
    "updatedAt": "2018-10-16T05:45:42.4645292"
},
"recipes": null,
"nick": "MadBear123",
"login": 10458175107962595193,
"salt": "Fv/S1pnpu1u0RA6RxE1wfwCmqhbkb0Fu0W2sOuFgv//PHyizyPtmuaX8OtYkCgSJPlKMGmE2qFgg2rgs70Ee9bbMU26iVhtIApqV/Zxac54P9EXBvgkAXede3YHzSPzHkvGz3WchUUDIQqHF+EmdvPT9KuYR1Djgywxh0bDbSJk=",
"passwordHash": "Yk5S3jutaKJpwSQoRH0nk2At3nYL/Wzi+8QGRFOZuByi54o+YJHuhPYRMMSG3Vmimv1UMRWe+VA8ym2xQxoEJA==",
"role": "user",
"restoreKey": "Vub!@g17@kcP",
"id": 16,
"createdAt": "2018-10-14T11:11:43.9902857",
"updatedAt": "2018-10-14T11:11:43.990384"
}
0 голосов
/ 18 февраля 2019

Для зацикливания ссылок вы не можете управлять им в EF Core.

Для общей обработки мы настраиваем его в Startup.cs как

.AddJsonOptions(options =>
{
    options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json
        .ReferenceLoopHandling.Ignore;
    options.SerializerSettings.ContractResolver =
        new CamelCasePropertyNamesContractResolver();
});

И сценарий использования:

var user = await _userService.GetAsync(id);
return Ok(user);

Для вашей проблемы вы изменили user перед возвратомreturn Ok(user.ShapeData(fields));, из-за которого JsonSerialize не смог узнать ссылку на цикл.

Для обходного пути перед преобразованием в ExpandoObject обработайте ссылку на цикл следующим образом:

public static ExpandoObject ShapeData<TSource>(this TSource source, string fields)
{
    if (source == null)
    {
        throw new ArgumentNullException("source");
    }
    var result =JsonConvert.SerializeObject(source, new JsonSerializerSettings{
        ReferenceLoopHandling = ReferenceLoopHandling.Ignore
    } );
    source = JsonConvert.DeserializeObject<TSource>(result);
    var dataShapedObject = new ExpandoObject();

    //your rest code
    return dataShapedObject;
}

Обновление

Для нуля в UserImage, это вызвано тем, что вы указываете свойства как public string ImageContent { get; protected set; }.

Попробуйте настроить DefaultContractResolver для десериализации защищенных свойств.

IncludePrivateStateContractResolver.cs

public class IncludePrivateStateContractResolver : DefaultContractResolver
{
    protected override List<MemberInfo> GetSerializableMembers(Type objectType)
    {
        const BindingFlags BindingFlags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public;
        var properties = objectType.GetProperties(BindingFlags);//.Where(p => p.HasSetter() && p.HasGetter());
        var fields = objectType.GetFields(BindingFlags);

        var allMembers = properties.Cast<MemberInfo>().Union(fields);
        return allMembers.ToList();
    }

    protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
    {
        var prop = base.CreateProperty(member, memberSerialization);

        if (!prop.Writable)
        {
            var property = member as PropertyInfo;
            if (property != null)
            {
                prop.Writable = property.HasSetter();
            }
            else
            {
                var field = member as FieldInfo;
                if (field != null)
                {
                    prop.Writable = true;
                }
            }
        }

        if (!prop.Readable)
        {
            var field = member as FieldInfo;
            if (field != null)
            {
                prop.Readable = true;
            }
        }

        return prop;
    }
}

public static class TypeExtensions
{
    public static bool HasSetter(this PropertyInfo property)
    {
        //In this way we can check for private setters in base classes
        return property.DeclaringType.GetMethods(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)
                                     .Any(m => m.Name == "set_" + property.Name);
    }

    public static bool HasGetter(this PropertyInfo property)
    {
        //In this way we can check for private getters in base classes
        return property.DeclaringType.GetMethods(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)
                                     .Any(m => m.Name == "get_" + property.Name);
    }
}

usecase:

public static ExpandoObject ShapeData<TSource>(this TSource source, string fields)
{
    var soureType = source.GetType();
    if (source == null)
    {
        throw new ArgumentNullException("source");
    }
    var serializeSettings = new JsonSerializerSettings{
        ReferenceLoopHandling = ReferenceLoopHandling.Ignore
    };
    var deserializeSettings = new JsonSerializerSettings{
        ContractResolver = new IncludePrivateStateContractResolver()
    };
    var result =JsonConvert.SerializeObject(source, serializeSettings );
    var castType = JsonConvert.DeserializeObject<TSource>(result, deserializeSettings);
    var dataShapedObject = new ExpandoObject();
...