8000 Fix #625: xml output encoding don't match writer setting by lhyqy5 · Pull Request #634 · reactiveui/refit · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix #625: xml output encoding don't match writer setting #634

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions Refit.Tests/XmlContentSerializerTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;
using Xunit;

Expand Down Expand Up @@ -92,6 +94,29 @@ public async Task ShouldDeserializeFromXmlAsync()
Assert.Equal("123", dto.Identifier);
}

[Fact]
public async Task XmlEncodingShouldMatchWriterSettingAsync()
{
var encoding = Encoding.UTF7;
var serializerSettings = new XmlContentSerializerSettings
{
XmlReaderWriterSettings = new XmlReaderWriterSettings()
{
WriterSettings = new XmlWriterSettings()
{
Encoding = encoding
}
}
};
var sut = new XmlContentSerializer(serializerSettings);

var dto = BuildDto();
var content = await sut.SerializeAsync(dto);
var xml = XDocument.Parse(await content.ReadAsStringAsync());
var documentEncoding = xml.Declaration.Encoding;
Assert.Equal(encoding.WebName, documentEncoding);
}

private static Dto BuildDto()
{
var dto = new Dto
Expand Down
8 changes: 5 additions & 3 deletions Refit/XmlContentSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ public Task<HttpContent> SerializeAsync<T>(T item)
{
var xmlSerializer = new XmlSerializer(item.GetType(), settings.XmlAttributeOverrides);

using (var output = new StringWriter())
using (var stream = new MemoryStream())
{
using (var writer = XmlWriter.Create(output, settings.XmlReaderWriterSettings.WriterSettings))
using (var writer = XmlWriter.Create(stream, settings.XmlReaderWriterSettings.WriterSettings))
{
var encoding = settings.XmlReaderWriterSettings.WriterSettings?.Encoding ?? Encoding.Unicode;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What should the default be? Unicode or UTF8? Any reason for Unicode over UTF8 as the default?

Copy link
Contributor Author
@lhyqy5 lhyqy5 Mar 9, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

StringWriter default encoding is Unicode, so I keep it, and utf-16 is the previous behavior

xmlSerializer.Serialize(writer, item, settings.XmlNamespaces);
var content = new StringContent(output.ToString(), Encoding.UTF8, "application/xml");
var str = encoding.GetString(stream.ToArray());
var content = new StringContent(str, encoding, "application/xml");
return Task.FromResult((HttpContent)content);
}
}
Expand Down
0