(bugfix): custom ObjectResult implementation should return camelCase json
This commit is contained in:
parent
7704163be1
commit
f6a7b13326
@ -14,6 +14,7 @@
|
|||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
|
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.3.0" />
|
||||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.0" />
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.0" />
|
||||||
<PackageReference Include="xunit" Version="2.9.3" />
|
<PackageReference Include="xunit" Version="2.9.3" />
|
||||||
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5">
|
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5">
|
||||||
|
|||||||
@ -1,9 +1,10 @@
|
|||||||
using Xunit;
|
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using MaksIT.Results;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
using MaksIT.Results.Mvc;
|
using MaksIT.Results.Mvc;
|
||||||
|
|
||||||
namespace MaksIT.Results.Tests {
|
|
||||||
|
namespace MaksIT.Results.Tests;
|
||||||
public class ResultTests {
|
public class ResultTests {
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Result_Ok_ShouldReturnSuccess() {
|
public void Result_Ok_ShouldReturnSuccess() {
|
||||||
@ -133,5 +134,33 @@ namespace MaksIT.Results.Tests {
|
|||||||
Assert.Equal((int)HttpStatusCode.OK, objectResult.StatusCode);
|
Assert.Equal((int)HttpStatusCode.OK, objectResult.StatusCode);
|
||||||
Assert.Equal(value, objectResult.Value);
|
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 -->
|
<!-- NuGet package metadata -->
|
||||||
<PackageId>MaksIT.Results</PackageId>
|
<PackageId>MaksIT.Results</PackageId>
|
||||||
<Version>1.0.9</Version>
|
<Version>1.1.0</Version>
|
||||||
<Authors>Maksym Sadovnychyy</Authors>
|
<Authors>Maksym Sadovnychyy</Authors>
|
||||||
<Company>MAKS-IT</Company>
|
<Company>MAKS-IT</Company>
|
||||||
<Product>MaksIT.Results</Product>
|
<Product>MaksIT.Results</Product>
|
||||||
|
|||||||
@ -1,29 +1,34 @@
|
|||||||
using Microsoft.AspNetCore.Mvc;
|
using System.Text.Json;
|
||||||
using System;
|
using System.Text.Json.Serialization;
|
||||||
using System.Collections.Generic;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Text.Json;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace MaksIT.Results.Mvc;
|
namespace MaksIT.Results.Mvc;
|
||||||
|
|
||||||
public class ObjectResult : IActionResult {
|
public class ObjectResult(object? value) : IActionResult {
|
||||||
public object? Value { get; }
|
private static readonly JsonSerializerOptions _jsonSerializerOptions = new() {
|
||||||
public int? StatusCode { get; set; }
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
|
||||||
|
};
|
||||||
|
|
||||||
public ObjectResult(object? value) {
|
public object? Value { get; } = value;
|
||||||
Value = value;
|
public int? StatusCode { get; set; }
|
||||||
}
|
|
||||||
|
|
||||||
public async Task ExecuteResultAsync(ActionContext context) {
|
public async Task ExecuteResultAsync(ActionContext context) {
|
||||||
var response = context.HttpContext.Response;
|
var response = context.HttpContext.Response;
|
||||||
|
|
||||||
if (StatusCode.HasValue) {
|
if (StatusCode.HasValue) {
|
||||||
response.StatusCode = StatusCode.Value;
|
response.StatusCode = StatusCode.Value;
|
||||||
}
|
}
|
||||||
|
|
||||||
response.ContentType = "application/json";
|
response.ContentType = "application/json";
|
||||||
|
|
||||||
if (Value is not null) {
|
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