Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fixed value not initialized #16

Closed
Falco20019 opened this issue Sep 21, 2016 · 1 comment
Closed

Fixed value not initialized #16

Falco20019 opened this issue Sep 21, 2016 · 1 comment

Comments

@Falco20019
Copy link

Falco20019 commented Sep 21, 2016

I have a XSD containing following entry:
<xs:attribute name="version" type="xs:normalizedString" use="required" fixed="0500"/>

The xsd.exe initializes the value in the constructor with 0500, so that the user does not have to deal with it. It would be great, if you could add this too!

This is the patch I used to achieve a similar code generation. You might have to check if it's wanted that the FixedValue is used as DefaultValue in cases where the attribute is optional. The xsd.exe does not check this and always uses the FixedValue when no DefaultValue is given.

 XmlSchemaClassGenerator/Generator.cs | 2 ++
 XmlSchemaClassGenerator/TypeModel.cs | 9 +++++----
 2 files changed, 7 insertions(+), 4 deletions(-)

diff --git XmlSchemaClassGenerator/Generator.cs XmlSchemaClassGenerator/Generator.cs
index 284fb1b..04fce92 100644
--- XmlSchemaClassGenerator/Generator.cs
+++ XmlSchemaClassGenerator/Generator.cs
@@ -708,6 +708,7 @@ namespace XmlSchemaClassGenerator
                             IsAttribute = true,
                             IsNullable = attribute.Use != XmlSchemaUse.Required,
                             DefaultValue = attribute.DefaultValue,
+                            FixedValue = attribute.FixedValue,
                             Form = attribute.Form == XmlSchemaForm.None ? attribute.GetSchema().AttributeFormDefault : attribute.Form,
                             XmlNamespace = attribute.QualifiedName.Namespace != "" && attribute.QualifiedName.Namespace != typeModel.XmlSchemaName.Namespace ? attribute.QualifiedName.Namespace : null,
                         };
@@ -780,6 +781,7 @@ namespace XmlSchemaClassGenerator
                         IsNullable = item.MinOccurs < 1.0m,
                         IsCollection = item.MaxOccurs > 1.0m || particle.MaxOccurs > 1.0m, // http://msdn.microsoft.com/en-us/library/vstudio/d3hx2s7e(v=vs.100).aspx
                         DefaultValue = element.DefaultValue,
+                        FixedValue = element.FixedValue,
                         Form = element.Form == XmlSchemaForm.None ? element.GetSchema().ElementFormDefault : element.Form,
                         XmlNamespace = element.QualifiedName.Namespace != "" && element.QualifiedName.Namespace != typeModel.XmlSchemaName.Namespace ? element.QualifiedName.Namespace : null,
                     };
diff --git XmlSchemaClassGenerator/TypeModel.cs XmlSchemaClassGenerator/TypeModel.cs
index 12bb39b..163fc5f 100644
--- XmlSchemaClassGenerator/TypeModel.cs
+++ XmlSchemaClassGenerator/TypeModel.cs
@@ -402,6 +402,7 @@ namespace XmlSchemaClassGenerator
         public bool IsNillable { get; set; }
         public bool IsCollection { get; set; }
         public string DefaultValue { get; set; }
+        public string FixedValue { get; set; }
         public XmlSchemaForm Form { get; set; }
         public string XmlNamespace { get; set; }
         public List<DocumentationModel> Documentation { get; private set; }
@@ -612,7 +613,7 @@ namespace XmlSchemaClassGenerator
             var typeReference = TypeReference;
             var simpleType = propertyType as SimpleModel;

-            var requiresBackingField = withDataBinding || DefaultValue != null || IsCollection || isArray;
+            var requiresBackingField = withDataBinding || DefaultValue != null || FixedValue != null || IsCollection || isArray;
             var backingField = new CodeMemberField(typeReference, OwningType.GetUniqueFieldName(this))
             {
                 Attributes = MemberAttributes.Private
@@ -626,7 +627,7 @@ namespace XmlSchemaClassGenerator
                 typeDeclaration.Members.Add(backingField);
             }

-            if (DefaultValue == null)
+            if (DefaultValue == null && FixedValue == null)
             {
                 var propertyName = isNullableValueType && Configuration.GenerateNullables ? Name + "Value" : Name;
                 member = new CodeMemberField(typeReference, propertyName);
@@ -645,13 +646,13 @@ namespace XmlSchemaClassGenerator
             }
             else
             {
-                var defaultValueExpression = propertyType.GetDefaultValueFor(DefaultValue);
+                var defaultValueExpression = propertyType.GetDefaultValueFor(DefaultValue ?? FixedValue);
                 backingField.InitExpression = defaultValueExpression;

                 member = new CodeMemberField(typeReference, Name);
                 member.Name += GetAccessors(member.Name, backingField.Name, propertyType.GetPropertyValueTypeCode(), false, withDataBinding);

-                if (IsNullable)
+                if (DefaultValue != null && IsNullable)
                 {
                     if (!(defaultValueExpression is CodeObjectCreateExpression))
                     {
@mganss mganss closed this as completed in 7b0ce51 Sep 22, 2016
@mganss
Copy link
Owner

mganss commented Sep 22, 2016

Thanks, I made it so that when the element/attribute is optional, the default value is not set. You could take this further e.g. by hardcoding the fixed value in the setter or not generating a setter at all when the element/attribute is not optional, but for now I've kept it simple. This means that you can set a different value than the fixed one.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants