Skip to content
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

Add support for DefaultValueAttributes #907

Merged
merged 2 commits into from
Oct 19, 2022
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
49 changes: 49 additions & 0 deletions src/SoapCore.Tests/Wsdl/Services/DefaultValuesAttributesService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System.ComponentModel;
using System.ServiceModel;

namespace SoapCore.Tests.Wsdl.Services
{
#pragma warning disable SA1649 // File name should match first type name
#pragma warning disable SA1402 // File may only contain a single type
[ServiceContract(Namespace = "http://bagov.net")]
public interface IDefaultValueAttributesService
{
[OperationContract]
DefaultValueAttributesResponseType GetResponse();
}

public class DefaultValueAttributesService : IDefaultValueAttributesService
{
public DefaultValueAttributesResponseType GetResponse()
{
return new DefaultValueAttributesResponseType();
}
}

public class DefaultValueAttributesResponseType
{
public bool BooleanWithNoDefaultProperty { get; set; }

[DefaultValue(null)]
public bool BooleanWithDefaultNullProperty { get; set; }

[DefaultValue(false)]
public bool BooleanWithDefaultFalseProperty { get; set; }

[DefaultValue(true)]
public string BooleanWithDefaultTrueProperty { get; set; }

public int IntWithNoDefaultProperty { get; set; }

[DefaultValue(42)]
public int IntWithDefaultProperty { get; set; }

public string StringWithNoDefaultProperty { get; set; }

[DefaultValue(null)]
public string StringWithDefaultNullProperty { get; set; }

[DefaultValue("default")]
public string StringWithDefaultProperty { get; set; }
}
}
40 changes: 40 additions & 0 deletions src/SoapCore.Tests/Wsdl/WsdlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,46 @@ public async Task CheckMessageHeadersServiceWsdl()
Assert.IsNotNull(stringPropertyElement);
}

[TestMethod]
public async Task CheckDefaultValueAttributesServiceWsdl()
{
var wsdl = await GetWsdlFromMetaBodyWriter<DefaultValueAttributesService>(SoapSerializer.XmlSerializer);
Trace.TraceInformation(wsdl);
Assert.IsNotNull(wsdl);

Assert.IsFalse(wsdl.Contains("name=\"\""));

var root = XElement.Parse(wsdl);
var nm = Namespaces.CreateDefaultXmlNamespaceManager();

var booleanWithNoDefaultPropertyElement = root.XPathSelectElement("//xsd:element[@name='BooleanWithNoDefaultProperty' and @minOccurs='1' and @maxOccurs='1' and not(@default)]", nm);
Assert.IsNotNull(booleanWithNoDefaultPropertyElement);

var booleanWithDefaultNullPropertyElement = root.XPathSelectElement("//xsd:element[@name='BooleanWithDefaultNullProperty' and @minOccurs='1' and @maxOccurs='1' and not(@default)]", nm);
Assert.IsNotNull(booleanWithDefaultNullPropertyElement);

var booleanWithDefaultFalsePropertyElement = root.XPathSelectElement("//xsd:element[@name='BooleanWithDefaultFalseProperty' and @minOccurs='0' and @maxOccurs='1' and @default='false']", nm);
Assert.IsNotNull(booleanWithDefaultFalsePropertyElement);

var booleanWithDefaultTruePropertyElement = root.XPathSelectElement("//xsd:element[@name='BooleanWithDefaultTrueProperty' and @minOccurs='0' and @maxOccurs='1' and @default='true']", nm);
Assert.IsNotNull(booleanWithDefaultTruePropertyElement);

var intWithNoDefaultPropertyElement = root.XPathSelectElement("//xsd:element[@name='IntWithNoDefaultProperty' and @minOccurs='1' and @maxOccurs='1' and not(@default)]", nm);
Assert.IsNotNull(intWithNoDefaultPropertyElement);

var intWithDefaultPropertyElement = root.XPathSelectElement("//xsd:element[@name='IntWithDefaultProperty' and @minOccurs='0' and @maxOccurs='1' and @default='42']", nm);
Assert.IsNotNull(intWithDefaultPropertyElement);

var stringWithNoDefaultPropertyElement = root.XPathSelectElement("//xsd:element[@name='StringWithNoDefaultProperty' and @minOccurs='0' and @maxOccurs='1' and not(@default)]", nm);
Assert.IsNotNull(stringWithNoDefaultPropertyElement);

var stringWithDefaultNullPropertyElement = root.XPathSelectElement("//xsd:element[@name='StringWithDefaultNullProperty' and @minOccurs='0' and @maxOccurs='1' and not(@default)]", nm);
Assert.IsNotNull(stringWithDefaultNullPropertyElement);

var stringWithDefaultPropertyElement = root.XPathSelectElement("//xsd:element[@name='StringWithDefaultProperty' and @minOccurs='0' and @maxOccurs='1' and @default='default']", nm);
Assert.IsNotNull(stringWithDefaultPropertyElement);
}

[TestMethod]
public async Task CheckDataContractKnownTypeAttributeServiceWsdl()
{
Expand Down
24 changes: 21 additions & 3 deletions src/SoapCore/Meta/MetaBodyWriter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Reflection;
Expand Down Expand Up @@ -875,7 +876,20 @@ private void AddSchemaTypePropertyOrField(XmlDictionaryWriter writer, MemberInfo
}
else
{
AddSchemaType(writer, toBuild, parentTypeToBuild.ChildElementName ?? member.Name, isArray: createListWithoutProxyType, isListWithoutWrapper: createListWithoutProxyType, isUnqualified: isUnqualified);
string defaultValue = null;
var defaultAttributeValue = member.GetCustomAttribute<DefaultValueAttribute>()?.Value;
if (defaultAttributeValue != null)
{
if (defaultAttributeValue is bool value)
{
defaultValue = value ? "true" : "false";
}
else
{
defaultValue = defaultAttributeValue.ToString();
}
}
AddSchemaType(writer, toBuild, parentTypeToBuild.ChildElementName ?? member.Name, isArray: createListWithoutProxyType, isListWithoutWrapper: createListWithoutProxyType, isUnqualified: isUnqualified, defaultValue: defaultValue);
}
}

Expand All @@ -895,7 +909,7 @@ private void AddSchemaType(XmlDictionaryWriter writer, Type type, string name, b
AddSchemaType(writer, new TypeToBuild(type), name, isArray, @namespace, isAttribute, isUnqualified: isUnqualified);
}

private void AddSchemaType(XmlDictionaryWriter writer, TypeToBuild toBuild, string name, bool isArray = false, string @namespace = null, bool isAttribute = false, bool isListWithoutWrapper = false, bool isUnqualified = false)
private void AddSchemaType(XmlDictionaryWriter writer, TypeToBuild toBuild, string name, bool isArray = false, string @namespace = null, bool isAttribute = false, bool isListWithoutWrapper = false, bool isUnqualified = false, string defaultValue = null)
{
var type = toBuild.Type;

Expand Down Expand Up @@ -982,8 +996,12 @@ private void AddSchemaType(XmlDictionaryWriter writer, TypeToBuild toBuild, stri
}
else
{
writer.WriteAttributeString("minOccurs", type.IsValueType ? "1" : "0");
writer.WriteAttributeString("minOccurs", type.IsValueType && defaultValue == null ? "1" : "0");
writer.WriteAttributeString("maxOccurs", "1");
if (defaultValue != null)
{
writer.WriteAttributeString("default", defaultValue);
}
}

if (string.IsNullOrEmpty(name))
Expand Down