Add CreateXmlReader overload - adds Type parameter (#13302)

* Add CreateXmlReader overload - adds Type parameter

This change allows inheritors of XmlSerializerInputFormatter to return a different XmlReader depending on the type being serialized. For example, an inheritor could return XSD-validating readers that validate against one schema or another (or perhaps not validate at all), depending on the value of the type parameter.
This commit is contained in:
Brian Friesen 2019-08-23 18:32:25 -04:00 committed by Pranav K
parent 5c05b9288a
commit 8b861ea7d7
2 changed files with 15 additions and 2 deletions

View File

@ -47,6 +47,7 @@ namespace Microsoft.AspNetCore.Mvc.Formatters
protected override bool CanReadType(System.Type type) { throw null; }
protected virtual System.Xml.Serialization.XmlSerializer CreateSerializer(System.Type type) { throw null; }
protected virtual System.Xml.XmlReader CreateXmlReader(System.IO.Stream readStream, System.Text.Encoding encoding) { throw null; }
protected virtual System.Xml.XmlReader CreateXmlReader(System.IO.Stream readStream, System.Text.Encoding encoding, System.Type type) { throw null; }
protected virtual System.Xml.Serialization.XmlSerializer GetCachedSerializer(System.Type type) { throw null; }
protected virtual System.Type GetSerializableType(System.Type declaredType) { throw null; }
[System.Diagnostics.DebuggerStepThroughAttribute]

View File

@ -119,8 +119,8 @@ namespace Microsoft.AspNetCore.Mvc.Formatters
try
{
using var xmlReader = CreateXmlReader(readStream, encoding);
var type = GetSerializableType(context.ModelType);
var type = GetSerializableType(context.ModelType);
using var xmlReader = CreateXmlReader(readStream, encoding, type);
var serializer = GetCachedSerializer(type);
@ -191,6 +191,18 @@ namespace Microsoft.AspNetCore.Mvc.Formatters
return wrapperProvider?.WrappingType ?? declaredType;
}
/// <summary>
/// Called during deserialization to get the <see cref="XmlReader"/>.
/// </summary>
/// <param name="readStream">The <see cref="Stream"/> from which to read.</param>
/// <param name="encoding">The <see cref="Encoding"/> used to read the stream.</param>
/// <param name="type">The <see cref="Type"/> that is to be deserialized.</param>
/// <returns>The <see cref="XmlReader"/> used during deserialization.</returns>
protected virtual XmlReader CreateXmlReader(Stream readStream, Encoding encoding, Type type)
{
return CreateXmlReader(readStream, encoding);
}
/// <summary>
/// Called during deserialization to get the <see cref="XmlReader"/>.
/// </summary>