Skip to content

Commit

Permalink
Name clash on source generation (#65)
Browse files Browse the repository at this point in the history
* Initial prototype using dfs and checking for cycles

* Polish debugging and fix tests

* Update tests and change accessibility for variables for sourcegenerator helper

* Add unit tests for diagnostics for users

* Move info logs to only successful generation

* Get rid of cycle check for next pr

* Get rid of stack logic for cycle checks

* Cache property values for typewrapper methods

* Added more collections relying on T

* Fix tests along with adding dicitonary support

* Changes on ds and tests to handle name clashes

* Update nits and got rid of stacks and queues

* Make tests static and delete todos

* Delete todos for dictionary types json context

* Move test classes to its own file
  • Loading branch information
kevinwkt authored Aug 26, 2020
1 parent bf5a0a1 commit 5ca701f
Show file tree
Hide file tree
Showing 9 changed files with 324 additions and 124 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,78 +10,15 @@

namespace System.Text.Json.SourceGeneration.Tests
{
[JsonSerializable]
public class Location
{
public int Id { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string PostalCode { get; set; }
public string Name { get; set; }
public string PhoneNumber { get; set; }
public string Country { get; set; }
}

[JsonSerializable]
public class ActiveOrUpcomingEvent
{
public int Id { get; set; }
public string ImageUrl { get; set; }
public string Name { get; set; }
public string CampaignName { get; set; }
public string CampaignManagedOrganizerName { get; set; }
public string Description { get; set; }
public DateTimeOffset StartDate { get; set; }
public DateTimeOffset EndDate { get; set; }
}

[JsonSerializable]
public class CampaignSummaryViewModel
{
public int Id { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string ImageUrl { get; set; }
public string OrganizationName { get; set; }
public string Headline { get; set; }
}

[JsonSerializable]
public class IndexViewModel
{
public List<ActiveOrUpcomingEvent> ActiveOrUpcomingEvents { get; set; }
public CampaignSummaryViewModel FeaturedCampaign { get; set; }
public bool IsNewAccount { get; set; }
public bool HasFeaturedCampaign => FeaturedCampaign != null;
}

[JsonSerializable]
public class WeatherForecastWithPOCOs
{
public DateTimeOffset Date { get; set; }
public int TemperatureCelsius { get; set; }
public string Summary { get; set; }
public string SummaryField;
public List<DateTimeOffset> DatesAvailable { get; set; }
public Dictionary<string, HighLowTemps> TemperatureRanges { get; set; }
public string[] SummaryWords { get; set; }
}

public class HighLowTemps
{
public int High { get; set; }
public int Low { get; set; }
}

public static class JsonSerializerSourceGeneratorTests
{
[Fact]
public static void RoundTripLocation()
{
Location expected = CreateLocation();

// Location is renamed to SystemTextJsonSourceGenerationTestsLocation given there is another type with the name Location.
// Warning to the user is displayed with this detailed at compile time.
string json = JsonSerializer.Serialize(expected, JsonContext.Instance.SystemTextJsonSourceGenerationTestsLocation);
Location obj = JsonSerializer.Deserialize(json, JsonContext.Instance.SystemTextJsonSourceGenerationTestsLocation);

Expand All @@ -93,8 +30,8 @@ public static void RoundTripIndexViewModel()
{
IndexViewModel expected = CreateIndexViewModel();

string json = JsonSerializer.Serialize(expected, JsonContext.Instance.SystemTextJsonSourceGenerationTestsIndexViewModel);
IndexViewModel obj = JsonSerializer.Deserialize(json, JsonContext.Instance.SystemTextJsonSourceGenerationTestsIndexViewModel);
string json = JsonSerializer.Serialize(expected, JsonContext.Instance.IndexViewModel);
IndexViewModel obj = JsonSerializer.Deserialize(json, JsonContext.Instance.IndexViewModel);

VerifyIndexViewModel(expected, obj);
}
Expand All @@ -104,8 +41,8 @@ public static void RoundTripCampaignSummaryViewModel()
{
CampaignSummaryViewModel expected = CreateCampaignSummaryViewModel();

string json = JsonSerializer.Serialize(expected, JsonContext.Instance.SystemTextJsonSourceGenerationTestsCampaignSummaryViewModel);
CampaignSummaryViewModel obj = JsonSerializer.Deserialize(json, JsonContext.Instance.SystemTextJsonSourceGenerationTestsCampaignSummaryViewModel);
string json = JsonSerializer.Serialize(expected, JsonContext.Instance.CampaignSummaryViewModel);
CampaignSummaryViewModel obj = JsonSerializer.Deserialize(json, JsonContext.Instance.CampaignSummaryViewModel);

VerifyCampaignSummaryViewModel(expected, obj);
}
Expand All @@ -115,8 +52,8 @@ public static void RoundTripActiveOrUpcomingEvent()
{
ActiveOrUpcomingEvent expected = CreateActiveOrUpcomingEvent();

string json = JsonSerializer.Serialize(expected, JsonContext.Instance.SystemTextJsonSourceGenerationTestsActiveOrUpcomingEvent);
ActiveOrUpcomingEvent obj = JsonSerializer.Deserialize(json, JsonContext.Instance.SystemTextJsonSourceGenerationTestsActiveOrUpcomingEvent);
string json = JsonSerializer.Serialize(expected, JsonContext.Instance.ActiveOrUpcomingEvent);
ActiveOrUpcomingEvent obj = JsonSerializer.Deserialize(json, JsonContext.Instance.ActiveOrUpcomingEvent);

VerifyActiveOrUpcomingEvent(expected, obj);
}
Expand All @@ -126,12 +63,23 @@ public static void RoundTripCollectionsDictionary()
{
WeatherForecastWithPOCOs expected = CreateWeatherForecastWithPOCOs();

string json = JsonSerializer.Serialize(expected, JsonContext.Instance.SystemTextJsonSourceGenerationTestsWeatherForecastWithPOCOs);
WeatherForecastWithPOCOs obj = JsonSerializer.Deserialize(json, JsonContext.Instance.SystemTextJsonSourceGenerationTestsWeatherForecastWithPOCOs);
string json = JsonSerializer.Serialize(expected, JsonContext.Instance.WeatherForecastWithPOCOs);
WeatherForecastWithPOCOs obj = JsonSerializer.Deserialize(json, JsonContext.Instance.WeatherForecastWithPOCOs);

VerifyWeatherForecastWithPOCOs(expected, obj);
}

[Fact]
public static void RoundTripTypeNameClash()
{
RepeatedTypes.Location expected = CreateRepeatedLocation();

string json = JsonSerializer.Serialize(expected, JsonContext.Instance.Location);
RepeatedTypes.Location obj = JsonSerializer.Deserialize(json, JsonContext.Instance.Location);

VerifyRepeatedLocation(expected, obj);
}

internal static Location CreateLocation()
{
return new Location
Expand Down Expand Up @@ -312,5 +260,32 @@ internal static void VerifyWeatherForecastWithPOCOs(WeatherForecastWithPOCOs exp
Assert.Equal(expected.SummaryWords[i], obj.SummaryWords[i]);
}
}

internal static RepeatedTypes.Location CreateRepeatedLocation()
{
return new RepeatedTypes.Location
{
FakeId = 1234,
FakeAddress1 = "The Street Name",
FakeAddress2 = "20/11",
FakeCity = "The City",
FakeState = "The State",
FakePostalCode = "abc-12",
FakeName = "Nonexisting",
FakePhoneNumber = "+0 11 222 333 44",
FakeCountry = "The Greatest"
};
}
internal static void VerifyRepeatedLocation(RepeatedTypes.Location expected, RepeatedTypes.Location obj)
{
Assert.Equal(expected.FakeAddress1, obj.FakeAddress1);
Assert.Equal(expected.FakeAddress2, obj.FakeAddress2);
Assert.Equal(expected.FakeCity, obj.FakeCity);
Assert.Equal(expected.FakeState, obj.FakeState);
Assert.Equal(expected.FakePostalCode, obj.FakePostalCode);
Assert.Equal(expected.FakeName, obj.FakeName);
Assert.Equal(expected.FakePhoneNumber, obj.FakePhoneNumber);
Assert.Equal(expected.FakeCountry, obj.FakeCountry);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
</ItemGroup>

<ItemGroup>
<Compile Include="TestClasses.cs" />
<Compile Include="JsonSourceGeneratorTests.cs" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.Json.Serialization;

namespace System.Text.Json.SourceGeneration.Tests.RepeatedTypes
{
[JsonSerializable]
public class Location
{
public int FakeId { get; set; }
public string FakeAddress1 { get; set; }
public string FakeAddress2 { get; set; }
public string FakeCity { get; set; }
public string FakeState { get; set; }
public string FakePostalCode { get; set; }
public string FakeName { get; set; }
public string FakePhoneNumber { get; set; }
public string FakeCountry { get; set; }
}
}

namespace System.Text.Json.SourceGeneration.Tests
{
[JsonSerializable]
public class Location
{
public int Id { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string PostalCode { get; set; }
public string Name { get; set; }
public string PhoneNumber { get; set; }
public string Country { get; set; }
}

[JsonSerializable]
public class ActiveOrUpcomingEvent
{
public int Id { get; set; }
public string ImageUrl { get; set; }
public string Name { get; set; }
public string CampaignName { get; set; }
public string CampaignManagedOrganizerName { get; set; }
public string Description { get; set; }
public DateTimeOffset StartDate { get; set; }
public DateTimeOffset EndDate { get; set; }
}

[JsonSerializable]
public class CampaignSummaryViewModel
{
public int Id { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string ImageUrl { get; set; }
public string OrganizationName { get; set; }
public string Headline { get; set; }
}

[JsonSerializable]
public class IndexViewModel
{
public List<ActiveOrUpcomingEvent> ActiveOrUpcomingEvents { get; set; }
public CampaignSummaryViewModel FeaturedCampaign { get; set; }
public bool IsNewAccount { get; set; }
public bool HasFeaturedCampaign => FeaturedCampaign != null;
}

[JsonSerializable]
public class WeatherForecastWithPOCOs
{
public DateTimeOffset Date { get; set; }
public int TemperatureCelsius { get; set; }
public string Summary { get; set; }
public string SummaryField;
public List<DateTimeOffset> DatesAvailable { get; set; }
public Dictionary<string, HighLowTemps> TemperatureRanges { get; set; }
public string[] SummaryWords { get; set; }
}

public class HighLowTemps
{
public int High { get; set; }
public int Low { get; set; }
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ public class CampaignSummaryViewModel
public static Compilation CreateActiveOrUpcomingEventCompilation()
{
string source = @"
using System;
namespace ReferencedAssembly
{
public class ActiveOrUpcomingEvent
Expand Down Expand Up @@ -149,5 +150,50 @@ public class HighLowTemps

return CreateCompilation(source);
}

public static Compilation CreateRepeatedLocationsCompilation()
{
string source = @"
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text.Json.Serialization;
namespace Fake
{
[JsonSerializable]
public class Location
{
public int FakeId { get; set; }
public string FakeAddress1 { get; set; }
public string FakeAddress2 { get; set; }
public string FakeCity { get; set; }
public string FakeState { get; set; }
public string FakePostalCode { get; set; }
public string FakeName { get; set; }
public string FakePhoneNumber { get; set; }
public string FakeCountry { get; set; }
}
}
namespace HelloWorld
{
[JsonSerializable]
public class Location
{
public int Id { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string PostalCode { get; set; }
public string Name { get; set; }
public string PhoneNumber { get; set; }
public string Country { get; set; }
}
}";

return CreateCompilation(source);
}
}
}
Loading

0 comments on commit 5ca701f

Please sign in to comment.