I'm trying to change a property to required because swagger docs shows it as optional.
ServiceStack shows an ApiMember(IsRequired=true) attribute however I cannot find it in the Swashbuckle package.
Is there an alternative?
I did it by adding the DataAnnotations library to my class and then I added [Required] to the property. Swagger no longer shows it as optional.
@jreames9 Thanks, it worked!
The full namespace I used is System.ComponentModel.DataAnnotations.
Example for others:
using System;
using System.ComponentModel.DataAnnotations;
namespace WebApi1.Models
{
public class User
{
[Required]
public string Name { get; set; }
// The age is optional
public int Age { get; set; }
}
}
Is it possible to set this on a action parameter? At the moment even with the code above, the action parameter is shown as optional by Swashbuckle
I have a similar issue. My definitions are created at runtime. They're not even types so I can't decorate them with an attribute.
Sure would be nice if it defaulted to required with some mechanism to specify "Optional" so we don't have to decorate a bazillion properties.
This is why TypeScript's strictnull mode is awesome.
Annotate it with [JsonRequired].
Please note that it is a part of Newtonsoft.Json
Most helpful comment
Sure would be nice if it defaulted to required with some mechanism to specify "Optional" so we don't have to decorate a bazillion properties.