From 02c36a1c4824936682b26b6c133d11bebee822a2 Mon Sep 17 00:00:00 2001 From: Pranav K Date: Sun, 27 Sep 2015 00:40:43 -0700 Subject: [PATCH] Replace NotNullAttribute with thrown exceptions --- .../ApiController.cs | 124 ++++++++++-- .../BadRequestErrorMessageResult.cs | 8 +- .../ContentNegotiationResult.cs | 10 +- .../DefaultContentNegotiator.cs | 119 +++++++++-- ...onConventionsApplicationModelConvention.cs | 5 + ...piOverloadingApplicationModelConvention.cs | 6 + ...erConventionsApplicationModelConvention.cs | 5 + .../WebApiRoutesApplicationModelConvention.cs | 6 + .../FormDataCollectionExtensions.cs | 8 +- .../HttpError.cs | 23 ++- .../HttpRequestMessageExtensions.cs | 188 +++++++++++++++--- .../HttpRequestMessageFeature.cs | 9 +- .../HttpResponseException.cs | 9 +- .../HttpResponseExceptionActionFilter.cs | 11 +- .../InvalidModelStateResult.cs | 8 +- .../ResponseMessageResult.cs | 8 +- .../project.json | 6 +- 17 files changed, 461 insertions(+), 92 deletions(-) diff --git a/src/Microsoft.AspNet.Mvc.WebApiCompatShim/ApiController.cs b/src/Microsoft.AspNet.Mvc.WebApiCompatShim/ApiController.cs index ecfd45bc8c..61dca0573a 100644 --- a/src/Microsoft.AspNet.Mvc.WebApiCompatShim/ApiController.cs +++ b/src/Microsoft.AspNet.Mvc.WebApiCompatShim/ApiController.cs @@ -9,9 +9,7 @@ using Microsoft.AspNet.Http; using Microsoft.AspNet.Mvc; using Microsoft.AspNet.Mvc.ModelBinding; using Microsoft.AspNet.Mvc.ModelBinding.Validation; -using Microsoft.AspNet.Mvc.Routing; using Microsoft.AspNet.Mvc.WebApiCompatShim; -using Microsoft.Framework.Internal; using Microsoft.Net.Http.Headers; using Newtonsoft.Json; @@ -128,8 +126,13 @@ namespace System.Web.Http /// The user-visible error message. /// A with the specified error message. [NonAction] - public virtual BadRequestErrorMessageResult BadRequest([NotNull] string message) + public virtual BadRequestErrorMessageResult BadRequest(string message) { + if (message == null) + { + throw new ArgumentNullException(nameof(message)); + } + return new BadRequestErrorMessageResult(message); } @@ -139,8 +142,13 @@ namespace System.Web.Http /// The model state to include in the error. /// An with the specified model state. [NonAction] - public virtual InvalidModelStateResult BadRequest([NotNull] ModelStateDictionary modelState) + public virtual InvalidModelStateResult BadRequest(ModelStateDictionary modelState) { + if (modelState == null) + { + throw new ArgumentNullException(nameof(modelState)); + } + return new InvalidModelStateResult(modelState, includeErrorDetail: false); } @@ -160,8 +168,13 @@ namespace System.Web.Http /// The content value to negotiate and format in the entity body. /// A with the specified values. [NonAction] - public virtual NegotiatedContentResult Content(HttpStatusCode statusCode, [NotNull] T value) + public virtual NegotiatedContentResult Content(HttpStatusCode statusCode, T value) { + if (value == null) + { + throw new ArgumentNullException(nameof(value)); + } + return new NegotiatedContentResult(statusCode, value); } @@ -174,8 +187,13 @@ namespace System.Web.Http /// The content value to format in the entity body. /// A with the specified values. [NonAction] - public virtual CreatedResult Created([NotNull] string location, object content) + public virtual CreatedResult Created(string location, object content) { + if (location == null) + { + throw new ArgumentNullException(nameof(location)); + } + return new CreatedResult(location, content); } @@ -186,8 +204,13 @@ namespace System.Web.Http /// The content value to format in the entity body. /// A with the specified values. [NonAction] - public virtual CreatedResult Created([NotNull] Uri uri, object content) + public virtual CreatedResult Created(Uri uri, object content) { + if (uri == null) + { + throw new ArgumentNullException(nameof(uri)); + } + string location; if (uri.IsAbsoluteUri) { @@ -209,10 +232,15 @@ namespace System.Web.Http /// A with the specified values. [NonAction] public virtual CreatedAtRouteResult CreatedAtRoute( - [NotNull] string routeName, + string routeName, object routeValues, object content) { + if (routeName == null) + { + throw new ArgumentNullException(nameof(routeName)); + } + return new CreatedAtRouteResult(routeName, routeValues, content); } @@ -232,8 +260,13 @@ namespace System.Web.Http /// The exception to include in the error. /// An with the specified exception. [NonAction] - public virtual ExceptionResult InternalServerError([NotNull] Exception exception) + public virtual ExceptionResult InternalServerError(Exception exception) { + if (exception == null) + { + throw new ArgumentNullException(nameof(exception)); + } + return new ExceptionResult(exception, includeErrorDetail: false); } @@ -244,8 +277,13 @@ namespace System.Web.Http /// The content value to serialize in the entity body. /// A with the specified value. [NonAction] - public virtual JsonResult Json([NotNull] T content) + public virtual JsonResult Json(T content) { + if (content == null) + { + throw new ArgumentNullException(nameof(content)); + } + return new JsonResult(content); } @@ -257,8 +295,18 @@ namespace System.Web.Http /// The serializer settings. /// A with the specified values. [NonAction] - public virtual JsonResult Json([NotNull] T content, [NotNull] JsonSerializerSettings serializerSettings) + public virtual JsonResult Json(T content, JsonSerializerSettings serializerSettings) { + if (content == null) + { + throw new ArgumentNullException(nameof(content)); + } + + if (serializerSettings == null) + { + throw new ArgumentNullException(nameof(serializerSettings)); + } + return new JsonResult(content, serializerSettings); } @@ -272,10 +320,25 @@ namespace System.Web.Http /// A with the specified values. [NonAction] public virtual JsonResult Json( - [NotNull] T content, - [NotNull] JsonSerializerSettings serializerSettings, - [NotNull] Encoding encoding) + T content, + JsonSerializerSettings serializerSettings, + Encoding encoding) { + if (content == null) + { + throw new ArgumentNullException(nameof(content)); + } + + if (serializerSettings == null) + { + throw new ArgumentNullException(nameof(serializerSettings)); + } + + if (encoding == null) + { + throw new ArgumentNullException(nameof(encoding)); + } + var result = new JsonResult(content, serializerSettings); result.ContentType = new MediaTypeHeaderValue("application/json") { @@ -323,8 +386,13 @@ namespace System.Web.Http /// The location to which to redirect. /// A with the specified value. [NonAction] - public virtual RedirectResult Redirect([NotNull] string location) + public virtual RedirectResult Redirect(string location) { + if (location == null) + { + throw new ArgumentNullException(nameof(location)); + } + // This is how redirect was implemented in legacy webapi - string URIs are assumed to be absolute. return Redirect(new Uri(location)); } @@ -335,8 +403,13 @@ namespace System.Web.Http /// The location to which to redirect. /// A with the specified value. [NonAction] - public virtual RedirectResult Redirect([NotNull] Uri location) + public virtual RedirectResult Redirect(Uri location) { + if (location == null) + { + throw new ArgumentNullException(nameof(location)); + } + string uri; if (location.IsAbsoluteUri) { @@ -357,8 +430,18 @@ namespace System.Web.Http /// The route data to use for generating the URL. /// A with the specified values. [NonAction] - public virtual RedirectToRouteResult RedirectToRoute([NotNull] string routeName, [NotNull] object routeValues) + public virtual RedirectToRouteResult RedirectToRoute(string routeName, object routeValues) { + if (routeName == null) + { + throw new ArgumentNullException(nameof(routeName)); + } + + if (routeValues == null) + { + throw new ArgumentNullException(nameof(routeValues)); + } + return new RedirectToRouteResult(routeName, routeValues) { UrlHelper = Url, @@ -371,8 +454,13 @@ namespace System.Web.Http /// The HTTP response message. /// A for the specified response. [NonAction] - public virtual ResponseMessageResult ResponseMessage([NotNull] HttpResponseMessage response) + public virtual ResponseMessageResult ResponseMessage(HttpResponseMessage response) { + if (response == null) + { + throw new ArgumentNullException(nameof(response)); + } + return new ResponseMessageResult(response); } diff --git a/src/Microsoft.AspNet.Mvc.WebApiCompatShim/BadRequestErrorMessageResult.cs b/src/Microsoft.AspNet.Mvc.WebApiCompatShim/BadRequestErrorMessageResult.cs index a11a7376a6..ffe85db138 100644 --- a/src/Microsoft.AspNet.Mvc.WebApiCompatShim/BadRequestErrorMessageResult.cs +++ b/src/Microsoft.AspNet.Mvc.WebApiCompatShim/BadRequestErrorMessageResult.cs @@ -4,7 +4,6 @@ using System.Threading.Tasks; using Microsoft.AspNet.Http; using Microsoft.AspNet.Mvc; -using Microsoft.Framework.Internal; namespace System.Web.Http { @@ -16,9 +15,14 @@ namespace System.Web.Http { /// Initializes a new instance of the class. /// The user-visible error message. - public BadRequestErrorMessageResult([NotNull] string message) + public BadRequestErrorMessageResult(string message) : base(new HttpError(message)) { + if (message == null) + { + throw new ArgumentNullException(nameof(message)); + } + Message = message; } diff --git a/src/Microsoft.AspNet.Mvc.WebApiCompatShim/ContentNegotiator/ContentNegotiationResult.cs b/src/Microsoft.AspNet.Mvc.WebApiCompatShim/ContentNegotiator/ContentNegotiationResult.cs index 2c50b5f492..c9bc1e8048 100644 --- a/src/Microsoft.AspNet.Mvc.WebApiCompatShim/ContentNegotiator/ContentNegotiationResult.cs +++ b/src/Microsoft.AspNet.Mvc.WebApiCompatShim/ContentNegotiator/ContentNegotiationResult.cs @@ -3,11 +3,8 @@ #if DNXCORE50 -using Microsoft.AspNet.Mvc; using System.Collections.Generic; using System.Net.Http.Headers; -using System.Web.Http; -using Microsoft.Framework.Internal; namespace System.Net.Http.Formatting { @@ -24,8 +21,13 @@ namespace System.Net.Http.Formatting /// /// The formatter. /// The preferred media type. Can be null. - public ContentNegotiationResult([NotNull] MediaTypeFormatter formatter, MediaTypeHeaderValue mediaType) + public ContentNegotiationResult(MediaTypeFormatter formatter, MediaTypeHeaderValue mediaType) { + if (formatter == null) + { + throw new ArgumentNullException(nameof(formatter)); + } + _formatter = formatter; MediaType = mediaType; } diff --git a/src/Microsoft.AspNet.Mvc.WebApiCompatShim/ContentNegotiator/DefaultContentNegotiator.cs b/src/Microsoft.AspNet.Mvc.WebApiCompatShim/ContentNegotiator/DefaultContentNegotiator.cs index 00597e789f..35092e4373 100644 --- a/src/Microsoft.AspNet.Mvc.WebApiCompatShim/ContentNegotiator/DefaultContentNegotiator.cs +++ b/src/Microsoft.AspNet.Mvc.WebApiCompatShim/ContentNegotiator/DefaultContentNegotiator.cs @@ -57,10 +57,25 @@ namespace System.Net.Http.Formatting /// The result of the negotiation containing the most appropriate /// instance, or null if there is no appropriate formatter. public virtual ContentNegotiationResult Negotiate( - [NotNull] Type type, - [NotNull] HttpRequestMessage request, - [NotNull] IEnumerable formatters) + Type type, + HttpRequestMessage request, + IEnumerable formatters) { + if (type == null) + { + throw new ArgumentNullException(nameof(type)); + } + + if (request == null) + { + throw new ArgumentNullException(nameof(request)); + } + + if (formatters == null) + { + throw new ArgumentNullException(nameof(formatters)); + } + // Go through each formatter to compute how well it matches. var matches = ComputeFormatterMatches(type, request, formatters); @@ -96,10 +111,25 @@ namespace System.Net.Http.Formatting /// The set of objects from which to choose. /// A collection containing all the matches. protected virtual Collection ComputeFormatterMatches( - [NotNull] Type type, - [NotNull] HttpRequestMessage request, - [NotNull] IEnumerable formatters) + Type type, + HttpRequestMessage request, + IEnumerable formatters) { + if (type == null) + { + throw new ArgumentNullException(nameof(type)); + } + + if (request == null) + { + throw new ArgumentNullException(nameof(request)); + } + + if (formatters == null) + { + throw new ArgumentNullException(nameof(formatters)); + } + IEnumerable sortedAcceptValues = null; // Go through each formatter to find how well it matches. @@ -159,8 +189,13 @@ namespace System.Net.Http.Formatting /// The collection of matches. /// The determined to be the best match. protected virtual MediaTypeFormatterMatch SelectResponseMediaTypeFormatter( - [NotNull] ICollection matches) + ICollection matches) { + if (matches == null) + { + throw new ArgumentNullException(nameof(matches)); + } + // Performance-sensitive var matchList = matches.AsList(); @@ -269,9 +304,19 @@ namespace System.Net.Http.Formatting /// /// The determined to be the best match. protected virtual Encoding SelectResponseCharacterEncoding( - [NotNull] HttpRequestMessage request, - [NotNull] MediaTypeFormatter formatter) + HttpRequestMessage request, + MediaTypeFormatter formatter) { + if (request == null) + { + throw new ArgumentNullException(nameof(request)); + } + + if (formatter == null) + { + throw new ArgumentNullException(nameof(formatter)); + } + // If there are any SupportedEncodings then we pick an encoding var supportedEncodings = formatter.SupportedEncodings.ToList(); if (supportedEncodings.Count > 0) @@ -311,9 +356,19 @@ namespace System.Net.Http.Formatting /// A indicating the quality of the match or null is no match. /// protected virtual MediaTypeFormatterMatch MatchAcceptHeader( - [NotNull] IEnumerable sortedAcceptValues, - [NotNull] MediaTypeFormatter formatter) + IEnumerable sortedAcceptValues, + MediaTypeFormatter formatter) { + if (sortedAcceptValues == null) + { + throw new ArgumentNullException(nameof(sortedAcceptValues)); + } + + if (formatter == null) + { + throw new ArgumentNullException(nameof(formatter)); + } + foreach (MediaTypeWithQualityHeaderValue acceptMediaTypeValue in sortedAcceptValues) { var supportedMediaTypes = formatter.SupportedMediaTypes.ToList(); @@ -347,9 +402,19 @@ namespace System.Net.Http.Formatting /// A indicating the quality of the match or null is no match. /// protected virtual MediaTypeFormatterMatch MatchRequestMediaType( - [NotNull] HttpRequestMessage request, - [NotNull] MediaTypeFormatter formatter) + HttpRequestMessage request, + MediaTypeFormatter formatter) { + if (request == null) + { + throw new ArgumentNullException(nameof(request)); + } + + if (formatter == null) + { + throw new ArgumentNullException(nameof(formatter)); + } + if (request.Content != null) { var requestMediaType = request.Content.Headers.ContentType; @@ -385,8 +450,13 @@ namespace System.Net.Http.Formatting /// True if not ExcludeMatchOnTypeOnly and accept headers with a q-factor bigger than 0.0 are present. /// protected virtual bool ShouldMatchOnType( - [NotNull] IEnumerable sortedAcceptValues) + IEnumerable sortedAcceptValues) { + if (sortedAcceptValues == null) + { + throw new ArgumentNullException(nameof(sortedAcceptValues)); + } + return !(ExcludeMatchOnTypeOnly && sortedAcceptValues.Any()); } @@ -399,9 +469,19 @@ namespace System.Net.Http.Formatting /// A indicating the quality of the match or null is no match. /// protected virtual MediaTypeFormatterMatch MatchType( - [NotNull] Type type, - [NotNull] MediaTypeFormatter formatter) + Type type, + MediaTypeFormatter formatter) { + if (type == null) + { + throw new ArgumentNullException(nameof(type)); + } + + if (formatter == null) + { + throw new ArgumentNullException(nameof(formatter)); + } + // We already know that we do match on type -- otherwise we wouldn't even be called -- // so this is just a matter of determining how we match. MediaTypeHeaderValue mediaType = null; @@ -447,8 +527,13 @@ namespace System.Net.Http.Formatting /// The header values to sort. /// The sorted header values. protected virtual IEnumerable SortStringWithQualityHeaderValuesByQFactor( - [NotNull] ICollection headerValues) + ICollection headerValues) { + if (headerValues == null) + { + throw new ArgumentNullException(nameof(headerValues)); + } + if (headerValues.Count > 1) { // Use OrderBy() instead of Array.Sort() as it performs fewer comparisons. In this case the comparisons diff --git a/src/Microsoft.AspNet.Mvc.WebApiCompatShim/Conventions/WebApiActionConventionsApplicationModelConvention.cs b/src/Microsoft.AspNet.Mvc.WebApiCompatShim/Conventions/WebApiActionConventionsApplicationModelConvention.cs index 1a6bd6a4a3..6a1538833b 100644 --- a/src/Microsoft.AspNet.Mvc.WebApiCompatShim/Conventions/WebApiActionConventionsApplicationModelConvention.cs +++ b/src/Microsoft.AspNet.Mvc.WebApiCompatShim/Conventions/WebApiActionConventionsApplicationModelConvention.cs @@ -25,6 +25,11 @@ namespace Microsoft.AspNet.Mvc.WebApiCompatShim public void Apply(ControllerModel controller) { + if (controller == null) + { + throw new ArgumentNullException(nameof(controller)); + } + if (IsConventionApplicable(controller)) { var newActions = new List(); diff --git a/src/Microsoft.AspNet.Mvc.WebApiCompatShim/Conventions/WebApiOverloadingApplicationModelConvention.cs b/src/Microsoft.AspNet.Mvc.WebApiCompatShim/Conventions/WebApiOverloadingApplicationModelConvention.cs index f83e2f9a7e..e40d9f7159 100644 --- a/src/Microsoft.AspNet.Mvc.WebApiCompatShim/Conventions/WebApiOverloadingApplicationModelConvention.cs +++ b/src/Microsoft.AspNet.Mvc.WebApiCompatShim/Conventions/WebApiOverloadingApplicationModelConvention.cs @@ -1,6 +1,7 @@ // Copyright (c) .NET Foundation. 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.Linq; using Microsoft.AspNet.Mvc.ApplicationModels; @@ -10,6 +11,11 @@ namespace Microsoft.AspNet.Mvc.WebApiCompatShim { public void Apply(ActionModel action) { + if (action == null) + { + throw new ArgumentNullException(nameof(action)); + } + if (IsConventionApplicable(action.Controller)) { action.ActionConstraints.Add(new OverloadActionConstraint()); diff --git a/src/Microsoft.AspNet.Mvc.WebApiCompatShim/Conventions/WebApiParameterConventionsApplicationModelConvention.cs b/src/Microsoft.AspNet.Mvc.WebApiCompatShim/Conventions/WebApiParameterConventionsApplicationModelConvention.cs index aba7bbb127..28b1288c53 100644 --- a/src/Microsoft.AspNet.Mvc.WebApiCompatShim/Conventions/WebApiParameterConventionsApplicationModelConvention.cs +++ b/src/Microsoft.AspNet.Mvc.WebApiCompatShim/Conventions/WebApiParameterConventionsApplicationModelConvention.cs @@ -16,6 +16,11 @@ namespace Microsoft.AspNet.Mvc.WebApiCompatShim { public void Apply(ActionModel action) { + if (action == null) + { + throw new ArgumentNullException(nameof(action)); + } + if (IsConventionApplicable(action.Controller)) { var optionalParameters = new HashSet(); diff --git a/src/Microsoft.AspNet.Mvc.WebApiCompatShim/Conventions/WebApiRoutesApplicationModelConvention.cs b/src/Microsoft.AspNet.Mvc.WebApiCompatShim/Conventions/WebApiRoutesApplicationModelConvention.cs index c13d542d7e..433f2e4340 100644 --- a/src/Microsoft.AspNet.Mvc.WebApiCompatShim/Conventions/WebApiRoutesApplicationModelConvention.cs +++ b/src/Microsoft.AspNet.Mvc.WebApiCompatShim/Conventions/WebApiRoutesApplicationModelConvention.cs @@ -1,6 +1,7 @@ // Copyright (c) .NET Foundation. 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.Linq; using Microsoft.AspNet.Mvc.ApplicationModels; @@ -17,6 +18,11 @@ namespace Microsoft.AspNet.Mvc.WebApiCompatShim public void Apply(ControllerModel controller) { + if (controller == null) + { + throw new ArgumentNullException(nameof(controller)); + } + if (IsConventionApplicable(controller)) { controller.RouteConstraints.Add(new AreaAttribute(_area)); diff --git a/src/Microsoft.AspNet.Mvc.WebApiCompatShim/FormDataCollectionExtensions.cs b/src/Microsoft.AspNet.Mvc.WebApiCompatShim/FormDataCollectionExtensions.cs index dfebd2be28..6a61edf92d 100644 --- a/src/Microsoft.AspNet.Mvc.WebApiCompatShim/FormDataCollectionExtensions.cs +++ b/src/Microsoft.AspNet.Mvc.WebApiCompatShim/FormDataCollectionExtensions.cs @@ -5,7 +5,6 @@ using System; using System.Collections.Generic; using System.Net.Http.Formatting; using System.Text; -using Microsoft.Framework.Internal; namespace Microsoft.AspNet.Mvc.WebApiCompatShim { @@ -80,8 +79,13 @@ namespace Microsoft.AspNet.Mvc.WebApiCompatShim } public static IEnumerable> GetJQueryNameValuePairs( - [NotNull] this FormDataCollection formData) + this FormDataCollection formData) { + if (formData == null) + { + throw new ArgumentNullException(nameof(formData)); + } + var count = 0; foreach (var kv in formData) diff --git a/src/Microsoft.AspNet.Mvc.WebApiCompatShim/HttpError.cs b/src/Microsoft.AspNet.Mvc.WebApiCompatShim/HttpError.cs index c1b14bb6f7..18c8df65c3 100644 --- a/src/Microsoft.AspNet.Mvc.WebApiCompatShim/HttpError.cs +++ b/src/Microsoft.AspNet.Mvc.WebApiCompatShim/HttpError.cs @@ -6,9 +6,7 @@ using System.Linq; using System.Xml; using System.Xml.Schema; using System.Xml.Serialization; -using Microsoft.AspNet.Mvc; using Microsoft.AspNet.Mvc.ModelBinding; -using Microsoft.Framework.Internal; using ShimResources = Microsoft.AspNet.Mvc.WebApiCompatShim.Resources; namespace System.Web.Http @@ -34,9 +32,14 @@ namespace System.Web.Http /// . /// /// The error message to associate with this instance. - public HttpError([NotNull] string message) + public HttpError(string message) : this() { + if (message == null) + { + throw new ArgumentNullException(nameof(message)); + } + Message = message; } @@ -47,9 +50,14 @@ namespace System.Web.Http /// /// true to include the exception information in the error;false otherwise. /// - public HttpError([NotNull] Exception exception, bool includeErrorDetail) + public HttpError(Exception exception, bool includeErrorDetail) : this() { + if (exception == null) + { + throw new ArgumentNullException(nameof(exception)); + } + Message = ShimResources.HttpError_GenericError; if (includeErrorDetail) @@ -71,9 +79,14 @@ namespace System.Web.Http /// /// true to include exception messages in the error; false otherwise. /// - public HttpError([NotNull] ModelStateDictionary modelState, bool includeErrorDetail) + public HttpError(ModelStateDictionary modelState, bool includeErrorDetail) : this() { + if (modelState == null) + { + throw new ArgumentNullException(nameof(modelState)); + } + if (modelState.IsValid) { throw new ArgumentException(ShimResources.HttpError_ValidModelState, nameof(modelState)); diff --git a/src/Microsoft.AspNet.Mvc.WebApiCompatShim/HttpRequestMessage/HttpRequestMessageExtensions.cs b/src/Microsoft.AspNet.Mvc.WebApiCompatShim/HttpRequestMessage/HttpRequestMessageExtensions.cs index 850ef43fca..c8aa1d8c2a 100644 --- a/src/Microsoft.AspNet.Mvc.WebApiCompatShim/HttpRequestMessage/HttpRequestMessageExtensions.cs +++ b/src/Microsoft.AspNet.Mvc.WebApiCompatShim/HttpRequestMessage/HttpRequestMessageExtensions.cs @@ -9,7 +9,6 @@ using Microsoft.AspNet.Http; using Microsoft.AspNet.Mvc.ModelBinding; using Microsoft.AspNet.Mvc.WebApiCompatShim; using Microsoft.Framework.DependencyInjection; -using Microsoft.Framework.Internal; using Microsoft.Framework.OptionsModel; using ShimResources = Microsoft.AspNet.Mvc.WebApiCompatShim.Resources; @@ -37,9 +36,19 @@ namespace System.Net.Http /// range. /// public static HttpResponseMessage CreateErrorResponse( - [NotNull] this HttpRequestMessage request, - [NotNull] InvalidByteRangeException invalidByteRangeException) + this HttpRequestMessage request, + InvalidByteRangeException invalidByteRangeException) { + if (request == null) + { + throw new ArgumentNullException(nameof(request)); + } + + if (invalidByteRangeException == null) + { + throw new ArgumentNullException(nameof(invalidByteRangeException)); + } + var rangeNotSatisfiableResponse = request.CreateErrorResponse( HttpStatusCode.RequestedRangeNotSatisfiable, invalidByteRangeException); @@ -67,10 +76,20 @@ namespace System.Net.Http /// . /// public static HttpResponseMessage CreateErrorResponse( - [NotNull] this HttpRequestMessage request, + this HttpRequestMessage request, HttpStatusCode statusCode, - [NotNull] string message) + string message) { + if (request == null) + { + throw new ArgumentNullException(nameof(request)); + } + + if (message == null) + { + throw new ArgumentNullException(nameof(message)); + } + return request.CreateErrorResponse(statusCode, new HttpError(message)); } @@ -92,11 +111,26 @@ namespace System.Net.Http /// An error response for with error message /// and status code . public static HttpResponseMessage CreateErrorResponse( - [NotNull] this HttpRequestMessage request, + this HttpRequestMessage request, HttpStatusCode statusCode, - [NotNull] string message, - [NotNull] Exception exception) + string message, + Exception exception) { + if (request == null) + { + throw new ArgumentNullException(nameof(request)); + } + + if (message == null) + { + throw new ArgumentNullException(nameof(message)); + } + + if (exception == null) + { + throw new ArgumentNullException(nameof(exception)); + } + var error = new HttpError(exception, includeErrorDetail: false) { Message = message }; return request.CreateErrorResponse(statusCode, error); } @@ -118,10 +152,20 @@ namespace System.Net.Http /// An error response for with status code . /// public static HttpResponseMessage CreateErrorResponse( - [NotNull] this HttpRequestMessage request, + this HttpRequestMessage request, HttpStatusCode statusCode, - [NotNull] Exception exception) + Exception exception) { + if (request == null) + { + throw new ArgumentNullException(nameof(request)); + } + + if (exception == null) + { + throw new ArgumentNullException(nameof(exception)); + } + return request.CreateErrorResponse(statusCode, new HttpError(exception, includeErrorDetail: false)); } @@ -142,10 +186,20 @@ namespace System.Net.Http /// An error response for with status code . /// public static HttpResponseMessage CreateErrorResponse( - [NotNull] this HttpRequestMessage request, + this HttpRequestMessage request, HttpStatusCode statusCode, - [NotNull] ModelStateDictionary modelState) + ModelStateDictionary modelState) { + if (request == null) + { + throw new ArgumentNullException(nameof(request)); + } + + if (modelState == null) + { + throw new ArgumentNullException(nameof(modelState)); + } + return request.CreateErrorResponse(statusCode, new HttpError(modelState, includeErrorDetail: false)); } @@ -165,10 +219,20 @@ namespace System.Net.Http /// An error response wrapping with status code . /// public static HttpResponseMessage CreateErrorResponse( - [NotNull] this HttpRequestMessage request, + this HttpRequestMessage request, HttpStatusCode statusCode, - [NotNull] HttpError error) + HttpError error) { + if (request == null) + { + throw new ArgumentNullException(nameof(request)); + } + + if (error == null) + { + throw new ArgumentNullException(nameof(error)); + } + return request.CreateResponse(statusCode, error); } @@ -188,8 +252,18 @@ namespace System.Net.Http /// /// A response wrapping with status code. /// - public static HttpResponseMessage CreateResponse([NotNull] this HttpRequestMessage request, T value) + public static HttpResponseMessage CreateResponse(this HttpRequestMessage request, T value) { + if (request == null) + { + throw new ArgumentNullException(nameof(request)); + } + + if (value == null) + { + throw new ArgumentNullException(nameof(value)); + } + return request.CreateResponse(HttpStatusCode.OK, value, formatters: null); } @@ -232,11 +306,16 @@ namespace System.Net.Http /// The set of objects from which to choose. /// A response wrapping with . public static HttpResponseMessage CreateResponse( - [NotNull] this HttpRequestMessage request, + this HttpRequestMessage request, HttpStatusCode statusCode, T value, IEnumerable formatters) { + if (request == null) + { + throw new ArgumentNullException(nameof(request)); + } + var context = GetHttpContext(request); if (formatters == null) @@ -296,11 +375,26 @@ namespace System.Net.Http /// /// A response wrapping with . public static HttpResponseMessage CreateResponse( - [NotNull] this HttpRequestMessage request, + this HttpRequestMessage request, HttpStatusCode statusCode, - [NotNull] T value, - [NotNull] MediaTypeHeaderValue mediaType) + T value, + MediaTypeHeaderValue mediaType) { + if (request == null) + { + throw new ArgumentNullException(nameof(request)); + } + + if (value == null) + { + throw new ArgumentNullException(nameof(value)); + } + + if (mediaType == null) + { + throw new ArgumentNullException(nameof(mediaType)); + } + var context = GetHttpContext(request); // Get the default formatters from options @@ -330,11 +424,26 @@ namespace System.Net.Http /// The formatter to use. /// A response wrapping with . public static HttpResponseMessage CreateResponse( - [NotNull] this HttpRequestMessage request, + this HttpRequestMessage request, HttpStatusCode statusCode, - [NotNull] T value, - [NotNull] MediaTypeFormatter formatter) + T value, + MediaTypeFormatter formatter) { + if (request == null) + { + throw new ArgumentNullException(nameof(request)); + } + + if (value == null) + { + throw new ArgumentNullException(nameof(value)); + } + + if (formatter == null) + { + throw new ArgumentNullException(nameof(formatter)); + } + return request.CreateResponse(statusCode, value, formatter, (MediaTypeHeaderValue)null); } @@ -352,12 +461,27 @@ namespace System.Net.Http /// /// A response wrapping with . public static HttpResponseMessage CreateResponse( - [NotNull] this HttpRequestMessage request, + this HttpRequestMessage request, HttpStatusCode statusCode, - [NotNull] T value, - [NotNull] MediaTypeFormatter formatter, + T value, + MediaTypeFormatter formatter, string mediaType) { + if (request == null) + { + throw new ArgumentNullException(nameof(request)); + } + + if (value == null) + { + throw new ArgumentNullException(nameof(value)); + } + + if (formatter == null) + { + throw new ArgumentNullException(nameof(formatter)); + } + var mediaTypeHeader = mediaType != null ? new MediaTypeHeaderValue(mediaType) : null; return request.CreateResponse(statusCode, value, formatter, mediaTypeHeader); } @@ -376,12 +500,22 @@ namespace System.Net.Http /// /// A response wrapping with . public static HttpResponseMessage CreateResponse( - [NotNull] this HttpRequestMessage request, + this HttpRequestMessage request, HttpStatusCode statusCode, T value, - [NotNull] MediaTypeFormatter formatter, + MediaTypeFormatter formatter, MediaTypeHeaderValue mediaType) { + if (request == null) + { + throw new ArgumentNullException(nameof(request)); + } + + if (formatter == null) + { + throw new ArgumentNullException(nameof(formatter)); + } + var response = new HttpResponseMessage(statusCode) { RequestMessage = request, diff --git a/src/Microsoft.AspNet.Mvc.WebApiCompatShim/HttpRequestMessage/HttpRequestMessageFeature.cs b/src/Microsoft.AspNet.Mvc.WebApiCompatShim/HttpRequestMessage/HttpRequestMessageFeature.cs index 0af8ba7221..7c51ad3385 100644 --- a/src/Microsoft.AspNet.Mvc.WebApiCompatShim/HttpRequestMessage/HttpRequestMessageFeature.cs +++ b/src/Microsoft.AspNet.Mvc.WebApiCompatShim/HttpRequestMessage/HttpRequestMessageFeature.cs @@ -1,11 +1,11 @@ // Copyright (c) .NET Foundation. 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.Diagnostics; using System.Net.Http; using Microsoft.AspNet.Http; -using Microsoft.Framework.Internal; namespace Microsoft.AspNet.Mvc.WebApiCompatShim { @@ -14,8 +14,13 @@ namespace Microsoft.AspNet.Mvc.WebApiCompatShim private readonly HttpContext _httpContext; private HttpRequestMessage _httpRequestMessage; - public HttpRequestMessageFeature([NotNull] HttpContext httpContext) + public HttpRequestMessageFeature(HttpContext httpContext) { + if (httpContext == null) + { + throw new ArgumentNullException(nameof(httpContext)); + } + _httpContext = httpContext; } diff --git a/src/Microsoft.AspNet.Mvc.WebApiCompatShim/HttpResponseException.cs b/src/Microsoft.AspNet.Mvc.WebApiCompatShim/HttpResponseException.cs index 28b49b8739..69c1592aea 100644 --- a/src/Microsoft.AspNet.Mvc.WebApiCompatShim/HttpResponseException.cs +++ b/src/Microsoft.AspNet.Mvc.WebApiCompatShim/HttpResponseException.cs @@ -3,8 +3,6 @@ using System.Net; using System.Net.Http; -using Microsoft.AspNet.Mvc; -using Microsoft.Framework.Internal; using ShimResources = Microsoft.AspNet.Mvc.WebApiCompatShim.Resources; namespace System.Web.Http @@ -24,9 +22,14 @@ namespace System.Web.Http /// Initializes a new instance of the class. /// /// The response message. - public HttpResponseException([NotNull] HttpResponseMessage response) + public HttpResponseException(HttpResponseMessage response) : base(ShimResources.HttpResponseExceptionMessage) { + if (response == null) + { + throw new ArgumentNullException(nameof(response)); + } + Response = response; } diff --git a/src/Microsoft.AspNet.Mvc.WebApiCompatShim/HttpResponseExceptionActionFilter.cs b/src/Microsoft.AspNet.Mvc.WebApiCompatShim/HttpResponseExceptionActionFilter.cs index 414d47c10d..fd140e2d20 100644 --- a/src/Microsoft.AspNet.Mvc.WebApiCompatShim/HttpResponseExceptionActionFilter.cs +++ b/src/Microsoft.AspNet.Mvc.WebApiCompatShim/HttpResponseExceptionActionFilter.cs @@ -1,10 +1,10 @@ // Copyright (c) .NET Foundation. 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.Net.Http; using System.Web.Http; using Microsoft.AspNet.Mvc.Filters; -using Microsoft.Framework.Internal; namespace Microsoft.AspNet.Mvc.WebApiCompatShim { @@ -18,12 +18,17 @@ namespace Microsoft.AspNet.Mvc.WebApiCompatShim // Return a high number by default so that it runs closest to the action. public int Order { get; set; } = int.MaxValue - 10; - public void OnActionExecuting([NotNull] ActionExecutingContext context) + public void OnActionExecuting(ActionExecutingContext context) { } - public void OnActionExecuted([NotNull] ActionExecutedContext context) + public void OnActionExecuted(ActionExecutedContext context) { + if (context == null) + { + throw new ArgumentNullException(nameof(context)); + } + var httpResponseException = context.Exception as HttpResponseException; if (httpResponseException != null) { diff --git a/src/Microsoft.AspNet.Mvc.WebApiCompatShim/InvalidModelStateResult.cs b/src/Microsoft.AspNet.Mvc.WebApiCompatShim/InvalidModelStateResult.cs index eb8cde4e86..a8feb1d000 100644 --- a/src/Microsoft.AspNet.Mvc.WebApiCompatShim/InvalidModelStateResult.cs +++ b/src/Microsoft.AspNet.Mvc.WebApiCompatShim/InvalidModelStateResult.cs @@ -5,7 +5,6 @@ using System.Threading.Tasks; using Microsoft.AspNet.Http; using Microsoft.AspNet.Mvc; using Microsoft.AspNet.Mvc.ModelBinding; -using Microsoft.Framework.Internal; namespace System.Web.Http { @@ -20,9 +19,14 @@ namespace System.Web.Http /// /// if the error should include exception messages; otherwise, . /// - public InvalidModelStateResult([NotNull] ModelStateDictionary modelState, bool includeErrorDetail) + public InvalidModelStateResult(ModelStateDictionary modelState, bool includeErrorDetail) : base(new HttpError(modelState, includeErrorDetail)) { + if (modelState == null) + { + throw new ArgumentNullException(nameof(modelState)); + } + ModelState = modelState; IncludeErrorDetail = includeErrorDetail; } diff --git a/src/Microsoft.AspNet.Mvc.WebApiCompatShim/ResponseMessageResult.cs b/src/Microsoft.AspNet.Mvc.WebApiCompatShim/ResponseMessageResult.cs index c75f54e621..fd3a3eb243 100644 --- a/src/Microsoft.AspNet.Mvc.WebApiCompatShim/ResponseMessageResult.cs +++ b/src/Microsoft.AspNet.Mvc.WebApiCompatShim/ResponseMessageResult.cs @@ -3,7 +3,6 @@ using System.Net.Http; using Microsoft.AspNet.Mvc; -using Microsoft.Framework.Internal; namespace System.Web.Http { @@ -16,9 +15,14 @@ namespace System.Web.Http /// Initializes a new instance of the class. /// /// The response message. - public ResponseMessageResult([NotNull] HttpResponseMessage response) + public ResponseMessageResult(HttpResponseMessage response) : base(response) { + if (response == null) + { + throw new ArgumentNullException(nameof(response)); + } + Response = response; } diff --git a/src/Microsoft.AspNet.Mvc.WebApiCompatShim/project.json b/src/Microsoft.AspNet.Mvc.WebApiCompatShim/project.json index b3e32d71a9..d8f319a3b8 100644 --- a/src/Microsoft.AspNet.Mvc.WebApiCompatShim/project.json +++ b/src/Microsoft.AspNet.Mvc.WebApiCompatShim/project.json @@ -6,7 +6,7 @@ "url": "git://github.com/aspnet/mvc" }, "compilationOptions": { - "warningsAsErrors": false + "warningsAsErrors": true }, "dependencies": { "Microsoft.AspNet.WebUtilities": "1.0.0-*", @@ -16,10 +16,6 @@ "Microsoft.Framework.PropertyHelper.Sources": { "version": "1.0.0-*", "type": "build" - }, - "Microsoft.Framework.NotNullAttribute.Sources": { - "version": "1.0.0-*", - "type": "build" } }, "frameworks": {