Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fixed memory leak in XmlBodyDeserializer #925

Merged
merged 2 commits into from

2 participants

@thecodejunkie

Resolves issue #923

@grumpydev grumpydev merged commit de79bbb into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 24, 2013
  1. @thecodejunkie

    Fixed memory leak in XmlBodyDeserializer

    thecodejunkie authored
    Resolves issue #923
  2. @thecodejunkie

    Blame OCD

    thecodejunkie authored
This page is out of date. Refresh to see the latest.
View
67 src/Nancy.Tests/Unit/ModelBinding/DefaultBodyDeserializers/XmlBodyDeserializerfixture.cs
@@ -1,16 +1,10 @@
-using System.Xml.Serialization;
-
namespace Nancy.Tests.Unit.ModelBinding.DefaultBodyDeserializers
{
- using System;
using System.IO;
- using System.Linq;
using System.Text;
-
- using Nancy.Json;
using Nancy.ModelBinding;
- using Nancy.ModelBinding.DefaultBodyDeserializers;
-
+ using Nancy.ModelBinding.DefaultBodyDeserializers;
+ using System.Xml.Serialization;
using Xunit;
public class XmlBodyDeserializerFixture
@@ -25,61 +19,63 @@ public XmlBodyDeserializerFixture()
testModel = new TestModel {Bar = 13, Foo = "lil"};
testModelXml = ToXmlString(testModel);
}
- public static string ToXmlString<T>(T input)
- {
- string xml;
- var ser = new XmlSerializer(typeof(T));
- using (var ms = new MemoryStream())
- {
- ser.Serialize(ms, input);
- xml = Encoding.Default.GetString(ms.ToArray());
- }
- return xml;
- }
[Fact]
public void Should_report_true_for_can_deserialize_for_application_xml()
{
+ // Given
const string contentType = "application/xml";
+ // When
var result = this.deserialize.CanDeserialize(contentType);
+ // Then
result.ShouldBeTrue();
}
[Fact]
public void Should_report_true_for_can_deserialize_for_text_xml()
{
+ // Given
const string contentType = "text/xml";
+ // When
var result = this.deserialize.CanDeserialize(contentType);
+ // Then
result.ShouldBeTrue();
}
[Fact]
public void Should_report_true_for_can_deserialize_for_custom_xml()
{
+ // Given
const string contentType = "application/vnd.org.nancyfx.mything+xml";
+ // When
var result = this.deserialize.CanDeserialize(contentType);
+ // Then
result.ShouldBeTrue();
}
[Fact]
public void Should_report_false_for_can_deserialize_for_json_format()
{
+ // Given
const string contentType = "text/json";
+ // When
var result = this.deserialize.CanDeserialize(contentType);
+ // Then
result.ShouldBeFalse();
}
[Fact]
public void Should_deserialize_xml_model()
{
+ // Given
var bodyStream = new MemoryStream(Encoding.UTF8.GetBytes(this.testModelXml));
var context = new BindingContext()
{
@@ -87,33 +83,28 @@ public void Should_deserialize_xml_model()
ValidModelProperties = typeof(TestModel).GetProperties(),
};
+ // When
var result = (TestModel)this.deserialize.Deserialize(
"application/xml",
bodyStream,
context);
+ // Then
result.ShouldNotBeNull();
result.ShouldBeOfType(typeof(TestModel));
result.ShouldEqual(this.testModel);
- }
-
- [Fact]
- public void Should_only_set_allowed_properties()
- {
- var bodyStream = new MemoryStream(Encoding.UTF8.GetBytes(this.testModelXml));
- var context = new BindingContext()
- {
- DestinationType = typeof(TestModel),
- ValidModelProperties = typeof(TestModel).GetProperties().Where(p => p.Name == "Foo"),
- };
-
- var result = (TestModel)this.deserialize.Deserialize(
- "application/xml",
- bodyStream,
- context);
-
- result.Foo.ShouldEqual(testModel.Foo);
- result.Bar.ShouldNotEqual(testModel.Bar);
+ }
+
+ public static string ToXmlString<T>(T input)
+ {
+ string xml;
+ var ser = new XmlSerializer(typeof(T));
+ using (var ms = new MemoryStream())
+ {
+ ser.Serialize(ms, input);
+ xml = Encoding.Default.GetString(ms.ToArray());
+ }
+ return xml;
}
public class TestModel
View
1  src/Nancy/ModelBinding/DefaultBodyDeserializers/JsonBodyDeserializer.cs
@@ -3,7 +3,6 @@ namespace Nancy.ModelBinding.DefaultBodyDeserializers
using System;
using System.Collections;
using System.IO;
- using System.Linq;
using System.Reflection;
using Extensions;
using Json;
View
24 src/Nancy/ModelBinding/DefaultBodyDeserializers/XmlBodyDeserializer.cs
@@ -1,13 +1,9 @@
-using System;
-using System.Collections.Generic;
-using System.IO;
-using System.Linq;
-using System.Reflection;
-using System.Text;
-using System.Xml.Serialization;
-
namespace Nancy.ModelBinding.DefaultBodyDeserializers
-{
+{
+ using System;
+ using System.IO;
+ using System.Xml.Serialization;
+
public class XmlBodyDeserializer : IBodyDeserializer
{
public bool CanDeserialize(string contentType)
@@ -27,15 +23,7 @@ public bool CanDeserialize(string contentType)
public object Deserialize(string contentType, Stream bodyStream, BindingContext context)
{
- var attributeOverrides = new XmlAttributeOverrides();
- var propertiesToIgnore = context.DestinationType.GetProperties(BindingFlags.Public | BindingFlags.Instance)
- .Except(context.ValidModelProperties);
- foreach (var validModelProperty in propertiesToIgnore)
- {
- attributeOverrides.Add(context.DestinationType, validModelProperty.Name, new XmlAttributes{XmlIgnore = true});
- }
-
- var ser = new XmlSerializer(context.DestinationType, attributeOverrides);
+ var ser = new XmlSerializer(context.DestinationType);
return ser.Deserialize(bodyStream);
}
}
Something went wrong with that request. Please try again.