在子类中需要的注解覆盖了主类中的JsonIgnore属性。

huangapple go评论63阅读模式
英文:

Required Annotation in Sub-Class overrules JsonIgnore Attribute in Main-Class

问题

The [Required] attribute on the child class does take precedence over the [JsonIgnore] attribute on the parent class when it comes to JSON serialization and deserialization. This is why you're encountering issues with deserialization when using the [JsonIgnore] attribute.

To fix this, you can consider using a custom JSON converter or creating a separate DTO (Data Transfer Object) for serialization and deserialization. The DTO can exclude the reference to the parent and include only the necessary properties, while your original class with [Required] attributes can be used for UI and other purposes.

If you decide to use a custom JSON converter, you can implement custom logic to handle the serialization and deserialization of the parent-child relationship in a way that works for your requirements.

In summary, to address this issue, you may need to separate your serialization and UI concerns by using DTOs or custom converters to control how your objects are serialized and deserialized.

英文:

tldr: serialisation works as expected, deserialisation ignores [JsonIgnore] attribute when ignored child has a [Required] attribute.

I use the System.Text.Json Serializer (Newtonsoft is not included in the Project) to Read/Write a list of parent objects over REST. The parent class has a list of child objects. The Child objects have a referece set back to the parent. Although I talk about childs and parents, they do not inherent from each other. To avoid a loop i set the reference to the parent to [JsonIgnore]. Works on the GET-Endpoint. Failes on the PUT.
Error message:

  "title": "One or more validation errors occurred.",
  "status": 400,
  "traceId": "00-d41897820e0a0771cf9278cb64fb8bb4-36a021adba8a4493-00",
  "errors": {
    "": [
      "The input was not valid."
    ],
    "Parents[0].Childs[0].Parent.Prop1": [
      "The Prop1 field is required."
    ],
    "Parents[0].Childs[0].Parent.Prop2": [
      "The Prop2 field is required."
    ],
    "Parents[0].Childs[0].Parent.Prop3": [
      "The Prop3 field is required."
    ],
    "Parents[0].Childs[1].Parent.Prop1": [
      "The Prop1 field is required."
    ],
    "Parents[0].Childs[1].Parent.Prop2": [
      "The Prop2 field is required."
    ],
    "Parents[0].Childs[1].Parent.Prop3": [
      "The Prop3 field is required."
    ],
...

The Main-Class:

    public class Parent
    {
        public int ID { get; set; }

        
        [Required(AllowEmptyStrings = false)]
        public string Prop1{ get; set; } = string.Empty;

        
        [Required(AllowEmptyStrings = false)]
        public string Prop2 { get; set; } = string.Empty;

        
        [Required(AllowEmptyStrings = false)]
        public string Prop3 { get; set; } = string.Empty;

        
        public List<Child> Childs{ get; set; } = new();
    }

The Child-Class:

    public class Child
    {        
        public int Id { get; set; }

        [JsonIgnore] 
        public Parent Parent { get; set; } = new();

        [Required]
        public int ChildProp { get; set; }
       
    }

Is the [Required] Attribute of a Child higher-Prior than a [JsonIgnore] in the Parent?
How can I fix that? Removing the Required Annotations is not a Option because I neet them for my UI.

I tried to use the different Conditions of the JsonIgnore with no effect.

答案1

得分: 0

我找到了一个解决方案。将父属性的getter方法更改为internal就可以了。

public class Child
{        
    public int Id { get; set; }

    [JsonIgnore] 
    public Parent Parent { internal get; set; } = new();

    [Required]
    public int ChildProp { get; set; }
   
}

再次感谢AVTUNEY向我展示自定义JSON转换器。通过稍加努力,可以确实以这种方式解决问题。

英文:

I found a solution. Making the getter of the Parent-Property internal will do the trick.

public class Child
{        
    public int Id { get; set; }

    [JsonIgnore] 
    public Parent Parent { internal get; set; } = new();

    [Required]
    public int ChildProp { get; set; }
   
}

Thanks again for AVTUNEY for showing me custom json converters. With a little extra effort, the problem could certainly have been solved this way.

huangapple
  • 本文由 发表于 2023年4月17日 18:09:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76033992.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定