Skip to content

Commit

Permalink
Move to System.Text.Json (loic-sharma#559)
Browse files Browse the repository at this point in the history
Replaces Newtonsoft.Json with System.Text.Json. This will simplifies the route-to-code migration (see loic-sharma#558).
  • Loading branch information
loic-sharma authored Jul 22, 2020
1 parent 58e00e8 commit 9c16b00
Show file tree
Hide file tree
Showing 69 changed files with 1,012 additions and 660 deletions.
2 changes: 1 addition & 1 deletion .azure/pipelines/ci-official.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ jobs:
command: custom
workingDir: src/BaGet.UI
customCommand: run build

- task: Npm@1
displayName: Test frontend
inputs:
Expand Down
15 changes: 7 additions & 8 deletions src/BaGet.Core/Metadata/BaGetPackageMetadata.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Collections.Generic;
using System.Text.Json.Serialization;
using BaGet.Protocol.Models;
using Newtonsoft.Json;

namespace BaGet.Core
{
Expand All @@ -10,26 +10,25 @@ namespace BaGet.Core
/// </summary>
public class BaGetPackageMetadata : PackageMetadata
{
[JsonProperty("downloads")]
[JsonPropertyName("downloads")]
public long Downloads { get; set; }

[JsonProperty("hasReadme")]
[JsonPropertyName("hasReadme")]
public bool HasReadme { get; set; }

[JsonProperty("packageTypes")]
[JsonPropertyName("packageTypes")]
public IReadOnlyList<string> PackageTypes { get; set; }

/// <summary>
/// The package's release notes.
/// </summary>
[JsonProperty("releaseNotes")]
[JsonPropertyName("releaseNotes")]
public string ReleaseNotes { get; set; }

[JsonProperty("repositoryUrl")]
[JsonPropertyName("repositoryUrl")]
public string RepositoryUrl { get; set; }

[JsonProperty("repositoryType")]
[JsonPropertyName("repositoryType")]
public string RepositoryType { get; set; }

}
}
39 changes: 39 additions & 0 deletions src/BaGet.Core/Metadata/BaGetRegistrationIndexPage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Collections.Generic;
using System.Text.Json.Serialization;
using BaGet.Protocol.Models;

namespace BaGet.Core
{
/// <summary>
/// BaGet's extensions to a registration index page.
/// Extends <see cref="RegistrationIndexPage"/>.
/// </summary>
/// <remarks>
/// TODO: After this project is updated to .NET 5, make <see cref="BaGetRegistrationIndexPage"/>
/// extend <see cref="RegistrationIndexPage"/> and remove identical properties.
/// Properties that are modified should be marked with the "new" modified.
/// See: https://github.com/dotnet/runtime/pull/32107
/// </remarks>
public class BaGetRegistrationIndexPage
{
#region Original properties from RegistrationIndexPage.
[JsonPropertyName("@id")]
public string RegistrationPageUrl { get; set; }

[JsonPropertyName("count")]
public int Count { get; set; }

[JsonPropertyName("lower")]
public string Lower { get; set; }

[JsonPropertyName("upper")]
public string Upper { get; set; }
#endregion

/// <summary>
/// This was modified to use BaGet's extended registration index page item model.
/// </summary>
[JsonPropertyName("items")]
public IReadOnlyList<BaGetRegistrationIndexPageItem> ItemsOrNull { get; set; }
}
}
33 changes: 33 additions & 0 deletions src/BaGet.Core/Metadata/BaGetRegistrationIndexPageItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Text.Json.Serialization;
using BaGet.Protocol.Models;

namespace BaGet.Core
{
/// <summary>
/// BaGet's extensions to a registration index page.
/// Extends <see cref="RegistrationIndexPageItem"/>.
/// </summary>
/// <remarks>
/// TODO: After this project is updated to .NET 5, make <see cref="BaGetRegistrationIndexPageItem"/>
/// extend <see cref="RegistrationIndexPageItem"/> and remove identical properties.
/// Properties that are modified should be marked with the "new" modified.
/// See: https://github.com/dotnet/runtime/pull/32107
/// </remarks>
public class BaGetRegistrationIndexPageItem
{
#region Original properties from RegistrationIndexPageItem.
[JsonPropertyName("@id")]
public string RegistrationLeafUrl { get; set; }

[JsonPropertyName("packageContent")]
public string PackageContentUrl { get; set; }
#endregion

/// <summary>
/// The catalog entry containing the package metadata.
/// This was modified to use BaGet's extended package metadata model.
/// </summary>
[JsonPropertyName("catalogEntry")]
public BaGetPackageMetadata PackageMetadata { get; set; }
}
}
39 changes: 33 additions & 6 deletions src/BaGet.Core/Metadata/BaGetRegistrationIndexResponse.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,45 @@
using System.Collections.Generic;
using System.Text.Json.Serialization;
using BaGet.Protocol.Models;
using Newtonsoft.Json;

namespace BaGet.Core
{
/// <summary>
/// BaGet's extensions to a registration index response. These additions
/// are not part of the official protocol.
/// BaGet's extensions to a registration index response.
/// Extends <see cref="RegistrationIndexResponse"/>.
/// </summary>
public class BaGetRegistrationIndexResponse : RegistrationIndexResponse
/// <remarks>
/// TODO: After this project is updated to .NET 5, make <see cref="BaGetRegistrationIndexResponse"/>
/// extend <see cref="RegistrationIndexResponse"/> and remove identical properties.
/// Properties that are modified should be marked with the "new" modified.
/// See: https://github.com/dotnet/runtime/pull/32107
/// </remarks>
public class BaGetRegistrationIndexResponse
{
#region Original properties from RegistrationIndexResponse.
[JsonPropertyName("@id")]
public string RegistrationIndexUrl { get; set; }

[JsonPropertyName("@type")]
public IReadOnlyList<string> Type { get; set; }

[JsonPropertyName("count")]
public int Count { get; set; }
#endregion

/// <summary>
/// The pages that contain all of the versions of the package, ordered
/// by the package's version. This was modified to use BaGet's extended
/// registration index page model.
/// </summary>
[JsonPropertyName("items")]
public IReadOnlyList<BaGetRegistrationIndexPage> Pages { get; set; }

/// <summary>
/// How many times all versions of this package have been downloaded.
/// The package's total downloads across all versions.
/// This is not part of the official NuGet protocol.
/// </summary>
[JsonProperty("totalDownloads")]
[JsonPropertyName("totalDownloads")]
public long TotalDownloads { get; set; }
}
}
15 changes: 0 additions & 15 deletions src/BaGet.Core/Metadata/BaGetRegistrationLeafResponse.cs

This file was deleted.

3 changes: 2 additions & 1 deletion src/BaGet.Core/Metadata/DefaultPackageMetadataService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using BaGet.Protocol.Models;
using NuGet.Packaging.Core;
using NuGet.Versioning;

Expand Down Expand Up @@ -41,7 +42,7 @@ public async Task<BaGetRegistrationIndexResponse> GetRegistrationIndexOrNullAsyn
packages));
}

public async Task<BaGetRegistrationLeafResponse> GetRegistrationLeafOrNullAsync(
public async Task<RegistrationLeafResponse> GetRegistrationLeafOrNullAsync(
string id,
NuGetVersion version,
CancellationToken cancellationToken = default)
Expand Down
3 changes: 2 additions & 1 deletion src/BaGet.Core/Metadata/IPackageMetadataService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Threading;
using System.Threading.Tasks;
using BaGet.Protocol.Models;
using NuGet.Versioning;

namespace BaGet.Core
Expand Down Expand Up @@ -27,7 +28,7 @@ public interface IPackageMetadataService
/// <param name="packageVersion">The package's version.</param>
/// <param name="cancellationToken">A token to cancel the task.</param>
/// <returns>The registration leaf, or null if the package does not exist.</returns>
Task<BaGetRegistrationLeafResponse> GetRegistrationLeafOrNullAsync(
Task<RegistrationLeafResponse> GetRegistrationLeafOrNullAsync(
string packageId,
NuGetVersion packageVersion,
CancellationToken cancellationToken = default);
Expand Down
11 changes: 5 additions & 6 deletions src/BaGet.Core/Metadata/RegistrationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public virtual BaGetRegistrationIndexResponse BuildIndex(PackageRegistration reg
TotalDownloads = registration.Packages.Sum(p => p.Downloads),
Pages = new[]
{
new RegistrationIndexPage
new BaGetRegistrationIndexPage
{
RegistrationPageUrl = _url.GetRegistrationIndexUrl(registration.PackageId),
Count = registration.Packages.Count(),
Expand All @@ -41,25 +41,24 @@ public virtual BaGetRegistrationIndexResponse BuildIndex(PackageRegistration reg
};
}

public virtual BaGetRegistrationLeafResponse BuildLeaf(Package package)
public virtual RegistrationLeafResponse BuildLeaf(Package package)
{
var id = package.Id;
var version = package.Version;

return new BaGetRegistrationLeafResponse
return new RegistrationLeafResponse
{
Type = RegistrationLeafResponse.DefaultType,
Listed = package.Listed,
Downloads = package.Downloads,
Published = package.Published,
RegistrationLeafUrl = _url.GetRegistrationLeafUrl(id, version),
PackageContentUrl = _url.GetPackageDownloadUrl(id, version),
RegistrationIndexUrl = _url.GetRegistrationIndexUrl(id)
};
}

private RegistrationIndexPageItem ToRegistrationIndexPageItem(Package package) =>
new RegistrationIndexPageItem
private BaGetRegistrationIndexPageItem ToRegistrationIndexPageItem(Package package) =>
new BaGetRegistrationIndexPageItem
{
RegistrationLeafUrl = _url.GetRegistrationLeafUrl(package.Id, package.Version),
PackageContentUrl = _url.GetPackageDownloadUrl(package.Id, package.Version),
Expand Down
6 changes: 3 additions & 3 deletions src/BaGet.Core/Search/DependentsResponse.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Collections.Generic;
using Newtonsoft.Json;
using System.Text.Json.Serialization;

namespace BaGet.Core
{
Expand All @@ -12,13 +12,13 @@ public class DependentsResponse
/// <summary>
/// The total number of matches, disregarding skip and take.
/// </summary>
[JsonProperty("totalHits")]
[JsonPropertyName("totalHits")]
public long TotalHits { get; set; }

/// <summary>
/// The package IDs matched by the dependent query.
/// </summary>
[JsonProperty("data")]
[JsonPropertyName("data")]
public IReadOnlyList<string> Data { get; set; }
}
}
4 changes: 0 additions & 4 deletions src/BaGet.Hosting/BaGet.Hosting.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="$(MicrosoftAspNetCorePackageVersion)" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\BaGet.Core\BaGet.Core.csproj" />
</ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/BaGet.Hosting/Controllers/PackageMetadataController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public PackageMetadataController(IPackageMetadataService metadata)

// GET v3/registration/{id}.json
[HttpGet]
public async Task<ActionResult<RegistrationIndexResponse>> RegistrationIndexAsync(string id, CancellationToken cancellationToken)
public async Task<ActionResult<BaGetRegistrationIndexResponse>> RegistrationIndexAsync(string id, CancellationToken cancellationToken)
{
var index = await _metadata.GetRegistrationIndexOrNullAsync(id, cancellationToken);
if (index == null)
Expand Down
17 changes: 0 additions & 17 deletions src/BaGet.Hosting/Extensions/IEndpointRouteBuilderExtensions.cs

This file was deleted.

4 changes: 2 additions & 2 deletions src/BaGet.Hosting/Extensions/IServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ public static IServiceCollection AddBaGetWebApplication(
.AddControllers()
.AddApplicationPart(typeof(PackageContentController).Assembly)
.SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
.AddNewtonsoftJson(options =>
.AddJsonOptions(options =>
{
options.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Utc;
options.JsonSerializerOptions.IgnoreNullValues = true;
});

services.AddHttpContextAccessor();
Expand Down
2 changes: 1 addition & 1 deletion src/BaGet.Protocol/BaGet.Protocol.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="$(MicrosoftExtensionsPackageVersion)" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
<PackageReference Include="NuGet.Frameworks" Version="$(NuGetPackageVersion)" />
<PackageReference Include="NuGet.Versioning" Version="$(NuGetPackageVersion)" />
<PackageReference Include="System.Text.Json" Version="4.7.2" />
</ItemGroup>

</Project>
23 changes: 12 additions & 11 deletions src/BaGet.Protocol/Catalog/CatalogProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,18 +144,19 @@ private async Task<bool> ProcessLeafAsync(CatalogLeafItem leafItem, Cancellation
bool success;
try
{
switch (leafItem.Type)
if (leafItem.IsPackageDelete())
{
case CatalogLeafType.PackageDelete:
var packageDelete = await _client.GetPackageDeleteLeafAsync(leafItem.CatalogLeafUrl);
success = await _leafProcessor.ProcessPackageDeleteAsync(packageDelete, cancellationToken);
break;
case CatalogLeafType.PackageDetails:
var packageDetails = await _client.GetPackageDetailsLeafAsync(leafItem.CatalogLeafUrl);
success = await _leafProcessor.ProcessPackageDetailsAsync(packageDetails, cancellationToken);
break;
default:
throw new NotSupportedException($"The catalog leaf type '{leafItem.Type}' is not supported.");
var packageDelete = await _client.GetPackageDeleteLeafAsync(leafItem.CatalogLeafUrl);
success = await _leafProcessor.ProcessPackageDeleteAsync(packageDelete, cancellationToken);
}
else if (leafItem.IsPackageDetails())
{
var packageDetails = await _client.GetPackageDetailsLeafAsync(leafItem.CatalogLeafUrl);
success = await _leafProcessor.ProcessPackageDetailsAsync(packageDetails, cancellationToken);
}
else
{
throw new NotSupportedException($"The catalog leaf type '{leafItem.Type}' is not supported.");
}
}
catch (Exception exception)
Expand Down
Loading

0 comments on commit 9c16b00

Please sign in to comment.