Handle subtype with suffix being a subtype without a suffix (#8170)

This commit is contained in:
Justin Kotalik 2018-07-30 12:05:41 -07:00 committed by GitHub
parent 0102d4efab
commit 367717760b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 17 additions and 7 deletions

View File

@ -488,9 +488,10 @@ namespace Microsoft.AspNetCore.Mvc.Formatters
}
else
{
// The set has no suffix, so we're just looking for an exact match (which means that if 'this'
// has a suffix, it won't match).
return set.SubType.Equals(SubType, StringComparison.OrdinalIgnoreCase);
// If this subtype or suffix matches the subtype of the set,
// it is considered a subtype.
// Ex: application/json > application/val+json
return MatchesEitherSubtypeOrSuffix(set);
}
}
@ -507,6 +508,12 @@ namespace Microsoft.AspNetCore.Mvc.Formatters
return set.SubTypeSuffix.Equals(SubTypeSuffix, StringComparison.OrdinalIgnoreCase);
}
private bool MatchesEitherSubtypeOrSuffix(MediaType set)
{
return set.SubType.Equals(SubType, StringComparison.OrdinalIgnoreCase) ||
set.SubType.Equals(SubTypeSuffix, StringComparison.OrdinalIgnoreCase);
}
private bool ContainsAllParameters(MediaTypeParameterParser setParameters)
{
var parameterFound = true;

View File

@ -265,6 +265,9 @@ namespace Microsoft.AspNetCore.Mvc.Formatters
[Theory]
[InlineData("application/xml")]
[InlineData("application/mathml-content+xml")]
[InlineData("application/mathml-presentation+xml")]
[InlineData("application/mathml+xml; test=value")]
public void XMLFormatter_CanRead_ReturnsTrueForSupportedMediaTypes(string requestContentType)
{
// Arrange
@ -289,9 +292,6 @@ namespace Microsoft.AspNetCore.Mvc.Formatters
}
[Theory]
[InlineData("application/mathml-content+xml")]
[InlineData("application/mathml-presentation+xml")]
[InlineData("application/mathml+xml; undefined=ignored")]
[InlineData("application/octet-stream; padding=3")]
[InlineData("application/xml-dtd; undefined=ignored")]
[InlineData("multipart/mixed; boundary=gc0p4Jq0M2Yt08j34c0p")]

View File

@ -214,6 +214,8 @@ namespace Microsoft.AspNetCore.Mvc.Formatters
[InlineData("application/entity+json", "application/entity+json")]
[InlineData("application/*+json", "application/entity+json")]
[InlineData("application/*", "application/entity+json")]
[InlineData("application/json", "application/vnd.restful+json")]
[InlineData("application/json", "application/problem+json")]
public void IsSubsetOf_ReturnsTrueWhenExpected(string set, string subset)
{
// Arrange
@ -242,6 +244,7 @@ namespace Microsoft.AspNetCore.Mvc.Formatters
[InlineData("application/*+*", "application/json")]
[InlineData("application/entity+*", "application/entity+json")] // We don't allow suffixes to be wildcards
[InlineData("application/*+*", "application/entity+json")] // We don't allow suffixes to be wildcards
[InlineData("application/entity+json", "application/entity")]
public void IsSubsetOf_ReturnsFalseWhenExpected(string set, string subset)
{
// Arrange

View File

@ -254,7 +254,7 @@ namespace Microsoft.AspNetCore.Mvc.Formatters
[InlineData("application/some.entity+json;v=2", true)]
[InlineData("application/some.entity+xml", false)]
[InlineData("application/some.entity+*", false)]
[InlineData("text/some.entity+json", false)]
[InlineData("text/some.entity+json", true)]
[InlineData("", false)]
[InlineData(null, false)]
[InlineData("invalid", false)]