JamesNK / Newtonsoft.Json

Json.NET is a popular high-performance JSON framework for .NET
https://www.newtonsoft.com/json
MIT License
10.76k stars 3.25k forks source link

JsonProperty required allowed but not respected on record parameters #2951

Open mikethea1 opened 5 months ago

mikethea1 commented 5 months ago

Source/destination types

record R([JsonProperty(Required = Required.Always)] string A, [JsonProperty(Required = Required.Always)] string[] B);

Source/destination JSON

{}

Expected behavior

Deserialization error because the required properties were not present.

Actual behavior

Deserializes successfully with all properties null.

Steps to reproduce

JsonConvert.DeserializeObject<R>("{}");
MO2k4 commented 5 months ago

Your attribute is invalid, try to use [property:JsonProperty...]

elgonzo commented 5 months ago

@MO2k4

No, the application of the attribute as shown is not invalid. It's meant to support constructor parameters:

https://github.com/JamesNK/Newtonsoft.Json/blob/2eaa475f8853f2b01ac5591421dcabd7a44f79ce/Src/Newtonsoft.Json/JsonPropertyAttribute.cs#L34-L35