diff --git a/src/Microsoft.AspNet.Mvc.Xml/Formatters/XmlDataContractSerializerInputFormatter.cs b/src/Microsoft.AspNet.Mvc.Xml/Formatters/XmlDataContractSerializerInputFormatter.cs index dda0e1a700..cec1a3c38d 100644 --- a/src/Microsoft.AspNet.Mvc.Xml/Formatters/XmlDataContractSerializerInputFormatter.cs +++ b/src/Microsoft.AspNet.Mvc.Xml/Formatters/XmlDataContractSerializerInputFormatter.cs @@ -106,7 +106,7 @@ namespace Microsoft.AspNet.Mvc.Xml return GetDefaultValueForType(context.ModelType); } - return await ReadInternal(context); + return await ReadInternalAsync(context); } /// @@ -121,12 +121,23 @@ namespace Microsoft.AspNet.Mvc.Xml } /// - /// Called during deserialization to get the . + /// Gets the type to which the XML will be deserialized. /// - /// The used during deserialization. - protected virtual XmlObjectSerializer CreateDataContractSerializer(Type type) + /// The declared type. + /// The type to which the XML will be deserialized. + protected virtual Type GetSerializableType([NotNull] Type declaredType) { - return new DataContractSerializer(SerializableErrorWrapper.CreateSerializableType(type), _serializerSettings); + return SerializableErrorWrapper.CreateSerializableType(declaredType); + } + + /// + /// Called during deserialization to get the . + /// + /// The type of object for which the serializer should be created. + /// The used during deserialization. + protected virtual DataContractSerializer CreateSerializer([NotNull] Type type) + { + return new DataContractSerializer(type, _serializerSettings); } private object GetDefaultValueForType(Type modelType) @@ -139,16 +150,16 @@ namespace Microsoft.AspNet.Mvc.Xml return null; } - private Task ReadInternal(InputFormatterContext context) + private Task ReadInternalAsync(InputFormatterContext context) { - var type = context.ModelType; var request = context.ActionContext.HttpContext.Request; using (var xmlReader = CreateXmlReader(new DelegatingStream(request.Body))) { - var xmlSerializer = CreateDataContractSerializer(type); - var deserializedObject = xmlSerializer.ReadObject(xmlReader); - deserializedObject = SerializableErrorWrapper.UnwrapSerializableErrorObject(type, deserializedObject); + var type = GetSerializableType(context.ModelType); + var dataContractSerializer = CreateSerializer(type); + var deserializedObject = dataContractSerializer.ReadObject(xmlReader); + deserializedObject = SerializableErrorWrapper.UnwrapSerializableErrorObject(context.ModelType, deserializedObject); return Task.FromResult(deserializedObject); } } diff --git a/src/Microsoft.AspNet.Mvc.Xml/Formatters/XmlSerializerInputFormatter.cs b/src/Microsoft.AspNet.Mvc.Xml/Formatters/XmlSerializerInputFormatter.cs index 4bce11b677..6b9611c71a 100644 --- a/src/Microsoft.AspNet.Mvc.Xml/Formatters/XmlSerializerInputFormatter.cs +++ b/src/Microsoft.AspNet.Mvc.Xml/Formatters/XmlSerializerInputFormatter.cs @@ -70,7 +70,7 @@ namespace Microsoft.AspNet.Mvc.Xml } return SupportedMediaTypes - .Any(supportedMediaType => supportedMediaType.IsSubsetOf(requestContentType)); + .Any(supportedMediaType => supportedMediaType.IsSubsetOf(requestContentType)); } /// @@ -86,7 +86,17 @@ namespace Microsoft.AspNet.Mvc.Xml return GetDefaultValueForType(context.ModelType); } - return await ReadInternal(context); + return await ReadInternalAsync(context); + } + + /// + /// Gets the type to which the XML will be deserialized. + /// + /// The declared type. + /// The type to which the XML will be deserialized. + protected virtual Type GetSerializableType([NotNull] Type declaredType) + { + return SerializableErrorWrapper.CreateSerializableType(declaredType); } /// @@ -106,7 +116,7 @@ namespace Microsoft.AspNet.Mvc.Xml /// The used during deserialization. protected virtual XmlSerializer CreateXmlSerializer(Type type) { - return new XmlSerializer(SerializableErrorWrapper.CreateSerializableType(type)); + return new XmlSerializer(type); } private object GetDefaultValueForType(Type modelType) @@ -119,16 +129,16 @@ namespace Microsoft.AspNet.Mvc.Xml return null; } - private Task ReadInternal(InputFormatterContext context) + private Task ReadInternalAsync(InputFormatterContext context) { - var type = context.ModelType; var request = context.ActionContext.HttpContext.Request; using (var xmlReader = CreateXmlReader(new DelegatingStream(request.Body))) { + var type = GetSerializableType(context.ModelType); var xmlSerializer = CreateXmlSerializer(type); var deserializedObject = xmlSerializer.Deserialize(xmlReader); - deserializedObject = SerializableErrorWrapper.UnwrapSerializableErrorObject(type, deserializedObject); + deserializedObject = SerializableErrorWrapper.UnwrapSerializableErrorObject(context.ModelType, deserializedObject); return Task.FromResult(deserializedObject); } }