Skip to content

Commit

Permalink
Do not emit DefaultValueAttribute for xml:lang and xml:space attribut…
Browse files Browse the repository at this point in the history
…es (see #245)
  • Loading branch information
Michael Ganss committed Jan 12, 2021
1 parent 7412170 commit b918fa9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
10 changes: 10 additions & 0 deletions XmlSchemaClassGenerator/CodeUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -399,5 +399,15 @@ public static CodeTypeReference CreateTypeReference(Type t, GeneratorConfigurati
else
return new CodeTypeReference(t, conf.CodeTypeReferenceOptions);
}

/// <summary>
/// See https://github.com/mganss/XmlSchemaClassGenerator/issues/245
/// and https://docs.microsoft.com/en-us/dotnet/api/system.xml.serialization.xmlattributeattribute#remarks
/// </summary>
public static bool IsXmlLangOrSpace(XmlQualifiedName name)
{
return name != null && name.Namespace == "http://www.w3.org/XML/1998/namespace"
&& (name.Name == "lang" || name.Name == "space");
}
}
}
6 changes: 4 additions & 2 deletions XmlSchemaClassGenerator/TypeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,8 @@ public void AddInterfaceMembersTo(CodeTypeDeclaration typeDeclaration)
{
var defaultValueExpression = propertyType.GetDefaultValueFor(DefaultValue, IsAttribute);

if ((defaultValueExpression is CodePrimitiveExpression) || (defaultValueExpression is CodeFieldReferenceExpression))
if ((defaultValueExpression is CodePrimitiveExpression) || (defaultValueExpression is CodeFieldReferenceExpression)
&& !CodeUtilities.IsXmlLangOrSpace(XmlSchemaName))
{
var defaultValueAttribute = CreateDefaultValueAttribute(typeReference, defaultValueExpression);
member.CustomAttributes.Add(defaultValueAttribute);
Expand Down Expand Up @@ -927,7 +928,8 @@ public void AddMembersTo(CodeTypeDeclaration typeDeclaration, bool withDataBindi

member.Name += GetAccessors(member.Name, backingField.Name, propertyType.GetPropertyValueTypeCode(), false, withDataBinding);

if (IsNullable && ((defaultValueExpression is CodePrimitiveExpression) || (defaultValueExpression is CodeFieldReferenceExpression)))
if (IsNullable && ((defaultValueExpression is CodePrimitiveExpression) || (defaultValueExpression is CodeFieldReferenceExpression))
&& !CodeUtilities.IsXmlLangOrSpace(XmlSchemaName))
{
var defaultValueAttribute = CreateDefaultValueAttribute(typeReference, defaultValueExpression);
member.CustomAttributes.Add(defaultValueAttribute);
Expand Down

0 comments on commit b918fa9

Please sign in to comment.