Skip to content

Commit 613907a

Browse files
authored
Merge pull request #585 from riccardofrancesconi/EmitFractionDigitsDataAnnotationInGeneratedModels
Add support for custom FractionDigitsAttribute
2 parents b584a23 + 3f0c013 commit 613907a

10 files changed

Lines changed: 634 additions & 43 deletions

File tree

README.md

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,11 @@ Options:
210210
force URI scheme when resolving URLs (default is
211211
none; can be: none, same, or any defined value
212212
for scheme, like https or http)
213+
--ema, --emitMetadataAttributes
214+
emit metadata helper attributes (default is false)
215+
--mn, --metadataNamespace=VALUE
216+
namespace for generated metadata helper attributes (
217+
default is XmlSchemaClassGenerator.Metadata)
213218
</pre>
214219

215220
For use from code use the [library NuGet package](https://www.nuget.org/packages/XmlSchemaClassGenerator-beta/):
@@ -236,12 +241,41 @@ Specifying the `NamespaceProvider` is optional. If you don't provide one, C# nam
236241
var generator = new Generator
237242
{
238243
NamespaceProvider = new NamespaceProvider
239-
{
244+
{
240245
GenerateNamespace = key => ...
241246
}
242247
};
243248
```
244249

250+
### Metadata attributes
251+
252+
When `EmitMetadataAttributes` is enabled, the generator emits custom attributes for XML schema restrictions that aren't covered by standard DataAnnotations. For example, `xs:fractionDigits` becomes `FractionDigitsAttribute`:
253+
254+
**Schema:**
255+
```xml
256+
<xs:element name="price">
257+
<xs:simpleType>
258+
<xs:restriction base="xs:decimal">
259+
<xs:fractionDigits value="2"/>
260+
</xs:restriction>
261+
</xs:simpleType>
262+
</xs:element>
263+
```
264+
265+
**Command line:**
266+
```
267+
xscgen --ema --mn MyApp.Metadata -o Generated schema.xsd
268+
```
269+
270+
**Generated code:**
271+
```C#
272+
[System.ComponentModel.DataAnnotations.Required()]
273+
[MyApp.Metadata.FractionDigits(2)]
274+
public decimal Price { get; set; }
275+
```
276+
277+
The attribute definition is automatically generated in the specified namespace. If not specified, the default namespace is `XmlSchemaClassGenerator.Metadata`.
278+
245279
### Mapping xsd files to C# namespaces
246280

247281
Using the optional `|` syntax of the `-n` command line option you can map individual xsd files to C# namespaces. If you have several input files using the same XML namespace you can still generate an individual C# namespace for the types defined within a single xsd file. For example, if you have two input files `a.xsd` and `b.xsd` both of which have the same `targetNamespace` of `http://example.com/namespace` you can generate the C# namespaces `Example.NamespaceA` and `Example.NamespaceB`:

XmlSchemaClassGenerator.Console/Program.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,12 @@ static int Main(string[] args)
6969
var separateNamespaceHierarchy = false;
7070
var serializeEmptyCollections = false;
7171
var allowDtdParse = false;
72-
var omitXmlIncludeAttribute = false;
73-
var enumCollection = false;
74-
NamingScheme? namingScheme = null;
72+
var omitXmlIncludeAttribute = false;
73+
var enumCollection = false;
74+
NamingScheme? namingScheme = null;
7575
var forceUriScheme = "none";
76+
var emitMetadataAttributes = false;
77+
var metadataNamespace = GeneratorConfiguration.DefaultMetadataNamespace;
7678

7779

7880
var options = new OptionSet {
@@ -171,7 +173,7 @@ A file name may be given by appending a pipe sign (|) followed by a file name (l
171173
{ "uc|unionCommonType", "generate a common type for unions if possible (default is false)", v => unionCommonType = v != null },
172174
{ "ec|serializeEmptyCollections", "serialize empty collections (default is false)", v => serializeEmptyCollections = v != null },
173175
{ "dtd|allowDtdParse", "allows dtd parse (default is false)", v => allowDtdParse = v != null },
174-
{ "oxi|omitXmlIncludeAttribute", "omit generation of XmlIncludeAttribute for derived types (default is false)", v => omitXmlIncludeAttribute = v != null },
176+
{ "oxi|omitXmlIncludeAttribute", "omit generation of XmlIncludeAttribute for derived types (default is false)", v => omitXmlIncludeAttribute = v != null },
175177
{ "ecl|enumCollection", "generate typed enum collections for xs:list types instead of string collections (default is false)", v => enumCollection = v != null },
176178
{ "ns|namingScheme=", @"use the specified naming scheme for class and property names (default is Pascal; can be: Direct, Pascal, Legacy)",
177179
v =>
@@ -185,7 +187,9 @@ A file name may be given by appending a pipe sign (|) followed by a file name (l
185187
};
186188
}
187189
},
188-
{ "fu|forceUriScheme=", "force URI scheme when resolving URLs (default is none; can be: none, same, or any defined value for scheme, like https or http)", v => forceUriScheme = v }
190+
{ "fu|forceUriScheme=", "force URI scheme when resolving URLs (default is none; can be: none, same, or any defined value for scheme, like https or http)", v => forceUriScheme = v },
191+
{ "ema|emitMetadataAttributes", "emit metadata helper attributes (default is false)", v => emitMetadataAttributes = v != null },
192+
{ "mn|metadataNamespace=", $"namespace for generated metadata helper attributes (default is {GeneratorConfiguration.DefaultMetadataNamespace})", v => metadataNamespace = v }
189193
};
190194

191195
var globsAndUris = options.Parse(args);
@@ -274,9 +278,11 @@ A file name may be given by appending a pipe sign (|) followed by a file name (l
274278
SeparateNamespaceHierarchy = separateNamespaceHierarchy,
275279
SerializeEmptyCollections = serializeEmptyCollections,
276280
AllowDtdParse = allowDtdParse,
277-
OmitXmlIncludeAttribute = omitXmlIncludeAttribute,
278-
EnumCollection = enumCollection,
279-
ForceUriScheme = forceUriScheme
281+
OmitXmlIncludeAttribute = omitXmlIncludeAttribute,
282+
EnumCollection = enumCollection,
283+
ForceUriScheme = forceUriScheme,
284+
EmitMetadataAttributes = emitMetadataAttributes,
285+
MetadataNamespace = metadataNamespace
280286
};
281287

282288
if (namingScheme != null)

0 commit comments

Comments
 (0)