Issue #1081 - XML DataContract Formatter can fail to write when instance type != declaredType.
Adding Functional tests appropriately.
This commit is contained in:
parent
fe0a9331d2
commit
4c951cc635
|
|
@ -2,10 +2,10 @@
|
||||||
// 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 System.Collections.Generic;
|
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Net.Http.Headers;
|
using System.Net.Http.Headers;
|
||||||
|
using System.Runtime.Serialization;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNet.Builder;
|
using Microsoft.AspNet.Builder;
|
||||||
using Microsoft.AspNet.TestHost;
|
using Microsoft.AspNet.TestHost;
|
||||||
|
|
@ -29,10 +29,10 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
|
||||||
// Arrange
|
// Arrange
|
||||||
var server = TestServer.Create(_services, _app);
|
var server = TestServer.Create(_services, _app);
|
||||||
var client = server.CreateClient();
|
var client = server.CreateClient();
|
||||||
|
|
||||||
// Act
|
|
||||||
var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost/Home/GetDummyClass?sampleInput=10");
|
var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost/Home/GetDummyClass?sampleInput=10");
|
||||||
request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/xml;charset=utf-8"));
|
request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/xml;charset=utf-8"));
|
||||||
|
|
||||||
|
// Act
|
||||||
var response = await client.SendAsync(request);
|
var response = await client.SendAsync(request);
|
||||||
|
|
||||||
//Assert
|
//Assert
|
||||||
|
|
@ -49,10 +49,10 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
|
||||||
// Arrange
|
// Arrange
|
||||||
var server = TestServer.Create(_services, _app);
|
var server = TestServer.Create(_services, _app);
|
||||||
var client = server.CreateClient();
|
var client = server.CreateClient();
|
||||||
|
|
||||||
// Act
|
|
||||||
var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost/XmlSerializer/GetDummyClass?sampleInput=10");
|
var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost/XmlSerializer/GetDummyClass?sampleInput=10");
|
||||||
request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/xml;charset=utf-8"));
|
request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/xml;charset=utf-8"));
|
||||||
|
|
||||||
|
// Act
|
||||||
var response = await client.SendAsync(request);
|
var response = await client.SendAsync(request);
|
||||||
|
|
||||||
//Assert
|
//Assert
|
||||||
|
|
@ -68,11 +68,11 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
|
||||||
// Arrange
|
// Arrange
|
||||||
var server = TestServer.Create(_services, _app);
|
var server = TestServer.Create(_services, _app);
|
||||||
var client = server.CreateClient();
|
var client = server.CreateClient();
|
||||||
|
|
||||||
// Act
|
|
||||||
var request = new HttpRequestMessage(HttpMethod.Post,
|
var request = new HttpRequestMessage(HttpMethod.Post,
|
||||||
"http://localhost/DataContractSerializer/GetPerson?name=HelloWorld");
|
"http://localhost/DataContractSerializer/GetPerson?name=HelloWorld");
|
||||||
request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/xml;charset=utf-8"));
|
request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/xml;charset=utf-8"));
|
||||||
|
|
||||||
|
// Act
|
||||||
var response = await client.SendAsync(request);
|
var response = await client.SendAsync(request);
|
||||||
|
|
||||||
//Assert
|
//Assert
|
||||||
|
|
@ -82,5 +82,64 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
|
||||||
"<Name>HelloWorld</Name></Person>",
|
"<Name>HelloWorld</Name></Person>",
|
||||||
await response.Content.ReadAsStringAsync());
|
await response.Content.ReadAsStringAsync());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task XmlSerializerOutputFormatter_WhenDerivedClassIsReturned()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var server = TestServer.Create(_services, _app);
|
||||||
|
var client = server.CreateClient();
|
||||||
|
var request = new HttpRequestMessage(
|
||||||
|
HttpMethod.Post, "http://localhost/XmlSerializer/GetDerivedDummyClass?sampleInput=10");
|
||||||
|
request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/xml;charset=utf-8"));
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var response = await client.SendAsync(request);
|
||||||
|
|
||||||
|
//Assert
|
||||||
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||||
|
Assert.Equal("<DummyClass xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
|
||||||
|
"xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xsi:type=\"DerivedDummyClass\">" +
|
||||||
|
"<SampleInt>10</SampleInt><SampleIntInDerived>50</SampleIntInDerived></DummyClass>",
|
||||||
|
await response.Content.ReadAsStringAsync());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task XmlDataContractSerializerOutputFormatter_WhenDerivedClassIsReturned()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var server = TestServer.Create(_services, _app);
|
||||||
|
var client = server.CreateClient();
|
||||||
|
var request = new HttpRequestMessage(
|
||||||
|
HttpMethod.Post, "http://localhost/Home/GetDerivedDummyClass?sampleInput=10");
|
||||||
|
request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/xml;charset=utf-8"));
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var response = await client.SendAsync(request);
|
||||||
|
|
||||||
|
//Assert
|
||||||
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||||
|
Assert.Equal("<DummyClass xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" " +
|
||||||
|
"i:type=\"DerivedDummyClass\" xmlns=\"http://schemas.datacontract.org/2004/07/FormatterWebSite\"" +
|
||||||
|
"><SampleInt>10</SampleInt><SampleIntInDerived>50</SampleIntInDerived></DummyClass>",
|
||||||
|
await response.Content.ReadAsStringAsync());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task XmlSerializerFormatter_DoesNotWriteDictionaryObjects()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var server = TestServer.Create(_services, _app);
|
||||||
|
var client = server.CreateClient();
|
||||||
|
var request = new HttpRequestMessage(
|
||||||
|
HttpMethod.Post, "http://localhost/XmlSerializer/GetDictionary");
|
||||||
|
request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/xml;charset=utf-8"));
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var response = await client.SendAsync(request);
|
||||||
|
|
||||||
|
//Assert
|
||||||
|
Assert.Equal(HttpStatusCode.NotAcceptable, response.StatusCode);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -24,5 +24,15 @@ namespace FormatterWebSite.Controllers
|
||||||
{
|
{
|
||||||
return dummy != null;
|
return dummy != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[HttpPost]
|
||||||
|
public DummyClass GetDerivedDummyClass(int sampleInput)
|
||||||
|
{
|
||||||
|
return new DerivedDummyClass
|
||||||
|
{
|
||||||
|
SampleInt = sampleInput,
|
||||||
|
SampleIntInDerived = 50
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
|
// 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.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
|
using System.Collections.Generic;
|
||||||
using Microsoft.AspNet.Mvc;
|
using Microsoft.AspNet.Mvc;
|
||||||
|
|
||||||
namespace FormatterWebSite
|
namespace FormatterWebSite
|
||||||
|
|
@ -23,5 +24,24 @@ namespace FormatterWebSite
|
||||||
{
|
{
|
||||||
return new DummyClass { SampleInt = sampleInput };
|
return new DummyClass { SampleInt = sampleInput };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[HttpPost]
|
||||||
|
public DummyClass GetDerivedDummyClass(int sampleInput)
|
||||||
|
{
|
||||||
|
return new DerivedDummyClass
|
||||||
|
{
|
||||||
|
SampleInt = sampleInput,
|
||||||
|
SampleIntInDerived = 50
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost]
|
||||||
|
public Dictionary<string, string> GetDictionary()
|
||||||
|
{
|
||||||
|
return new Dictionary<string, string>
|
||||||
|
{
|
||||||
|
{ "Hello", "World" }
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -0,0 +1,10 @@
|
||||||
|
// 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.
|
||||||
|
|
||||||
|
namespace FormatterWebSite
|
||||||
|
{
|
||||||
|
public class DerivedDummyClass : DummyClass
|
||||||
|
{
|
||||||
|
public int SampleIntInDerived { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,8 +1,13 @@
|
||||||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
|
// 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.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
|
using System.Runtime.Serialization;
|
||||||
|
using System.Xml.Serialization;
|
||||||
|
|
||||||
namespace FormatterWebSite
|
namespace FormatterWebSite
|
||||||
{
|
{
|
||||||
|
[KnownType(typeof(DerivedDummyClass))]
|
||||||
|
[XmlInclude(typeof(DerivedDummyClass))]
|
||||||
public class DummyClass
|
public class DummyClass
|
||||||
{
|
{
|
||||||
public int SampleInt { get; set; }
|
public int SampleInt { get; set; }
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,16 @@
|
||||||
"Microsoft.AspNet.Mvc.TestConfiguration": ""
|
"Microsoft.AspNet.Mvc.TestConfiguration": ""
|
||||||
},
|
},
|
||||||
"configurations": {
|
"configurations": {
|
||||||
"aspnet50": { },
|
"aspnet50": {
|
||||||
"aspnetcore50": { }
|
"dependencies": {
|
||||||
|
"System.Xml": ""
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"aspnetcore50": {
|
||||||
|
"dependencies": {
|
||||||
|
"System.Runtime.Serialization.Xml": "4.0.0.0",
|
||||||
|
"System.Xml.ReaderWriter": "4.0.10.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue