Allow PostAsJsonAsync to accept Json with any case (#11254)
This commit is contained in:
parent
17c7ea95a2
commit
730db07d29
|
|
@ -9,7 +9,8 @@ namespace Microsoft.AspNetCore.Components
|
||||||
{
|
{
|
||||||
public static readonly JsonSerializerOptions Options = new JsonSerializerOptions
|
public static readonly JsonSerializerOptions Options = new JsonSerializerOptions
|
||||||
{
|
{
|
||||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||||
|
PropertyNameCaseInsensitive = true,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,7 @@ namespace Microsoft.AspNetCore.Components.Test
|
||||||
private readonly JsonSerializerOptions _jsonSerializerOptions = new JsonSerializerOptions
|
private readonly JsonSerializerOptions _jsonSerializerOptions = new JsonSerializerOptions
|
||||||
{
|
{
|
||||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||||
|
PropertyNameCaseInsensitive = true,
|
||||||
};
|
};
|
||||||
|
|
||||||
const string TestUri = "http://example.com/some/uri";
|
const string TestUri = "http://example.com/some/uri";
|
||||||
|
|
@ -90,6 +91,50 @@ namespace Microsoft.AspNetCore.Components.Test
|
||||||
Assert.Equal(123, result.Age);
|
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]
|
[Theory]
|
||||||
[InlineData("Put")]
|
[InlineData("Put")]
|
||||||
[InlineData("Post")]
|
[InlineData("Post")]
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ namespace Microsoft.AspNetCore.Components.E2ETest
|
||||||
public static JsonSerializerOptions Options { get; } = new JsonSerializerOptions
|
public static JsonSerializerOptions Options { get; } = new JsonSerializerOptions
|
||||||
{
|
{
|
||||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||||
|
PropertyNameCaseInsensitive = true,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ namespace BasicTestApp
|
||||||
public static JsonSerializerOptions Options { get; } = new JsonSerializerOptions
|
public static JsonSerializerOptions Options { get; } = new JsonSerializerOptions
|
||||||
{
|
{
|
||||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||||
|
PropertyNameCaseInsensitive = true,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue