Skip to content

Commit

Permalink
Merge pull request #141 from ivanpovazan/sub_groups_ns_fix
Browse files Browse the repository at this point in the history
Fix for wrong namespace specification of XML element attribute of derived types
  • Loading branch information
mganss authored Sep 28, 2019
2 parents 78b1ccb + 10388bf commit 0e5e71b
Show file tree
Hide file tree
Showing 9 changed files with 1,981 additions and 15 deletions.
21 changes: 21 additions & 0 deletions XmlSchemaClassGenerator.Tests/XmlSchemaClassGenerator.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@
<None Update="xml\assistedLiving_min.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="xml\bpmnSimple.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="xml\compulsoryAuction_max.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down Expand Up @@ -182,6 +185,21 @@
<None Update="xsd\array-order\array-order.xsd">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="xsd\bpmn\BPMN20.xsd">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="xsd\bpmn\BPMNDI.xsd">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="xsd\bpmn\DC.xsd">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="xsd\bpmn\DI.xsd">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="xsd\bpmn\Semantic.xsd">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="xsd\client\client.xsd">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down Expand Up @@ -471,4 +489,7 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<Folder Include="xsd\bpmn\" />
</ItemGroup>
</Project>
72 changes: 70 additions & 2 deletions XmlSchemaClassGenerator.Tests/XmlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using System.Xml.XPath;
using Ganss.IO;
using Microsoft.CodeAnalysis;
using Microsoft.Xml.XMLGen;
Expand Down Expand Up @@ -77,6 +78,7 @@ private IEnumerable<string> ConvertXml(string name, string xsd, Generator genera
const string TimePattern = @"xsd\time\time.xsd";
const string TableauPattern = @"xsd\ts-api\*.xsd";
const string VSTstPattern = @"xsd\vstst\vstst.xsd";
const string BpmnPattern = @"xsd\bpmn\*.xsd";

// IATA test takes too long to perform every time

Expand Down Expand Up @@ -366,6 +368,62 @@ void TestCompareToXsd(Type t1, Type t2, string file)
}
}

[Fact, TestPriority(1)]
[UseCulture("en-US")]
public void TestBpmn()
{
var assembly = Compiler.Generate("Bpmn", BpmnPattern, new Generator
{
DataAnnotationMode = DataAnnotationMode.All,
GenerateNullables = true,
MemberVisitor = (member, model) => { }
});
Assert.NotNull(assembly);

var type = assembly.GetTypes().SingleOrDefault(t => t.Name == "TDefinitions");
Assert.NotNull(type);

var serializer = new XmlSerializer(type);
serializer.UnknownNode += new XmlNodeEventHandler(UnknownNodeHandler);
serializer.UnknownAttribute += new XmlAttributeEventHandler(UnknownAttributeHandler);
var unknownNodeError = false;
var unknownAttrError = false;

void UnknownNodeHandler(object sender, XmlNodeEventArgs e)
{
unknownNodeError = true;
}

void UnknownAttributeHandler(object sender, XmlAttributeEventArgs e)
{
unknownAttrError = true;
}

var xml = ReadXml("bpmnSimple");
var reader = XmlReader.Create(new StringReader(xml));

var isDeserializable = serializer.CanDeserialize(reader);
Assert.True(isDeserializable);

var deserializedObject = serializer.Deserialize(reader);
Assert.False(unknownNodeError);
Assert.False(unknownAttrError);

var serializedXml = Serialize(serializer, deserializedObject, GetNamespacesFromSource(xml));
File.WriteAllText("file.xml", serializedXml);

var deserializedXml = serializer.Deserialize(new StringReader(serializedXml));
AssertEx.Equal(deserializedObject, deserializedXml);
}

private IDictionary<string, string> GetNamespacesFromSource(string source)
{
XPathDocument doc = new XPathDocument(new StringReader(source));
XPathNavigator namespaceNavigator = doc.CreateNavigator();
namespaceNavigator.MoveToFollowing(XPathNodeType.Element);
return namespaceNavigator.GetNamespacesInScope(XmlNamespaceScope.All);
}

[Fact, TestPriority(3)]
public void CanSerializeAndDeserializeAllExampleXmlFiles()
{
Expand Down Expand Up @@ -397,11 +455,21 @@ void TestRoundtrip(Type t, string file)
}
}

string Serialize(XmlSerializer serializer, object o)
string Serialize(XmlSerializer serializer, object o, IDictionary<string, string> prefixToNsMap = null)
{
var sw = new StringWriter();
var ns = new XmlSerializerNamespaces();
ns.Add("", null);
if (prefixToNsMap == null)
{
ns.Add("", null);
}
else
{
foreach (var ptns in prefixToNsMap)
{
ns.Add(ptns.Key, ptns.Value);
}
}
serializer.Serialize(sw, o, ns);
var serializedXml = sw.ToString();
return serializedXml;
Expand Down
44 changes: 44 additions & 0 deletions XmlSchemaClassGenerator.Tests/xml/bpmnSimple.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" id="Definitions_1dhbs6d" targetNamespace="http://bpmn.io/schema/bpmn" exporter="Camunda Modeler" exporterVersion="3.1.2">
<bpmn:process id="Process_1szwmzf" isExecutable="true">
<bpmn:startEvent id="StartEvent_1" name="Start&#10;&#10;">
<bpmn:outgoing>SequenceFlow_15dil8x</bpmn:outgoing>
</bpmn:startEvent>
<bpmn:task id="Task_0hzgmx7" name="Action">
<bpmn:incoming>SequenceFlow_15dil8x</bpmn:incoming>
<bpmn:outgoing>SequenceFlow_1k24n4s</bpmn:outgoing>
</bpmn:task>
<bpmn:endEvent id="EndEvent_0covrtz" name="End">
<bpmn:incoming>SequenceFlow_1k24n4s</bpmn:incoming>
</bpmn:endEvent>
<bpmn:sequenceFlow id="SequenceFlow_15dil8x" sourceRef="StartEvent_1" targetRef="Task_0hzgmx7" />
<bpmn:sequenceFlow id="SequenceFlow_1k24n4s" sourceRef="Task_0hzgmx7" targetRef="EndEvent_0covrtz" />
</bpmn:process>
<bpmndi:BPMNDiagram id="BPMNDiagram_1">
<bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="Process_1szwmzf">
<bpmndi:BPMNShape id="_BPMNShape_StartEvent_2" bpmnElement="StartEvent_1">
<dc:Bounds x="179" y="103" width="36" height="36" />
<bpmndi:BPMNLabel>
<dc:Bounds x="185" y="146" width="25" height="40" />
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Task_0hzgmx7_di" bpmnElement="Task_0hzgmx7">
<dc:Bounds x="276" y="81" width="100" height="80" />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="EndEvent_0covrtz_di" bpmnElement="EndEvent_0covrtz">
<dc:Bounds x="444" y="103" width="36" height="36" />
<bpmndi:BPMNLabel>
<dc:Bounds x="452" y="146" width="20" height="14" />
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge id="SequenceFlow_15dil8x_di" bpmnElement="SequenceFlow_15dil8x">
<di:waypoint x="215" y="121" />
<di:waypoint x="276" y="121" />
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="SequenceFlow_1k24n4s_di" bpmnElement="SequenceFlow_1k24n4s">
<di:waypoint x="376" y="121" />
<di:waypoint x="444" y="121" />
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</bpmn:definitions>
37 changes: 37 additions & 0 deletions XmlSchemaClassGenerator.Tests/xsd/bpmn/BPMN20.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema elementFormDefault="qualified" attributeFormDefault="unqualified"
xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI"
targetNamespace="http://www.omg.org/spec/BPMN/20100524/MODEL">

<xsd:import namespace="http://www.omg.org/spec/BPMN/20100524/DI" schemaLocation="BPMNDI.xsd"/>
<xsd:include schemaLocation="Semantic.xsd"/>

<xsd:element name="definitions" type="tDefinitions"/>
<xsd:complexType name="tDefinitions">
<xsd:sequence>
<xsd:element ref="import" minOccurs="0" maxOccurs="unbounded"/>
<xsd:element ref="extension" minOccurs="0" maxOccurs="unbounded"/>
<xsd:element ref="rootElement" minOccurs="0" maxOccurs="unbounded"/>
<xsd:element ref="bpmndi:BPMNDiagram" minOccurs="0" maxOccurs="unbounded"/>
<xsd:element ref="relationship" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
<xsd:attribute name="id" type="xsd:ID" use="optional"/>
<xsd:attribute name="name" type="xsd:string"/>
<xsd:attribute name="targetNamespace" type="xsd:anyURI" use="required"/>
<xsd:attribute name="expressionLanguage" type="xsd:anyURI" use="optional" default="http://www.w3.org/1999/XPath"/>
<xsd:attribute name="typeLanguage" type="xsd:anyURI" use="optional" default="http://www.w3.org/2001/XMLSchema"/>
<xsd:attribute name="exporter" type="xsd:string"/>
<xsd:attribute name="exporterVersion" type="xsd:string"/>
<xsd:anyAttribute namespace="##other" processContents="lax"/>
</xsd:complexType>

<xsd:element name="import" type="tImport"/>
<xsd:complexType name="tImport">
<xsd:attribute name="namespace" type="xsd:anyURI" use="required"/>
<xsd:attribute name="location" type="xsd:string" use="required"/>
<xsd:attribute name="importType" type="xsd:anyURI" use="required"/>
</xsd:complexType>

</xsd:schema>
100 changes: 100 additions & 0 deletions XmlSchemaClassGenerator.Tests/xsd/bpmn/BPMNDI.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" targetNamespace="http://www.omg.org/spec/BPMN/20100524/DI" elementFormDefault="qualified" attributeFormDefault="unqualified">

<xsd:import namespace="http://www.omg.org/spec/DD/20100524/DC" schemaLocation="DC.xsd" />
<xsd:import namespace="http://www.omg.org/spec/DD/20100524/DI" schemaLocation="DI.xsd" />

<xsd:element name="BPMNDiagram" type="bpmndi:BPMNDiagram" />
<xsd:element name="BPMNPlane" type="bpmndi:BPMNPlane" />
<xsd:element name="BPMNLabelStyle" type="bpmndi:BPMNLabelStyle" />
<xsd:element name="BPMNShape" type="bpmndi:BPMNShape" substitutionGroup="di:DiagramElement" />
<xsd:element name="BPMNLabel" type="bpmndi:BPMNLabel" />
<xsd:element name="BPMNEdge" type="bpmndi:BPMNEdge" substitutionGroup="di:DiagramElement" />

<xsd:complexType name="BPMNDiagram">
<xsd:complexContent>
<xsd:extension base="di:Diagram">
<xsd:sequence>
<xsd:element ref="bpmndi:BPMNPlane" />
<xsd:element ref="bpmndi:BPMNLabelStyle" maxOccurs="unbounded" minOccurs="0" />
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>

<xsd:complexType name="BPMNPlane">
<xsd:complexContent>
<xsd:extension base="di:Plane">
<xsd:attribute name="bpmnElement" type="xsd:QName" />
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>

<xsd:complexType name="BPMNEdge">
<xsd:complexContent>
<xsd:extension base="di:LabeledEdge">
<xsd:sequence>
<xsd:element ref="bpmndi:BPMNLabel" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="bpmnElement" type="xsd:QName" />
<xsd:attribute name="sourceElement" type="xsd:QName" />
<xsd:attribute name="targetElement" type="xsd:QName" />
<xsd:attribute name="messageVisibleKind" type="bpmndi:MessageVisibleKind" />
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>

<xsd:complexType name="BPMNShape">
<xsd:complexContent>
<xsd:extension base="di:LabeledShape">
<xsd:sequence>
<xsd:element ref="bpmndi:BPMNLabel" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="bpmnElement" type="xsd:QName" />
<xsd:attribute name="isHorizontal" type="xsd:boolean" />
<xsd:attribute name="isExpanded" type="xsd:boolean" />
<xsd:attribute name="isMarkerVisible" type="xsd:boolean" />
<xsd:attribute name="isMessageVisible" type="xsd:boolean" />
<xsd:attribute name="participantBandKind" type="bpmndi:ParticipantBandKind" />
<xsd:attribute name="choreographyActivityShape" type="xsd:QName"/>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>

<xsd:complexType name="BPMNLabel">
<xsd:complexContent>
<xsd:extension base="di:Label">
<xsd:attribute name="labelStyle" type="xsd:QName" />
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>

<xsd:complexType name="BPMNLabelStyle">
<xsd:complexContent>
<xsd:extension base="di:Style">
<xsd:sequence>
<xsd:element ref="dc:Font" />
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>

<xsd:simpleType name="ParticipantBandKind">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="top_initiating" />
<xsd:enumeration value="middle_initiating" />
<xsd:enumeration value="bottom_initiating" />
<xsd:enumeration value="top_non_initiating" />
<xsd:enumeration value="middle_non_initiating" />
<xsd:enumeration value="bottom_non_initiating" />
</xsd:restriction>
</xsd:simpleType>

<xsd:simpleType name="MessageVisibleKind">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="initiating" />
<xsd:enumeration value="non_initiating" />
</xsd:restriction>
</xsd:simpleType>

</xsd:schema>
29 changes: 29 additions & 0 deletions XmlSchemaClassGenerator.Tests/xsd/bpmn/DC.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" targetNamespace="http://www.omg.org/spec/DD/20100524/DC" elementFormDefault="qualified" attributeFormDefault="unqualified">

<xsd:element name="Font" type="dc:Font" />
<xsd:element name="Point" type="dc:Point" />
<xsd:element name="Bounds" type="dc:Bounds" />

<xsd:complexType name="Font">
<xsd:attribute name="name" type="xsd:string" />
<xsd:attribute name="size" type="xsd:double" />
<xsd:attribute name="isBold" type="xsd:boolean" />
<xsd:attribute name="isItalic" type="xsd:boolean" />
<xsd:attribute name="isUnderline" type="xsd:boolean" />
<xsd:attribute name="isStrikeThrough" type="xsd:boolean" />
</xsd:complexType>

<xsd:complexType name="Point">
<xsd:attribute name="x" type="xsd:double" use="required" />
<xsd:attribute name="y" type="xsd:double" use="required" />
</xsd:complexType>

<xsd:complexType name="Bounds">
<xsd:attribute name="x" type="xsd:double" use="required" />
<xsd:attribute name="y" type="xsd:double" use="required" />
<xsd:attribute name="width" type="xsd:double" use="required" />
<xsd:attribute name="height" type="xsd:double" use="required" />
</xsd:complexType>

</xsd:schema>
Loading

0 comments on commit 0e5e71b

Please sign in to comment.