Skip to content

Commit acca9af

Browse files
committed
added @captainsafia's content
1 parent 51e67e0 commit acca9af

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

src/quickstart/openapi.md

+60
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,63 @@ Using the default configuration, the Swagger UI test page is now available at `h
7575
![Swagger UI](https://github.com/bradygaster/minimal-apis.github.io/blob/bradyg/openapi-doc/src/.vuepress/public/openapi/swagger-ui.png?raw=true)
7676

7777

78+
## Customizing OpenAPI
79+
80+
Numerous extension methods exist to enable customization of the generated OpenAPI description. Endpoints defined via minimal APIs can be annotated with metadata for use in OpenAPI libraries with a series of extension methods.
81+
82+
You can set the EndpointName or `EndpointGroupName` of an endpoint using the `WithName` and `WithGroupName` extension methods as follows.
83+
84+
```csharp
85+
string SomeMessage() => "Hello World.";
86+
app.MapGet("/hello", SomeMessage).WithName("WelcomeMessage").WithGroupName("Greetings");
87+
```
88+
89+
> Note: If you have multiple `WithName` statements on the same endpoint, the last endpoint will be favored.
90+
91+
On the topic of endpoint names, by default, endpoints that use a method group or named lambda will have their endpoint name set to the method name. For example, in the code snippet below, the endpoint will have a default endpoint name of `SomeMessage`.
92+
93+
```csharp
94+
string SomeMessage() => "Hello World.";
95+
app.MapGet("/hello", SomeMessage);
96+
```
97+
98+
This default endpoint name can be overloaded by using the `WithName` extension method as referenced above.
99+
100+
```csharp
101+
string SomeMessage() => "Hello World.";
102+
app.MapGet("/hello", SomeMessage).WithName("MyOwnSpecialName");
103+
```
104+
105+
To omit an endpoint from being displayed in API metadata, you can use the ExcludeFromDescription extension method on the endpoint.
106+
107+
```csharp
108+
string SomeMessage() => "Hello World.";
109+
app.MapGet("/hello", SomeMessage).ExcludeFromDescription();
110+
```
111+
112+
Let's say that you did want an endpoint to be annotated. In addition to endpoint names, you can also use the various `ProducesX` endpoints to indicate the response types of a method. The available extension methods are:
113+
114+
```csharp
115+
Produces<TResponse>(int statusCode = 200,
116+
string? contentType = "application/json",
117+
params string[] additionalContentTypes)
118+
119+
Produces(int statusCode = 200,
120+
System.Type? responseType = null,
121+
string? contentType = "application/json",
122+
params string[] additionalContentTypes)
123+
124+
ProducesProblem(int statusCode,
125+
string? contentType = "application/problem+json")
126+
127+
ProducesValidationProblem(int statusCode = 400,
128+
string? contentType = "application/validationproblem+json")
129+
```
130+
131+
In general, the `Produces` extension methods give you the flexibility to set a `ProblemDetails` response type for your endpoint or define what response it returns on happy-path scenarios. You can do this with the generic-typed implementation `Produces<TResponse>` or with the `Produces` attribute. So, for example, to define the response metadata for a POST method that returns a `Todo` or a `ProblemDetails` response you can annotate it using the following extension methods.
132+
133+
```csharp
134+
app.MapPost("/todos", (Todo todo) => { ... })
135+
.ProducesProblem(401)
136+
.Produces<Todo>(201);
137+
```

0 commit comments

Comments
 (0)