Allow PostAsJsonAsync to accept Json with any case (#11254)

This commit is contained in:
Pranav K 2019-06-17 09:00:59 -07:00 committed by GitHub
parent 17c7ea95a2
commit 730db07d29
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 49 additions and 1 deletions

View File

@ -9,7 +9,8 @@ namespace Microsoft.AspNetCore.Components
{
public static readonly JsonSerializerOptions Options = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
PropertyNameCaseInsensitive = true,
};
}
}

View File

@ -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<Person>(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<Person>(TestUri);
// Assert
Assert.Equal("TestPerson", result.Name);
Assert.Equal(23, result.Age);
}
[Theory]
[InlineData("Put")]
[InlineData("Post")]

View File

@ -10,6 +10,7 @@ namespace Microsoft.AspNetCore.Components.E2ETest
public static JsonSerializerOptions Options { get; } = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
PropertyNameCaseInsensitive = true,
};
}
}

View File

@ -10,6 +10,7 @@ namespace BasicTestApp
public static JsonSerializerOptions Options { get; } = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
PropertyNameCaseInsensitive = true,
};
}
}