This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Added InputFormatter base type #1973
Merged
+254
−226
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
105 changes: 105 additions & 0 deletions
105
src/Microsoft.AspNet.Mvc.Core/Formatters/InputFormatter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNet.Mvc.Core; | ||
using Microsoft.Net.Http.Headers; | ||
|
||
namespace Microsoft.AspNet.Mvc | ||
{ | ||
/// <summary> | ||
/// Reads an object from the request body. | ||
/// </summary> | ||
public abstract class InputFormatter : IInputFormatter | ||
{ | ||
/// <summary> | ||
/// Gets the mutable collection of character encodings supported by | ||
/// this <see cref="InputFormatter"/>. The encodings are | ||
/// used when reading the data. | ||
/// </summary> | ||
public IList<Encoding> SupportedEncodings { get; } = new List<Encoding>(); | ||
|
||
/// <summary> | ||
/// Gets the mutable collection of <see cref="MediaTypeHeaderValue"/> elements supported by | ||
/// this <see cref="InputFormatter"/>. | ||
/// </summary> | ||
public IList<MediaTypeHeaderValue> SupportedMediaTypes { get; } = new List<MediaTypeHeaderValue>(); | ||
|
||
protected object GetDefaultValueForType(Type modelType) | ||
{ | ||
if (modelType.GetTypeInfo().IsValueType) | ||
{ | ||
return Activator.CreateInstance(modelType); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public virtual bool CanRead(InputFormatterContext context) | ||
{ | ||
var contentType = context.ActionContext.HttpContext.Request.ContentType; | ||
MediaTypeHeaderValue requestContentType; | ||
if (!MediaTypeHeaderValue.TryParse(contentType, out requestContentType)) | ||
{ | ||
return false; | ||
} | ||
|
||
return SupportedMediaTypes | ||
.Any(supportedMediaType => supportedMediaType.IsSubsetOf(requestContentType)); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public virtual async Task<object> ReadAsync(InputFormatterContext context) | ||
{ | ||
var request = context.ActionContext.HttpContext.Request; | ||
if (request.ContentLength == 0) | ||
{ | ||
return GetDefaultValueForType(context.ModelType); | ||
} | ||
|
||
return await ReadRequestBodyAsync(context); | ||
} | ||
|
||
/// <summary> | ||
/// Reads the request body. | ||
/// </summary> | ||
/// <param name="context">The <see cref="InputFormatterContext"/> associated with the call.</param> | ||
/// <returns>A task which can read the request body.</returns> | ||
public abstract Task<object> ReadRequestBodyAsync(InputFormatterContext context); | ||
|
||
/// <summary> | ||
/// Returns encoding based on content type charset parameter. | ||
/// </summary> | ||
protected Encoding SelectCharacterEncoding(MediaTypeHeaderValue contentType) | ||
{ | ||
if (contentType != null) | ||
{ | ||
var charset = contentType.Charset; | ||
if (!string.IsNullOrWhiteSpace(contentType.Charset)) | ||
{ | ||
foreach (var supportedEncoding in SupportedEncodings) | ||
{ | ||
if (string.Equals(charset, supportedEncoding.WebName, StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
return supportedEncoding; | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (SupportedEncodings.Count > 0) | ||
{ | ||
return SupportedEncodings[0]; | ||
} | ||
|
||
// No supported encoding was found so there is no way for us to start reading. | ||
throw new InvalidOperationException(Resources.FormatInputFormatterNoEncoding(GetType().FullName)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,7 +34,7 @@ protected OutputFormatter() | |
/// this <see cref="OutputFormatter"/>. The encodings are | ||
/// used when writing the data. | ||
/// </summary> | ||
public IList<Encoding> SupportedEncodings { get; private set; } | ||
public IList<Encoding> SupportedEncodings { get; } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same cleanup as InputFormatter |
||
|
||
/// <summary> | ||
/// Gets the mutable collection of <see cref="MediaTypeHeaderValue"/> elements supported by | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should you also move the
SelectCharacterEncoding
method to this base class?...Ideally I would like to see a test where you create a custom formatter using this base type just to see how the experience would be like...currently that picture is missing...please update the PR with it..There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved. Will update tests shortly.