-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #375 from meirkr/meirk/fix_sequence_with_default
In case a element which is defined as a sequence, has a default value,
- Loading branch information
Showing
3 changed files
with
64 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.ObjectModel; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace XmlSchemaClassGenerator.Tests | ||
{ | ||
public class DefaultInSequenceTests | ||
{ | ||
/// <summary> | ||
/// In case a element which is defined as a sequence, has a default value, | ||
/// the default value shall be ignored (it was wrong to initialize a collection with int value, for example). | ||
/// bakcing field was like: private Collection<int> _someElementWithDefaultValue = 0; | ||
/// In such case, the syntax breaks compliation. | ||
/// We would like to verify the default is ignored. | ||
/// </summary> | ||
[Fact] | ||
public void SequenceWithDefaultValue_CompilationPass() | ||
{ | ||
var assembly = Compiler.Generate(nameof(SequenceWithDefaultValue_CompilationPass), | ||
"xsd/SequenceWithDefault/*.xsd", new Generator | ||
{ | ||
GenerateNullables = true, | ||
UseShouldSerializePattern = true, | ||
NamespaceProvider = new NamespaceProvider | ||
{ | ||
GenerateNamespace = key => "Test" | ||
} | ||
}); | ||
|
||
var type = assembly.GetType("Test.SomeComplexType"); | ||
|
||
var collectionProperties = type | ||
.GetProperties() | ||
.Select(p => (p.Name, p.PropertyType)) | ||
.OrderBy(p => p.Name) | ||
.ToArray(); | ||
|
||
Assert.Equal(new[] | ||
{ | ||
("SomeElementWithDefault", typeof(Collection<int>)), | ||
}, collectionProperties); | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
XmlSchemaClassGenerator.Tests/xsd/SequenceWithDefault/SomeComplexType.xsd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?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="SomeComplexType"> | ||
<xs:sequence maxOccurs="unbounded" minOccurs="1"> | ||
<xs:element name="someElementWithDefault" type="xs:int" default="0"/> | ||
</xs:sequence> | ||
</xs:complexType> | ||
</xs:schema> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters