Nswag: C# Web API Controller : Generator does not handle "required" fields for path and query param

Created on 13 Feb 2019  路  1Comment  路  Source: RicoSuter/NSwag

Hello, I use NSwag to generate C# Web API controller code from swagger document. and It looks like required field of path/query param is not "translated" into "System.ComponentModel.DataAnnotations.Required"

For instance: here's apart of my swagger path definition file

"/myroute": {
  "get": {
        "parameters": [
          {
             "name": "type",
             "type": "string",
             "in": "query",
             "required": true          // <---  required parameters 
          }
        ]
    }
}

I would want the code generated to be

[Microsoft.AspNetCore.Mvc.HttpGet, Microsoft.AspNetCore.Mvc.Route("myroute")]
public Task<string> GetMethod([System.ComponentModel.DataAnnotations.Required] string type) // <-- The attribute Required was added
{
    return type;
}

What is you opinion ?

I think the only thing needed is to modify the template Controller.liquid by adding
something like this :

{% if parameter.IsRequired %}[System.ComponentModel.DataAnnotations.Required]{% endif %}
help wanted NSwag.CodeGeneration.CSharp (Controllers)

>All comments

In ASP.NET Core we need to use BindRequiredAttribute and not the data annotation's RequiredAttribute

Was this page helpful?
0 / 5 - 0 ratings