public override void ClassWithNullableProperties_Roundtrip() { RunTest(new ClassWithNullableProperties { Uri = new Uri("http://contoso.com"), Array = new int[] { 42 }, Poco = new ClassWithNullableProperties.MyPoco(), NullableUri = new Uri("http://contoso.com"), NullableArray = new int[] { 42 }, NullablePoco = new ClassWithNullableProperties.MyPoco() }); RunTest(new ClassWithNullableProperties()); void RunTest(ClassWithNullableProperties expected) { string json = JsonSerializer.Serialize(expected, DefaultContext.ClassWithNullableProperties); ClassWithNullableProperties actual = JsonSerializer.Deserialize(json, ((ITestContext)MetadataWithPerTypeAttributeContext.Default).ClassWithNullableProperties); Assert.Equal(expected.Uri, actual.Uri); Assert.Equal(expected.Array, actual.Array); Assert.Equal(expected.Poco, actual.Poco); Assert.Equal(expected.NullableUri, actual.NullableUri); Assert.Equal(expected.NullableArray, actual.NullableArray); Assert.Equal(expected.NullablePoco, actual.NullablePoco); Assert.Equal(expected.NullableUriParameter, actual.NullableUriParameter); Assert.Equal(expected.NullableArrayParameter, actual.NullableArrayParameter); Assert.Equal(expected.NullablePocoParameter, actual.NullablePocoParameter); } }
internal void Should_disable_filling_nullable_fields_and_properties(BaseRecursiveTransformFactory factory) { var transform = factory.GetTransform <ClassWithNullableProperties>(); var instance = new ClassWithNullableProperties(); transform.ApplyTo(instance); instance.NullableByteProperty.Should().BeNull(); instance.NullableShortProperty.Should().BeNull(); instance.NullableUShortProperty.Should().BeNull(); instance.NullableIntProperty.Should().BeNull(); instance.NullableUIntProperty.Should().BeNull(); instance.NullableLongProperty.Should().BeNull(); instance.NullableULongProperty.Should().BeNull(); instance.NullableFloatProperty.Should().BeNull(); instance.NullableDoubleProperty.Should().BeNull(); instance.NullableDecimalProperty.Should().BeNull(); instance.NullableDateTimeProperty.Should().BeNull(); }
public void AllFieldsAreNullableForDifferentReasons() { string dummy; AssignedNull = null; DetectedOnlyOnce = null; if (ComparedEqualToNullOnLeft == null) { return; } if (null == ComparedEqualToNullOnRight) { return; } if (ComparedNotEqualToNullOnLeft != null) { return; } if (null != ComparedNotEqualToNullOnRight) { return; } if (DetectedOnlyOnce == null) { return; } if (null == DetectedOnlyOnce) { return; } AssignedResultOfAsOperator = AssignedNull as string; DetectedOnlyOnce = AssignedNull as string; UsedInConditionalAccess?.Length.ToString(); DetectedOnlyOnce?.Length.ToString(); dummy = UsedInCoalesceExpression ?? string.Empty; dummy = DetectedOnlyOnce ?? string.Empty; this.AssignedNullUsingThis = null; if (this.ComparedEqualToNullOnLeftUsingThis == null) { return; } if (null == this.ComparedEqualToNullOnRightUsingThis) { return; } if (this.ComparedNotEqualToNullOnLeftUsingThis != null) { return; } if (null != this.ComparedNotEqualToNullOnRightUsingThis) { return; } this.AssignedResultOfAsOperatorUsingThis = AssignedNull as string; this.UsedInConditionalAccessUsingThis?.Length.ToString(); dummy = this.UsedInCoalesceExpressionUsingThis ?? string.Empty; StaticAssignedNull = null; if (StaticComparedEqualToNullOnLeft == null) { return; } if (null == StaticComparedEqualToNullOnRight) { return; } if (StaticComparedNotEqualToNullOnLeft != null) { return; } if (null != StaticComparedNotEqualToNullOnRight) { return; } StaticAssignedResultOfAsOperator = AssignedNull as string; StaticUsedInConditionalAccess?.Length.ToString(); dummy = StaticUsedInCoalesceExpression ?? string.Empty; ClassPropertyCanBeDeclaredAsNullable.StaticAssignedNullUsingClassName = null; if (ClassPropertyCanBeDeclaredAsNullable.StaticComparedEqualToNullOnLeftUsingClassName == null) { return; } if (null == ClassPropertyCanBeDeclaredAsNullable.StaticComparedEqualToNullOnRightUsingClassName) { return; } if (ClassPropertyCanBeDeclaredAsNullable.StaticComparedNotEqualToNullOnLeftUsingClassName != null) { return; } if (null != ClassPropertyCanBeDeclaredAsNullable.StaticComparedNotEqualToNullOnRightUsingClassName) { return; } ClassPropertyCanBeDeclaredAsNullable.StaticAssignedResultOfAsOperatorUsingClassName = AssignedNull as string; ClassPropertyCanBeDeclaredAsNullable.StaticUsedInConditionalAccessUsingClassName?.Length.ToString(); dummy = ClassPropertyCanBeDeclaredAsNullable.StaticUsedInCoalesceExpressionUsingClassName ?? string.Empty; var classWithProperties = new ClassWithNullableProperties(); classWithProperties.AssignedNull = null; if (classWithProperties.ComparedEqualToNullOnLeft == null) { return; } if (null == classWithProperties.ComparedEqualToNullOnRight) { return; } if (classWithProperties.ComparedNotEqualToNullOnLeft != null) { return; } if (null != classWithProperties.ComparedNotEqualToNullOnRight) { return; } classWithProperties.AssignedResultOfAsOperator = AssignedNull as string; classWithProperties.UsedInConditionalAccess?.Length.ToString(); dummy = classWithProperties.UsedInCoalesceExpression ?? string.Empty; ClassWithNullableProperties.StaticAssignedNull = null; if (ClassWithNullableProperties.StaticComparedEqualToNullOnLeft == null) { return; } if (null == ClassWithNullableProperties.StaticComparedEqualToNullOnRight) { return; } if (ClassWithNullableProperties.StaticComparedNotEqualToNullOnLeft != null) { return; } if (null != ClassWithNullableProperties.StaticComparedNotEqualToNullOnRight) { return; } ClassWithNullableProperties.StaticAssignedResultOfAsOperator = AssignedNull as string; ClassWithNullableProperties.StaticUsedInConditionalAccess?.Length.ToString(); dummy = ClassWithNullableProperties.StaticUsedInCoalesceExpression ?? string.Empty; var anotherClassWithFields = new ClassWithNullableProperties { IntializedToDefaultInInitializationList = default,