Skip to content

Commit

Permalink
Fix order for substitution groups
Browse files Browse the repository at this point in the history
Don't initialize collections with default
Some VS suggestions
Target net5.0
  • Loading branch information
Michael Ganss committed Dec 9, 2020
1 parent 373bee1 commit 57bca66
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 39 deletions.
26 changes: 8 additions & 18 deletions XmlSchemaClassGenerator.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,29 +89,19 @@ A file name may be given by appending a pipe sign (|) followed by a file name (l
{ "u|enableUpaCheck", "should XmlSchemaSet check for Unique Particle Attribution (UPA) (default is enabled)", v => enableUpaCheck = v != null },
{ "ct|collectionType=", "collection type to use (default is " + typeof(Collection<>).FullName + ")", v => collectionType = v == null ? typeof(Collection<>) : Type.GetType(v, true) },
{ "cit|collectionImplementationType=", "the default collection type implementation to use (default is null)", v => collectionImplementationType = v == null ? null : Type.GetType(v, true) },
{ "csm|collectionSettersMode=", @"generate a private, public or public setters
{ "csm|collectionSettersMode=", @"generate a private, public or public setters
without backing field initialization for collections
(default is Private; can be: {Private, Public, PublicWithoutConstructorInitialization})",
v =>
{
switch (v)
collectionSettersMode = v switch
{
case "pr":
case "Private":
collectionSettersMode = CollectionSettersMode.Private;
break;
case "pu":
case "Public":
collectionSettersMode = CollectionSettersMode.Public;
break;
case "puwci":
case "PublicWithoutConstructorInitialization":
collectionSettersMode = CollectionSettersMode.PublicWithoutConstructorInitialization;
break;
default:
collectionSettersMode = CollectionSettersMode.Private;
break;
}
"pr" or "Private" => CollectionSettersMode.Private,
"pu" or "Public" => CollectionSettersMode.Public,
"puwci" or "PublicWithoutConstructorInitialization" =>
CollectionSettersMode.PublicWithoutConstructorInitialization,
_ => CollectionSettersMode.Private
};
}},
{ "ctro|codeTypeReferenceOptions=", "the default CodeTypeReferenceOptions Flags to use (default is unset; can be: {GlobalReference, GenericTypeParameter})", v => codeTypeReferenceOptions = v == null ? default : (CodeTypeReferenceOptions)Enum.Parse(typeof(CodeTypeReferenceOptions), v, false) },
{ "tvpn|textValuePropertyName=", "the name of the property that holds the text value of an element (default is Value)", v => textValuePropertyName = v },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\XmlSchemaClassGenerator\XmlSchemaClassGenerator.csproj" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netcoreapp2.2</TargetFrameworks>
<TargetFrameworks>net5.0</TargetFrameworks>
<NetStandardImplicitPackageVersion>2.0.0</NetStandardImplicitPackageVersion>
<AssemblyName>XmlSchemaClassGenerator.Tests</AssemblyName>
<PackageId>XmlSchemaClassGenerator.Tests</PackageId>
Expand Down Expand Up @@ -873,6 +873,9 @@
<None Update="xsd\nullable\optional.xsd">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="xsd\simple\default.xsd">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="xsd\simple\restriction.xsd">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
14 changes: 14 additions & 0 deletions XmlSchemaClassGenerator.Tests/xsd/simple/default.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="default"
targetNamespace="http://tempuri.org/default.xsd"
elementFormDefault="qualified"
xmlns="http://tempuri.org/default.xsd"
xmlns:mstns="http://tempuri.org/default.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
<xs:complexType name="TestType">
<xs:sequence>
<xs:element name="Holiday" type="xs:string" default="AnyDay" minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:schema>
45 changes: 29 additions & 16 deletions XmlSchemaClassGenerator/ModelBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,29 +77,42 @@ public ModelBuilder(GeneratorConfiguration configuration, XmlSchemaSet set)

private void CreateSubstitutes()
{
var properties = Types.Values.OfType<ClassModel>().SelectMany(c => c.Properties).Where(p => p.XmlSchemaName != null).ToList();
var classesProps = Types.Values.OfType<ClassModel>().Select(c => c.Properties.Where(p => p.XmlSchemaName != null).ToList()).ToList();

foreach (var prop in properties)
foreach (var classProps in classesProps)
{
var substitutes = GetSubstitutedElements(prop.XmlSchemaName).ToList();
var order = 0;

if (_configuration.SeparateSubstitutes)
foreach (var prop in classProps)
{
var elems = GetElements(prop.XmlParticle, prop.XmlParent);
if (_configuration.EmitOrder)
{
prop.Order = order;
order++;
}

var substitutes = GetSubstitutedElements(prop.XmlSchemaName).ToList();

foreach (var substitute in substitutes)
if (_configuration.SeparateSubstitutes)
{
var cls = (ClassModel)prop.OwningType;
var schema = substitute.Element.GetSchema();
var source = CodeUtilities.CreateUri(schema.SourceUri);
var props = CreatePropertiesForElements(source, cls, prop.XmlParticle, elems, substitute);
var elems = GetElements(prop.XmlParticle, prop.XmlParent);

foreach (var substitute in substitutes)
{
var cls = (ClassModel)prop.OwningType;
var schema = substitute.Element.GetSchema();
var source = CodeUtilities.CreateUri(schema.SourceUri);
var props = CreatePropertiesForElements(source, cls, prop.XmlParticle, elems, substitute, order);

cls.Properties.AddRange(props);

cls.Properties.AddRange(props);
order += props.Count();
}
}
else
{
prop.Substitutes = substitutes;
}
}
else
{
prop.Substitutes = substitutes;
}
}
}
Expand Down Expand Up @@ -404,7 +417,7 @@ private TypeModel CreateTypeModel(Uri source, XmlSchemaComplexType complexType,
IsAbstract = complexType.IsAbstract,
IsAnonymous = string.IsNullOrEmpty(complexType.QualifiedName.Name),
IsMixed = complexType.IsMixed,
IsSubstitution = complexType.Parent is XmlSchemaElement && !((XmlSchemaElement)complexType.Parent).SubstitutionGroup.IsEmpty
IsSubstitution = complexType.Parent is XmlSchemaElement parent && !parent.SubstitutionGroup.IsEmpty
};

classModel.Documentation.AddRange(docs);
Expand Down
4 changes: 2 additions & 2 deletions XmlSchemaClassGenerator/TypeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ protected void GenerateTypeAttribute(CodeTypeDeclaration typeDeclaration)
var typeAttribute = new CodeAttributeDeclaration(new CodeTypeReference(typeof(XmlTypeAttribute), Configuration.CodeTypeReferenceOptions),
new CodeAttributeArgument(new CodePrimitiveExpression(XmlSchemaName.Name)),
new CodeAttributeArgument("Namespace", new CodePrimitiveExpression(XmlSchemaName.Namespace)));
if (IsAnonymous && (!(this is ClassModel classModel) || classModel.BaseClass == null))
if (IsAnonymous && (this is not ClassModel classModel || classModel.BaseClass == null))
{
// don't generate AnonymousType if it's derived class, otherwise XmlSerializer will
// complain with "InvalidOperationException: Cannot include anonymous type '...'"
Expand Down Expand Up @@ -865,7 +865,7 @@ public void AddMembersTo(CodeTypeDeclaration typeDeclaration, bool withDataBindi
typeDeclaration.Members.Add(backingField);
}

if (DefaultValue == null)
if (DefaultValue == null || ((IsCollection || isArray || (IsList && IsAttribute)) && IsNullable))
{
var propertyName = Name;

Expand Down
4 changes: 2 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ test_script:
- ps: |
if (-not $env:APPVEYOR_PULL_REQUEST_NUMBER) {
dotnet tool install --global dotnet-sonarscanner
dotnet sonarscanner begin /k:"mganss_XmlSchemaClassGenerator" /v:$env:APPVEYOR_BUILD_VERSION /o:"mganss-github" /d:sonar.host.url="https://sonarcloud.io" /d:sonar.login="$env:sonar_token" /d:sonar.cs.opencover.reportsPaths="$($env:APPVEYOR_BUILD_FOLDER)\coverage.netcoreapp2.2.xml" /d:sonar.coverage.exclusions="**/Program.cs"
dotnet sonarscanner begin /k:"mganss_XmlSchemaClassGenerator" /v:$env:APPVEYOR_BUILD_VERSION /o:"mganss-github" /d:sonar.host.url="https://sonarcloud.io" /d:sonar.login="$env:sonar_token" /d:sonar.cs.opencover.reportsPaths="$($env:APPVEYOR_BUILD_FOLDER)\coverage.net5.0.xml" /d:sonar.coverage.exclusions="**/Program.cs"
dotnet build
}
- dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=opencover /p:CoverletOutput="..\coverage.xml" XmlSchemaClassGenerator.Tests\XmlSchemaClassGenerator.Tests.csproj /p:Include="[XmlSchemaClassGenerator]*"
Expand All @@ -31,7 +31,7 @@ test_script:
dotnet sonarscanner end /d:sonar.login="$env:sonar_token"
}
- pip install codecov
- codecov -f "coverage.netcoreapp2.2.xml"
- codecov -f "coverage.net5.0.xml"
artifacts:
- path: 'XmlSchemaClassGenerator\**\*.*nupkg'
- path: 'XmlSchemaClassGenerator.Console\**\*.*nupkg'
Expand Down
1 change: 1 addition & 0 deletions xscgen-proj/xscgen-proj.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\XmlSchemaClassGenerator\XmlSchemaClassGenerator.csproj" />
Expand Down
1 change: 1 addition & 0 deletions xscgen/xscgen.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\XmlSchemaClassGenerator\XmlSchemaClassGenerator.csproj" />
Expand Down

0 comments on commit 57bca66

Please sign in to comment.