(bugfix): custom ObjectResult implementation should return camelCase json
This commit is contained in:
parent
7704163be1
commit
f6a7b13326
@ -14,6 +14,7 @@
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.3.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.0" />
|
||||
<PackageReference Include="xunit" Version="2.9.3" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5">
|
||||
|
||||
@ -1,9 +1,10 @@
|
||||
using Xunit;
|
||||
using System.Net;
|
||||
using MaksIT.Results;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using MaksIT.Results.Mvc;
|
||||
|
||||
namespace MaksIT.Results.Tests {
|
||||
|
||||
namespace MaksIT.Results.Tests;
|
||||
public class ResultTests {
|
||||
[Fact]
|
||||
public void Result_Ok_ShouldReturnSuccess() {
|
||||
@ -133,5 +134,33 @@ namespace MaksIT.Results.Tests {
|
||||
Assert.Equal((int)HttpStatusCode.OK, objectResult.StatusCode);
|
||||
Assert.Equal(value, objectResult.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ObjectResult_ShouldSerializeToCamelCaseJson() {
|
||||
// Arrange
|
||||
var testObject = new TestPascalCase { FirstName = "John", LastName = "Doe" };
|
||||
var objectResult = new ObjectResult(testObject);
|
||||
var context = new DefaultHttpContext();
|
||||
var memoryStream = new MemoryStream();
|
||||
context.Response.Body = memoryStream;
|
||||
var actionContext = new ActionContext {
|
||||
HttpContext = context
|
||||
};
|
||||
|
||||
// Act
|
||||
await objectResult.ExecuteResultAsync(actionContext);
|
||||
|
||||
// Assert
|
||||
memoryStream.Seek(0, SeekOrigin.Begin);
|
||||
var json = await new StreamReader(memoryStream).ReadToEndAsync();
|
||||
Assert.Contains("\"firstName\"", json);
|
||||
Assert.Contains("\"lastName\"", json);
|
||||
Assert.DoesNotContain("\"FirstName\"", json);
|
||||
Assert.DoesNotContain("\"LastName\"", json);
|
||||
}
|
||||
|
||||
private class TestPascalCase {
|
||||
public string FirstName { get; set; }
|
||||
public string LastName { get; set; }
|
||||
}
|
||||
}
|
||||
@ -8,7 +8,7 @@
|
||||
|
||||
<!-- NuGet package metadata -->
|
||||
<PackageId>MaksIT.Results</PackageId>
|
||||
<Version>1.0.9</Version>
|
||||
<Version>1.1.0</Version>
|
||||
<Authors>Maksym Sadovnychyy</Authors>
|
||||
<Company>MAKS-IT</Company>
|
||||
<Product>MaksIT.Results</Product>
|
||||
|
||||
@ -1,29 +1,34 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
|
||||
namespace MaksIT.Results.Mvc;
|
||||
|
||||
public class ObjectResult : IActionResult {
|
||||
public object? Value { get; }
|
||||
public int? StatusCode { get; set; }
|
||||
public class ObjectResult(object? value) : IActionResult {
|
||||
private static readonly JsonSerializerOptions _jsonSerializerOptions = new() {
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
|
||||
};
|
||||
|
||||
public ObjectResult(object? value) {
|
||||
Value = value;
|
||||
}
|
||||
public object? Value { get; } = value;
|
||||
public int? StatusCode { get; set; }
|
||||
|
||||
public async Task ExecuteResultAsync(ActionContext context) {
|
||||
var response = context.HttpContext.Response;
|
||||
|
||||
if (StatusCode.HasValue) {
|
||||
response.StatusCode = StatusCode.Value;
|
||||
}
|
||||
|
||||
response.ContentType = "application/json";
|
||||
|
||||
if (Value is not null) {
|
||||
await JsonSerializer.SerializeAsync(response.Body, Value);
|
||||
await JsonSerializer.SerializeAsync(
|
||||
response.Body,
|
||||
Value,
|
||||
Value?.GetType() ?? typeof(object),
|
||||
_jsonSerializerOptions
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user