dotnet / dotnet/aspnetcore

Remove default Response (200) on methods POST, PUT and DELETE

Open
#43,330 4 comments 0 reactions 0 assignees View on GitHub
area-minimal
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 5h
Merged PRs (30d)
276

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Is your feature request related to a problem? Please describe the problem.

I was working on a Minimal API project, and I realize endpoints are being defaulted to at least have a 200 response. Take this for example.

```
app.MapDelete("/specials/{id}", (Guid id) =>
{
if (id == Guid.Empty) return Results.BadRequest();

var pizzaSpecial = specials.FirstOrDefault(s => s.Id == id);
if (pizzaSpecial is null) return Results.NotFound();

specials.Remove(pizzaSpecial);

return Results.NoContent();
})
.WithOpenApi(o =>
{
o.Summary = "Remove pizza special";
o.Description = "Removes a pizza special by its unique identifier";
o.Responses.Clear();
o.Responses.Add(StatusCodes.Status400BadRequest.ToString(), new OpenApiResponse { Description = "The given id is incorrect" });
o.Responses.Add(StatusCodes.Status404NotFound.ToString(), new OpenApiResponse { Description = "The given id could not be found" });
o.Responses.Add(StatusCodes.Status204NoContent.ToString(), new OpenApiResponse { Description = "Pizza special removed" });

return o;
});
```

If I don't call `Clear()` before adding the allowed responses, Swagger will also display 200 is allowed, which was not my plan. I understand the intent was sort of give developers a starting point, but at the same time it would have been better if devs are given the freedom to decide what responses to allow.

### Describe the solution you'd like

Since we are talking about Minimal, I would suggest not defaulting a response (see below). That way devs would know by looking at Swagger they need to list the allowed response(s).

image

### Additional context

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.