Skip to content

Commit

Permalink
STJ: Fix deserialization of UInt16 properties (#56793)
Browse files Browse the repository at this point in the history
Fix #56792
  • Loading branch information
jasper-d committed Aug 3, 2021
1 parent f598846 commit a33ea98
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1051,7 +1051,7 @@ private void PopulateNumberTypes()
_numberTypes.Add(ResolveType(SpecialType.System_Int32));
_numberTypes.Add(ResolveType(SpecialType.System_Int64));
_numberTypes.Add(ResolveType(SpecialType.System_Single));
_numberTypes.Add(ResolveType(SpecialType.System_UInt64));
_numberTypes.Add(ResolveType(SpecialType.System_UInt16));
_numberTypes.Add(ResolveType(SpecialType.System_UInt32));
_numberTypes.Add(ResolveType(SpecialType.System_UInt64));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace System.Text.Json.SourceGeneration.Tests
public interface ITestContext
{
public JsonTypeInfo<Location> Location { get; }
public JsonTypeInfo<NumberTypes> NumberTypes { get; }
public JsonTypeInfo<RepeatedTypes.Location> RepeatedLocation { get; }
public JsonTypeInfo<ActiveOrUpcomingEvent> ActiveOrUpcomingEvent { get; }
public JsonTypeInfo<CampaignSummaryViewModel> CampaignSummaryViewModel { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace System.Text.Json.SourceGeneration.Tests
{
[JsonSerializable(typeof(Location))]
[JsonSerializable(typeof(RepeatedTypes.Location), TypeInfoPropertyName = "RepeatedLocation")]
[JsonSerializable(typeof(NumberTypes))]
[JsonSerializable(typeof(ActiveOrUpcomingEvent))]
[JsonSerializable(typeof(CampaignSummaryViewModel))]
[JsonSerializable(typeof(IndexViewModel))]
Expand Down Expand Up @@ -38,6 +39,7 @@ public sealed class MetadataAndSerializationContextTests : RealWorldContextTests
public override void EnsureFastPathGeneratedAsExpected()
{
Assert.NotNull(MetadataAndSerializationContext.Default.Location.Serialize);
Assert.NotNull(MetadataAndSerializationContext.Default.NumberTypes.Serialize);
Assert.NotNull(MetadataAndSerializationContext.Default.RepeatedLocation.Serialize);
Assert.NotNull(MetadataAndSerializationContext.Default.ActiveOrUpcomingEvent.Serialize);
Assert.NotNull(MetadataAndSerializationContext.Default.CampaignSummaryViewModel.Serialize);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace System.Text.Json.SourceGeneration.Tests
{
[JsonSerializable(typeof(Location), GenerationMode = JsonSourceGenerationMode.Metadata)]
[JsonSerializable(typeof(RepeatedTypes.Location), TypeInfoPropertyName = "RepeatedLocation", GenerationMode = JsonSourceGenerationMode.Metadata)]
[JsonSerializable(typeof(NumberTypes), GenerationMode = JsonSourceGenerationMode.Metadata)]
[JsonSerializable(typeof(ActiveOrUpcomingEvent), GenerationMode = JsonSourceGenerationMode.Metadata)]
[JsonSerializable(typeof(CampaignSummaryViewModel), GenerationMode = JsonSourceGenerationMode.Metadata)]
[JsonSerializable(typeof(IndexViewModel), GenerationMode = JsonSourceGenerationMode.Metadata)]
Expand Down Expand Up @@ -38,6 +39,7 @@ public override void EnsureFastPathGeneratedAsExpected()
{
Assert.Null(MetadataWithPerTypeAttributeContext.Default.Location.Serialize);
Assert.Null(MetadataWithPerTypeAttributeContext.Default.RepeatedLocation.Serialize);
Assert.Null(MetadataWithPerTypeAttributeContext.Default.NumberTypes.Serialize);
Assert.Null(MetadataWithPerTypeAttributeContext.Default.ActiveOrUpcomingEvent.Serialize);
Assert.Null(MetadataWithPerTypeAttributeContext.Default.CampaignSummaryViewModel.Serialize);
Assert.Null(MetadataWithPerTypeAttributeContext.Default.IndexViewModel.Serialize);
Expand All @@ -59,6 +61,7 @@ public override void EnsureFastPathGeneratedAsExpected()
[JsonSourceGenerationOptions(GenerationMode = JsonSourceGenerationMode.Metadata)]
[JsonSerializable(typeof(Location))]
[JsonSerializable(typeof(RepeatedTypes.Location), TypeInfoPropertyName = "RepeatedLocation")]
[JsonSerializable(typeof(NumberTypes))]
[JsonSerializable(typeof(ActiveOrUpcomingEvent))]
[JsonSerializable(typeof(CampaignSummaryViewModel))]
[JsonSerializable(typeof(IndexViewModel))]
Expand Down Expand Up @@ -89,6 +92,7 @@ public override void EnsureFastPathGeneratedAsExpected()
{
Assert.Null(MetadataContext.Default.Location.Serialize);
Assert.Null(MetadataContext.Default.RepeatedLocation.Serialize);
Assert.Null(MetadataContext.Default.NumberTypes.Serialize);
Assert.Null(MetadataContext.Default.ActiveOrUpcomingEvent.Serialize);
Assert.Null(MetadataContext.Default.CampaignSummaryViewModel.Serialize);
Assert.Null(MetadataContext.Default.IndexViewModel.Serialize);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace System.Text.Json.SourceGeneration.Tests
{
[JsonSerializable(typeof(Location), GenerationMode = JsonSourceGenerationMode.Metadata)]
[JsonSerializable(typeof(RepeatedTypes.Location), TypeInfoPropertyName = "RepeatedLocation", GenerationMode = JsonSourceGenerationMode.Serialization)]
[JsonSerializable(typeof(NumberTypes), GenerationMode = JsonSourceGenerationMode.Metadata)]
[JsonSerializable(typeof(ActiveOrUpcomingEvent), GenerationMode = JsonSourceGenerationMode.Metadata)]
[JsonSerializable(typeof(CampaignSummaryViewModel), GenerationMode = JsonSourceGenerationMode.Serialization)]
[JsonSerializable(typeof(IndexViewModel), GenerationMode = JsonSourceGenerationMode.Metadata)]
Expand Down Expand Up @@ -38,6 +39,7 @@ public override void EnsureFastPathGeneratedAsExpected()
{
Assert.Null(MixedModeContext.Default.Location.Serialize);
Assert.NotNull(MixedModeContext.Default.RepeatedLocation.Serialize);
Assert.Null(MixedModeContext.Default.NumberTypes.Serialize);
Assert.NotNull(MixedModeContext.Default.CampaignSummaryViewModel.Serialize);
Assert.Null(MixedModeContext.Default.IndexViewModel.Serialize);
Assert.Null(MixedModeContext.Default.WeatherForecastWithPOCOs.Serialize);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ public virtual void RoundTripLocation()
VerifyLocation(expected, obj);
}

[Fact]
public virtual void RoundTripNumberTypes()
{
NumberTypes expected = CreateNumberTypes();

string json = JsonSerializer.Serialize(expected, DefaultContext.NumberTypes);
NumberTypes obj = JsonSerializer.Deserialize(json, DefaultContext.NumberTypes);
VerifyNumberTypes(expected, obj);
}


[Fact]
public virtual void RoundTripIndexViewModel()
{
Expand Down Expand Up @@ -127,6 +138,39 @@ protected static void VerifyLocation(Location expected, Location obj)
Assert.Equal(expected.Country, obj.Country);
}

protected static NumberTypes CreateNumberTypes()
{
return new NumberTypes
{
Single = 1.1f,
Double = 2.2d,
Decimal = 3.3m,
SByte = -1,
Byte = 1,
UShort = 2,
Short = -2,
UInt = 3,
Int = -3,
ULong = 4,
Long = -4,
};
}

protected static void VerifyNumberTypes(NumberTypes expected, NumberTypes obj)
{
Assert.Equal(expected.Single, obj.Single);
Assert.Equal(expected.Double, obj.Double);
Assert.Equal(expected.Decimal, obj.Decimal);
Assert.Equal(expected.SByte, obj.SByte);
Assert.Equal(expected.Byte, obj.Byte);
Assert.Equal(expected.UShort, obj.UShort);
Assert.Equal(expected.Short, obj.Short);
Assert.Equal(expected.UInt, obj.UInt);
Assert.Equal(expected.Int, obj.Int);
Assert.Equal(expected.ULong, obj.ULong);
Assert.Equal(expected.Long, obj.Long);
}

protected static ActiveOrUpcomingEvent CreateActiveOrUpcomingEvent()
{
return new ActiveOrUpcomingEvent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace System.Text.Json.SourceGeneration.Tests
[JsonSourceGenerationOptions(GenerationMode = JsonSourceGenerationMode.Serialization)]
[JsonSerializable(typeof(Location))]
[JsonSerializable(typeof(RepeatedTypes.Location), TypeInfoPropertyName = "RepeatedLocation")]
[JsonSerializable(typeof(NumberTypes))]
[JsonSerializable(typeof(ActiveOrUpcomingEvent))]
[JsonSerializable(typeof(CampaignSummaryViewModel))]
[JsonSerializable(typeof(IndexViewModel))]
Expand All @@ -32,6 +33,7 @@ internal partial class SerializationContext : JsonSerializerContext, ITestContex

[JsonSerializable(typeof(Location), GenerationMode = JsonSourceGenerationMode.Serialization)]
[JsonSerializable(typeof(RepeatedTypes.Location), GenerationMode = JsonSourceGenerationMode.Serialization, TypeInfoPropertyName = "RepeatedLocation")]
[JsonSerializable(typeof(NumberTypes), GenerationMode = JsonSourceGenerationMode.Serialization)]
[JsonSerializable(typeof(ActiveOrUpcomingEvent), GenerationMode = JsonSourceGenerationMode.Serialization)]
[JsonSerializable(typeof(CampaignSummaryViewModel), GenerationMode = JsonSourceGenerationMode.Serialization)]
[JsonSerializable(typeof(IndexViewModel), GenerationMode = JsonSourceGenerationMode.Serialization)]
Expand All @@ -56,6 +58,7 @@ internal partial class SerializationWithPerTypeAttributeContext : JsonSerializer
[JsonSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase)]
[JsonSerializable(typeof(Location), GenerationMode = JsonSourceGenerationMode.Serialization)]
[JsonSerializable(typeof(RepeatedTypes.Location), GenerationMode = JsonSourceGenerationMode.Serialization, TypeInfoPropertyName = "RepeatedLocation")]
[JsonSerializable(typeof(NumberTypes), GenerationMode = JsonSourceGenerationMode.Serialization)]
[JsonSerializable(typeof(ActiveOrUpcomingEvent), GenerationMode = JsonSourceGenerationMode.Serialization)]
[JsonSerializable(typeof(CampaignSummaryViewModel), GenerationMode = JsonSourceGenerationMode.Serialization)]
[JsonSerializable(typeof(IndexViewModel), GenerationMode = JsonSourceGenerationMode.Serialization)]
Expand Down Expand Up @@ -91,6 +94,7 @@ public override void EnsureFastPathGeneratedAsExpected()
{
Assert.NotNull(SerializationContext.Default.Location.Serialize);
Assert.NotNull(SerializationContext.Default.RepeatedLocation.Serialize);
Assert.NotNull(SerializationContext.Default.NumberTypes.Serialize);
Assert.NotNull(SerializationContext.Default.ActiveOrUpcomingEvent.Serialize);
Assert.NotNull(SerializationContext.Default.CampaignSummaryViewModel.Serialize);
Assert.NotNull(SerializationContext.Default.IndexViewModel.Serialize);
Expand Down Expand Up @@ -124,6 +128,20 @@ public override void RoundTripLocation()
AssertFastPathLogicCorrect(json, obj, DefaultContext.Location);
}

[Fact]
public override void RoundTripNumberTypes()
{
NumberTypes expected = CreateNumberTypes();

string json = JsonSerializer.Serialize(expected, DefaultContext.NumberTypes);
JsonTestHelper.AssertThrows_PropMetadataInit(() => JsonSerializer.Deserialize(json, DefaultContext.NumberTypes), typeof(NumberTypes));

NumberTypes obj = JsonSerializer.Deserialize(json, ((ITestContext)MetadataWithPerTypeAttributeContext.Default).NumberTypes);
VerifyNumberTypes(expected, obj);

AssertFastPathLogicCorrect(json, obj, DefaultContext.NumberTypes);
}

[Fact]
public override void RoundTripIndexViewModel()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@ public class Location
public string Country { get; set; }
}

public class NumberTypes
{
public float Single { get; set; }
public double Double { get; set; }
public decimal Decimal { get; set; }
public sbyte SByte { get; set; }
public byte Byte { get; set; }
public ushort UShort { get; set; }
public short Short { get; set; }
public uint UInt { get; set; }
public int Int { get; set; }
public ulong ULong { get; set; }
public long Long { get; set; }
}

public class ActiveOrUpcomingEvent
{
public int Id { get; set; }
Expand Down

0 comments on commit a33ea98

Please sign in to comment.