From 9627d93a0d7c0fd68785f3aac30de52133fcea69 Mon Sep 17 00:00:00 2001 From: Pranav K Date: Thu, 6 Jul 2017 17:50:02 -0700 Subject: [PATCH] Revive call to FormattingUtilities in XmlDataContractSerializerOutputFormatter Fixes #6235 --- .../XmlDataContractSerializerOutputFormatter.cs | 4 +++- .../ApiExplorerTest.cs | 13 ++++++------- ...piExplorerResponseTypeWithAttributeController.cs | 9 ++++----- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/Microsoft.AspNetCore.Mvc.Formatters.Xml/XmlDataContractSerializerOutputFormatter.cs b/src/Microsoft.AspNetCore.Mvc.Formatters.Xml/XmlDataContractSerializerOutputFormatter.cs index 57b4071cfe..31d22e625c 100644 --- a/src/Microsoft.AspNetCore.Mvc.Formatters.Xml/XmlDataContractSerializerOutputFormatter.cs +++ b/src/Microsoft.AspNetCore.Mvc.Formatters.Xml/XmlDataContractSerializerOutputFormatter.cs @@ -132,7 +132,9 @@ protected virtual DataContractSerializer CreateSerializer(Type type) try { - // Use FormattingUtilities here when https://github.com/aspnet/Mvc/issues/6235 is resolved. + // Verify that type is a valid data contract by forcing the serializer to try to create a data contract + FormattingUtilities.XsdDataContractExporter.GetRootElementName(type); + // If the serializer does not support this type it will throw an exception. return new DataContractSerializer(type, _serializerSettings); } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/ApiExplorerTest.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/ApiExplorerTest.cs index 8b121a46ad..c2363f7399 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/ApiExplorerTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/ApiExplorerTest.cs @@ -1,8 +1,9 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -using System.Linq; using System.Collections.Generic; +using System.Linq; +using System.Net; using System.Net.Http; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc.Formatters; @@ -10,8 +11,6 @@ using Microsoft.AspNetCore.Testing.xunit; using Newtonsoft.Json; using Xunit; -using Microsoft.AspNetCore.Http; -using System.Net; namespace Microsoft.AspNetCore.Mvc.FunctionalTests { @@ -571,7 +570,7 @@ public async Task ExplicitResponseTypeDecoration_SuppressesDefaultStatus() { // Arrange var type1 = typeof(ApiExplorerWebSite.Product).FullName; - var type2 = typeof(ModelStateDictionary).FullName; + var type2 = typeof(SerializableError).FullName; var expectedMediaTypes = new[] { "application/json", "text/json", "application/xml", "text/xml" }; // Act @@ -603,7 +602,7 @@ public async Task ExplicitResponseTypeDecoration_SuppressesDefaultStatus_AlsoHon { // Arrange var type1 = typeof(ApiExplorerWebSite.Product).FullName; - var type2 = typeof(ModelStateDictionary).FullName; + var type2 = typeof(SerializableError).FullName; var expectedMediaTypes = new[] { "text/xml" }; // Act @@ -635,7 +634,7 @@ public async Task ExplicitResponseTypeDecoration_WithExplicitDefaultStatus() { // Arrange var type1 = typeof(ApiExplorerWebSite.Product).FullName; - var type2 = typeof(ModelStateDictionary).FullName; + var type2 = typeof(SerializableError).FullName; var expectedMediaTypes = new[] { "application/json", "text/json", "application/xml", "text/xml" }; // Act @@ -667,7 +666,7 @@ public async Task ExplicitResponseTypeDecoration_WithExplicitDefaultStatus_Speci { // Arrange var type1 = typeof(ApiExplorerWebSite.Product).FullName; - var type2 = typeof(ModelStateDictionary).FullName; + var type2 = typeof(SerializableError).FullName; var expectedMediaTypes = new[] { "text/xml" }; // Act diff --git a/test/WebSites/ApiExplorerWebSite/Controllers/ApiExplorerResponseTypeWithAttributeController.cs b/test/WebSites/ApiExplorerWebSite/Controllers/ApiExplorerResponseTypeWithAttributeController.cs index 81eca53ce2..e902e8ec56 100644 --- a/test/WebSites/ApiExplorerWebSite/Controllers/ApiExplorerResponseTypeWithAttributeController.cs +++ b/test/WebSites/ApiExplorerWebSite/Controllers/ApiExplorerResponseTypeWithAttributeController.cs @@ -3,7 +3,6 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.Mvc.ModelBinding; namespace ApiExplorerWebSite { @@ -58,7 +57,7 @@ public Product GetProduct() } [ProducesResponseType(typeof(Product), 201)] - [ProducesResponseType(typeof(ModelStateDictionary), 400)] + [ProducesResponseType(typeof(SerializableError), 400)] public Product CreateProductWithDefaultResponseContentTypes(Product product) { return null; @@ -66,21 +65,21 @@ public Product CreateProductWithDefaultResponseContentTypes(Product product) [Produces("text/xml")] // Has status code as 200 but is not applied as it does not set 'Type' [ProducesResponseType(typeof(Product), 201)] - [ProducesResponseType(typeof(ModelStateDictionary), 400)] + [ProducesResponseType(typeof(SerializableError), 400)] public Product CreateProductWithLimitedResponseContentTypes(Product product) { return null; } [ProducesResponseType(typeof(Product), 200)] - [ProducesResponseType(typeof(ModelStateDictionary), 400)] + [ProducesResponseType(typeof(SerializableError), 400)] public Product UpdateProductWithDefaultResponseContentTypes(Product product) { return null; } [Produces("text/xml", Type = typeof(Product))] // Has status code as 200 - [ProducesResponseType(typeof(ModelStateDictionary), 400)] + [ProducesResponseType(typeof(SerializableError), 400)] public Product UpdateProductWithLimitedResponseContentTypes(Product product) { return null;