forked from domaindrivendev/Swashbuckle.AspNetCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenApiEndpoints.cs
More file actions
72 lines (63 loc) · 2.59 KB
/
OpenApiEndpoints.cs
File metadata and controls
72 lines (63 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using System.Text.Json.Serialization;
using Microsoft.AspNetCore.Mvc;
namespace WebApi.EndPoints
{
/// <summary>
/// Class of Extensions to add WithOpenApiEndpoints
/// </summary>
public static class OpenApiEndpoints
{
/// <summary>
/// Extension to add WithOpenApiEndpoints
/// </summary>
public static IEndpointRouteBuilder MapWithOpenApiEndpoints(this IEndpointRouteBuilder app)
{
string[] summaries = [
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
];
var group = app.MapGroup("/WithOpenApi")
.WithTags("WithOpenApi")
.DisableAntiforgery();
group.MapGet("weatherforecast", () =>
{
var forecast = Enumerable.Range(1, 5).Select(index =>
new WeatherForecast
(
DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
Random.Shared.Next(-20, 55),
summaries[Random.Shared.Next(summaries.Length)]
))
.ToArray();
return forecast;
})
.WithName("GetWeatherForecast")
.WithOpenApi();
group.MapPost("/multipleForms", ([FromForm] Person person, [FromForm] Address address) =>
{
return $"{person} {address}";
})
.WithOpenApi();
group.MapPost("/IFromFile", (IFormFile file) =>
{
return file.FileName;
}).WithOpenApi();
group.MapPost("/IFromFileCollection", (IFormFileCollection collection) =>
{
return $"{collection.Count} {string.Join(',', collection.Select(f => f.FileName))}";
}).WithOpenApi();
group.MapPost("/IFromBody", (OrganizationCustomExchangeRatesDto dto) =>
{
return $"{dto}";
}).WithOpenApi();
return app;
}
}
record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
{
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}
record class Person(string FirstName, string LastName);
record class Address(string Street, string City, string State, string ZipCode);
sealed record OrganizationCustomExchangeRatesDto([property: JsonRequired] CurrenciesRate[] CurrenciesRates);
sealed record CurrenciesRate([property: JsonRequired] string CurrencyFrom, [property: JsonRequired] string CurrencyTo, double Rate);
}