[JsonPatch]Rename SimpleObjectAdapter; Include statuscode in JsonPatchException

This commit is contained in:
KevinDockx 2015-03-24 14:22:47 +01:00 committed by Kirthi Krishnamraju
parent 2d304e5da1
commit 0462dd6be3
7 changed files with 84 additions and 65 deletions

View File

@ -12,11 +12,11 @@ using Newtonsoft.Json.Serialization;
namespace Microsoft.AspNet.JsonPatch.Adapters namespace Microsoft.AspNet.JsonPatch.Adapters
{ {
public class SimpleObjectAdapter<T> : IObjectAdapter<T> where T : class public class ObjectAdapter<T> : IObjectAdapter<T> where T : class
{ {
public IContractResolver ContractResolver { get; set; } public IContractResolver ContractResolver { get; set; }
public SimpleObjectAdapter(IContractResolver contractResolver) public ObjectAdapter(IContractResolver contractResolver)
{ {
ContractResolver = contractResolver; ContractResolver = contractResolver;
} }
@ -160,7 +160,7 @@ namespace Microsoft.AspNet.JsonPatch.Adapters
throw new JsonPatchException<T>(operationToReport, throw new JsonPatchException<T>(operationToReport,
string.Format("Patch failed: provided path is invalid for array property type at " + string.Format("Patch failed: provided path is invalid for array property type at " +
"location path: {0}: position larger than array size", "location path: {0}: position larger than array size",
path), path, 422),
objectToApplyTo); objectToApplyTo);
} }
} }
@ -171,7 +171,7 @@ namespace Microsoft.AspNet.JsonPatch.Adapters
string.Format("Patch failed: provided path is invalid for array property type at location " + string.Format("Patch failed: provided path is invalid for array property type at location " +
"path: {0}: expected array", "path: {0}: expected array",
path), path),
objectToApplyTo); objectToApplyTo, 422);
} }
} }
else else
@ -251,7 +251,7 @@ namespace Microsoft.AspNet.JsonPatch.Adapters
string.Format("Patch failed: provided from path is invalid for array property type at " + string.Format("Patch failed: provided from path is invalid for array property type at " +
"location from: {0}: invalid position", "location from: {0}: invalid position",
operation.from), operation.from),
objectToApplyTo); objectToApplyTo, 422);
} }
valueAtFromLocation = array[positionAsInteger]; valueAtFromLocation = array[positionAsInteger];
@ -262,7 +262,7 @@ namespace Microsoft.AspNet.JsonPatch.Adapters
string.Format("Patch failed: provided from path is invalid for array property type at " + string.Format("Patch failed: provided from path is invalid for array property type at " +
"location from: {0}: expected array", "location from: {0}: expected array",
operation.from), operation.from),
objectToApplyTo); objectToApplyTo, 422);
} }
} }
else else
@ -360,7 +360,7 @@ namespace Microsoft.AspNet.JsonPatch.Adapters
string.Format("Patch failed: provided path is invalid for array property type at " + string.Format("Patch failed: provided path is invalid for array property type at " +
"location path: {0}: position larger than array size", "location path: {0}: position larger than array size",
path), path),
objectToApplyTo); objectToApplyTo, 422);
} }
} }
} }
@ -370,7 +370,7 @@ namespace Microsoft.AspNet.JsonPatch.Adapters
string.Format("Patch failed: provided path is invalid for array property type at " + string.Format("Patch failed: provided path is invalid for array property type at " +
"location path: {0}: expected array", "location path: {0}: expected array",
path), path),
objectToApplyTo); objectToApplyTo, 422);
} }
} }
else else
@ -481,7 +481,7 @@ namespace Microsoft.AspNet.JsonPatch.Adapters
string.Format("Patch failed: provided from path is invalid for array property type at " + string.Format("Patch failed: provided from path is invalid for array property type at " +
"location path: {0}: invalid position", "location path: {0}: invalid position",
operation.path), operation.path),
objectToApplyTo); objectToApplyTo, 422);
} }
valueAtPathLocation = array[positionInPathAsInteger]; valueAtPathLocation = array[positionInPathAsInteger];
@ -492,7 +492,7 @@ namespace Microsoft.AspNet.JsonPatch.Adapters
string.Format("Patch failed: provided from path is invalid for array property type at " + string.Format("Patch failed: provided from path is invalid for array property type at " +
"location path: {0}: expected array", "location path: {0}: expected array",
operation.path), operation.path),
objectToApplyTo); objectToApplyTo, 422);
} }
} }
else else
@ -600,7 +600,7 @@ namespace Microsoft.AspNet.JsonPatch.Adapters
string.Format("Patch failed: provided from path is invalid for array property type at " + string.Format("Patch failed: provided from path is invalid for array property type at " +
"location from: {0}: invalid position", "location from: {0}: invalid position",
operation.from), operation.from),
objectToApplyTo); objectToApplyTo, 422);
} }
valueAtFromLocation = array[positionAsInteger]; valueAtFromLocation = array[positionAsInteger];
@ -611,7 +611,7 @@ namespace Microsoft.AspNet.JsonPatch.Adapters
string.Format("Patch failed: provided from path is invalid for array property type at " + string.Format("Patch failed: provided from path is invalid for array property type at " +
"location from: {0}: expected array", "location from: {0}: expected array",
operation.from), operation.from),
objectToApplyTo); objectToApplyTo, 422);
} }
} }
else else
@ -636,14 +636,14 @@ namespace Microsoft.AspNet.JsonPatch.Adapters
throw new JsonPatchException<T>( throw new JsonPatchException<T>(
operation, operation,
string.Format("Patch failed: property at location {0} does not exist", propertyPath), string.Format("Patch failed: property at location {0} does not exist", propertyPath),
objectToApplyTo); objectToApplyTo, 422);
} }
if (patchProperty.Property.Ignored) if (patchProperty.Property.Ignored)
{ {
throw new JsonPatchException<T>( throw new JsonPatchException<T>(
operation, operation,
string.Format("Patch failed: cannot update property at location {0}", propertyPath), string.Format("Patch failed: cannot update property at location {0}", propertyPath),
objectToApplyTo); objectToApplyTo, 422);
} }
} }
@ -663,7 +663,7 @@ namespace Microsoft.AspNet.JsonPatch.Adapters
var errorMessage = "Patch failed: provided value is invalid for property type at location path: "; var errorMessage = "Patch failed: provided value is invalid for property type at location path: ";
if (!result.CanBeConverted) if (!result.CanBeConverted)
{ {
throw new JsonPatchException<T>(operation, string.Format(errorMessage + "{0}", path), objectToApplyTo); throw new JsonPatchException<T>(operation, string.Format(errorMessage + "{0}", path), objectToApplyTo, 422);
} }
} }
} }

View File

@ -16,7 +16,7 @@ namespace Microsoft.AspNet.JsonPatch.Converters
{ {
public override bool CanConvert(Type objectType) public override bool CanConvert(Type objectType)
{ {
return true; return true;
} }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, public override object ReadJson(JsonReader reader, Type objectType, object existingValue,
@ -60,7 +60,7 @@ namespace Microsoft.AspNet.JsonPatch.Converters
} }
catch (Exception ex) catch (Exception ex)
{ {
throw new JsonPatchException("The JsonPatchDocument was malformed and could not be parsed.", ex); throw new JsonPatchException("The JsonPatchDocument was malformed and could not be parsed.", ex, 400);
} }
} }

View File

@ -2,14 +2,14 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
using Microsoft.AspNet.JsonPatch.Operations;
namespace Microsoft.AspNet.JsonPatch.Exceptions namespace Microsoft.AspNet.JsonPatch.Exceptions
{ {
public class JsonPatchException : Exception public class JsonPatchException<T> : JsonPatchException where T : class
{ {
public new Exception InnerException { get; internal set; } public Operation<T> FailedOperation { get; private set; }
public new T AffectedObject { get; private set; }
public object AffectedObject { get; private set; }
private string _message = ""; private string _message = "";
public override string Message public override string Message
@ -18,6 +18,7 @@ namespace Microsoft.AspNet.JsonPatch.Exceptions
{ {
return _message; return _message;
} }
} }
public JsonPatchException() public JsonPatchException()
@ -25,9 +26,23 @@ namespace Microsoft.AspNet.JsonPatch.Exceptions
} }
public JsonPatchException(string message, Exception innerException) public JsonPatchException(Operation<T> operation, string message, T affectedObject)
{ {
FailedOperation = operation;
_message = message; _message = message;
AffectedObject = affectedObject;
}
public JsonPatchException(Operation<T> operation, string message, T affectedObject, int statusCode)
: this(operation, message, affectedObject)
{
StatusCode = statusCode;
}
public JsonPatchException(Operation<T> operation, string message, T affectedObject,
int statusCode, Exception innerException)
: this(operation, message, affectedObject, statusCode)
{
InnerException = innerException; InnerException = innerException;
} }
} }

View File

@ -0,0 +1,45 @@
// 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;
namespace Microsoft.AspNet.JsonPatch.Exceptions
{
public class JsonPatchException : Exception
{
public new Exception InnerException { get; internal set; }
public int StatusCode { get; internal set; }
public object AffectedObject { get; private set; }
private string _message = "";
public override string Message
{
get
{
return _message;
}
}
public JsonPatchException()
{
}
public JsonPatchException(string message, Exception innerException)
{
_message = message;
InnerException = innerException;
}
public JsonPatchException(string message, Exception innerException, int statusCode)
: this(message, innerException)
{
StatusCode = statusCode;
}
}
}

View File

@ -1,41 +0,0 @@
// 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 Microsoft.AspNet.JsonPatch.Operations;
namespace Microsoft.AspNet.JsonPatch.Exceptions
{
public class JsonPatchException<T> : JsonPatchException where T : class
{
public Operation<T> FailedOperation { get; private set; }
public new T AffectedObject { get; private set; }
private string _message = "";
public override string Message
{
get
{
return _message;
}
}
public JsonPatchException()
{
}
public JsonPatchException(Operation<T> operation, string message, T affectedObject)
{
FailedOperation = operation;
_message = message;
AffectedObject = affectedObject;
}
public JsonPatchException(Operation<T> operation, string message, T affectedObject, Exception innerException)
: this(operation, message, affectedObject)
{
InnerException = innerException;
}
}
}

View File

@ -354,7 +354,7 @@ namespace Microsoft.AspNet.JsonPatch
public void ApplyTo(T objectToApplyTo) public void ApplyTo(T objectToApplyTo)
{ {
ApplyTo(objectToApplyTo, new SimpleObjectAdapter<T>(ContractResolver)); ApplyTo(objectToApplyTo, new ObjectAdapter<T>(ContractResolver));
} }
public void ApplyTo(T objectToApplyTo, IObjectAdapter<T> adapter) public void ApplyTo(T objectToApplyTo, IObjectAdapter<T> adapter)

View File

@ -10,7 +10,7 @@ using Xunit;
namespace Microsoft.AspNet.JsonPatch.Test namespace Microsoft.AspNet.JsonPatch.Test
{ {
public class SimpleObjectAdapterTests public class ObjectAdapterTests
{ {
[Fact] [Fact]
public void AddResultsShouldReplace() public void AddResultsShouldReplace()