Skip to content
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -424,3 +424,4 @@ src/siri/**/Generated/
# Local xscgen pre-release package (band-aid until upstream merges).
# Must appear after both the *.nupkg and **/[Pp]ackages/* rules to negate them.
!**/[Pp]ackages/XmlSchemaClassGenerator-beta.*.nupkg
!**/[Pp]ackages/XmlSchemaClassGenerator*.nupkg
Binary file not shown.
Binary file not shown.
Binary file not shown.
3 changes: 2 additions & 1 deletion src/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
<PackageVersion Include="MinVer" Version="6.0.0" />

<!-- Generator dependencies -->
<PackageVersion Include="XmlSchemaClassGenerator-beta" Version="99.0.5-local" />
<PackageVersion Include="XmlSchemaClassGenerator-beta" Version="99.0.7-local" />
<PackageVersion Include="XmlSchemaClassGenerator.Analyzer" Version="99.0.7-local" />
<PackageVersion Include="System.CommandLine" Version="2.0.2" />

<!-- Test dependencies -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ [new NamespaceKey(SharedDefaults.GmlXmlNamespace)] = gmlNamespace,
[new NamespaceKey(SharedDefaults.W3XmlNamespace)] = w3Namespace
};

var generator = CreateBaseGenerator(outputDirectory, namespaceProvider, verbose);
var generator = CreateBaseGenerator(outputDirectory, rootNamespace, namespaceProvider, verbose);
// Set NamingProvider after the object initializer to avoid being overwritten
// by any NamingScheme setter. This renames abstract dummy elements (e.g. 'StopPlace_')
// so that concrete elements get the clean C# class name (e.g. 'StopPlace').
Expand Down Expand Up @@ -91,7 +91,7 @@ [new NamespaceKey(SharedDefaults.GmlXmlNamespace)] = gmlNamespace,
[new NamespaceKey(SharedDefaults.W3XmlNamespace)] = w3Namespace
};

var generator = CreateBaseGenerator(outputDirectory, namespaceProvider, verbose);
var generator = CreateBaseGenerator(outputDirectory, rootNamespace, namespaceProvider, verbose);
// SIRI doesn't have the underscore naming convention, use default PascalCase
generator.NamingProvider = new NamingProvider(NamingScheme.PascalCase);

Expand All @@ -115,6 +115,7 @@ [new NamespaceKey(SharedDefaults.W3XmlNamespace)] = w3Namespace

private static XscGenerator CreateBaseGenerator(
string outputDirectory,
string rootNamespace,
NamespaceProvider namespaceProvider,
bool verbose)
{
Expand Down Expand Up @@ -143,10 +144,12 @@ private static XscGenerator CreateBaseGenerator(
CollectionType = typeof(List<>),
EnumCollection = true,

// Nullable and required
// Nullable, required, and choice groups
EnableNullableDirective = true,
GenerateRequiredModifier = true,
UseShouldSerializePattern = true,
GenerateChoiceGroupAttributes = true,
ChoiceGroupAttributeNamespace = rootNamespace,

// Minimal attribute noise
GenerateSerializableAttribute = false,
Expand Down
6 changes: 6 additions & 0 deletions src/netex/NeTEx.Models.targets
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
<None Include="../Spillgebees.NeTEx.Models/README.md" Pack="true" PackagePath="" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="XmlSchemaClassGenerator.Analyzer" PrivateAssets="none">
<IncludeAssets>analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>

<!-- Trigger generator to run before compilation if NetexVersion is set and no generated files exist -->
<Target Name="GenerateNetexModels"
Condition="'$(NetexVersion)' != ''"
Expand Down
123 changes: 123 additions & 0 deletions src/netex/Spillgebees.NeTEx.Models.Tests/ChoiceGroupAttributeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
using System.Reflection;
using AwesomeAssertions;
using Spillgebees.NeTEx.Models.V1_3_1;
using Spillgebees.NeTEx.Models.V1_3_1.NeTEx;

namespace Spillgebees.NeTEx.Models.Tests;

/// <summary>
/// Tests verifying that the generated NeTEx v1.3.1 models carry correct
/// <see cref="XmlChoiceGroupAttribute"/> annotations on properties that
/// originate from <c>xs:choice</c> groups in the NeTEx XSD schemas.
/// </summary>
public class ChoiceGroupAttributeTests
{
// -- helpers ------------------------------------------------------------------

private static XmlChoiceGroupAttribute? GetChoiceGroupAttribute<T>(string propertyName) =>
typeof(T).GetProperty(propertyName)?
.GetCustomAttribute<XmlChoiceGroupAttribute>();

private static XmlChoiceGroupAttribute GetRequiredChoiceGroupAttribute<T>(string propertyName)
{
var attr = GetChoiceGroupAttribute<T>(propertyName);
attr.Should().NotBeNull($"property {typeof(T).Name}.{propertyName} should have [XmlChoiceGroupAttribute]");
return attr!;
}

// -- DistanceMatrixElementDerivedViewStructure: two parallel choice groups -----

[Test]
public void Should_have_choice_group_on_start_stop_point_ref()
{
var attr = GetRequiredChoiceGroupAttribute<DistanceMatrixElementDerivedViewStructure>(
nameof(DistanceMatrixElementDerivedViewStructure.StartStopPointRef));

attr.ArmId.Should().Be(0);
}

[Test]
public void Should_have_choice_group_on_start_tariff_zone_ref()
{
var attr = GetRequiredChoiceGroupAttribute<DistanceMatrixElementDerivedViewStructure>(
nameof(DistanceMatrixElementDerivedViewStructure.StartTariffZoneRef));

attr.ArmId.Should().Be(1);
}

[Test]
public void Should_have_same_group_id_for_start_choice()
{
var stopRef = GetRequiredChoiceGroupAttribute<DistanceMatrixElementDerivedViewStructure>(
nameof(DistanceMatrixElementDerivedViewStructure.StartStopPointRef));
var zoneRef = GetRequiredChoiceGroupAttribute<DistanceMatrixElementDerivedViewStructure>(
nameof(DistanceMatrixElementDerivedViewStructure.StartTariffZoneRef));

stopRef.GroupId.Should().Be(zoneRef.GroupId);
}

[Test]
public void Should_have_choice_group_on_end_stop_point_ref()
{
var attr = GetRequiredChoiceGroupAttribute<DistanceMatrixElementDerivedViewStructure>(
nameof(DistanceMatrixElementDerivedViewStructure.EndStopPointRef));

attr.ArmId.Should().Be(0);
}

[Test]
public void Should_have_choice_group_on_end_tariff_zone_ref()
{
var attr = GetRequiredChoiceGroupAttribute<DistanceMatrixElementDerivedViewStructure>(
nameof(DistanceMatrixElementDerivedViewStructure.EndTariffZoneRef));

attr.ArmId.Should().Be(1);
}

[Test]
public void Should_have_same_group_id_for_end_choice()
{
var stopRef = GetRequiredChoiceGroupAttribute<DistanceMatrixElementDerivedViewStructure>(
nameof(DistanceMatrixElementDerivedViewStructure.EndStopPointRef));
var zoneRef = GetRequiredChoiceGroupAttribute<DistanceMatrixElementDerivedViewStructure>(
nameof(DistanceMatrixElementDerivedViewStructure.EndTariffZoneRef));

stopRef.GroupId.Should().Be(zoneRef.GroupId);
}

[Test]
public void Should_have_different_group_ids_for_start_and_end_choices()
{
var startAttr = GetRequiredChoiceGroupAttribute<DistanceMatrixElementDerivedViewStructure>(
nameof(DistanceMatrixElementDerivedViewStructure.StartStopPointRef));
var endAttr = GetRequiredChoiceGroupAttribute<DistanceMatrixElementDerivedViewStructure>(
nameof(DistanceMatrixElementDerivedViewStructure.EndStopPointRef));

startAttr.GroupId.Should().NotBe(endAttr.GroupId);
}

[Test]
public void Should_not_have_choice_group_on_non_choice_property()
{
// StartName is not part of any choice group
var attr = GetChoiceGroupAttribute<DistanceMatrixElementDerivedViewStructure>("StartName");

attr.Should().BeNull();
}

// -- FareStructureElementVersionStructure: multiple choice groups in NeTEx -----

[Test]
public void Should_have_multiple_distinct_choice_groups_on_fare_structure_element()
{
var properties = typeof(FareStructureElementVersionStructure).GetProperties();
var choiceGroupIds = properties
.Select(p => p.GetCustomAttribute<XmlChoiceGroupAttribute>())
.Where(a => a is not null)
.Select(a => a!.GroupId)
.Distinct()
.ToList();

choiceGroupIds.Count.Should().BeGreaterThanOrEqualTo(2);
}
}
6 changes: 6 additions & 0 deletions src/siri/SIRI.Models.targets
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
<None Include="../Spillgebees.SIRI.Models/README.md" Pack="true" PackagePath="" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="XmlSchemaClassGenerator.Analyzer" PrivateAssets="none">
<IncludeAssets>analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>

<!-- Trigger generator to run before compilation if SiriVersion is set and no generated files exist -->
<Target Name="GenerateSiriModels"
Condition="'$(SiriVersion)' != ''"
Expand Down
Loading