/// <inheritdoc /> public virtual IReadOnlyList <string>?GetSupportedContentTypes( string contentType, Type objectType) { if (SupportedMediaTypes.Count == 0) { var message = Resources.FormatFormatter_NoMediaTypes( GetType().FullName, nameof(SupportedMediaTypes)); throw new InvalidOperationException(message); } if (!CanWriteType(objectType)) { return(null); } List <string>?mediaTypes = null; var parsedContentType = contentType != null ? new MediaType(contentType) : default(MediaType); foreach (var mediaType in SupportedMediaTypes) { var parsedMediaType = new MediaType(mediaType); if (parsedMediaType.HasWildcard) { // For supported media types that are wildcard patterns, confirm that the requested // media type satisfies the wildcard pattern (e.g., if "text/entity+json;v=2" requested // and formatter supports "text/*+json"). // Treat contentType like it came from a [Produces] attribute. if (contentType != null && parsedContentType.IsSubsetOf(parsedMediaType)) { if (mediaTypes == null) { mediaTypes = new List <string>(SupportedMediaTypes.Count); } mediaTypes.Add(contentType); } } else { // Confirm this formatter supports a more specific media type than requested e.g. OK if "text/*" // requested and formatter supports "text/plain". Treat contentType like it came from an Accept header. if (contentType == null || parsedMediaType.IsSubsetOf(parsedContentType)) { if (mediaTypes == null) { mediaTypes = new List <string>(SupportedMediaTypes.Count); } mediaTypes.Add(mediaType); } } } return(mediaTypes); }
[InlineData("application/*+*", "application/entity+json")] // We don't allow suffixes to be wildcards public void IsSubsetOf_ReturnsFalseWhenExpected(string set, string subset) { // Arrange var setMediaType = new MediaType(set); var subSetMediaType = new MediaType(subset); // Act var result = subSetMediaType.IsSubsetOf(setMediaType); // Assert Assert.False(result); }
public void IsSubsetOf(string set, string subset, bool expectedResult) { // Arrange var setMediaType = new MediaType(set); var subSetMediaType = new MediaType(subset); // Act var result = subSetMediaType.IsSubsetOf(setMediaType); // Assert Assert.Equal(expectedResult, result); }
private bool IsSubsetOfAnySupportedContentType(string contentType) { var parsedContentType = new MediaType(contentType); for (var i = 0; i < SupportedMediaTypes.Count; i++) { var supportedMediaType = new MediaType(SupportedMediaTypes[i]); if (parsedContentType.IsSubsetOf(supportedMediaType)) { return(true); } } return(false); }
/// <inheritdoc /> public virtual IReadOnlyList <string> GetSupportedContentTypes( string contentType, Type objectType) { if (SupportedMediaTypes.Count == 0) { var message = Resources.FormatFormatter_NoMediaTypes( GetType().FullName, nameof(SupportedMediaTypes)); throw new InvalidOperationException(message); } if (!CanWriteType(objectType)) { return(null); } if (contentType == null) { // If contentType is null, then any type we support is valid. return(SupportedMediaTypes); } else { List <string> mediaTypes = null; var parsedContentType = new MediaType(contentType); // Confirm this formatter supports a more specific media type than requested e.g. OK if "text/*" // requested and formatter supports "text/plain". Treat contentType like it came from an Accept header. foreach (var mediaType in SupportedMediaTypes) { var parsedMediaType = new MediaType(mediaType); if (parsedMediaType.IsSubsetOf(parsedContentType)) { if (mediaTypes == null) { mediaTypes = new List <string>(); } mediaTypes.Add(mediaType); } } return(mediaTypes); } }
/// <inheritdoc /> public virtual bool CanWriteResult(OutputFormatterCanWriteContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (SupportedMediaTypes.Count == 0) { var message = Resources.FormatFormatter_NoMediaTypes( GetType().FullName, nameof(SupportedMediaTypes)); throw new InvalidOperationException(message); } if (!CanWriteType(context.ObjectType)) { return(false); } if (!context.ContentType.HasValue) { // If the desired content type is set to null, then the current formatter can write anything // it wants. context.ContentType = new StringSegment(SupportedMediaTypes[0]); return(true); } else { // Confirm this formatter supports a more specific media type than requested e.g. OK if "text/*" // requested and formatter supports "text/plain". contentType is typically what we got in an Accept // header. var parsedContentType = new MediaType(context.ContentType); for (var i = 0; i < SupportedMediaTypes.Count; i++) { var supportedMediaType = new MediaType(SupportedMediaTypes[i]); if (supportedMediaType.IsSubsetOf(parsedContentType)) { context.ContentType = new StringSegment(SupportedMediaTypes[i]); return(true); } } } return(false); }
/// <inheritdoc /> public virtual bool CanWriteResult(OutputFormatterCanWriteContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (SupportedMediaTypes.Count == 0) { var message = Resources.FormatFormatter_NoMediaTypes( GetType().FullName, nameof(SupportedMediaTypes)); throw new InvalidOperationException(message); } if (!CanWriteType(context.ObjectType)) { return(false); } if (!context.ContentType.HasValue) { // If the desired content type is set to null, then the current formatter can write anything // it wants. context.ContentType = new StringSegment(SupportedMediaTypes[0]); return(true); } else { var parsedContentType = new MediaType(context.ContentType); for (var i = 0; i < SupportedMediaTypes.Count; i++) { var supportedMediaType = new MediaType(SupportedMediaTypes[i]); if (supportedMediaType.HasWildcard) { // For supported media types that are wildcard patterns, confirm that the requested // media type satisfies the wildcard pattern (e.g., if "text/entity+json;v=2" requested // and formatter supports "text/*+json"). // We only do this when comparing against server-defined content types (e.g., those // from [Produces] or Response.ContentType), otherwise we'd potentially be reflecting // back arbitrary Accept header values. if (context.ContentTypeIsServerDefined && parsedContentType.IsSubsetOf(supportedMediaType)) { return(true); } } else { // For supported media types that are not wildcard patterns, confirm that this formatter // supports a more specific media type than requested e.g. OK if "text/*" requested and // formatter supports "text/plain". // contentType is typically what we got in an Accept header. if (supportedMediaType.IsSubsetOf(parsedContentType)) { context.ContentType = new StringSegment(SupportedMediaTypes[i]); return(true); } } } } return(false); }