Swashbuckle.webapi: How to change a property from optional to required?

Created on 8 Jan 2016  路  7Comments  路  Source: domaindrivendev/Swashbuckle.WebApi

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?

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.

All 7 comments

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

joseph-ortiz picture joseph-ortiz  路  4Comments

benpriebe picture benpriebe  路  5Comments

johnpmcclung picture johnpmcclung  路  3Comments

DotNetRockStar picture DotNetRockStar  路  3Comments

qwertykeith picture qwertykeith  路  5Comments