Skip to content

Commit 74feff9

Browse files
authored
Merge pull request #284 from gureedo/master
any attribute is not rendered if complex type extends other complex type
2 parents c7f5dcd + bb89889 commit 74feff9

File tree

9 files changed

+173
-19
lines changed

9 files changed

+173
-19
lines changed

XmlSchemaClassGenerator.Tests/Compiler.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ public static Assembly GenerateFiles(string name, IEnumerable<string> files, Gen
112112
CompactTypeNames = generatorPrototype.CompactTypeNames,
113113
UniqueTypeNamesAcrossNamespaces = generatorPrototype.UniqueTypeNamesAcrossNamespaces,
114114
CreateGeneratedCodeAttributeVersion = generatorPrototype.CreateGeneratedCodeAttributeVersion,
115-
NetCoreSpecificCode = generatorPrototype.NetCoreSpecificCode
115+
NetCoreSpecificCode = generatorPrototype.NetCoreSpecificCode,
116+
NamingScheme = generatorPrototype.NamingScheme
116117
};
117118

118119
gen.CommentLanguages.Clear();

XmlSchemaClassGenerator.Tests/XmlSchemaClassGenerator.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
2525
</PackageReference>
2626
<PackageReference Include="Glob.cs" Version="5.1.766" />
27-
<PackageReference Include="Microsoft.Net.Compilers.netcore" Version="1.3.2" />
27+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.11.0" />
2828
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
2929
<PackageReference Include="System.CodeDom" Version="5.0.0" />
3030
<PackageReference Include="System.Text.Encoding.CodePages" Version="5.0.0" />

XmlSchemaClassGenerator.Tests/XmlTests.cs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -300,12 +300,24 @@ public void TestListWithPublicPropertySettersWithoutConstructorsSpecified()
300300
}
301301
}
302302

303+
public static IEnumerable<object[]> TestSimpleData() {
304+
foreach (var referenceMode in new[]
305+
{ CodeTypeReferenceOptions.GlobalReference, /*CodeTypeReferenceOptions.GenericTypeParameter*/ })
306+
foreach (var namingScheme in new[] { NamingScheme.Direct, NamingScheme.PascalCase })
307+
foreach (var collectionType in new[] { typeof(Collection<>), /*typeof(Array)*/ })
308+
{
309+
yield return new object[] { referenceMode, namingScheme, collectionType };
310+
}
311+
}
303312

304-
[Fact, TestPriority(1)]
313+
314+
[Theory, TestPriority(1)]
315+
[MemberData(nameof(TestSimpleData))]
305316
[UseCulture("en-US")]
306-
public void TestSimple()
317+
public void TestSimple(CodeTypeReferenceOptions referenceMode, NamingScheme namingScheme, Type collectionType)
307318
{
308-
Compiler.Generate("Simple", SimplePattern, new Generator
319+
var name = $"Simple_{referenceMode}_{namingScheme}_{collectionType}";
320+
Compiler.Generate(name, SimplePattern, new Generator
309321
{
310322
GenerateNullables = true,
311323
IntegerDataType = typeof(int),
@@ -316,10 +328,13 @@ public void TestSimple()
316328
GenerateInterfaces = true,
317329
NamespacePrefix = "Simple",
318330
GenerateDescriptionAttribute = true,
319-
CodeTypeReferenceOptions = CodeTypeReferenceOptions.GlobalReference,
320-
NetCoreSpecificCode = true
331+
CodeTypeReferenceOptions = referenceMode,
332+
NetCoreSpecificCode = true,
333+
NamingScheme = namingScheme,
334+
CollectionType = collectionType,
335+
CollectionSettersMode = CollectionSettersMode.Public
321336
});
322-
TestSamples("Simple", SimplePattern);
337+
TestSamples(name, SimplePattern);
323338
}
324339

325340
[Fact, TestPriority(1)]
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<xs:schema id="default"
4+
targetNamespace="http://tempuri.org/default.xsd"
5+
elementFormDefault="qualified"
6+
xmlns="http://tempuri.org/default.xsd"
7+
xmlns:mstns="http://tempuri.org/default.xsd"
8+
xmlns:xs="http://www.w3.org/2001/XMLSchema"
9+
>
10+
<xs:complexType name="AnyTest_ExtendedString">
11+
<xs:simpleContent>
12+
<xs:extension base="xs:string">
13+
<xs:anyAttribute namespace="##any" processContents="lax"/>
14+
</xs:extension>
15+
</xs:simpleContent>
16+
</xs:complexType>
17+
18+
<xs:complexType name="AnyTest_Type">
19+
<xs:sequence/>
20+
<xs:anyAttribute namespace="##any" processContents="lax"/>
21+
</xs:complexType>
22+
23+
<xs:complexType name="AnyTest_ExtendedTypeWithAnyAttr">
24+
<xs:complexContent>
25+
<xs:extension base="AnyTest_Type">
26+
<xs:anyAttribute namespace="##any" processContents="lax"/>
27+
</xs:extension>
28+
</xs:complexContent>
29+
</xs:complexType>
30+
31+
<xs:complexType name="AnyTest_ExtendedTypeWithoutAnyAttr">
32+
<xs:complexContent>
33+
<xs:extension base="AnyTest_Type" />
34+
</xs:complexContent>
35+
</xs:complexType>
36+
37+
</xs:schema>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<xs:schema id="default"
4+
targetNamespace="http://tempuri.org/default.xsd"
5+
elementFormDefault="qualified"
6+
xmlns="http://tempuri.org/default.xsd"
7+
xmlns:mstns="http://tempuri.org/default.xsd"
8+
xmlns:xs="http://www.w3.org/2001/XMLSchema"
9+
>
10+
<xs:complexType name="AmbiguityTest_Type">
11+
<xs:sequence>
12+
<xs:choice minOccurs="0" maxOccurs="unbounded">
13+
<xs:element name="Property">
14+
<xs:complexType>
15+
<xs:sequence>
16+
<xs:element name="Value" type="AmbiguityTest_NestedType" minOccurs="0" maxOccurs="unbounded" />
17+
</xs:sequence>
18+
<xs:attribute name="value" type="xs:string" use="optional" default="" />
19+
</xs:complexType>
20+
</xs:element>
21+
</xs:choice>
22+
</xs:sequence>
23+
</xs:complexType>
24+
25+
<xs:complexType name="AmbiguityTest_NestedType">
26+
<xs:attribute name="NestedItem" type="xs:string" use="required" />
27+
</xs:complexType>
28+
29+
</xs:schema>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<xs:schema id="default"
4+
targetNamespace="http://tempuri.org/default.xsd"
5+
elementFormDefault="qualified"
6+
xmlns="http://tempuri.org/default.xsd"
7+
xmlns:mstns="http://tempuri.org/default.xsd"
8+
xmlns:xs="http://www.w3.org/2001/XMLSchema"
9+
>
10+
11+
<xs:complexType name="NestedTest_NestedType">
12+
<xs:sequence>
13+
<xs:element name="TestField" type="xs:string" />
14+
</xs:sequence>
15+
</xs:complexType>
16+
17+
<xs:complexType name="NestedTest_Type">
18+
<xs:sequence>
19+
<xs:element name="TestField" type="NestedTest_NestedType" minOccurs="0" maxOccurs="1" />
20+
<xs:element name="TestCollectionField" type="NestedTest_NestedType" minOccurs="0" maxOccurs="unbounded" />
21+
</xs:sequence>
22+
</xs:complexType>
23+
24+
</xs:schema>

XmlSchemaClassGenerator/CodeUtilities.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -269,11 +269,6 @@ public static string GetUniqueFieldName(this TypeModel typeModel, PropertyModel
269269
var i = 0;
270270
foreach (var prop in classModel.Properties)
271271
{
272-
if (!classModel.Configuration.EnableDataBinding && !(prop.Type is SimpleModel))
273-
{
274-
continue;
275-
}
276-
277272
if (propertyModel == prop)
278273
{
279274
i += 1;
@@ -399,7 +394,20 @@ public static CodeTypeReference CreateTypeReference(Type t, GeneratorConfigurati
399394
return typeRef;
400395
}
401396
else
402-
return new CodeTypeReference(t, conf.CodeTypeReferenceOptions);
397+
{
398+
var typeRef = new CodeTypeReference(t, conf.CodeTypeReferenceOptions);
399+
400+
foreach (var typeArg in typeRef.TypeArguments)
401+
{
402+
if (typeArg is CodeTypeReference typeArgRef)
403+
{
404+
typeArgRef.Options = conf.CodeTypeReferenceOptions;
405+
}
406+
}
407+
408+
return typeRef;
409+
}
410+
403411
}
404412

405413
public static CodeTypeReference CreateTypeReference(string namespaceName, string typeName, GeneratorConfiguration conf)

XmlSchemaClassGenerator/ModelBuilder.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Xml;
66
using System.Xml.Linq;
77
using System.Xml.Schema;
8+
using System.Xml.Serialization;
89

910
namespace XmlSchemaClassGenerator
1011
{
@@ -577,7 +578,29 @@ private TypeModel CreateTypeModel(Uri source, XmlSchemaComplexType complexType,
577578
}
578579
}
579580

581+
XmlSchemaAnyAttribute anyAttribute = null;
580582
if (complexType.AnyAttribute != null)
583+
anyAttribute = complexType.AnyAttribute;
584+
else if (complexType.AttributeWildcard != null)
585+
{
586+
var hasAnyAttribute = true;
587+
for (var baseType = complexType.BaseXmlSchemaType; baseType != null; baseType = baseType.BaseXmlSchemaType)
588+
{
589+
if (baseType is not XmlSchemaComplexType baseComplexType)
590+
continue;
591+
592+
if (baseComplexType.AttributeWildcard != null)
593+
{
594+
hasAnyAttribute = false;
595+
break;
596+
}
597+
}
598+
599+
if (hasAnyAttribute)
600+
anyAttribute = complexType.AttributeWildcard;
601+
}
602+
603+
if (anyAttribute != null)
581604
{
582605
var property = new PropertyModel(_configuration)
583606
{
@@ -589,7 +612,7 @@ private TypeModel CreateTypeModel(Uri source, XmlSchemaComplexType complexType,
589612
IsAny = true
590613
};
591614

592-
var attributeDocs = GetDocumentation(complexType.AnyAttribute);
615+
var attributeDocs = GetDocumentation(anyAttribute);
593616
property.Documentation.AddRange(attributeDocs);
594617

595618
classModel.Properties.Add(property);

XmlSchemaClassGenerator/TypeModel.cs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.CodeDom;
33
using System.CodeDom.Compiler;
4-
using System.Collections;
54
using System.Collections.Generic;
65
using System.ComponentModel;
76
using System.Diagnostics;
@@ -172,17 +171,32 @@ protected void GenerateSerializableAttribute(CodeTypeDeclaration typeDeclaration
172171
public virtual CodeTypeReference GetReferenceFor(NamespaceModel referencingNamespace, bool collection = false, bool forInit = false, bool attribute = false)
173172
{
174173
string name;
174+
var referencingOptions = Configuration.CodeTypeReferenceOptions;
175175
if (referencingNamespace == Namespace)
176+
{
176177
name = Name;
178+
referencingOptions = CodeTypeReferenceOptions.GenericTypeParameter;
179+
}
180+
else if ((referencingNamespace ?? Namespace).IsAmbiguous)
181+
{
182+
name = string.Format("global::{0}.{1}", Namespace.Name, Name);
183+
referencingOptions = CodeTypeReferenceOptions.GenericTypeParameter;
184+
}
177185
else
178-
name = string.Format("{2}{0}.{1}", Namespace.Name, Name, ((referencingNamespace ?? Namespace).IsAmbiguous ? "global::" : string.Empty));
186+
{
187+
name = string.Format("{0}.{1}", Namespace.Name, Name);
188+
}
179189

180190
if (collection)
181191
{
182192
name = forInit ? SimpleModel.GetCollectionImplementationName(name, Configuration) : SimpleModel.GetCollectionDefinitionName(name, Configuration);
193+
if (Configuration.CollectionType == typeof(System.Array))
194+
referencingOptions = CodeTypeReferenceOptions.GenericTypeParameter;
195+
else
196+
referencingOptions = Configuration.CodeTypeReferenceOptions;
183197
}
184198

185-
return new CodeTypeReference(name, Configuration.CodeTypeReferenceOptions);
199+
return new CodeTypeReference(name, referencingOptions);
186200
}
187201

188202
public virtual CodeExpression GetDefaultValueFor(string defaultString, bool attribute)
@@ -1411,7 +1425,10 @@ private static string GetFullTypeName(string typeName, CodeTypeReference typeRef
14111425
typeRef.ArrayElementType = new CodeTypeReference(typeName);
14121426
typeRef.ArrayRank = 1;
14131427
}
1414-
var typeOfExpr = new CodeTypeOfExpression(typeRef);
1428+
var typeOfExpr = new CodeTypeOfExpression(typeRef)
1429+
{
1430+
Type = { Options = CodeTypeReferenceOptions.GenericTypeParameter }
1431+
};
14151432
var writer = new System.IO.StringWriter();
14161433
CSharpProvider.GenerateCodeFromExpression(typeOfExpr, writer, new CodeGeneratorOptions());
14171434
var fullTypeName = writer.ToString();

0 commit comments

Comments
 (0)