Marking a property as Json Required with Newtonsoft just makes object null

I have a request object for my API and I want to enforce that my properties are always present in the body. Here is my object:

public class Code
{
    [JsonProperty("id", Required = Required.Always)]
    public string id { get; set; }

    [JsonProperty("type", Required = Required.Always)]
    public string type { get; set; }
}

However, when I don't pass in the type property in my request body, my Code object is null. Instead I would want a bad request error to get propagated back to the client. Will the Newtonsoft decorator not do that for me here? Or would I have to manually add checks to see if the properties are not null?



Read more here: https://stackoverflow.com/questions/66995271/marking-a-property-as-json-required-with-newtonsoft-just-makes-object-null

Content Attribution

This content was originally published by DannyD at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: