Skip to content

Commit

Permalink
Replace hard-coded status codes with fields (dotnet#15426)
Browse files Browse the repository at this point in the history
* Replace hard-coded status codes with fields

* more work
  • Loading branch information
scottaddie authored Oct 31, 2019
1 parent 7433eb2 commit 0ed167e
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 22 deletions.
2 changes: 1 addition & 1 deletion aspnetcore/tutorials/getting-started-with-NSwag.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ The preceding action returns `IActionResult`, but inside the action it's returni

::: moniker range=">= aspnetcore-2.1"

Because NSwag uses [Reflection](/dotnet/csharp/programming-guide/concepts/reflection), and the recommended return type for web API actions is [ActionResult\<T>](xref:Microsoft.AspNetCore.Mvc.ActionResult%601), it can only infer the return type defined by `T`. You can't automatically infer other possible return types.
Because NSwag uses [Reflection](/dotnet/csharp/programming-guide/concepts/reflection), and the recommended return type for web API actions is [ActionResult\<T>](xref:Microsoft.AspNetCore.Mvc.ActionResult%601), it can only infer the return type defined by `T`. You can't automatically infer other possible return types.

Consider the following example:

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using TodoApi.Models;
Expand Down Expand Up @@ -69,8 +70,8 @@ public IActionResult GetById(long id)
/// <response code="201">Returns the newly created item</response>
/// <response code="400">If the item is null</response>
#region snippet_CreateActionAttributes
[ProducesResponseType(typeof(TodoItem), 201)] // Created
[ProducesResponseType(400)] // BadRequest
[ProducesResponseType(typeof(TodoItem), StatusCodes.Status201Created)] // Created
[ProducesResponseType(StatusCodes.Status400BadRequest)] // BadRequest
#endregion snippet_CreateActionAttributes
#region snippet_CreateAction
[HttpPost]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using TodoApi.Models;
Expand Down Expand Up @@ -69,8 +70,8 @@ public IActionResult GetById(long id)
/// <response code="201">Returns the newly created item</response>
/// <response code="400">If the item is null</response>
[HttpPost]
[ProducesResponseType(typeof(TodoItem), 201)]
[ProducesResponseType(400)]
[ProducesResponseType(typeof(TodoItem), StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public IActionResult Create([FromBody] TodoItem item)
{
if (item == null)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using TodoApi.Models;
Expand Down Expand Up @@ -68,8 +69,8 @@ public ActionResult<TodoItem> GetById(long id)
/// <response code="201">Returns the newly created item</response>
/// <response code="400">If the item is null</response>
#region snippet_CreateActionAttributes
[ProducesResponseType(201)] // Created
[ProducesResponseType(400)] // BadRequest
[ProducesResponseType(StatusCodes.Status201Created)] // Created
[ProducesResponseType(StatusCodes.Status400BadRequest)] // BadRequest
#endregion snippet_CreateActionAttributes
#region snippet_CreateAction
[HttpPost]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using TodoApi.Models;

namespace TodoApi.Controllers
{
#region snippet_TodoController
#region snippet_TodoController
[Produces("application/json")]
[Route("api/[controller]")]
[ApiController]
Expand Down Expand Up @@ -68,8 +69,8 @@ public ActionResult<TodoItem> GetById(long id)
/// <response code="201">Returns the newly created item</response>
/// <response code="400">If the item is null</response>
[HttpPost]
[ProducesResponseType(201)]
[ProducesResponseType(400)]
[ProducesResponseType(StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public ActionResult<TodoItem> Create(TodoItem item)
{
_context.TodoItems.Add(item);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using TodoApi.Models;

namespace TodoApi.Controllers
{
#region snippet_TodoController
#region snippet_TodoController
[Produces("application/json")]
[Route("api/[controller]")]
[ApiController]
Expand Down Expand Up @@ -68,8 +69,8 @@ public ActionResult<TodoItem> GetById(long id)
/// <response code="201">Returns the newly created item</response>
/// <response code="400">If the item is null</response>
[HttpPost]
[ProducesResponseType(201)]
[ProducesResponseType(400)]
[ProducesResponseType(StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public ActionResult<TodoItem> Create(TodoItem item)
{
_context.TodoItems.Add(item);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,8 @@ Add a [\<remarks>](/dotnet/csharp/programming-guide/xmldoc/remarks) element to t
/// <response code="201">Returns the newly created item</response>
/// <response code="400">If the item is null</response>
[HttpPost]
[ProducesResponseType(201)]
[ProducesResponseType(400)]
[ProducesResponseType(StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public ActionResult<TodoItem> Create(TodoItem item)
{
_context.TodoItems.Add(item);
Expand Down Expand Up @@ -404,8 +404,8 @@ The `Create` action returns an HTTP 201 status code on success. An HTTP 400 stat
/// <response code="201">Returns the newly created item</response>
/// <response code="400">If the item is null</response>
[HttpPost]
[ProducesResponseType(201)]
[ProducesResponseType(400)]
[ProducesResponseType(StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public ActionResult<TodoItem> Create(TodoItem item)
```

Expand Down

0 comments on commit 0ed167e

Please sign in to comment.