Skip to content

Commit 151de39

Browse files
committed
Fixed natural-id/id/version output ordering bug
Reorganised the sorting structure a little bit to make it clearer what's happening. We could probably code-gen this from the schema if we really wanted.
1 parent a71f81f commit 151de39

File tree

7 files changed

+87
-53
lines changed

7 files changed

+87
-53
lines changed

src/FluentNHibernate.Testing/DomainModel/Mapping/ClassMapXmlCreationTester.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,23 @@ public void AssociationsToProxiedTypeUsesSpecifiedType()
497497
.Element("class/bag[@name='Siblings']/many-to-many").HasAttribute("class", typeof(ProxiedObject).AssemblyQualifiedName)
498498
.Element("class/map[@name='MapOfChildren']/one-to-many").HasAttribute("class", typeof(ProxiedObject).AssemblyQualifiedName);
499499
}
500+
501+
[Test]
502+
public void ShouldOutputNaturalIdBeforeVersion()
503+
{
504+
new MappingTester<MappedObject>()
505+
.ForMapping(map =>
506+
{
507+
map.Id(x => x.Id);
508+
map.Version(x => x.Version);
509+
map.NaturalId()
510+
.Property(x => x.Name)
511+
.Property(x => x.NickName);
512+
})
513+
.Element("class/id").ShouldBeInParentAtPosition(0)
514+
.Element("class/natural-id").ShouldBeInParentAtPosition(1)
515+
.Element("class/version").ShouldBeInParentAtPosition(2);
516+
}
500517
}
501518

502519
public class SecondMappedObject

src/FluentNHibernate.Testing/MappingModel/SortingTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public void ShouldSortNodes()
1919
var sorter = new XmlClasslikeNodeSorter();
2020

2121
var xml = @"<class><property /><joined-subclass /><many-to-one /><union-subclass /><cache /><key /><one-to-one /></class>";
22-
var expected = @"<class><cache /><key /><one-to-one /><property /><many-to-one /><joined-subclass /><union-subclass /></class>";
22+
var expected = @"<class><cache /><key /><property /><many-to-one /><one-to-one /><joined-subclass /><union-subclass /></class>";
2323

2424
var doc = new XmlDocument();
2525
doc.LoadXml(xml);

src/FluentNHibernate/MappingModel/Output/Sorting/BaseXmlNodeSorter.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ namespace FluentNHibernate.MappingModel.Output.Sorting
66
{
77
public abstract class BaseXmlNodeSorter
88
{
9-
protected const int First = 0;
10-
protected const int Anywhere = 1;
11-
protected const int Last = 2;
9+
protected const int Top = 0;
10+
protected const int Middle = 1;
11+
protected const int Bottom = 2;
1212

1313
public XmlNode Sort(XmlNode node)
1414
{
@@ -33,9 +33,9 @@ public XmlNode Sort(XmlNode node)
3333
var ySort = sorting[y.Name];
3434

3535
//General Position
36-
if (xSort.Position != ySort.Position) return xSort.Position.CompareTo(ySort.Position);
36+
if (xSort.DocumentSection != ySort.DocumentSection) return xSort.DocumentSection.CompareTo(ySort.DocumentSection);
3737
//Sub-Position if positions are the same
38-
if (xSort.Level != ySort.Level) return xSort.Level.CompareTo(ySort.Level);
38+
if (xSort.RankWithinSection != ySort.RankWithinSection) return xSort.RankWithinSection.CompareTo(ySort.RankWithinSection);
3939

4040
//Relative Index based on the order the part was added
4141
return Array.IndexOf(originalSortOrder, x).CompareTo(Array.IndexOf(originalSortOrder, y));

src/FluentNHibernate/MappingModel/Output/Sorting/SortValue.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ namespace FluentNHibernate.MappingModel.Output.Sorting
22
{
33
public class SortValue
44
{
5-
public int Position { get; set; }
6-
public int Level { get; set; }
5+
public int DocumentSection { get; set; }
6+
public int RankWithinSection { get; set; }
77
}
88
}

src/FluentNHibernate/MappingModel/Output/Sorting/XmlClasslikeNodeSorter.cs

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.Xml;
34

@@ -8,35 +9,51 @@ public class XmlClasslikeNodeSorter : BaseXmlNodeSorter
89
protected override IDictionary<string, SortValue> GetSorting()
910
{
1011
return new Dictionary<string, SortValue>
11-
{
12-
{ "cache", new SortValue { Position = First, Level = 1 } },
13-
{ "tuplizer", new SortValue { Position = First, Level = 1 } },
14-
{ "key", new SortValue { Position = First, Level = 1 } },
15-
{ "id", new SortValue { Position = First, Level = 2 } },
16-
{ "composite-id", new SortValue { Position = First, Level = 2 } },
17-
{ "discriminator", new SortValue { Position = First, Level = 3 } },
18-
{ "version", new SortValue { Position = First, Level = 4 } },
19-
{ "component", new SortValue { Position = Anywhere, Level = 1 } },
20-
{ "dynamic-component", new SortValue { Position = Anywhere, Level = 1 } },
21-
{ "natural-id", new SortValue { Position = Anywhere, Level = 1 } },
22-
{ "one-to-one", new SortValue { Position = Anywhere, Level = 2 } },
23-
{ "parent", new SortValue { Position = First, Level = 3 } },
24-
{ "property", new SortValue { Position = Anywhere, Level = 3 } },
25-
{ "many-to-one", new SortValue { Position = Anywhere, Level = 4 } },
26-
{ "array", new SortValue { Position = Anywhere, Level = 4 } },
27-
{ "bag", new SortValue { Position = Anywhere, Level = 4 } },
28-
{ "set", new SortValue { Position = Anywhere, Level = 4 } },
29-
{ "map", new SortValue { Position = Anywhere, Level = 4 } },
30-
{ "list", new SortValue { Position = Anywhere, Level = 4 } },
31-
{ "joined-subclass", new SortValue { Position = Anywhere, Level = 5 } },
32-
{ "union-subclass", new SortValue { Position = Anywhere, Level = 5 } },
33-
{ "subclass", new SortValue { Position = Last, Level = 3 } },
34-
{ "join", new SortValue { Position = Last, Level = 3 } },
35-
{ "any", new SortValue { Position = Anywhere, Level = 2 } },
36-
{ "filter", new SortValue { Position = Last, Level = 5 } },
37-
{ "sql-insert", new SortValue { Position = Last, Level = 5 } },
38-
{ "sql-update", new SortValue { Position = Last, Level = 5 } },
39-
{ "sql-delete", new SortValue { Position = Last, Level = 5 } },
12+
{
13+
// top section
14+
{ "meta", new SortValue { DocumentSection = Top, RankWithinSection = 1 } },
15+
{ "subselect", new SortValue { DocumentSection = Top, RankWithinSection = 2 } },
16+
{ "cache", new SortValue { DocumentSection = Top, RankWithinSection = 3 } },
17+
{ "synchronize", new SortValue { DocumentSection = Top, RankWithinSection = 4 } },
18+
{ "comment", new SortValue { DocumentSection = Top, RankWithinSection = 5 } },
19+
{ "tuplizer", new SortValue { DocumentSection = Top, RankWithinSection = 6 } },
20+
{ "key", new SortValue { DocumentSection = Top, RankWithinSection = 7 } },
21+
{ "parent", new SortValue { DocumentSection = Top, RankWithinSection = 7 } },
22+
{ "id", new SortValue { DocumentSection = Top, RankWithinSection = 7 } },
23+
{ "composite-id", new SortValue { DocumentSection = Top, RankWithinSection = 7 } },
24+
{ "discriminator", new SortValue { DocumentSection = Top, RankWithinSection = 8 } },
25+
{ "natural-id", new SortValue { DocumentSection = Top, RankWithinSection = 9 } },
26+
{ "version", new SortValue { DocumentSection = Top, RankWithinSection = 10 } },
27+
{ "timestamp", new SortValue { DocumentSection = Top, RankWithinSection = 10 } },
28+
29+
// middle section
30+
{ "property", new SortValue { DocumentSection = Middle, RankWithinSection = 1 } },
31+
{ "many-to-one", new SortValue { DocumentSection = Middle, RankWithinSection = 1 } },
32+
{ "one-to-one", new SortValue { DocumentSection = Middle, RankWithinSection = 1 } },
33+
{ "component", new SortValue { DocumentSection = Middle, RankWithinSection = 1 } },
34+
{ "dynamic-component", new SortValue { DocumentSection = Middle, RankWithinSection = 1 } },
35+
{ "properties", new SortValue { DocumentSection = Middle, RankWithinSection = 1 } },
36+
{ "any", new SortValue { DocumentSection = Middle, RankWithinSection = 1 } },
37+
{ "map", new SortValue { DocumentSection = Middle, RankWithinSection = 1 } },
38+
{ "set", new SortValue { DocumentSection = Middle, RankWithinSection = 1 } },
39+
{ "list", new SortValue { DocumentSection = Middle, RankWithinSection = 1 } },
40+
{ "bag", new SortValue { DocumentSection = Middle, RankWithinSection = 1 } },
41+
{ "idbag", new SortValue { DocumentSection = Middle, RankWithinSection = 1 } },
42+
{ "array", new SortValue { DocumentSection = Middle, RankWithinSection = 1 } },
43+
{ "primitive-array", new SortValue { DocumentSection = Middle, RankWithinSection = 1 } },
44+
45+
// bottom section
46+
{ "join", new SortValue { DocumentSection = Bottom, RankWithinSection = 1 } },
47+
{ "subclass", new SortValue { DocumentSection = Bottom, RankWithinSection = 2 } },
48+
{ "joined-subclass", new SortValue { DocumentSection = Bottom, RankWithinSection = 3 } },
49+
{ "union-subclass", new SortValue { DocumentSection = Bottom, RankWithinSection = 4 } },
50+
{ "loader", new SortValue { DocumentSection = Bottom, RankWithinSection = 5 } },
51+
{ "sql-insert", new SortValue { DocumentSection = Bottom, RankWithinSection = 6 } },
52+
{ "sql-update", new SortValue { DocumentSection = Bottom, RankWithinSection = 7 } },
53+
{ "sql-delete", new SortValue { DocumentSection = Bottom, RankWithinSection = 8 } },
54+
{ "filter", new SortValue { DocumentSection = Bottom, RankWithinSection = 9 } },
55+
{ "query", new SortValue { DocumentSection = Bottom, RankWithinSection = 10 } },
56+
{ "sql-query", new SortValue { DocumentSection = Bottom, RankWithinSection = 11 } },
4057
};
4158
}
4259

src/FluentNHibernate/MappingModel/Output/Sorting/XmlCollectionNodeSorter.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@ protected override IDictionary<string, SortValue> GetSorting()
99
{
1010
return new Dictionary<string, SortValue>
1111
{
12-
{ "meta", new SortValue { Position = First, Level = 1 } },
13-
{ "jcs-cache", new SortValue { Position = First, Level = 1 } },
14-
{ "cache", new SortValue { Position = First, Level = 1 } },
15-
{ "key", new SortValue { Position = First, Level = 3 } },
16-
{ "index", new SortValue { Position = First, Level = 4 } },
17-
{ "list-index", new SortValue { Position = First, Level = 4 } },
18-
{ "index-many-to-many", new SortValue { Position = First, Level = 4} },
19-
{ "element", new SortValue { Position = Anywhere, Level = 1 } },
20-
{ "one-to-many", new SortValue { Position = Anywhere, Level = 1 } },
21-
{ "many-to-many", new SortValue { Position = Anywhere, Level = 1 } },
22-
{ "composite-element", new SortValue { Position = Anywhere, Level = 1 } },
23-
{ "many-to-any", new SortValue { Position = Anywhere, Level = 1 } },
24-
{ "filter", new SortValue { Position = Last, Level = 1 } },
12+
{ "meta", new SortValue { DocumentSection = Top, RankWithinSection = 1 } },
13+
{ "jcs-cache", new SortValue { DocumentSection = Top, RankWithinSection = 1 } },
14+
{ "cache", new SortValue { DocumentSection = Top, RankWithinSection = 1 } },
15+
{ "key", new SortValue { DocumentSection = Top, RankWithinSection = 3 } },
16+
{ "index", new SortValue { DocumentSection = Top, RankWithinSection = 4 } },
17+
{ "list-index", new SortValue { DocumentSection = Top, RankWithinSection = 4 } },
18+
{ "index-many-to-many", new SortValue { DocumentSection = Top, RankWithinSection = 4} },
19+
{ "element", new SortValue { DocumentSection = Middle, RankWithinSection = 1 } },
20+
{ "one-to-many", new SortValue { DocumentSection = Middle, RankWithinSection = 1 } },
21+
{ "many-to-many", new SortValue { DocumentSection = Middle, RankWithinSection = 1 } },
22+
{ "composite-element", new SortValue { DocumentSection = Middle, RankWithinSection = 1 } },
23+
{ "many-to-any", new SortValue { DocumentSection = Middle, RankWithinSection = 1 } },
24+
{ "filter", new SortValue { DocumentSection = Bottom, RankWithinSection = 1 } },
2525
};
2626
}
2727

src/FluentNHibernate/MappingModel/Output/Sorting/XmlIdNodeSorter.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ protected override IDictionary<string, SortValue> GetSorting()
99
{
1010
return new Dictionary<string, SortValue>
1111
{
12-
{ "meta", new SortValue { Position = First, Level = 1 } },
13-
{ "column", new SortValue { Position = Anywhere, Level = 1 } },
14-
{ "generator", new SortValue { Position = Last, Level = 1 } },
12+
{ "meta", new SortValue { DocumentSection = Top, RankWithinSection = 1 } },
13+
{ "column", new SortValue { DocumentSection = Middle, RankWithinSection = 1 } },
14+
{ "generator", new SortValue { DocumentSection = Bottom, RankWithinSection = 1 } },
1515
};
1616
}
1717

0 commit comments

Comments
 (0)