diff --git a/src/Microsoft.AspNetCore.Mvc.Core/Formatters/MediaType.cs b/src/Microsoft.AspNetCore.Mvc.Core/Formatters/MediaType.cs index ea332084ca..00e2459f09 100644 --- a/src/Microsoft.AspNetCore.Mvc.Core/Formatters/MediaType.cs +++ b/src/Microsoft.AspNetCore.Mvc.Core/Formatters/MediaType.cs @@ -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; diff --git a/test/Microsoft.AspNetCore.Mvc.Core.Test/Formatters/InputFormatterTest.cs b/test/Microsoft.AspNetCore.Mvc.Core.Test/Formatters/InputFormatterTest.cs index 2ad5752053..d5b631d9b6 100644 --- a/test/Microsoft.AspNetCore.Mvc.Core.Test/Formatters/InputFormatterTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Core.Test/Formatters/InputFormatterTest.cs @@ -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")] diff --git a/test/Microsoft.AspNetCore.Mvc.Core.Test/Formatters/MediaTypeTest.cs b/test/Microsoft.AspNetCore.Mvc.Core.Test/Formatters/MediaTypeTest.cs index 28591cf017..9ab131f8ec 100644 --- a/test/Microsoft.AspNetCore.Mvc.Core.Test/Formatters/MediaTypeTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Core.Test/Formatters/MediaTypeTest.cs @@ -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 diff --git a/test/Microsoft.AspNetCore.Mvc.Formatters.Json.Test/JsonInputFormatterTest.cs b/test/Microsoft.AspNetCore.Mvc.Formatters.Json.Test/JsonInputFormatterTest.cs index d17e4748fa..30868308ad 100644 --- a/test/Microsoft.AspNetCore.Mvc.Formatters.Json.Test/JsonInputFormatterTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Formatters.Json.Test/JsonInputFormatterTest.cs @@ -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)]