Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
seungyongshim committed Feb 18, 2024
1 parent 08269ab commit a0cc239
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,17 @@ app.UseExceptionHandler();
* https://medium.com/@niteshsinghal85/multiple-request-response-examples-for-swagger-ui-in-asp-net-core-864c0bdc6619

## Request Body에서 null을 명시할 경우 무시할 수 없음
* JsonIgnoreCondition은 현재 null 쓰기무시만 있고 null 읽기무시는 없음
* https://github.com/dotnet/runtime/issues/66490#issuecomment-1142804662
* `JsonIgnoreCondition.WhenReadingNull`이 추가될 예정도 없음
* https://stackoverflow.com/questions/77516935/ignore-json-null-value-in-system-text-json-deserialize

```csharp
builder.Services.ConfigureHttpJsonOptions(options => {
options.SerializerOptions.WriteIndented = true;
options.SerializerOptions.IncludeFields = true;
options.SerializerOptions.PreferredObjectCreationHandling = JsonObjectCreationHandling.Populate;
options.SerializerOptions.TypeInfoResolver = new DefaultJsonTypeInfoResolver
{
Modifiers = { InterceptNullSetter }
};
});
```
16 changes: 12 additions & 4 deletions src/WebApplicationMinimalApi8/Dto/MessageDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,31 @@ namespace WebApplicationMinimalApi8.Dto;
public record MessageDto
{

public string Body { get; init; } = "안녕하신지";
public string NullString { get; init; } = "기본값";
public IEnumerable<string> NullStrings { get; init; } = [];

}

public class MessageDtoValidator : AbstractValidator<MessageDto>
{
public MessageDtoValidator()
{
RuleFor(x => x.Body).NotEmpty().NotNull();
RuleFor(x => x.NullString).NotEmpty();
}
}

#pragma warning disable CS8625

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 });
yield return SwaggerExample.Create("null", new MessageDto
{
NullString = null,
NullStrings = null
});
yield return SwaggerExample.Create("notnull", new MessageDto { NullString = "", NullStrings = [""] });
yield return SwaggerExample.Create("error", new MessageDto { NullString = "" });
}
}
28 changes: 28 additions & 0 deletions src/WebApplicationMinimalApi8/Program.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using System.Reflection.Metadata.Ecma335;
using System.Text.Json.Serialization.Metadata;
using System.Text.Json.Serialization;
using FluentValidation;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.Filters;
using WebApplicationMinimalApi8.Dto;
using WebApplicationMinimalApi8.EndpointFilters;
using WebApplicationMinimalApi8.ExceptionHandlers;
using System.Linq;

var builder = WebApplication.CreateBuilder(args);

Expand All @@ -16,6 +19,15 @@
builder.Services.AddSwaggerExamplesFromAssemblyOf<Program>();
builder.Services.AddValidatorsFromAssemblyContaining<Program>();
builder.AddFluentValidationEndpointFilter();
builder.Services.ConfigureHttpJsonOptions(options => {
options.SerializerOptions.WriteIndented = true;
options.SerializerOptions.IncludeFields = true;
options.SerializerOptions.PreferredObjectCreationHandling = JsonObjectCreationHandling.Populate;
options.SerializerOptions.TypeInfoResolver = new DefaultJsonTypeInfoResolver
{
Modifiers = { InterceptNullSetter }
};
});

if (builder.Environment.IsEnvironment("Best"))
{
Expand Down Expand Up @@ -49,3 +61,19 @@

app.Run();

static void InterceptNullSetter(JsonTypeInfo typeInfo)
{
foreach (var (propertyInfo, setProperty) in from propertyInfo in typeInfo.Properties
let setProperty = propertyInfo.Set
where setProperty is not null
select (propertyInfo, setProperty))
{
propertyInfo.Set = (obj, value) =>
{
if (value != null)
{
setProperty(obj, value);
}
};
}
}

0 comments on commit a0cc239

Please sign in to comment.