diff --git a/src/Components/Components/src/JsonSerializerOptionsProvider.cs b/src/Components/Components/src/JsonSerializerOptionsProvider.cs index 522b9fd782..1bb2f88bdb 100644 --- a/src/Components/Components/src/JsonSerializerOptionsProvider.cs +++ b/src/Components/Components/src/JsonSerializerOptionsProvider.cs @@ -9,7 +9,8 @@ namespace Microsoft.AspNetCore.Components { public static readonly JsonSerializerOptions Options = new JsonSerializerOptions { - PropertyNamingPolicy = JsonNamingPolicy.CamelCase + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, + PropertyNameCaseInsensitive = true, }; } } diff --git a/src/Components/Components/test/HttpClientJsonExtensionsTest.cs b/src/Components/Components/test/HttpClientJsonExtensionsTest.cs index d353a065cc..bc643cf0eb 100644 --- a/src/Components/Components/test/HttpClientJsonExtensionsTest.cs +++ b/src/Components/Components/test/HttpClientJsonExtensionsTest.cs @@ -16,6 +16,7 @@ namespace Microsoft.AspNetCore.Components.Test private readonly JsonSerializerOptions _jsonSerializerOptions = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, + PropertyNameCaseInsensitive = true, }; const string TestUri = "http://example.com/some/uri"; @@ -90,6 +91,50 @@ namespace Microsoft.AspNetCore.Components.Test Assert.Equal(123, result.Age); } + [Fact] + public async Task ReadAsJsonAsync_ReadsCamelCasedJson() + { + var input = "{\"name\": \"TestPerson\", \"age\": 23 }"; + + // Arrange + var httpClient = new HttpClient(new TestHttpMessageHandler(req => + { + return Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK) + { + Content = new StringContent(input) + }); + })); + + // Act + var result = await httpClient.GetJsonAsync(TestUri); + + // Assert + Assert.Equal("TestPerson", result.Name); + Assert.Equal(23, result.Age); + } + + [Fact] + public async Task ReadAsJsonAsync_ReadsPascalCasedJson() + { + var input = "{\"Name\": \"TestPerson\", \"Age\": 23 }"; + + // Arrange + var httpClient = new HttpClient(new TestHttpMessageHandler(req => + { + return Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK) + { + Content = new StringContent(input) + }); + })); + + // Act + var result = await httpClient.GetJsonAsync(TestUri); + + // Assert + Assert.Equal("TestPerson", result.Name); + Assert.Equal(23, result.Age); + } + [Theory] [InlineData("Put")] [InlineData("Post")] diff --git a/src/Components/test/E2ETest/TestJsonSerializerOptionsProvider.cs b/src/Components/test/E2ETest/TestJsonSerializerOptionsProvider.cs index b1a919f6f4..2f893e85ea 100644 --- a/src/Components/test/E2ETest/TestJsonSerializerOptionsProvider.cs +++ b/src/Components/test/E2ETest/TestJsonSerializerOptionsProvider.cs @@ -10,6 +10,7 @@ namespace Microsoft.AspNetCore.Components.E2ETest public static JsonSerializerOptions Options { get; } = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, + PropertyNameCaseInsensitive = true, }; } } diff --git a/src/Components/test/testassets/BasicTestApp/TestJsonSerializerOptionsProvider.cs b/src/Components/test/testassets/BasicTestApp/TestJsonSerializerOptionsProvider.cs index 32ae56e555..5dd50878fb 100644 --- a/src/Components/test/testassets/BasicTestApp/TestJsonSerializerOptionsProvider.cs +++ b/src/Components/test/testassets/BasicTestApp/TestJsonSerializerOptionsProvider.cs @@ -10,6 +10,7 @@ namespace BasicTestApp public static JsonSerializerOptions Options { get; } = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, + PropertyNameCaseInsensitive = true, }; } }