RicoSuter/NSwag

Returned status codes not matching OpenApi specified status code

Open

#2,087 opened on Apr 8, 2019

View on GitHub
 (0 comments) (3 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

Possibly related to #1918

I have an HTTP PUT request specified in my OpenAPI code which should return an HTTP 201 Created response.

Unfortunately, currently it seems to only be capable of returning a 200 OK response in WebAPI. Nswag should honour the specification laid out in the OpenAPI documentation.


I have found what seems like a reasonable workaround, but am lacking one step necessary to get it to work:

My Controller.liquid file contains the following:

    return _implementation.{{ operation.ActualOperationName }}Async(HttpContext{% if operation.Parameters.size > 0 -%}, {% endif -%} {% for parameter in operation.Parameters %}{{ parameter.VariableName }}{% if parameter.IsLast == false %}, {% endif %}{% endfor %});

(simplified slightly to remove extraneous information)

This results in code such as

return _implementation.MyMethodNameAsync(HttpContext, myHeader, body, pathParam);

I have worked out that if I can switch in the liquid template on whether or not the required status code is 201, the following code could be produced instead with relative ease:

var response = _implementation.MyMethodNameAsync(HttpContext, myHeader, body, pathParam);
return Created(HttpContext.Request.GetDisplayUrl(), response.Result);

The question is: is it possible to, and if so, how can I, switch on a conditional in the template based on whether an operation's expected status code is Created?

Contributor guide