Skip to content

Commit

Permalink
Merge pull request #1010 from vidrenning/indentwsdl
Browse files Browse the repository at this point in the history
Add option IndentWsdl to control if the generated WSDL should be indented
  • Loading branch information
andersjonsson authored Feb 7, 2024
2 parents 180355b + 90653e7 commit aa687d9
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/SoapCore.Tests/Wsdl/WsdlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1149,13 +1149,13 @@ private async Task<string> GetWsdlFromMetaBodyWriter<T>(SoapSerializer serialize
var bodyWriter = serializer == SoapSerializer.DataContractSerializer
? new MetaWCFBodyWriter(service, baseUrl, defaultBindingName, false, new[] { new SoapBindingInfo(MessageVersion.None, bindingName, portName) }) as BodyWriter
: new MetaBodyWriter(service, baseUrl, xmlNamespaceManager, defaultBindingName, new[] { new SoapBindingInfo(MessageVersion.None, bindingName, portName) }, useMicrosoftGuid) as BodyWriter;
var encoder = new SoapMessageEncoder(MessageVersion.Soap12WSAddressingAugust2004, Encoding.UTF8, false, XmlDictionaryReaderQuotas.Max, false, true, false, null, bindingName, portName);
var encoder = new SoapMessageEncoder(MessageVersion.Soap12WSAddressingAugust2004, Encoding.UTF8, false, XmlDictionaryReaderQuotas.Max, false, false, null, bindingName, portName);
var responseMessage = Message.CreateMessage(encoder.MessageVersion, null, bodyWriter);
responseMessage = new MetaMessage(responseMessage, service, xmlNamespaceManager, defaultBindingName, false);

using (var memoryStream = new MemoryStream())
{
await encoder.WriteMessageAsync(responseMessage, memoryStream);
await encoder.WriteMessageAsync(responseMessage, memoryStream, true);
memoryStream.Position = 0;

using (var streamReader = new StreamReader(memoryStream))
Expand Down
12 changes: 5 additions & 7 deletions src/SoapCore/MessageEncoder/SoapMessageEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,10 @@ public class SoapMessageEncoder
private readonly bool _overwriteResponseContentType;
private readonly bool _optimizeWriteForUtf8;
private readonly bool _omitXmlDeclaration;
private readonly bool _indentXml;
private readonly bool _checkXmlCharacters;

public SoapMessageEncoder(MessageVersion version, Encoding writeEncoding, bool overwriteResponseContentType, XmlDictionaryReaderQuotas quotas, bool omitXmlDeclaration, bool indentXml, bool checkXmlCharacters, XmlNamespaceManager xmlNamespaceOverrides, string bindingName, string portName, int maxSoapHeaderSize = SoapMessageEncoderDefaults.MaxSoapHeaderSizeDefault)
public SoapMessageEncoder(MessageVersion version, Encoding writeEncoding, bool overwriteResponseContentType, XmlDictionaryReaderQuotas quotas, bool omitXmlDeclaration, bool checkXmlCharacters, XmlNamespaceManager xmlNamespaceOverrides, string bindingName, string portName, int maxSoapHeaderSize = SoapMessageEncoderDefaults.MaxSoapHeaderSizeDefault)
{
_indentXml = indentXml;
_omitXmlDeclaration = omitXmlDeclaration;
_checkXmlCharacters = checkXmlCharacters;
BindingName = bindingName;
Expand Down Expand Up @@ -164,7 +162,7 @@ public Task<Message> ReadMessageAsync(Stream stream, int maxSizeOfHeaders, strin
return Task.FromResult(message);
}

public virtual async Task WriteMessageAsync(Message message, HttpContext httpContext, PipeWriter pipeWriter)
public virtual async Task WriteMessageAsync(Message message, HttpContext httpContext, PipeWriter pipeWriter, bool indentXml)
{
if (message == null)
{
Expand All @@ -189,7 +187,7 @@ public virtual async Task WriteMessageAsync(Message message, HttpContext httpCon
using (var xmlTextWriter = XmlWriter.Create(stringWriter, new XmlWriterSettings
{
OmitXmlDeclaration = _optimizeWriteForUtf8 && _omitXmlDeclaration, //can only omit if utf-8
Indent = _indentXml,
Indent = indentXml,
Encoding = _writeEncoding,
CloseOutput = true,
CheckCharacters = _checkXmlCharacters
Expand Down Expand Up @@ -217,7 +215,7 @@ public virtual async Task WriteMessageAsync(Message message, HttpContext httpCon
}
}

public virtual Task WriteMessageAsync(Message message, Stream stream)
public virtual Task WriteMessageAsync(Message message, Stream stream, bool indentXml)
{
if (message == null)
{
Expand All @@ -234,7 +232,7 @@ public virtual Task WriteMessageAsync(Message message, Stream stream)
using var xmlTextWriter = XmlWriter.Create(stream, new XmlWriterSettings
{
OmitXmlDeclaration = _optimizeWriteForUtf8 && _omitXmlDeclaration, //can only omit if utf-8,
Indent = _indentXml,
Indent = indentXml,
Encoding = _writeEncoding,
CloseOutput = false,
CheckCharacters = _checkXmlCharacters
Expand Down
8 changes: 8 additions & 0 deletions src/SoapCore/SoapCoreOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace SoapCore
{
public class SoapCoreOptions
{
private bool? _indentWsdl = null;

/// <summary>
/// Gets or sets the Path of the Service
/// </summary>
Expand Down Expand Up @@ -105,6 +107,12 @@ public class SoapCoreOptions
/// </summary>
public bool IndentXml { get; set; } = true;

/// <summary>
/// Gets or sets a value indicating whether to indent the generated WSDL.
/// <para>Defaults to the value of <see cref="IndentXml"/></para>
/// </summary>
public bool IndentWsdl { get => _indentWsdl ?? IndentXml; set => _indentWsdl = value; }

/// <summary>
/// Gets or sets a value indicating whether to check to make sure that the XmlOutput doesn't contain invalid characters
/// <para>Defaults to true</para>
Expand Down
16 changes: 8 additions & 8 deletions src/SoapCore/SoapEndpointMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public SoapEndpointMiddleware(ILogger<SoapEndpointMiddleware<T_MESSAGE>> logger,
for (var i = 0; i < options.EncoderOptions.Length; i++)
{
var encoderOptions = options.EncoderOptions[i];
_messageEncoders[i] = new SoapMessageEncoder(encoderOptions.MessageVersion, encoderOptions.WriteEncoding, encoderOptions.OverwriteResponseContentType, encoderOptions.ReaderQuotas, options.OmitXmlDeclaration, options.IndentXml, options.CheckXmlCharacters, encoderOptions.XmlNamespaceOverrides, encoderOptions.BindingName, encoderOptions.PortName, encoderOptions.MaxSoapHeaderSize);
_messageEncoders[i] = new SoapMessageEncoder(encoderOptions.MessageVersion, encoderOptions.WriteEncoding, encoderOptions.OverwriteResponseContentType, encoderOptions.ReaderQuotas, options.OmitXmlDeclaration, options.CheckXmlCharacters, encoderOptions.XmlNamespaceOverrides, encoderOptions.BindingName, encoderOptions.PortName, encoderOptions.MaxSoapHeaderSize);
}
}

Expand Down Expand Up @@ -171,14 +171,14 @@ public async Task Invoke(HttpContext httpContext, IServiceProvider serviceProvid
}

#if !NETCOREAPP3_0_OR_GREATER
private static Task WriteMessageAsync(SoapMessageEncoder messageEncoder, Message responseMessage, HttpContext httpContext)
private static Task WriteMessageAsync(SoapMessageEncoder messageEncoder, Message responseMessage, HttpContext httpContext, bool indentXml)
{
return messageEncoder.WriteMessageAsync(responseMessage, httpContext.Response.Body);
return messageEncoder.WriteMessageAsync(responseMessage, httpContext.Response.Body, indentXml);
}
#else
private static Task WriteMessageAsync(SoapMessageEncoder messageEncoder, Message responseMessage, HttpContext httpContext)
private static Task WriteMessageAsync(SoapMessageEncoder messageEncoder, Message responseMessage, HttpContext httpContext, bool indentXml)
{
return messageEncoder.WriteMessageAsync(responseMessage, httpContext, httpContext.Response.BodyWriter);
return messageEncoder.WriteMessageAsync(responseMessage, httpContext, httpContext.Response.BodyWriter, indentXml);
}
#endif

Expand Down Expand Up @@ -257,7 +257,7 @@ private async Task ProcessMeta(HttpContext httpContext, bool showDocumentation)
httpContext.Response.ContentType = "text/html;charset=UTF-8";

using var ms = new MemoryStream();
await messageEncoder.WriteMessageAsync(responseMessage, ms);
await messageEncoder.WriteMessageAsync(responseMessage, ms, _options.IndentWsdl);
ms.Position = 0;
using var sr = new StreamReader(ms);
var wsdl = await sr.ReadToEndAsync();
Expand All @@ -272,7 +272,7 @@ private async Task ProcessMeta(HttpContext httpContext, bool showDocumentation)
//we should use text/xml in wsdl page for browser compability.
httpContext.Response.ContentType = "text/xml;charset=UTF-8"; // _messageEncoders[0].ContentType;

await WriteMessageAsync(messageEncoder, responseMessage, httpContext);
await WriteMessageAsync(messageEncoder, responseMessage, httpContext, _options.IndentWsdl);
}

private async Task ProcessOperation(HttpContext httpContext, IServiceProvider serviceProvider)
Expand Down Expand Up @@ -324,7 +324,7 @@ private async Task ProcessOperation(HttpContext httpContext, IServiceProvider se

if (responseMessage != null)
{
await WriteMessageAsync(messageEncoder, responseMessage, httpContext);
await WriteMessageAsync(messageEncoder, responseMessage, httpContext, _options.IndentXml);
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/SoapCore/SoapOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public class SoapOptions

public bool IndentXml { get; set; } = true;

public bool IndentWsdl { get; set; } = true;

public bool UseMicrosoftGuid { get; set; } = false;

/// <summary>
Expand Down Expand Up @@ -92,6 +94,7 @@ public static SoapOptions FromSoapCoreOptions(SoapCoreOptions opt, Type serviceT
OmitXmlDeclaration = opt.OmitXmlDeclaration,
StandAloneAttribute = opt.StandAloneAttribute,
IndentXml = opt.IndentXml,
IndentWsdl = opt.IndentWsdl,
XmlNamespacePrefixOverrides = opt.XmlNamespacePrefixOverrides,
WsdlFileOptions = opt.WsdlFileOptions,
AdditionalEnvelopeXmlnsAttributes = opt.AdditionalEnvelopeXmlnsAttributes,
Expand Down

0 comments on commit aa687d9

Please sign in to comment.