RicoSuter/NSwag

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

Open

#1,955 opened on Feb 13, 2019

View on GitHub
 (1 comment) (0 reactions) (0 assignees)C# (1,189 forks)batch import
help wantedproject: NSwag.CodeGeneration.CSharp (Controllers)

Repository metrics

Stars
 (6,291 stars)
PR merge metrics
 (No merged PRs in 30d)

Description

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 %}

Contributor guide