diff --git a/src/Json.Schema.ToDotNet.UnitTests/DataModelGeneratorTests.cs b/src/Json.Schema.ToDotNet.UnitTests/DataModelGeneratorTests.cs
index c21d728..3b3b50f 100644
--- a/src/Json.Schema.ToDotNet.UnitTests/DataModelGeneratorTests.cs
+++ b/src/Json.Schema.ToDotNet.UnitTests/DataModelGeneratorTests.cs
@@ -141,7 +141,7 @@ public partial class C
[DataMember(Name = ""booleanProperty"", IsRequired = false, EmitDefaultValue = false)]
public bool BooleanProperty { get; set; }
[DataMember(Name = ""integerProperty"", IsRequired = false, EmitDefaultValue = false)]
- public int IntegerProperty { get; set; }
+ public int? IntegerProperty { get; set; }
}
}";
@@ -212,7 +212,10 @@ public int GetHashCode(C obj)
result = (result * 31) + obj.NumberProperty.GetHashCode();
result = (result * 31) + obj.BooleanProperty.GetHashCode();
- result = (result * 31) + obj.IntegerProperty.GetHashCode();
+ if (obj.IntegerProperty != null)
+ {
+ result = (result * 31) + obj.IntegerProperty.GetHashCode();
+ }
}
return result;
@@ -310,29 +313,29 @@ namespace N
public partial class C
{
[DataMember(Name = ""integerProperty_default"", IsRequired = false, EmitDefaultValue = false)]
- public BigInteger IntegerProperty_default { get; set; }
+ public BigInteger? IntegerProperty_default { get; set; }
[DataMember(Name = ""integerProperty_min_0_max_na"", IsRequired = false, EmitDefaultValue = false)]
- public BigInteger IntegerProperty_min_0_max_na { get; set; }
+ public BigInteger? IntegerProperty_min_0_max_na { get; set; }
[DataMember(Name = ""integerProperty_min_minus1_max_na"", IsRequired = false, EmitDefaultValue = false)]
- public BigInteger IntegerProperty_min_minus1_max_na { get; set; }
+ public BigInteger? IntegerProperty_min_minus1_max_na { get; set; }
[DataMember(Name = ""integerProperty_min_1_max_na"", IsRequired = false, EmitDefaultValue = false)]
- public BigInteger IntegerProperty_min_1_max_na { get; set; }
+ public BigInteger? IntegerProperty_min_1_max_na { get; set; }
[DataMember(Name = ""integerProperty_min_na_max_0"", IsRequired = false, EmitDefaultValue = false)]
- public BigInteger IntegerProperty_min_na_max_0 { get; set; }
+ public BigInteger? IntegerProperty_min_na_max_0 { get; set; }
[DataMember(Name = ""integerProperty_min_0_max_int64times10"", IsRequired = false, EmitDefaultValue = false)]
public BigInteger IntegerProperty_min_0_max_int64times10 { get; set; }
[DataMember(Name = ""integerProperty_min_0_max_int64"", IsRequired = false, EmitDefaultValue = false)]
- public long IntegerProperty_min_0_max_int64 { get; set; }
+ public long? IntegerProperty_min_0_max_int64 { get; set; }
[DataMember(Name = ""integerProperty_min_0_max_int32add1"", IsRequired = false, EmitDefaultValue = false)]
- public long IntegerProperty_min_0_max_int32add1 { get; set; }
+ public long? IntegerProperty_min_0_max_int32add1 { get; set; }
[DataMember(Name = ""integerProperty_min_0_max_int32"", IsRequired = false, EmitDefaultValue = false)]
- public int IntegerProperty_min_0_max_int32 { get; set; }
+ public int? IntegerProperty_min_0_max_int32 { get; set; }
[DataMember(Name = ""integerProperty_min_int32_max_0"", IsRequired = false, EmitDefaultValue = false)]
- public int IntegerProperty_min_int32_max_0 { get; set; }
+ public int? IntegerProperty_min_int32_max_0 { get; set; }
[DataMember(Name = ""integerProperty_min_int32minus1_max_0"", IsRequired = false, EmitDefaultValue = false)]
- public long IntegerProperty_min_int32minus1_max_0 { get; set; }
+ public long? IntegerProperty_min_int32minus1_max_0 { get; set; }
[DataMember(Name = ""integerProperty_min_int64_max_0"", IsRequired = false, EmitDefaultValue = false)]
- public long IntegerProperty_min_int64_max_0 { get; set; }
+ public long? IntegerProperty_min_int64_max_0 { get; set; }
[DataMember(Name = ""integerProperty_min_int64times10_max_0"", IsRequired = false, EmitDefaultValue = false)]
public BigInteger IntegerProperty_min_int64times10_max_0 { get; set; }
[DataMember(Name = ""integerProperty_min_huge_max_0"", IsRequired = false, EmitDefaultValue = false)]
@@ -354,35 +357,35 @@ namespace N
public partial class C
{
[DataMember(Name = ""integerProperty_default"", IsRequired = false, EmitDefaultValue = false)]
- public BigInteger IntegerProperty_default { get; set; }
+ public BigInteger? IntegerProperty_default { get; set; }
[DataMember(Name = ""integerProperty_min_0_max_na"", IsRequired = false, EmitDefaultValue = false)]
- public BigInteger IntegerProperty_min_0_max_na { get; set; }
+ public BigInteger? IntegerProperty_min_0_max_na { get; set; }
[DataMember(Name = ""integerProperty_min_minus1_max_na"", IsRequired = false, EmitDefaultValue = false)]
- public BigInteger IntegerProperty_min_minus1_max_na { get; set; }
+ public BigInteger? IntegerProperty_min_minus1_max_na { get; set; }
[DataMember(Name = ""integerProperty_min_1_max_na"", IsRequired = false, EmitDefaultValue = false)]
- public BigInteger IntegerProperty_min_1_max_na { get; set; }
+ public BigInteger? IntegerProperty_min_1_max_na { get; set; }
[DataMember(Name = ""integerProperty_min_na_max_0"", IsRequired = false, EmitDefaultValue = false)]
- public BigInteger IntegerProperty_min_na_max_0 { get; set; }
+ public BigInteger? IntegerProperty_min_na_max_0 { get; set; }
[DataMember(Name = ""integerProperty_min_0_max_int64times10"", IsRequired = false, EmitDefaultValue = false)]
- public BigInteger IntegerProperty_min_0_max_int64times10 { get; set; }
+ public BigInteger? IntegerProperty_min_0_max_int64times10 { get; set; }
[DataMember(Name = ""integerProperty_min_0_max_int64"", IsRequired = false, EmitDefaultValue = false)]
- public BigInteger IntegerProperty_min_0_max_int64 { get; set; }
+ public BigInteger? IntegerProperty_min_0_max_int64 { get; set; }
[DataMember(Name = ""integerProperty_min_0_max_int32add1"", IsRequired = false, EmitDefaultValue = false)]
- public BigInteger IntegerProperty_min_0_max_int32add1 { get; set; }
+ public BigInteger? IntegerProperty_min_0_max_int32add1 { get; set; }
[DataMember(Name = ""integerProperty_min_0_max_int32"", IsRequired = false, EmitDefaultValue = false)]
- public BigInteger IntegerProperty_min_0_max_int32 { get; set; }
+ public BigInteger? IntegerProperty_min_0_max_int32 { get; set; }
[DataMember(Name = ""integerProperty_min_int32_max_0"", IsRequired = false, EmitDefaultValue = false)]
- public BigInteger IntegerProperty_min_int32_max_0 { get; set; }
+ public BigInteger? IntegerProperty_min_int32_max_0 { get; set; }
[DataMember(Name = ""integerProperty_min_int32minus1_max_0"", IsRequired = false, EmitDefaultValue = false)]
- public BigInteger IntegerProperty_min_int32minus1_max_0 { get; set; }
+ public BigInteger? IntegerProperty_min_int32minus1_max_0 { get; set; }
[DataMember(Name = ""integerProperty_min_int64_max_0"", IsRequired = false, EmitDefaultValue = false)]
- public BigInteger IntegerProperty_min_int64_max_0 { get; set; }
+ public BigInteger? IntegerProperty_min_int64_max_0 { get; set; }
[DataMember(Name = ""integerProperty_min_int64times10_max_0"", IsRequired = false, EmitDefaultValue = false)]
- public BigInteger IntegerProperty_min_int64times10_max_0 { get; set; }
+ public BigInteger? IntegerProperty_min_int64times10_max_0 { get; set; }
[DataMember(Name = ""integerProperty_min_huge_max_0"", IsRequired = false, EmitDefaultValue = false)]
- public BigInteger IntegerProperty_min_huge_max_0 { get; set; }
+ public BigInteger? IntegerProperty_min_huge_max_0 { get; set; }
[DataMember(Name = ""integerProperty_min_0_max_huge"", IsRequired = false, EmitDefaultValue = false)]
- public BigInteger IntegerProperty_min_0_max_huge { get; set; }
+ public BigInteger? IntegerProperty_min_0_max_huge { get; set; }
}
}";
const string ExpectedClass_Long =
@@ -397,35 +400,35 @@ namespace N
public partial class C
{
[DataMember(Name = ""integerProperty_default"", IsRequired = false, EmitDefaultValue = false)]
- public long IntegerProperty_default { get; set; }
+ public long? IntegerProperty_default { get; set; }
[DataMember(Name = ""integerProperty_min_0_max_na"", IsRequired = false, EmitDefaultValue = false)]
- public long IntegerProperty_min_0_max_na { get; set; }
+ public long? IntegerProperty_min_0_max_na { get; set; }
[DataMember(Name = ""integerProperty_min_minus1_max_na"", IsRequired = false, EmitDefaultValue = false)]
- public long IntegerProperty_min_minus1_max_na { get; set; }
+ public long? IntegerProperty_min_minus1_max_na { get; set; }
[DataMember(Name = ""integerProperty_min_1_max_na"", IsRequired = false, EmitDefaultValue = false)]
- public long IntegerProperty_min_1_max_na { get; set; }
+ public long? IntegerProperty_min_1_max_na { get; set; }
[DataMember(Name = ""integerProperty_min_na_max_0"", IsRequired = false, EmitDefaultValue = false)]
- public long IntegerProperty_min_na_max_0 { get; set; }
+ public long? IntegerProperty_min_na_max_0 { get; set; }
[DataMember(Name = ""integerProperty_min_0_max_int64times10"", IsRequired = false, EmitDefaultValue = false)]
- public long IntegerProperty_min_0_max_int64times10 { get; set; }
+ public long? IntegerProperty_min_0_max_int64times10 { get; set; }
[DataMember(Name = ""integerProperty_min_0_max_int64"", IsRequired = false, EmitDefaultValue = false)]
- public long IntegerProperty_min_0_max_int64 { get; set; }
+ public long? IntegerProperty_min_0_max_int64 { get; set; }
[DataMember(Name = ""integerProperty_min_0_max_int32add1"", IsRequired = false, EmitDefaultValue = false)]
- public long IntegerProperty_min_0_max_int32add1 { get; set; }
+ public long? IntegerProperty_min_0_max_int32add1 { get; set; }
[DataMember(Name = ""integerProperty_min_0_max_int32"", IsRequired = false, EmitDefaultValue = false)]
- public long IntegerProperty_min_0_max_int32 { get; set; }
+ public long? IntegerProperty_min_0_max_int32 { get; set; }
[DataMember(Name = ""integerProperty_min_int32_max_0"", IsRequired = false, EmitDefaultValue = false)]
- public long IntegerProperty_min_int32_max_0 { get; set; }
+ public long? IntegerProperty_min_int32_max_0 { get; set; }
[DataMember(Name = ""integerProperty_min_int32minus1_max_0"", IsRequired = false, EmitDefaultValue = false)]
- public long IntegerProperty_min_int32minus1_max_0 { get; set; }
+ public long? IntegerProperty_min_int32minus1_max_0 { get; set; }
[DataMember(Name = ""integerProperty_min_int64_max_0"", IsRequired = false, EmitDefaultValue = false)]
- public long IntegerProperty_min_int64_max_0 { get; set; }
+ public long? IntegerProperty_min_int64_max_0 { get; set; }
[DataMember(Name = ""integerProperty_min_int64times10_max_0"", IsRequired = false, EmitDefaultValue = false)]
- public long IntegerProperty_min_int64times10_max_0 { get; set; }
+ public long? IntegerProperty_min_int64times10_max_0 { get; set; }
[DataMember(Name = ""integerProperty_min_huge_max_0"", IsRequired = false, EmitDefaultValue = false)]
- public long IntegerProperty_min_huge_max_0 { get; set; }
+ public long? IntegerProperty_min_huge_max_0 { get; set; }
[DataMember(Name = ""integerProperty_min_0_max_huge"", IsRequired = false, EmitDefaultValue = false)]
- public long IntegerProperty_min_0_max_huge { get; set; }
+ public long? IntegerProperty_min_0_max_huge { get; set; }
}
}";
const string ExpectedClass_Int =
@@ -440,35 +443,35 @@ namespace N
public partial class C
{
[DataMember(Name = ""integerProperty_default"", IsRequired = false, EmitDefaultValue = false)]
- public int IntegerProperty_default { get; set; }
+ public int? IntegerProperty_default { get; set; }
[DataMember(Name = ""integerProperty_min_0_max_na"", IsRequired = false, EmitDefaultValue = false)]
- public int IntegerProperty_min_0_max_na { get; set; }
+ public int? IntegerProperty_min_0_max_na { get; set; }
[DataMember(Name = ""integerProperty_min_minus1_max_na"", IsRequired = false, EmitDefaultValue = false)]
- public int IntegerProperty_min_minus1_max_na { get; set; }
+ public int? IntegerProperty_min_minus1_max_na { get; set; }
[DataMember(Name = ""integerProperty_min_1_max_na"", IsRequired = false, EmitDefaultValue = false)]
- public int IntegerProperty_min_1_max_na { get; set; }
+ public int? IntegerProperty_min_1_max_na { get; set; }
[DataMember(Name = ""integerProperty_min_na_max_0"", IsRequired = false, EmitDefaultValue = false)]
- public int IntegerProperty_min_na_max_0 { get; set; }
+ public int? IntegerProperty_min_na_max_0 { get; set; }
[DataMember(Name = ""integerProperty_min_0_max_int64times10"", IsRequired = false, EmitDefaultValue = false)]
- public int IntegerProperty_min_0_max_int64times10 { get; set; }
+ public int? IntegerProperty_min_0_max_int64times10 { get; set; }
[DataMember(Name = ""integerProperty_min_0_max_int64"", IsRequired = false, EmitDefaultValue = false)]
- public int IntegerProperty_min_0_max_int64 { get; set; }
+ public int? IntegerProperty_min_0_max_int64 { get; set; }
[DataMember(Name = ""integerProperty_min_0_max_int32add1"", IsRequired = false, EmitDefaultValue = false)]
- public int IntegerProperty_min_0_max_int32add1 { get; set; }
+ public int? IntegerProperty_min_0_max_int32add1 { get; set; }
[DataMember(Name = ""integerProperty_min_0_max_int32"", IsRequired = false, EmitDefaultValue = false)]
- public int IntegerProperty_min_0_max_int32 { get; set; }
+ public int? IntegerProperty_min_0_max_int32 { get; set; }
[DataMember(Name = ""integerProperty_min_int32_max_0"", IsRequired = false, EmitDefaultValue = false)]
- public int IntegerProperty_min_int32_max_0 { get; set; }
+ public int? IntegerProperty_min_int32_max_0 { get; set; }
[DataMember(Name = ""integerProperty_min_int32minus1_max_0"", IsRequired = false, EmitDefaultValue = false)]
- public int IntegerProperty_min_int32minus1_max_0 { get; set; }
+ public int? IntegerProperty_min_int32minus1_max_0 { get; set; }
[DataMember(Name = ""integerProperty_min_int64_max_0"", IsRequired = false, EmitDefaultValue = false)]
- public int IntegerProperty_min_int64_max_0 { get; set; }
+ public int? IntegerProperty_min_int64_max_0 { get; set; }
[DataMember(Name = ""integerProperty_min_int64times10_max_0"", IsRequired = false, EmitDefaultValue = false)]
- public int IntegerProperty_min_int64times10_max_0 { get; set; }
+ public int? IntegerProperty_min_int64times10_max_0 { get; set; }
[DataMember(Name = ""integerProperty_min_huge_max_0"", IsRequired = false, EmitDefaultValue = false)]
- public int IntegerProperty_min_huge_max_0 { get; set; }
+ public int? IntegerProperty_min_huge_max_0 { get; set; }
[DataMember(Name = ""integerProperty_min_0_max_huge"", IsRequired = false, EmitDefaultValue = false)]
- public int IntegerProperty_min_0_max_huge { get; set; }
+ public int? IntegerProperty_min_0_max_huge { get; set; }
}
}";
GeneratesPropertiesWithIntegerTypes_Helper(GenerateJsonIntegerOption.Auto, ExpectedClass_Auto);
@@ -1347,19 +1350,19 @@ public partial class Color
/// The value of the R component.
///
[DataMember(Name = ""red"", IsRequired = false, EmitDefaultValue = false)]
- public int Red { get; set; }
+ public int? Red { get; set; }
///
/// The value of the G component.
///
[DataMember(Name = ""green"", IsRequired = false, EmitDefaultValue = false)]
- public int Green { get; set; }
+ public int? Green { get; set; }
///
/// The value of the B component.
///
[DataMember(Name = ""blue"", IsRequired = false, EmitDefaultValue = false)]
- public int Blue { get; set; }
+ public int? Blue { get; set; }
}
}";
@@ -1418,9 +1421,20 @@ public int GetHashCode(Color obj)
int result = 17;
unchecked
{
- result = (result * 31) + obj.Red.GetHashCode();
- result = (result * 31) + obj.Green.GetHashCode();
- result = (result * 31) + obj.Blue.GetHashCode();
+ if (obj.Red != null)
+ {
+ result = (result * 31) + obj.Red.GetHashCode();
+ }
+
+ if (obj.Green != null)
+ {
+ result = (result * 31) + obj.Green.GetHashCode();
+ }
+
+ if (obj.Blue != null)
+ {
+ result = (result * 31) + obj.Blue.GetHashCode();
+ }
}
return result;
@@ -1758,7 +1772,7 @@ public SNodeKind SNodeKind
/// An integer property.
///
[DataMember(Name = ""integerProperty"", IsRequired = false, EmitDefaultValue = false)]
- public int IntegerProperty { get; set; }
+ public int? IntegerProperty { get; set; }
///
/// An integer property with a default value.
@@ -1976,7 +1990,7 @@ public C()
///
/// An initialization value for the property.
///
- public C(int integerProperty, int integerPropertyWithDefault, double numberProperty, double numberPropertyWithDefault, string stringProperty, string stringPropertyWithDefault, bool booleanProperty, bool booleanPropertyWithTrueDefault, bool booleanPropertyWithFalseDefault, Color enumeratedPropertyWithDefault, IEnumerable arrayProp, Uri uriProp, DateTime dateTimeProp, D referencedTypeProp, IEnumerable arrayOfRefProp, IEnumerable> arrayOfArrayProp, IDictionary dictionaryProp, IDictionary dictionaryWithPrimitiveSchemaProp, IDictionary dictionaryWithObjectSchemaProp, IDictionary> dictionaryWithObjectArraySchemaProp, IDictionary dictionaryWithUriKeyProp, IDictionary dictionaryWithHintedValueProp)
+ public C(int? integerProperty, int integerPropertyWithDefault, double numberProperty, double numberPropertyWithDefault, string stringProperty, string stringPropertyWithDefault, bool booleanProperty, bool booleanPropertyWithTrueDefault, bool booleanPropertyWithFalseDefault, Color enumeratedPropertyWithDefault, IEnumerable arrayProp, Uri uriProp, DateTime dateTimeProp, D referencedTypeProp, IEnumerable arrayOfRefProp, IEnumerable> arrayOfArrayProp, IDictionary dictionaryProp, IDictionary dictionaryWithPrimitiveSchemaProp, IDictionary dictionaryWithObjectSchemaProp, IDictionary> dictionaryWithObjectArraySchemaProp, IDictionary dictionaryWithUriKeyProp, IDictionary dictionaryWithHintedValueProp)
{
Init(integerProperty, integerPropertyWithDefault, numberProperty, numberPropertyWithDefault, stringProperty, stringPropertyWithDefault, booleanProperty, booleanPropertyWithTrueDefault, booleanPropertyWithFalseDefault, enumeratedPropertyWithDefault, arrayProp, uriProp, dateTimeProp, referencedTypeProp, arrayOfRefProp, arrayOfArrayProp, dictionaryProp, dictionaryWithPrimitiveSchemaProp, dictionaryWithObjectSchemaProp, dictionaryWithObjectArraySchemaProp, dictionaryWithUriKeyProp, dictionaryWithHintedValueProp);
}
@@ -2018,7 +2032,7 @@ private ISNode DeepCloneCore()
return new C(this);
}
- private void Init(int integerProperty, int integerPropertyWithDefault, double numberProperty, double numberPropertyWithDefault, string stringProperty, string stringPropertyWithDefault, bool booleanProperty, bool booleanPropertyWithTrueDefault, bool booleanPropertyWithFalseDefault, Color enumeratedPropertyWithDefault, IEnumerable arrayProp, Uri uriProp, DateTime dateTimeProp, D referencedTypeProp, IEnumerable arrayOfRefProp, IEnumerable> arrayOfArrayProp, IDictionary dictionaryProp, IDictionary dictionaryWithPrimitiveSchemaProp, IDictionary dictionaryWithObjectSchemaProp, IDictionary> dictionaryWithObjectArraySchemaProp, IDictionary dictionaryWithUriKeyProp, IDictionary dictionaryWithHintedValueProp)
+ private void Init(int? integerProperty, int integerPropertyWithDefault, double numberProperty, double numberPropertyWithDefault, string stringProperty, string stringPropertyWithDefault, bool booleanProperty, bool booleanPropertyWithTrueDefault, bool booleanPropertyWithFalseDefault, Color enumeratedPropertyWithDefault, IEnumerable arrayProp, Uri uriProp, DateTime dateTimeProp, D referencedTypeProp, IEnumerable arrayOfRefProp, IEnumerable> arrayOfArrayProp, IDictionary dictionaryProp, IDictionary dictionaryWithPrimitiveSchemaProp, IDictionary dictionaryWithObjectSchemaProp, IDictionary> dictionaryWithObjectArraySchemaProp, IDictionary dictionaryWithUriKeyProp, IDictionary dictionaryWithHintedValueProp)
{
IntegerProperty = integerProperty;
IntegerPropertyWithDefault = integerPropertyWithDefault;
@@ -2655,7 +2669,7 @@ public partial class Def2
public static IComparer Comparer => Def2Comparer.Instance;
[DataMember(Name = ""prop2"", IsRequired = false, EmitDefaultValue = false)]
- public int Prop2 { get; set; }
+ public int? Prop2 { get; set; }
}
}";
@@ -2704,7 +2718,10 @@ public int GetHashCode(Def2 obj)
int result = 17;
unchecked
{
- result = (result * 31) + obj.Prop2.GetHashCode();
+ if (obj.Prop2 != null)
+ {
+ result = (result * 31) + obj.Prop2.GetHashCode();
+ }
}
return result;
@@ -3268,7 +3285,7 @@ public partial class C
public static IComparer Comparer => CComparer.Instance;
[DataMember(Name = ""intDefProp"", IsRequired = false, EmitDefaultValue = false)]
- public int IntDefProp { get; set; }
+ public int? IntDefProp { get; set; }
}
}";
const string ExpectedEqualityComparerClass =
@@ -3316,7 +3333,10 @@ public int GetHashCode(C obj)
int result = 17;
unchecked
{
- result = (result * 31) + obj.IntDefProp.GetHashCode();
+ if (obj.IntDefProp != null)
+ {
+ result = (result * 31) + obj.IntDefProp.GetHashCode();
+ }
}
return result;
@@ -3398,6 +3418,15 @@ public void GeneratesAttributesForPropertiesOfPrimitiveTypeWithDefaults()
""description"": ""An integer property with a default value."",
""default"": 42
},
+ ""integerPropertyRequired"": {
+ ""type"": ""integer"",
+ ""description"": ""An integer property, required.""
+ },
+ ""integerPropertyRequiredAndWithDefault"": {
+ ""type"": ""integer"",
+ ""description"": ""An integer property, required and with a default value."",
+ ""default"": 42
+ },
""numberProperty"": {
""type"": ""number"",
""description"": ""A number property.""
@@ -3438,7 +3467,8 @@ public void GeneratesAttributesForPropertiesOfPrimitiveTypeWithDefaults()
},
""default"": []
}
- }
+ },
+ ""required"": [ ""integerPropertyRequired"", ""integerPropertyRequiredAndWithDefault"" ]
}";
const string ExpectedClass =
@"using System;
@@ -3458,7 +3488,7 @@ public partial class C
/// An integer property.
///
[DataMember(Name = ""integerProperty"", IsRequired = false, EmitDefaultValue = false)]
- public int IntegerProperty { get; set; }
+ public int? IntegerProperty { get; set; }
///
/// An integer property with a default value.
@@ -3468,6 +3498,20 @@ public partial class C
[JsonProperty(DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
public int IntegerPropertyWithDefault { get; set; }
+ ///
+ /// An integer property, required.
+ ///
+ [DataMember(Name = ""integerPropertyRequired"", IsRequired = true)]
+ public int IntegerPropertyRequired { get; set; }
+
+ ///
+ /// An integer property, required and with a default value.
+ ///
+ [DataMember(Name = ""integerPropertyRequiredAndWithDefault"", IsRequired = true)]
+ [DefaultValue(42)]
+ [JsonProperty(DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
+ public int IntegerPropertyRequiredAndWithDefault { get; set; }
+
///
/// A number property.
///
diff --git a/src/Json.Schema.ToDotNet.UnitTests/Hints/AttributeHintTests.cs b/src/Json.Schema.ToDotNet.UnitTests/Hints/AttributeHintTests.cs
index ad7a4ca..c34e449 100644
--- a/src/Json.Schema.ToDotNet.UnitTests/Hints/AttributeHintTests.cs
+++ b/src/Json.Schema.ToDotNet.UnitTests/Hints/AttributeHintTests.cs
@@ -43,7 +43,7 @@ public partial class C
{
[DataMember(Name = ""theProperty"", IsRequired = false, EmitDefaultValue = false)]
[Test]
- public int TheProperty { get; set; }
+ public int? TheProperty { get; set; }
}
}"
),
@@ -85,7 +85,7 @@ public partial class C
{
[DataMember(Name = ""theProperty"", IsRequired = false, EmitDefaultValue = false)]
[Test(typeof(string))]
- public int TheProperty { get; set; }
+ public int? TheProperty { get; set; }
}
}"
),
@@ -129,7 +129,7 @@ public partial class C
{
[DataMember(Name = ""theProperty"", IsRequired = false, EmitDefaultValue = false)]
[Test(typeof(string), 42, ""a"")]
- public int TheProperty { get; set; }
+ public int? TheProperty { get; set; }
}
}"
),
@@ -171,7 +171,7 @@ public partial class C
{
[DataMember(Name = ""theProperty"", IsRequired = false, EmitDefaultValue = false)]
[Test(DefaultValueHandling = DefaultValueHandling.Ignore)]
- public int TheProperty { get; set; }
+ public int? TheProperty { get; set; }
}
}"
),
@@ -214,7 +214,7 @@ public partial class C
{
[DataMember(Name = ""theProperty"", IsRequired = false, EmitDefaultValue = false)]
[Test(DefaultValueHandling = DefaultValueHandling.Ignore, DefaultValue = 42)]
- public int TheProperty { get; set; }
+ public int? TheProperty { get; set; }
}
}"
),
@@ -260,7 +260,7 @@ public partial class C
[DataMember(Name = ""theProperty"", IsRequired = false, EmitDefaultValue = false)]
[Test1]
[Test2]
- public int TheProperty { get; set; }
+ public int? TheProperty { get; set; }
}
}"
),
@@ -301,7 +301,7 @@ public partial class C
{
[DataMember(Name = ""theProperty"", IsRequired = false, EmitDefaultValue = false)]
[Test]
- public int TheProperty { get; set; }
+ public int? TheProperty { get; set; }
}
}"
),
diff --git a/src/Json.Schema.ToDotNet.UnitTests/Hints/InterfaceHintTests.cs b/src/Json.Schema.ToDotNet.UnitTests/Hints/InterfaceHintTests.cs
index 285d8b2..c135cc4 100644
--- a/src/Json.Schema.ToDotNet.UnitTests/Hints/InterfaceHintTests.cs
+++ b/src/Json.Schema.ToDotNet.UnitTests/Hints/InterfaceHintTests.cs
@@ -69,13 +69,13 @@ public partial class C : IC
/// The value.
///
[DataMember(Name = ""value"", IsRequired = false, EmitDefaultValue = false)]
- public int Value { get; set; }
+ public int? Value { get; set; }
///
/// Internal value.
///
[DataMember(Name = ""value2"", IsRequired = false, EmitDefaultValue = false)]
- internal int Value2 { get; set; }
+ internal int? Value2 { get; set; }
}
}",
@@ -92,7 +92,7 @@ public partial interface IC
///
/// The value.
///
- int Value { get; }
+ int? Value { get; }
}
}"
}
diff --git a/src/Json.Schema.ToDotNet.UnitTests/Hints/PropertyModifiersHintTests.cs b/src/Json.Schema.ToDotNet.UnitTests/Hints/PropertyModifiersHintTests.cs
index 94d6ca9..be2df72 100644
--- a/src/Json.Schema.ToDotNet.UnitTests/Hints/PropertyModifiersHintTests.cs
+++ b/src/Json.Schema.ToDotNet.UnitTests/Hints/PropertyModifiersHintTests.cs
@@ -43,7 +43,7 @@ namespace N
public partial class C
{
[DataMember(Name = ""theProperty"", IsRequired = false, EmitDefaultValue = false)]
- int TheProperty { get; set; }
+ int? TheProperty { get; set; }
}
}"
),
@@ -83,7 +83,7 @@ namespace N
public partial class C
{
[DataMember(Name = ""theProperty"", IsRequired = false, EmitDefaultValue = false)]
- internal int TheProperty { get; set; }
+ internal int? TheProperty { get; set; }
}
}"
),
@@ -124,7 +124,7 @@ namespace N
public partial class C
{
[DataMember(Name = ""theProperty"", IsRequired = false, EmitDefaultValue = false)]
- internal override int TheProperty { get; set; }
+ internal override int? TheProperty { get; set; }
}
}"
),
@@ -194,7 +194,7 @@ namespace N
public partial class C
{
[DataMember(Name = ""theProperty"", IsRequired = false, EmitDefaultValue = false)]
- internal override int TheProperty { get; set; }
+ internal override int? TheProperty { get; set; }
}
}"
),
diff --git a/src/Json.Schema.ToDotNet/PropertyInfoDictionary.cs b/src/Json.Schema.ToDotNet/PropertyInfoDictionary.cs
index 45ac8c3..464b7e9 100644
--- a/src/Json.Schema.ToDotNet/PropertyInfoDictionary.cs
+++ b/src/Json.Schema.ToDotNet/PropertyInfoDictionary.cs
@@ -44,6 +44,9 @@ public class PropertyInfoDictionary : IReadOnlyDictionary
// Location{}[] is a dictionary property whose elements are arrays.
internal const string DictionaryMarker = "{}";
+ internal const string IntType = "System.int";
+ internal const string LongType = "System.long";
+ internal const string GuidType = "System.Guid";
internal const string BigIntegerType = "System.Numerics.BigInteger";
///
@@ -234,6 +237,10 @@ private void AddPropertyInfoFromPropertySchema(
bool isOfSchemaDefinedType = false;
int arrayRank = 0;
+ bool scalarValueTypeNullable = !isRequired && propertySchema.Default == null;
+ HashKind scalarValueTypeHashKind =
+ scalarValueTypeNullable ? HashKind.ScalarReferenceType : HashKind.ScalarValueType;
+
if (propertySchema.IsDateTime())
{
comparisonKind = ComparisonKind.OperatorEquals;
@@ -251,9 +258,9 @@ private void AddPropertyInfoFromPropertySchema(
else if (propertySchema.IsUuid())
{
comparisonKind = ComparisonKind.OperatorEquals;
- hashKind = isRequired ? HashKind.ScalarValueType : HashKind.ScalarReferenceType;
+ hashKind = scalarValueTypeHashKind;
initializationKind = InitializationKind.SimpleAssign;
- type = MakeNamedType(isRequired ? "System.Guid" : "System.Guid?", out namespaceName);
+ type = MakeNamedType(GuidType, out namespaceName, scalarValueTypeNullable);
}
else if (propertySchema.ShouldBeDictionary(_typeName, schemaPropertyName, _hintDictionary, out DictionaryHint dictionaryHint))
{
@@ -280,7 +287,7 @@ private void AddPropertyInfoFromPropertySchema(
initializationKind = InitializationKind.SimpleAssign;
type = MakeNamedType(referencedEnumTypeName, out namespaceName);
}
- else if (propertySchema.ShouldBeEnum(_typeName, schemaPropertyName, _hintDictionary, out EnumHint enumHint))
+ else if (propertySchema.ShouldBeEnum(_typeName, schemaPropertyName, _hintDictionary, out EnumHint enumHint))
{
comparisonKind = ComparisonKind.OperatorEquals;
hashKind = HashKind.ScalarValueType;
@@ -313,14 +320,15 @@ private void AddPropertyInfoFromPropertySchema(
case SchemaType.Integer:
comparisonKind = ComparisonKind.OperatorEquals;
- hashKind = HashKind.ScalarValueType;
+ hashKind = scalarValueTypeHashKind;
initializationKind = InitializationKind.SimpleAssign;
type = MakeProperIntegerType(_generateJsonIntegerAs,
propertySchema.Minimum, propertySchema.ExclusiveMinimum,
propertySchema.Maximum, propertySchema.ExclusiveMaximum,
+ scalarValueTypeNullable,
out namespaceName);
break;
-
+
case SchemaType.String:
comparisonKind = ComparisonKind.OperatorEquals;
hashKind = HashKind.ScalarReferenceType;
@@ -445,8 +453,13 @@ private TypeSyntax MakeObjectTypeFromReference(UriOrFragment reference, out stri
return MakeNamedType(className, out namespaceName);
}
- private TypeSyntax MakeNamedType(string typeName, out string namespaceName)
+ private TypeSyntax MakeNamedType(string typeName, out string namespaceName, bool nullable = false)
{
+ if (nullable)
+ {
+ typeName += "?";
+ }
+
string unqualifiedTypeName = GetUnqualifiedTypeName(typeName, out namespaceName);
return SyntaxFactory.ParseTypeName(unqualifiedTypeName);
@@ -630,7 +643,9 @@ private string GetUnqualifiedTypeName(string typeName, out string namespaceName)
namespaceName = null;
}
- return unqualifiedTypeName.ToPascalCase();
+ namespaceName = namespaceName == "System" ? null : namespaceName;
+
+ return typeName.StartsWith("System.") ? unqualifiedTypeName : unqualifiedTypeName.ToPascalCase();
}
private void OnAdditionalTypeRequired(CodeGenHint hint, JsonSchema schema)
@@ -687,6 +702,7 @@ private TypeSyntax GetConcreteType(string propertyName, string interfaceName)
internal TypeSyntax MakeProperIntegerType(GenerateJsonIntegerOption generateJsonIntegerAs,
double? minimum, bool? exclusiveMinimum,
double? maximum, bool? exclusiveMaximum,
+ bool nullable,
out string namespaceName)
{
namespaceName = null;
@@ -694,13 +710,13 @@ internal TypeSyntax MakeProperIntegerType(GenerateJsonIntegerOption generateJson
switch (generateJsonIntegerAs)
{
case GenerateJsonIntegerOption.Long:
- return SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.LongKeyword));
+ return MakeNamedType(LongType, out namespaceName, nullable);
case GenerateJsonIntegerOption.BigInteger:
- return MakeNamedType(BigIntegerType, out namespaceName);
+ return MakeNamedType(BigIntegerType, out namespaceName, nullable);
case GenerateJsonIntegerOption.Auto:
if (!minimum.HasValue || !maximum.HasValue)
{
- return MakeNamedType(BigIntegerType, out namespaceName);
+ return MakeNamedType(BigIntegerType, out namespaceName, nullable);
}
if (exclusiveMinimum == true)
@@ -720,12 +736,12 @@ internal TypeSyntax MakeProperIntegerType(GenerateJsonIntegerOption generateJson
if (minimum.Value < int.MinValue || maximum.Value > int.MaxValue)
{
- return SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.LongKeyword));
+ return MakeNamedType(LongType, out namespaceName, nullable);
}
- return SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.IntKeyword));
+ return MakeNamedType(IntType, out namespaceName, nullable);
default:
- return SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.IntKeyword));
+ return MakeNamedType(IntType, out namespaceName, nullable);
}
}
diff --git a/src/ReleaseHistory.md b/src/ReleaseHistory.md
index 1c7e697..697ca83 100644
--- a/src/ReleaseHistory.md
+++ b/src/ReleaseHistory.md
@@ -1,6 +1,7 @@
# Microsoft Json Schema Packages
## **Unreleased**
+* BREAKING: .NET type to express Json integers now will be nullable if the property is not required and also without default. [#167](https://github.com/microsoft/jschema/pull/167)
* FEATURE: Add new option for specifying .NET type to express Json numbers: `--generate-json-number-as = double | float | decimal` with a default of `double`. [#166](https://github.com/microsoft/jschema/pull/166)
## **2.1.0** [Pointer](https://www.nuget.org/packages/Microsoft.Json.Pointer/2.1.0) | [Schema](https://www.nuget.org/packages/Microsoft.Json.Schema/2.1.0)| [Schema.ToDotNet](https://www.nuget.org/packages/Microsoft.Json.Schema.ToDotNet/2.1.0)| [Schema.Validation](https://www.nuget.org/packages/Microsoft.Json.Schema.Validation/2.1.0)