generated from easy-template/csharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
df3c090
commit a7bf70c
Showing
5 changed files
with
63 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,28 @@ | ||
using FluentValidation; | ||
using Swashbuckle.AspNetCore.Filters; | ||
|
||
namespace WebApplicationMinimalApi8.Dto; | ||
|
||
public record MessageDto | ||
{ | ||
[SwaggerParameterExample] | ||
public required string Body { get; init; } | ||
|
||
public string Body { get; init; } = "안녕하신지"; | ||
|
||
} | ||
|
||
public class MessageDtoValidator : AbstractValidator<MessageDto> | ||
{ | ||
public MessageDtoValidator() | ||
{ | ||
RuleFor(x => x.Body).NotEmpty(); | ||
RuleFor(x => x.Body).NotEmpty().NotNull(); | ||
} | ||
} | ||
} | ||
|
||
public class MessageDtoExmaple : IMultipleExamplesProvider<MessageDto> | ||
{ | ||
public IEnumerable<SwaggerExample<MessageDto>> GetExamples() | ||
{ | ||
yield return SwaggerExample.Create("빈값", new MessageDto { Body = "" }); | ||
yield return SwaggerExample.Create("null", new MessageDto { Body = null }); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/WebApplicationMinimalApi8/EndpointFilters/ResponseAddTraceIdFilter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System.Diagnostics; | ||
using System.Text.Json.Nodes; | ||
using System.Text.Json; | ||
|
||
namespace WebApplicationMinimalApi8.EndpointFilters; | ||
|
||
public class ResponseAddTraceIdFilter : IEndpointFilter | ||
{ | ||
public static JsonNode AddTraceId(object? value, string id) | ||
{ | ||
var root = JsonSerializer.SerializeToNode(value)!.Root; | ||
|
||
root["traceId"] = id; | ||
|
||
return root; | ||
} | ||
|
||
public async ValueTask<object?> InvokeAsync(EndpointFilterInvocationContext context, EndpointFilterDelegate next) | ||
{ | ||
var id = Activity.Current?.Id ?? context.HttpContext.TraceIdentifier; | ||
var ret = await next(context); | ||
|
||
return ret switch | ||
{ | ||
IStatusCodeHttpResult c => c switch | ||
{ | ||
IValueHttpResult v => Results.Text(AddTraceId(v.Value, id).ToJsonString(), "application/json", statusCode: c.StatusCode), | ||
{ } v => v | ||
}, | ||
_ => ret | ||
}; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters