From b62822365274c58cc67c5050cf22a066275a507e Mon Sep 17 00:00:00 2001 From: Martin Ruiz Date: Tue, 25 Jun 2024 18:15:31 -0700 Subject: [PATCH 01/17] Avoid using LoadJson and iterate by token --- .../PackageDependencyGroupConverter.cs | 53 ++++++++++++++----- 1 file changed, 41 insertions(+), 12 deletions(-) diff --git a/src/NuGet.Core/NuGet.Protocol/Converters/PackageDependencyGroupConverter.cs b/src/NuGet.Core/NuGet.Protocol/Converters/PackageDependencyGroupConverter.cs index fa2b6587317..ffe408fea03 100644 --- a/src/NuGet.Core/NuGet.Protocol/Converters/PackageDependencyGroupConverter.cs +++ b/src/NuGet.Core/NuGet.Protocol/Converters/PackageDependencyGroupConverter.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using System.Collections.Generic; using System.Linq; using Newtonsoft.Json; using Newtonsoft.Json.Linq; @@ -19,9 +20,47 @@ public class PackageDependencyGroupConverter : JsonConverter public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { - var set = JsonUtility.LoadJson(reader); + string fxName = null; + List packages = new List(); + while (reader.Read() && reader.TokenType != JsonToken.EndObject) + { + if (reader.TokenType.Equals(JsonToken.PropertyName)) + { + if (reader.Value.Equals(JsonProperties.Dependencies)) + { + // Dependencies are stored in an array + while (reader.Read() && reader.TokenType != JsonToken.EndArray) + { + string id = null; + string version = null; + while (reader.Read() && reader.TokenType != JsonToken.EndObject) + { + if (reader.TokenType.Equals(JsonToken.PropertyName) && reader.Value.Equals(JsonProperties.PackageId)) + { + reader.Read(); + id = reader.Value.ToString(); + } + if (reader.TokenType.Equals(JsonToken.PropertyName) && reader.Value.Equals(JsonProperties.Range)) + { + reader.Read(); + version = reader.Value.ToString(); + } + } - var fxName = set.Value(JsonProperties.TargetFramework); + if (id != null) + { + packages.Add(new Packaging.Core.PackageDependency(id, string.IsNullOrEmpty(version) ? null : VersionRange.Parse(version))); + } + } + reader.Read(); + } + if (reader.Value.Equals(JsonProperties.TargetFramework)) + { + reader.Read(); + fxName = reader.Value.ToString(); + } + } + } var framework = NuGetFramework.AnyFramework; @@ -31,19 +70,9 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist fxName = framework.GetShortFolderName(); } - var packages = (set[JsonProperties.Dependencies] as JArray ?? Enumerable.Empty()) - .Select(LoadDependency).ToList(); return new PackageDependencyGroup(framework, packages); } - private static Packaging.Core.PackageDependency LoadDependency(JToken dependency) - { - var ver = dependency.Value(JsonProperties.Range); - return new Packaging.Core.PackageDependency( - dependency.Value(JsonProperties.PackageId), - string.IsNullOrEmpty(ver) ? null : VersionRange.Parse(ver)); - } - public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { throw new NotImplementedException(); From 5299aceffa07764920cfa400ffb0d63b325b49fa Mon Sep 17 00:00:00 2001 From: Martin Ruiz Date: Tue, 25 Jun 2024 20:56:43 -0700 Subject: [PATCH 02/17] remove unnecesary imports --- .../Converters/PackageDependencyGroupConverter.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/NuGet.Core/NuGet.Protocol/Converters/PackageDependencyGroupConverter.cs b/src/NuGet.Core/NuGet.Protocol/Converters/PackageDependencyGroupConverter.cs index ffe408fea03..4646b358d3b 100644 --- a/src/NuGet.Core/NuGet.Protocol/Converters/PackageDependencyGroupConverter.cs +++ b/src/NuGet.Core/NuGet.Protocol/Converters/PackageDependencyGroupConverter.cs @@ -3,9 +3,7 @@ using System; using System.Collections.Generic; -using System.Linq; using Newtonsoft.Json; -using Newtonsoft.Json.Linq; using NuGet.Frameworks; using NuGet.Packaging; using NuGet.Versioning; From f050e963fe3c648e872365c6179c5eb7b98129be Mon Sep 17 00:00:00 2001 From: Martin Ruiz Date: Wed, 26 Jun 2024 16:51:11 -0700 Subject: [PATCH 03/17] fix token iteration --- .../Converters/PackageDependencyGroupConverter.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/NuGet.Core/NuGet.Protocol/Converters/PackageDependencyGroupConverter.cs b/src/NuGet.Core/NuGet.Protocol/Converters/PackageDependencyGroupConverter.cs index 4646b358d3b..1c1ab48e266 100644 --- a/src/NuGet.Core/NuGet.Protocol/Converters/PackageDependencyGroupConverter.cs +++ b/src/NuGet.Core/NuGet.Protocol/Converters/PackageDependencyGroupConverter.cs @@ -50,9 +50,9 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist packages.Add(new Packaging.Core.PackageDependency(id, string.IsNullOrEmpty(version) ? null : VersionRange.Parse(version))); } } - reader.Read(); + //reader.Read(); } - if (reader.Value.Equals(JsonProperties.TargetFramework)) + else if (reader.Value.Equals(JsonProperties.TargetFramework)) { reader.Read(); fxName = reader.Value.ToString(); From 92a5e08afdb83981cb0e01c20f690842750b6ef6 Mon Sep 17 00:00:00 2001 From: Martin Ruiz Date: Thu, 27 Jun 2024 11:38:33 -0700 Subject: [PATCH 04/17] remove comment --- .../NuGet.Protocol/Converters/PackageDependencyGroupConverter.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/NuGet.Core/NuGet.Protocol/Converters/PackageDependencyGroupConverter.cs b/src/NuGet.Core/NuGet.Protocol/Converters/PackageDependencyGroupConverter.cs index 1c1ab48e266..d20914b8dce 100644 --- a/src/NuGet.Core/NuGet.Protocol/Converters/PackageDependencyGroupConverter.cs +++ b/src/NuGet.Core/NuGet.Protocol/Converters/PackageDependencyGroupConverter.cs @@ -50,7 +50,6 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist packages.Add(new Packaging.Core.PackageDependency(id, string.IsNullOrEmpty(version) ? null : VersionRange.Parse(version))); } } - //reader.Read(); } else if (reader.Value.Equals(JsonProperties.TargetFramework)) { From 1cd48e32886136f2d90dd6f3d0cc213918ebf21c Mon Sep 17 00:00:00 2001 From: Martin Ruiz Date: Thu, 8 Aug 2024 15:04:15 -0700 Subject: [PATCH 05/17] Add converter to PackageDependency --- Benchmarks/Program.cs | 4 ++ .../Converters/PackageDependencyConverter.cs | 50 +++++++++++++++++++ .../PackageDependencyGroupConverter.cs | 25 ++-------- 3 files changed, 58 insertions(+), 21 deletions(-) create mode 100644 Benchmarks/Program.cs create mode 100644 src/NuGet.Core/NuGet.Protocol/Converters/PackageDependencyConverter.cs diff --git a/Benchmarks/Program.cs b/Benchmarks/Program.cs new file mode 100644 index 00000000000..38f36dbaa91 --- /dev/null +++ b/Benchmarks/Program.cs @@ -0,0 +1,4 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +Console.WriteLine("Hello, World!"); diff --git a/src/NuGet.Core/NuGet.Protocol/Converters/PackageDependencyConverter.cs b/src/NuGet.Core/NuGet.Protocol/Converters/PackageDependencyConverter.cs new file mode 100644 index 00000000000..d41fbff81ca --- /dev/null +++ b/src/NuGet.Core/NuGet.Protocol/Converters/PackageDependencyConverter.cs @@ -0,0 +1,50 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using Newtonsoft.Json; +using NuGet.Versioning; + +namespace NuGet.Protocol +{ + public class PackageDependencyConverter : JsonConverter + { + public override bool CanConvert(Type objectType) + { + return objectType == typeof(Packaging.Core.PackageDependency); + } + + public override bool CanWrite => false; + + public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) + { + string id = null; + string version = null; + + while (reader.Read() && reader.TokenType != JsonToken.EndObject) + { + if (reader.TokenType.Equals(JsonToken.PropertyName)) + { + if (reader.Value.Equals(JsonProperties.PackageId)) + { + id = reader.ReadAsString(); + } + if (reader.Value.Equals(JsonProperties.Range)) + { + version = id = reader.ReadAsString(); + } + } + } + if (id != null) + { + return new Packaging.Core.PackageDependency(id, string.IsNullOrEmpty(version) ? null : VersionRange.Parse(version)); + } + return null; + } + + public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) + { + throw new NotImplementedException(); + } + } +} diff --git a/src/NuGet.Core/NuGet.Protocol/Converters/PackageDependencyGroupConverter.cs b/src/NuGet.Core/NuGet.Protocol/Converters/PackageDependencyGroupConverter.cs index d20914b8dce..aeb3dbe256b 100644 --- a/src/NuGet.Core/NuGet.Protocol/Converters/PackageDependencyGroupConverter.cs +++ b/src/NuGet.Core/NuGet.Protocol/Converters/PackageDependencyGroupConverter.cs @@ -6,7 +6,6 @@ using Newtonsoft.Json; using NuGet.Frameworks; using NuGet.Packaging; -using NuGet.Versioning; namespace NuGet.Protocol { @@ -16,6 +15,8 @@ public class PackageDependencyGroupConverter : JsonConverter public override bool CanWrite => false; + private readonly PackageDependencyConverter _converter = new PackageDependencyConverter(); + public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { string fxName = null; @@ -29,26 +30,8 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist // Dependencies are stored in an array while (reader.Read() && reader.TokenType != JsonToken.EndArray) { - string id = null; - string version = null; - while (reader.Read() && reader.TokenType != JsonToken.EndObject) - { - if (reader.TokenType.Equals(JsonToken.PropertyName) && reader.Value.Equals(JsonProperties.PackageId)) - { - reader.Read(); - id = reader.Value.ToString(); - } - if (reader.TokenType.Equals(JsonToken.PropertyName) && reader.Value.Equals(JsonProperties.Range)) - { - reader.Read(); - version = reader.Value.ToString(); - } - } - - if (id != null) - { - packages.Add(new Packaging.Core.PackageDependency(id, string.IsNullOrEmpty(version) ? null : VersionRange.Parse(version))); - } + Packaging.Core.PackageDependency package = (Packaging.Core.PackageDependency)_converter.ReadJson(reader, typeof(Packaging.Core.PackageDependency), null, serializer); + packages.Add(package); } } else if (reader.Value.Equals(JsonProperties.TargetFramework)) From 5a2349d359520f8987ef3a1454486d8169d3e864 Mon Sep 17 00:00:00 2001 From: Martin Ruiz Date: Mon, 12 Aug 2024 11:50:00 -0700 Subject: [PATCH 06/17] Read as string --- .../Converters/PackageDependencyGroupConverter.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/NuGet.Core/NuGet.Protocol/Converters/PackageDependencyGroupConverter.cs b/src/NuGet.Core/NuGet.Protocol/Converters/PackageDependencyGroupConverter.cs index aeb3dbe256b..6694d4d8e7c 100644 --- a/src/NuGet.Core/NuGet.Protocol/Converters/PackageDependencyGroupConverter.cs +++ b/src/NuGet.Core/NuGet.Protocol/Converters/PackageDependencyGroupConverter.cs @@ -36,8 +36,7 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist } else if (reader.Value.Equals(JsonProperties.TargetFramework)) { - reader.Read(); - fxName = reader.Value.ToString(); + fxName = reader.ReadAsString(); } } } From 9556342138f95476381f3fcd90731a279fb91cf0 Mon Sep 17 00:00:00 2001 From: Martin Ruiz Date: Mon, 12 Aug 2024 11:53:39 -0700 Subject: [PATCH 07/17] Delete Benchmarks/Program.cs --- Benchmarks/Program.cs | 4 ---- 1 file changed, 4 deletions(-) delete mode 100644 Benchmarks/Program.cs diff --git a/Benchmarks/Program.cs b/Benchmarks/Program.cs deleted file mode 100644 index 38f36dbaa91..00000000000 --- a/Benchmarks/Program.cs +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -Console.WriteLine("Hello, World!"); From 7210a2e0028e675f4631ba900e23c3c6bf89079a Mon Sep 17 00:00:00 2001 From: Martin Ruiz Date: Mon, 12 Aug 2024 11:55:29 -0700 Subject: [PATCH 08/17] else if --- .../NuGet.Protocol/Converters/PackageDependencyConverter.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/NuGet.Core/NuGet.Protocol/Converters/PackageDependencyConverter.cs b/src/NuGet.Core/NuGet.Protocol/Converters/PackageDependencyConverter.cs index d41fbff81ca..5d0abf4ccf4 100644 --- a/src/NuGet.Core/NuGet.Protocol/Converters/PackageDependencyConverter.cs +++ b/src/NuGet.Core/NuGet.Protocol/Converters/PackageDependencyConverter.cs @@ -29,7 +29,7 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist { id = reader.ReadAsString(); } - if (reader.Value.Equals(JsonProperties.Range)) + else if (reader.Value.Equals(JsonProperties.Range)) { version = id = reader.ReadAsString(); } From edb5f6e9bc8195433ce93ee5c4263fed99501781 Mon Sep 17 00:00:00 2001 From: Martin Ruiz Date: Mon, 12 Aug 2024 12:02:53 -0700 Subject: [PATCH 09/17] rebase --- .../NuGet.Protocol/Converters/PackageDependencyConverter.cs | 5 +---- .../NuGet.Protocol/PublicAPI/net472/PublicAPI.Unshipped.txt | 6 ++++++ .../PublicAPI/netcoreapp5.0/PublicAPI.Unshipped.txt | 6 ++++++ .../PublicAPI/netstandard2.0/PublicAPI.Unshipped.txt | 6 ++++++ 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/NuGet.Core/NuGet.Protocol/Converters/PackageDependencyConverter.cs b/src/NuGet.Core/NuGet.Protocol/Converters/PackageDependencyConverter.cs index 5d0abf4ccf4..6ed448596bd 100644 --- a/src/NuGet.Core/NuGet.Protocol/Converters/PackageDependencyConverter.cs +++ b/src/NuGet.Core/NuGet.Protocol/Converters/PackageDependencyConverter.cs @@ -9,10 +9,7 @@ namespace NuGet.Protocol { public class PackageDependencyConverter : JsonConverter { - public override bool CanConvert(Type objectType) - { - return objectType == typeof(Packaging.Core.PackageDependency); - } + public override bool CanConvert(Type objectType) => objectType == typeof(Packaging.Core.PackageDependency); public override bool CanWrite => false; diff --git a/src/NuGet.Core/NuGet.Protocol/PublicAPI/net472/PublicAPI.Unshipped.txt b/src/NuGet.Core/NuGet.Protocol/PublicAPI/net472/PublicAPI.Unshipped.txt index 7dc5c58110b..cb1e3797bb2 100644 --- a/src/NuGet.Core/NuGet.Protocol/PublicAPI/net472/PublicAPI.Unshipped.txt +++ b/src/NuGet.Core/NuGet.Protocol/PublicAPI/net472/PublicAPI.Unshipped.txt @@ -1 +1,7 @@ #nullable enable +NuGet.Protocol.PackageDependencyConverter +NuGet.Protocol.PackageDependencyConverter.PackageDependencyConverter() -> void +override NuGet.Protocol.PackageDependencyConverter.CanWrite.get -> bool +~override NuGet.Protocol.PackageDependencyConverter.CanConvert(System.Type objectType) -> bool +~override NuGet.Protocol.PackageDependencyConverter.ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) -> object +~override NuGet.Protocol.PackageDependencyConverter.WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) -> void diff --git a/src/NuGet.Core/NuGet.Protocol/PublicAPI/netcoreapp5.0/PublicAPI.Unshipped.txt b/src/NuGet.Core/NuGet.Protocol/PublicAPI/netcoreapp5.0/PublicAPI.Unshipped.txt index 7dc5c58110b..cb1e3797bb2 100644 --- a/src/NuGet.Core/NuGet.Protocol/PublicAPI/netcoreapp5.0/PublicAPI.Unshipped.txt +++ b/src/NuGet.Core/NuGet.Protocol/PublicAPI/netcoreapp5.0/PublicAPI.Unshipped.txt @@ -1 +1,7 @@ #nullable enable +NuGet.Protocol.PackageDependencyConverter +NuGet.Protocol.PackageDependencyConverter.PackageDependencyConverter() -> void +override NuGet.Protocol.PackageDependencyConverter.CanWrite.get -> bool +~override NuGet.Protocol.PackageDependencyConverter.CanConvert(System.Type objectType) -> bool +~override NuGet.Protocol.PackageDependencyConverter.ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) -> object +~override NuGet.Protocol.PackageDependencyConverter.WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) -> void diff --git a/src/NuGet.Core/NuGet.Protocol/PublicAPI/netstandard2.0/PublicAPI.Unshipped.txt b/src/NuGet.Core/NuGet.Protocol/PublicAPI/netstandard2.0/PublicAPI.Unshipped.txt index 7dc5c58110b..cb1e3797bb2 100644 --- a/src/NuGet.Core/NuGet.Protocol/PublicAPI/netstandard2.0/PublicAPI.Unshipped.txt +++ b/src/NuGet.Core/NuGet.Protocol/PublicAPI/netstandard2.0/PublicAPI.Unshipped.txt @@ -1 +1,7 @@ #nullable enable +NuGet.Protocol.PackageDependencyConverter +NuGet.Protocol.PackageDependencyConverter.PackageDependencyConverter() -> void +override NuGet.Protocol.PackageDependencyConverter.CanWrite.get -> bool +~override NuGet.Protocol.PackageDependencyConverter.CanConvert(System.Type objectType) -> bool +~override NuGet.Protocol.PackageDependencyConverter.ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) -> object +~override NuGet.Protocol.PackageDependencyConverter.WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) -> void From 7856ac6d46ac73374ef24e6fa637283cd83ae54e Mon Sep 17 00:00:00 2001 From: Martin Ruiz Date: Mon, 19 Aug 2024 14:03:08 -0700 Subject: [PATCH 10/17] Improved code and added unit tests --- .../Converters/JsonExtensions.cs | 3 +- .../Converters/PackageDependencyConverter.cs | 20 ++-- .../PackageDependencyGroupConverter.cs | 8 +- .../PublicAPI/net472/PublicAPI.Unshipped.txt | 6 - .../netcoreapp5.0/PublicAPI.Unshipped.txt | 6 - .../netstandard2.0/PublicAPI.Unshipped.txt | 6 - .../PackageDependencyConverterTests.cs | 106 ++++++++++++++++++ 7 files changed, 124 insertions(+), 31 deletions(-) create mode 100644 test/NuGet.Core.Tests/NuGet.Protocol.Tests/Converters/PackageDependencyConverterTests.cs diff --git a/src/NuGet.Core/NuGet.Protocol/Converters/JsonExtensions.cs b/src/NuGet.Core/NuGet.Protocol/Converters/JsonExtensions.cs index ff0e076fe92..83595af2738 100644 --- a/src/NuGet.Core/NuGet.Protocol/Converters/JsonExtensions.cs +++ b/src/NuGet.Core/NuGet.Protocol/Converters/JsonExtensions.cs @@ -29,7 +29,8 @@ public static class JsonExtensions new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }, new FingerprintsConverter(), new VersionRangeConverter(), - new PackageVulnerabilityInfoConverter() + new PackageVulnerabilityInfoConverter(), + new PackageDependencyConverter() }, }; diff --git a/src/NuGet.Core/NuGet.Protocol/Converters/PackageDependencyConverter.cs b/src/NuGet.Core/NuGet.Protocol/Converters/PackageDependencyConverter.cs index 6ed448596bd..5e1208431f1 100644 --- a/src/NuGet.Core/NuGet.Protocol/Converters/PackageDependencyConverter.cs +++ b/src/NuGet.Core/NuGet.Protocol/Converters/PackageDependencyConverter.cs @@ -5,18 +5,16 @@ using Newtonsoft.Json; using NuGet.Versioning; -namespace NuGet.Protocol +namespace NuGet.Protocol.Converters { - public class PackageDependencyConverter : JsonConverter + internal class PackageDependencyConverter : JsonConverter { - public override bool CanConvert(Type objectType) => objectType == typeof(Packaging.Core.PackageDependency); - public override bool CanWrite => false; - public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) + public override Packaging.Core.PackageDependency ReadJson(JsonReader reader, Type objectType, Packaging.Core.PackageDependency existingValue, bool hasExistingValue, JsonSerializer serializer) { string id = null; - string version = null; + VersionRange version = null; while (reader.Read() && reader.TokenType != JsonToken.EndObject) { @@ -28,18 +26,22 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist } else if (reader.Value.Equals(JsonProperties.Range)) { - version = id = reader.ReadAsString(); + var versionString = reader.ReadAsString(); + if (!string.IsNullOrEmpty(versionString)) + { + version = serializer.Deserialize(reader); + } } } } if (id != null) { - return new Packaging.Core.PackageDependency(id, string.IsNullOrEmpty(version) ? null : VersionRange.Parse(version)); + return new Packaging.Core.PackageDependency(id, version); } return null; } - public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) + public override void WriteJson(JsonWriter writer, Packaging.Core.PackageDependency value, JsonSerializer serializer) { throw new NotImplementedException(); } diff --git a/src/NuGet.Core/NuGet.Protocol/Converters/PackageDependencyGroupConverter.cs b/src/NuGet.Core/NuGet.Protocol/Converters/PackageDependencyGroupConverter.cs index 6694d4d8e7c..e94b8cec97d 100644 --- a/src/NuGet.Core/NuGet.Protocol/Converters/PackageDependencyGroupConverter.cs +++ b/src/NuGet.Core/NuGet.Protocol/Converters/PackageDependencyGroupConverter.cs @@ -15,8 +15,6 @@ public class PackageDependencyGroupConverter : JsonConverter public override bool CanWrite => false; - private readonly PackageDependencyConverter _converter = new PackageDependencyConverter(); - public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { string fxName = null; @@ -30,7 +28,7 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist // Dependencies are stored in an array while (reader.Read() && reader.TokenType != JsonToken.EndArray) { - Packaging.Core.PackageDependency package = (Packaging.Core.PackageDependency)_converter.ReadJson(reader, typeof(Packaging.Core.PackageDependency), null, serializer); + Packaging.Core.PackageDependency package = serializer.Deserialize(reader); packages.Add(package); } } @@ -39,6 +37,10 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist fxName = reader.ReadAsString(); } } + else + { + reader.Skip(); + } } var framework = NuGetFramework.AnyFramework; diff --git a/src/NuGet.Core/NuGet.Protocol/PublicAPI/net472/PublicAPI.Unshipped.txt b/src/NuGet.Core/NuGet.Protocol/PublicAPI/net472/PublicAPI.Unshipped.txt index cb1e3797bb2..7dc5c58110b 100644 --- a/src/NuGet.Core/NuGet.Protocol/PublicAPI/net472/PublicAPI.Unshipped.txt +++ b/src/NuGet.Core/NuGet.Protocol/PublicAPI/net472/PublicAPI.Unshipped.txt @@ -1,7 +1 @@ #nullable enable -NuGet.Protocol.PackageDependencyConverter -NuGet.Protocol.PackageDependencyConverter.PackageDependencyConverter() -> void -override NuGet.Protocol.PackageDependencyConverter.CanWrite.get -> bool -~override NuGet.Protocol.PackageDependencyConverter.CanConvert(System.Type objectType) -> bool -~override NuGet.Protocol.PackageDependencyConverter.ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) -> object -~override NuGet.Protocol.PackageDependencyConverter.WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) -> void diff --git a/src/NuGet.Core/NuGet.Protocol/PublicAPI/netcoreapp5.0/PublicAPI.Unshipped.txt b/src/NuGet.Core/NuGet.Protocol/PublicAPI/netcoreapp5.0/PublicAPI.Unshipped.txt index cb1e3797bb2..7dc5c58110b 100644 --- a/src/NuGet.Core/NuGet.Protocol/PublicAPI/netcoreapp5.0/PublicAPI.Unshipped.txt +++ b/src/NuGet.Core/NuGet.Protocol/PublicAPI/netcoreapp5.0/PublicAPI.Unshipped.txt @@ -1,7 +1 @@ #nullable enable -NuGet.Protocol.PackageDependencyConverter -NuGet.Protocol.PackageDependencyConverter.PackageDependencyConverter() -> void -override NuGet.Protocol.PackageDependencyConverter.CanWrite.get -> bool -~override NuGet.Protocol.PackageDependencyConverter.CanConvert(System.Type objectType) -> bool -~override NuGet.Protocol.PackageDependencyConverter.ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) -> object -~override NuGet.Protocol.PackageDependencyConverter.WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) -> void diff --git a/src/NuGet.Core/NuGet.Protocol/PublicAPI/netstandard2.0/PublicAPI.Unshipped.txt b/src/NuGet.Core/NuGet.Protocol/PublicAPI/netstandard2.0/PublicAPI.Unshipped.txt index cb1e3797bb2..7dc5c58110b 100644 --- a/src/NuGet.Core/NuGet.Protocol/PublicAPI/netstandard2.0/PublicAPI.Unshipped.txt +++ b/src/NuGet.Core/NuGet.Protocol/PublicAPI/netstandard2.0/PublicAPI.Unshipped.txt @@ -1,7 +1 @@ #nullable enable -NuGet.Protocol.PackageDependencyConverter -NuGet.Protocol.PackageDependencyConverter.PackageDependencyConverter() -> void -override NuGet.Protocol.PackageDependencyConverter.CanWrite.get -> bool -~override NuGet.Protocol.PackageDependencyConverter.CanConvert(System.Type objectType) -> bool -~override NuGet.Protocol.PackageDependencyConverter.ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) -> object -~override NuGet.Protocol.PackageDependencyConverter.WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) -> void diff --git a/test/NuGet.Core.Tests/NuGet.Protocol.Tests/Converters/PackageDependencyConverterTests.cs b/test/NuGet.Core.Tests/NuGet.Protocol.Tests/Converters/PackageDependencyConverterTests.cs new file mode 100644 index 00000000000..1f9ea069f04 --- /dev/null +++ b/test/NuGet.Core.Tests/NuGet.Protocol.Tests/Converters/PackageDependencyConverterTests.cs @@ -0,0 +1,106 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using Newtonsoft.Json; +using NuGet.Protocol.Converters; +using NuGet.Versioning; +using NuGet.Protocol.Plugins; +using Xunit; + +namespace NuGet.Protocol.Tests +{ + public class PackageDependencyConverterTests + { + private static readonly PackageDependencyConverter _converter = new PackageDependencyConverter(); + + [Fact] + public void CanConvert_ReturnsTrueForPackageDependencyType() + { + var canConvert = _converter.CanConvert(typeof(Packaging.Core.PackageDependency)); + + Assert.True(canConvert); + } + + [Fact] + public void ReadJson_ReturnsPackageDependency() + { + const string id = "PackageA"; + var version = new VersionRange(new NuGetVersion(1, 4, 1)); + var expectedPackageDependency = new Packaging.Core.PackageDependency(id, version); + + var actualPackageDependency = _converter.ReadJson( + new JsonTextReader(new System.IO.StringReader(PackageRegistrationDependencyGroupsJson)), + typeof(Packaging.Core.PackageDependency), + existingValue: null, + serializer: JsonSerializationUtilities.Serializer); + Assert.Equal(expectedPackageDependency, actualPackageDependency); + } + + [Theory] + [InlineData(PackageRegistrationDependencyGroupsNoRangeJson)] + [InlineData(PackageRegistrationDependencyGroupsEmptyRangeJson)] + public void ReadJson_ReturnsPackageDependencyWithNoRange(string json) + { + const string id = "PackageA"; + var version = VersionRange.All; + var expectedPackageDependency = new Packaging.Core.PackageDependency(id, version); + + var actualPackageDependency = _converter.ReadJson( + new JsonTextReader(new System.IO.StringReader(json)), + typeof(Packaging.Core.PackageDependency), + existingValue: null, + serializer: JsonSerializationUtilities.Serializer); + Assert.Equal(expectedPackageDependency, actualPackageDependency); + } + + private const string PackageRegistrationDependencyGroupsJson = @"{""dependencyGroups"": [ + { + ""@id"": ""https://api.nuget.org/v3/catalog0/data/2015.02.01.06.24.00/PackageA.1.6.0.json#dependencygroup"", + ""@type"": ""PackageDependencyGroup"", + ""dependencies"": [ + { + ""@id"": ""https://api.nuget.org/v3/catalog0/data/2015.02.01.06.24.00/PackageA.1.6.0.json#dependencygroup/jquery"", + ""@type"": ""PackageDependency"", + ""id"": ""PackageA"", + ""range"": ""[1.4.1, )"", + ""registration"": ""https://api.nuget.org/v3/registration0/PackageA/index.json"" + } + ] + } + ], + }"; + + private const string PackageRegistrationDependencyGroupsNoRangeJson = @"{""dependencyGroups"": [ + { + ""@id"": ""https://api.nuget.org/v3/catalog0/data/2015.02.01.06.24.00/PackageA.1.6.0.json#dependencygroup"", + ""@type"": ""PackageDependencyGroup"", + ""dependencies"": [ + { + ""@id"": ""https://api.nuget.org/v3/catalog0/data/2015.02.01.06.24.00/PackageA.1.6.0.json#dependencygroup/jquery"", + ""@type"": ""PackageDependency"", + ""id"": ""PackageA"", + ""registration"": ""https://api.nuget.org/v3/registration0/PackageA/index.json"" + } + ] + } + ], + }"; + + private const string PackageRegistrationDependencyGroupsEmptyRangeJson = @"{""dependencyGroups"": [ + { + ""@id"": ""https://api.nuget.org/v3/catalog0/data/2015.02.01.06.24.00/PackageA.1.6.0.json#dependencygroup"", + ""@type"": ""PackageDependencyGroup"", + ""dependencies"": [ + { + ""@id"": ""https://api.nuget.org/v3/catalog0/data/2015.02.01.06.24.00/PackageA.1.6.0.json#dependencygroup/jquery"", + ""@type"": ""PackageDependency"", + ""id"": ""PackageA"", + ""range"": """", + ""registration"": ""https://api.nuget.org/v3/registration0/PackageA/index.json"" + } + ] + } + ], + }"; + } +} From 64bb8b3df81f9a048f3dbd1c8c715f7b3aac159d Mon Sep 17 00:00:00 2001 From: Martin Ruiz Date: Wed, 21 Aug 2024 10:39:51 -0700 Subject: [PATCH 11/17] deleted converter --- .../NuGet.Packaging/Core/PackageDependency.cs | 2 + .../Converters/JsonExtensions.cs | 3 +- .../Converters/PackageDependencyConverter.cs | 49 ------------------- .../PackageDependencyGroupConverter.cs | 7 ++- 4 files changed, 9 insertions(+), 52 deletions(-) delete mode 100644 src/NuGet.Core/NuGet.Protocol/Converters/PackageDependencyConverter.cs diff --git a/src/NuGet.Core/NuGet.Packaging/Core/PackageDependency.cs b/src/NuGet.Core/NuGet.Packaging/Core/PackageDependency.cs index 6b569d053e0..d3cd9a83d2c 100644 --- a/src/NuGet.Core/NuGet.Packaging/Core/PackageDependency.cs +++ b/src/NuGet.Core/NuGet.Packaging/Core/PackageDependency.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.Globalization; +using Newtonsoft.Json; using NuGet.Versioning; namespace NuGet.Packaging.Core @@ -44,6 +45,7 @@ public PackageDependency(string id) { } + [JsonConstructor] public PackageDependency(string id, VersionRange versionRange) : this(id, versionRange, include: null, exclude: null) { diff --git a/src/NuGet.Core/NuGet.Protocol/Converters/JsonExtensions.cs b/src/NuGet.Core/NuGet.Protocol/Converters/JsonExtensions.cs index 83595af2738..ff0e076fe92 100644 --- a/src/NuGet.Core/NuGet.Protocol/Converters/JsonExtensions.cs +++ b/src/NuGet.Core/NuGet.Protocol/Converters/JsonExtensions.cs @@ -29,8 +29,7 @@ public static class JsonExtensions new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }, new FingerprintsConverter(), new VersionRangeConverter(), - new PackageVulnerabilityInfoConverter(), - new PackageDependencyConverter() + new PackageVulnerabilityInfoConverter() }, }; diff --git a/src/NuGet.Core/NuGet.Protocol/Converters/PackageDependencyConverter.cs b/src/NuGet.Core/NuGet.Protocol/Converters/PackageDependencyConverter.cs deleted file mode 100644 index 5e1208431f1..00000000000 --- a/src/NuGet.Core/NuGet.Protocol/Converters/PackageDependencyConverter.cs +++ /dev/null @@ -1,49 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using Newtonsoft.Json; -using NuGet.Versioning; - -namespace NuGet.Protocol.Converters -{ - internal class PackageDependencyConverter : JsonConverter - { - public override bool CanWrite => false; - - public override Packaging.Core.PackageDependency ReadJson(JsonReader reader, Type objectType, Packaging.Core.PackageDependency existingValue, bool hasExistingValue, JsonSerializer serializer) - { - string id = null; - VersionRange version = null; - - while (reader.Read() && reader.TokenType != JsonToken.EndObject) - { - if (reader.TokenType.Equals(JsonToken.PropertyName)) - { - if (reader.Value.Equals(JsonProperties.PackageId)) - { - id = reader.ReadAsString(); - } - else if (reader.Value.Equals(JsonProperties.Range)) - { - var versionString = reader.ReadAsString(); - if (!string.IsNullOrEmpty(versionString)) - { - version = serializer.Deserialize(reader); - } - } - } - } - if (id != null) - { - return new Packaging.Core.PackageDependency(id, version); - } - return null; - } - - public override void WriteJson(JsonWriter writer, Packaging.Core.PackageDependency value, JsonSerializer serializer) - { - throw new NotImplementedException(); - } - } -} diff --git a/src/NuGet.Core/NuGet.Protocol/Converters/PackageDependencyGroupConverter.cs b/src/NuGet.Core/NuGet.Protocol/Converters/PackageDependencyGroupConverter.cs index e94b8cec97d..74938c3913b 100644 --- a/src/NuGet.Core/NuGet.Protocol/Converters/PackageDependencyGroupConverter.cs +++ b/src/NuGet.Core/NuGet.Protocol/Converters/PackageDependencyGroupConverter.cs @@ -26,9 +26,10 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist if (reader.Value.Equals(JsonProperties.Dependencies)) { // Dependencies are stored in an array + reader.Read(); // move to the start of the array while (reader.Read() && reader.TokenType != JsonToken.EndArray) { - Packaging.Core.PackageDependency package = serializer.Deserialize(reader); + var package = serializer.Deserialize(reader); packages.Add(package); } } @@ -36,6 +37,10 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist { fxName = reader.ReadAsString(); } + else + { + reader.Skip(); + } } else { From 9e99c329fd12c2dd2db4d1b5f8474b18d2a6a930 Mon Sep 17 00:00:00 2001 From: Martin Ruiz Date: Thu, 22 Aug 2024 13:45:07 -0700 Subject: [PATCH 12/17] fixed tests --- .../NuGet.Packaging/Core/PackageDependency.cs | 1 + .../PackageDependencyConverterTests.cs | 107 ++++++++---------- 2 files changed, 47 insertions(+), 61 deletions(-) diff --git a/src/NuGet.Core/NuGet.Packaging/Core/PackageDependency.cs b/src/NuGet.Core/NuGet.Packaging/Core/PackageDependency.cs index d3cd9a83d2c..fd4e88445fe 100644 --- a/src/NuGet.Core/NuGet.Packaging/Core/PackageDependency.cs +++ b/src/NuGet.Core/NuGet.Packaging/Core/PackageDependency.cs @@ -35,6 +35,7 @@ public class PackageDependency : IEquatable /// /// Range of versions allowed for the depenency /// + [JsonProperty(PropertyName = "range")] public VersionRange VersionRange { get { return _versionRange; } diff --git a/test/NuGet.Core.Tests/NuGet.Protocol.Tests/Converters/PackageDependencyConverterTests.cs b/test/NuGet.Core.Tests/NuGet.Protocol.Tests/Converters/PackageDependencyConverterTests.cs index 1f9ea069f04..5b8676fd450 100644 --- a/test/NuGet.Core.Tests/NuGet.Protocol.Tests/Converters/PackageDependencyConverterTests.cs +++ b/test/NuGet.Core.Tests/NuGet.Protocol.Tests/Converters/PackageDependencyConverterTests.cs @@ -2,21 +2,23 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using Newtonsoft.Json; -using NuGet.Protocol.Converters; +using NuGet.Packaging; using NuGet.Versioning; using NuGet.Protocol.Plugins; using Xunit; +using System.Collections.Generic; +using System.IO; namespace NuGet.Protocol.Tests { public class PackageDependencyConverterTests { - private static readonly PackageDependencyConverter _converter = new PackageDependencyConverter(); + private static readonly PackageDependencyGroupConverter _converter = new PackageDependencyGroupConverter(); [Fact] public void CanConvert_ReturnsTrueForPackageDependencyType() { - var canConvert = _converter.CanConvert(typeof(Packaging.Core.PackageDependency)); + var canConvert = _converter.CanConvert(typeof(PackageDependencyGroup)); Assert.True(canConvert); } @@ -27,80 +29,63 @@ public void ReadJson_ReturnsPackageDependency() const string id = "PackageA"; var version = new VersionRange(new NuGetVersion(1, 4, 1)); var expectedPackageDependency = new Packaging.Core.PackageDependency(id, version); + IEnumerable packages = new List() { expectedPackageDependency }; - var actualPackageDependency = _converter.ReadJson( - new JsonTextReader(new System.IO.StringReader(PackageRegistrationDependencyGroupsJson)), - typeof(Packaging.Core.PackageDependency), + using var stringReader = new StringReader(PackageRegistrationDependencyGroupsJson); + using var jsonReader = new JsonTextReader(stringReader); + + jsonReader.Read(); + + PackageDependencyGroup actualPackageDependencies = (PackageDependencyGroup)_converter.ReadJson( + jsonReader, + typeof(PackageDependencyGroup), existingValue: null, serializer: JsonSerializationUtilities.Serializer); - Assert.Equal(expectedPackageDependency, actualPackageDependency); + + Assert.Equal(packages, actualPackageDependencies.Packages); } - [Theory] - [InlineData(PackageRegistrationDependencyGroupsNoRangeJson)] - [InlineData(PackageRegistrationDependencyGroupsEmptyRangeJson)] - public void ReadJson_ReturnsPackageDependencyWithNoRange(string json) + [Fact] + public void ReadJson_ReturnsPackageDependencyWithNoRange() { const string id = "PackageA"; var version = VersionRange.All; var expectedPackageDependency = new Packaging.Core.PackageDependency(id, version); - var actualPackageDependency = _converter.ReadJson( - new JsonTextReader(new System.IO.StringReader(json)), - typeof(Packaging.Core.PackageDependency), + IEnumerable packages = new List() { expectedPackageDependency }; + using var stringReader = new StringReader(PackageRegistrationDependencyGroupsNoRangeJson); + using var jsonReader = new JsonTextReader(stringReader); + + jsonReader.Read(); + + PackageDependencyGroup actualPackageDependencies = (PackageDependencyGroup)_converter.ReadJson( + jsonReader, + typeof(PackageDependencyGroup), existingValue: null, serializer: JsonSerializationUtilities.Serializer); - Assert.Equal(expectedPackageDependency, actualPackageDependency); + Assert.Equal(packages, actualPackageDependencies.Packages); } - private const string PackageRegistrationDependencyGroupsJson = @"{""dependencyGroups"": [ - { - ""@id"": ""https://api.nuget.org/v3/catalog0/data/2015.02.01.06.24.00/PackageA.1.6.0.json#dependencygroup"", - ""@type"": ""PackageDependencyGroup"", - ""dependencies"": [ - { - ""@id"": ""https://api.nuget.org/v3/catalog0/data/2015.02.01.06.24.00/PackageA.1.6.0.json#dependencygroup/jquery"", - ""@type"": ""PackageDependency"", - ""id"": ""PackageA"", - ""range"": ""[1.4.1, )"", - ""registration"": ""https://api.nuget.org/v3/registration0/PackageA/index.json"" - } - ] - } - ], - }"; - - private const string PackageRegistrationDependencyGroupsNoRangeJson = @"{""dependencyGroups"": [ - { - ""@id"": ""https://api.nuget.org/v3/catalog0/data/2015.02.01.06.24.00/PackageA.1.6.0.json#dependencygroup"", - ""@type"": ""PackageDependencyGroup"", - ""dependencies"": [ - { - ""@id"": ""https://api.nuget.org/v3/catalog0/data/2015.02.01.06.24.00/PackageA.1.6.0.json#dependencygroup/jquery"", - ""@type"": ""PackageDependency"", - ""id"": ""PackageA"", - ""registration"": ""https://api.nuget.org/v3/registration0/PackageA/index.json"" - } - ] - } - ], + private const string PackageRegistrationDependencyGroupsJson = @"{ + ""dependencies"": [ + { + ""@id"": ""https://api.nuget.org/v3/catalog0/data/2015.02.01.06.24.00/PackageA.1.6.0.json#dependencygroup/jquery"", + ""@type"": ""PackageDependency"", + ""id"": ""PackageA"", + ""range"": ""[1.4.1, )"", + ""registration"": ""https://api.nuget.org/v3/registration0/PackageA/index.json"" + } + ] }"; - private const string PackageRegistrationDependencyGroupsEmptyRangeJson = @"{""dependencyGroups"": [ - { - ""@id"": ""https://api.nuget.org/v3/catalog0/data/2015.02.01.06.24.00/PackageA.1.6.0.json#dependencygroup"", - ""@type"": ""PackageDependencyGroup"", - ""dependencies"": [ - { - ""@id"": ""https://api.nuget.org/v3/catalog0/data/2015.02.01.06.24.00/PackageA.1.6.0.json#dependencygroup/jquery"", - ""@type"": ""PackageDependency"", - ""id"": ""PackageA"", - ""range"": """", - ""registration"": ""https://api.nuget.org/v3/registration0/PackageA/index.json"" - } - ] - } - ], + private const string PackageRegistrationDependencyGroupsNoRangeJson = @"{""dependencies"": [ + { + ""@id"": ""https://api.nuget.org/v3/catalog0/data/2015.02.01.06.24.00/PackageA.1.6.0.json#dependencygroup/jquery"", + ""@type"": ""PackageDependency"", + ""id"": ""PackageA"", + ""registration"": ""https://api.nuget.org/v3/registration0/PackageA/index.json"" + } + ] }"; } } From 9fa04258f7d74065ade3bb3950ed20ca7621c63c Mon Sep 17 00:00:00 2001 From: Martin Ruiz Date: Thu, 5 Sep 2024 16:29:00 -0700 Subject: [PATCH 13/17] update test to confirm that comments are ignored --- .../Converters/PackageDependencyConverterTests.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/test/NuGet.Core.Tests/NuGet.Protocol.Tests/Converters/PackageDependencyConverterTests.cs b/test/NuGet.Core.Tests/NuGet.Protocol.Tests/Converters/PackageDependencyConverterTests.cs index 5b8676fd450..9f7561cb397 100644 --- a/test/NuGet.Core.Tests/NuGet.Protocol.Tests/Converters/PackageDependencyConverterTests.cs +++ b/test/NuGet.Core.Tests/NuGet.Protocol.Tests/Converters/PackageDependencyConverterTests.cs @@ -70,6 +70,7 @@ public void ReadJson_ReturnsPackageDependencyWithNoRange() ""dependencies"": [ { ""@id"": ""https://api.nuget.org/v3/catalog0/data/2015.02.01.06.24.00/PackageA.1.6.0.json#dependencygroup/jquery"", + // comment to test that they are ignored ""@type"": ""PackageDependency"", ""id"": ""PackageA"", ""range"": ""[1.4.1, )"", From fbfce3e31267a3e4348832a1883e2348538c3a4c Mon Sep 17 00:00:00 2001 From: Martin Ruiz Date: Fri, 6 Sep 2024 10:09:43 -0700 Subject: [PATCH 14/17] Removed converter --- .../Core/PackageDependencyGroup.cs | 9 ++ .../Converters/JsonExtensions.cs | 3 +- .../Converters/NuGetFrameworkConverter.cs | 33 ++++++ .../PackageDependencyGroupConverter.cs | 67 ----------- .../Model/PackageSearchMetadata.cs | 2 +- .../PublicAPI/net472/PublicAPI.Shipped.txt | 6 - .../PublicAPI/net472/PublicAPI.Unshipped.txt | 5 + .../netcoreapp5.0/PublicAPI.Shipped.txt | 6 - .../netcoreapp5.0/PublicAPI.Unshipped.txt | 5 + .../netstandard2.0/PublicAPI.Shipped.txt | 6 - .../netstandard2.0/PublicAPI.Unshipped.txt | 5 + .../PackageDependencyGroupTests.cs | 107 ++++++++++++++++++ .../PackageDependencyConverterTests.cs | 92 --------------- 13 files changed, 167 insertions(+), 179 deletions(-) create mode 100644 src/NuGet.Core/NuGet.Protocol/Converters/NuGetFrameworkConverter.cs delete mode 100644 src/NuGet.Core/NuGet.Protocol/Converters/PackageDependencyGroupConverter.cs delete mode 100644 test/NuGet.Core.Tests/NuGet.Protocol.Tests/Converters/PackageDependencyConverterTests.cs diff --git a/src/NuGet.Core/NuGet.Packaging/Core/PackageDependencyGroup.cs b/src/NuGet.Core/NuGet.Packaging/Core/PackageDependencyGroup.cs index 62e636486ac..832bbf8c383 100644 --- a/src/NuGet.Core/NuGet.Packaging/Core/PackageDependencyGroup.cs +++ b/src/NuGet.Core/NuGet.Packaging/Core/PackageDependencyGroup.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.Globalization; +using Newtonsoft.Json; using NuGet.Frameworks; using NuGet.Packaging.Core; using NuGet.Shared; @@ -18,6 +19,12 @@ public class PackageDependencyGroup : IEquatable, IFrame private readonly NuGetFramework _targetFramework; private readonly IEnumerable _packages; + [JsonConstructor] + private PackageDependencyGroup(NuGetFramework targetFramework) + : this(targetFramework, new List()) + { + } + /// /// Dependency group /// @@ -42,6 +49,7 @@ public PackageDependencyGroup(NuGetFramework targetFramework, IEnumerable /// Dependency group target framework /// + [JsonProperty(PropertyName = "targetFramework")] public NuGetFramework TargetFramework { get { return _targetFramework; } @@ -50,6 +58,7 @@ public NuGetFramework TargetFramework /// /// Package dependencies /// + [JsonProperty(PropertyName = "dependencies")] public IEnumerable Packages { get { return _packages; } diff --git a/src/NuGet.Core/NuGet.Protocol/Converters/JsonExtensions.cs b/src/NuGet.Core/NuGet.Protocol/Converters/JsonExtensions.cs index ff0e076fe92..421d5467cbb 100644 --- a/src/NuGet.Core/NuGet.Protocol/Converters/JsonExtensions.cs +++ b/src/NuGet.Core/NuGet.Protocol/Converters/JsonExtensions.cs @@ -29,7 +29,8 @@ public static class JsonExtensions new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }, new FingerprintsConverter(), new VersionRangeConverter(), - new PackageVulnerabilityInfoConverter() + new PackageVulnerabilityInfoConverter(), + new NuGetFrameworkConverter() }, }; diff --git a/src/NuGet.Core/NuGet.Protocol/Converters/NuGetFrameworkConverter.cs b/src/NuGet.Core/NuGet.Protocol/Converters/NuGetFrameworkConverter.cs new file mode 100644 index 00000000000..918f62400ce --- /dev/null +++ b/src/NuGet.Core/NuGet.Protocol/Converters/NuGetFrameworkConverter.cs @@ -0,0 +1,33 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Xml.Linq; +using Newtonsoft.Json; +using NuGet.Frameworks; + +namespace NuGet.Protocol.Converters +{ + public class NuGetFrameworkConverter : JsonConverter + { + public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) + { + serializer.Serialize(writer, value.ToString()); + } + + public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) + { + var value = serializer.Deserialize(reader); + var framework = NuGetFramework.AnyFramework; + + if (!string.IsNullOrEmpty(value)) + { + framework = NuGetFramework.Parse(value); + } + + return framework; + } + + public override bool CanConvert(Type objectType) => objectType == typeof(NuGetFramework); + } +} diff --git a/src/NuGet.Core/NuGet.Protocol/Converters/PackageDependencyGroupConverter.cs b/src/NuGet.Core/NuGet.Protocol/Converters/PackageDependencyGroupConverter.cs deleted file mode 100644 index 74938c3913b..00000000000 --- a/src/NuGet.Core/NuGet.Protocol/Converters/PackageDependencyGroupConverter.cs +++ /dev/null @@ -1,67 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Collections.Generic; -using Newtonsoft.Json; -using NuGet.Frameworks; -using NuGet.Packaging; - -namespace NuGet.Protocol -{ - public class PackageDependencyGroupConverter : JsonConverter - { - public override bool CanConvert(Type objectType) => (objectType == typeof(PackageDependencyGroup)); - - public override bool CanWrite => false; - - public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) - { - string fxName = null; - List packages = new List(); - while (reader.Read() && reader.TokenType != JsonToken.EndObject) - { - if (reader.TokenType.Equals(JsonToken.PropertyName)) - { - if (reader.Value.Equals(JsonProperties.Dependencies)) - { - // Dependencies are stored in an array - reader.Read(); // move to the start of the array - while (reader.Read() && reader.TokenType != JsonToken.EndArray) - { - var package = serializer.Deserialize(reader); - packages.Add(package); - } - } - else if (reader.Value.Equals(JsonProperties.TargetFramework)) - { - fxName = reader.ReadAsString(); - } - else - { - reader.Skip(); - } - } - else - { - reader.Skip(); - } - } - - var framework = NuGetFramework.AnyFramework; - - if (!string.IsNullOrEmpty(fxName)) - { - framework = NuGetFramework.Parse(fxName); - fxName = framework.GetShortFolderName(); - } - - return new PackageDependencyGroup(framework, packages); - } - - public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) - { - throw new NotImplementedException(); - } - } -} diff --git a/src/NuGet.Core/NuGet.Protocol/Model/PackageSearchMetadata.cs b/src/NuGet.Core/NuGet.Protocol/Model/PackageSearchMetadata.cs index 383a4e85ca0..7ea297e2e94 100644 --- a/src/NuGet.Core/NuGet.Protocol/Model/PackageSearchMetadata.cs +++ b/src/NuGet.Core/NuGet.Protocol/Model/PackageSearchMetadata.cs @@ -22,7 +22,7 @@ public class PackageSearchMetadata : IPackageSearchMetadata [JsonConverter(typeof(MetadataFieldConverter))] public string Authors { get; private set; } - [JsonProperty(PropertyName = JsonProperties.DependencyGroups, ItemConverterType = typeof(PackageDependencyGroupConverter))] + [JsonProperty(PropertyName = JsonProperties.DependencyGroups)] public IEnumerable DependencySetsInternal { get; private set; } [JsonIgnore] diff --git a/src/NuGet.Core/NuGet.Protocol/PublicAPI/net472/PublicAPI.Shipped.txt b/src/NuGet.Core/NuGet.Protocol/PublicAPI/net472/PublicAPI.Shipped.txt index 6cfec1efc98..00d918eb6d2 100644 --- a/src/NuGet.Core/NuGet.Protocol/PublicAPI/net472/PublicAPI.Shipped.txt +++ b/src/NuGet.Core/NuGet.Protocol/PublicAPI/net472/PublicAPI.Shipped.txt @@ -721,8 +721,6 @@ NuGet.Protocol.ODataServiceDocumentResourceV2Provider.ODataServiceDocumentCacheI ~NuGet.Protocol.ODataServiceDocumentResourceV2Provider.ODataServiceDocumentCacheInfo.ServiceDocument.get -> NuGet.Protocol.ODataServiceDocumentResourceV2 ~NuGet.Protocol.ODataServiceDocumentResourceV2Provider.ODataServiceDocumentCacheInfo.ServiceDocument.set -> void NuGet.Protocol.ODataServiceDocumentResourceV2Provider.ODataServiceDocumentResourceV2Provider() -> void -NuGet.Protocol.PackageDependencyGroupConverter -NuGet.Protocol.PackageDependencyGroupConverter.PackageDependencyGroupConverter() -> void NuGet.Protocol.PackageDeprecationMetadata ~NuGet.Protocol.PackageDeprecationMetadata.AlternatePackage.get -> NuGet.Protocol.AlternatePackageMetadata ~NuGet.Protocol.PackageDeprecationMetadata.Message.get -> string @@ -1775,10 +1773,6 @@ override NuGet.Protocol.Model.PackageVulnerabilityInfo.GetHashCode() -> int ~override NuGet.Protocol.NuGetVersionConverter.ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) -> object ~override NuGet.Protocol.NuGetVersionConverter.WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) -> void ~override NuGet.Protocol.ODataServiceDocumentResourceV2Provider.TryCreate(NuGet.Protocol.Core.Types.SourceRepository source, System.Threading.CancellationToken token) -> System.Threading.Tasks.Task> -~override NuGet.Protocol.PackageDependencyGroupConverter.CanConvert(System.Type objectType) -> bool -override NuGet.Protocol.PackageDependencyGroupConverter.CanWrite.get -> bool -~override NuGet.Protocol.PackageDependencyGroupConverter.ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) -> object -~override NuGet.Protocol.PackageDependencyGroupConverter.WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) -> void ~override NuGet.Protocol.PackageDetailsUriResourceV3Provider.TryCreate(NuGet.Protocol.Core.Types.SourceRepository source, System.Threading.CancellationToken token) -> System.Threading.Tasks.Task> ~override NuGet.Protocol.PackageMetadataResourceV2Feed.GetMetadataAsync(NuGet.Packaging.Core.PackageIdentity package, NuGet.Protocol.Core.Types.SourceCacheContext sourceCacheContext, NuGet.Common.ILogger log, System.Threading.CancellationToken token) -> System.Threading.Tasks.Task ~override NuGet.Protocol.PackageMetadataResourceV2Feed.GetMetadataAsync(string packageId, bool includePrerelease, bool includeUnlisted, NuGet.Protocol.Core.Types.SourceCacheContext sourceCacheContext, NuGet.Common.ILogger log, System.Threading.CancellationToken token) -> System.Threading.Tasks.Task> diff --git a/src/NuGet.Core/NuGet.Protocol/PublicAPI/net472/PublicAPI.Unshipped.txt b/src/NuGet.Core/NuGet.Protocol/PublicAPI/net472/PublicAPI.Unshipped.txt index 7dc5c58110b..8cd587c8eaf 100644 --- a/src/NuGet.Core/NuGet.Protocol/PublicAPI/net472/PublicAPI.Unshipped.txt +++ b/src/NuGet.Core/NuGet.Protocol/PublicAPI/net472/PublicAPI.Unshipped.txt @@ -1 +1,6 @@ #nullable enable +NuGet.Protocol.Converters.NuGetFrameworkConverter +NuGet.Protocol.Converters.NuGetFrameworkConverter.NuGetFrameworkConverter() -> void +~override NuGet.Protocol.Converters.NuGetFrameworkConverter.CanConvert(System.Type objectType) -> bool +~override NuGet.Protocol.Converters.NuGetFrameworkConverter.ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) -> object +~override NuGet.Protocol.Converters.NuGetFrameworkConverter.WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) -> void diff --git a/src/NuGet.Core/NuGet.Protocol/PublicAPI/netcoreapp5.0/PublicAPI.Shipped.txt b/src/NuGet.Core/NuGet.Protocol/PublicAPI/netcoreapp5.0/PublicAPI.Shipped.txt index 4f3ea636015..13f5869448a 100644 --- a/src/NuGet.Core/NuGet.Protocol/PublicAPI/netcoreapp5.0/PublicAPI.Shipped.txt +++ b/src/NuGet.Core/NuGet.Protocol/PublicAPI/netcoreapp5.0/PublicAPI.Shipped.txt @@ -721,8 +721,6 @@ NuGet.Protocol.ODataServiceDocumentResourceV2Provider.ODataServiceDocumentCacheI ~NuGet.Protocol.ODataServiceDocumentResourceV2Provider.ODataServiceDocumentCacheInfo.ServiceDocument.get -> NuGet.Protocol.ODataServiceDocumentResourceV2 ~NuGet.Protocol.ODataServiceDocumentResourceV2Provider.ODataServiceDocumentCacheInfo.ServiceDocument.set -> void NuGet.Protocol.ODataServiceDocumentResourceV2Provider.ODataServiceDocumentResourceV2Provider() -> void -NuGet.Protocol.PackageDependencyGroupConverter -NuGet.Protocol.PackageDependencyGroupConverter.PackageDependencyGroupConverter() -> void NuGet.Protocol.PackageDeprecationMetadata ~NuGet.Protocol.PackageDeprecationMetadata.AlternatePackage.get -> NuGet.Protocol.AlternatePackageMetadata ~NuGet.Protocol.PackageDeprecationMetadata.Message.get -> string @@ -1766,10 +1764,6 @@ override NuGet.Protocol.Model.PackageVulnerabilityInfo.GetHashCode() -> int ~override NuGet.Protocol.NuGetVersionConverter.ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) -> object ~override NuGet.Protocol.NuGetVersionConverter.WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) -> void ~override NuGet.Protocol.ODataServiceDocumentResourceV2Provider.TryCreate(NuGet.Protocol.Core.Types.SourceRepository source, System.Threading.CancellationToken token) -> System.Threading.Tasks.Task> -~override NuGet.Protocol.PackageDependencyGroupConverter.CanConvert(System.Type objectType) -> bool -override NuGet.Protocol.PackageDependencyGroupConverter.CanWrite.get -> bool -~override NuGet.Protocol.PackageDependencyGroupConverter.ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) -> object -~override NuGet.Protocol.PackageDependencyGroupConverter.WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) -> void ~override NuGet.Protocol.PackageDetailsUriResourceV3Provider.TryCreate(NuGet.Protocol.Core.Types.SourceRepository source, System.Threading.CancellationToken token) -> System.Threading.Tasks.Task> ~override NuGet.Protocol.PackageMetadataResourceV2Feed.GetMetadataAsync(NuGet.Packaging.Core.PackageIdentity package, NuGet.Protocol.Core.Types.SourceCacheContext sourceCacheContext, NuGet.Common.ILogger log, System.Threading.CancellationToken token) -> System.Threading.Tasks.Task ~override NuGet.Protocol.PackageMetadataResourceV2Feed.GetMetadataAsync(string packageId, bool includePrerelease, bool includeUnlisted, NuGet.Protocol.Core.Types.SourceCacheContext sourceCacheContext, NuGet.Common.ILogger log, System.Threading.CancellationToken token) -> System.Threading.Tasks.Task> diff --git a/src/NuGet.Core/NuGet.Protocol/PublicAPI/netcoreapp5.0/PublicAPI.Unshipped.txt b/src/NuGet.Core/NuGet.Protocol/PublicAPI/netcoreapp5.0/PublicAPI.Unshipped.txt index 7dc5c58110b..8cd587c8eaf 100644 --- a/src/NuGet.Core/NuGet.Protocol/PublicAPI/netcoreapp5.0/PublicAPI.Unshipped.txt +++ b/src/NuGet.Core/NuGet.Protocol/PublicAPI/netcoreapp5.0/PublicAPI.Unshipped.txt @@ -1 +1,6 @@ #nullable enable +NuGet.Protocol.Converters.NuGetFrameworkConverter +NuGet.Protocol.Converters.NuGetFrameworkConverter.NuGetFrameworkConverter() -> void +~override NuGet.Protocol.Converters.NuGetFrameworkConverter.CanConvert(System.Type objectType) -> bool +~override NuGet.Protocol.Converters.NuGetFrameworkConverter.ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) -> object +~override NuGet.Protocol.Converters.NuGetFrameworkConverter.WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) -> void diff --git a/src/NuGet.Core/NuGet.Protocol/PublicAPI/netstandard2.0/PublicAPI.Shipped.txt b/src/NuGet.Core/NuGet.Protocol/PublicAPI/netstandard2.0/PublicAPI.Shipped.txt index 4f3ea636015..13f5869448a 100644 --- a/src/NuGet.Core/NuGet.Protocol/PublicAPI/netstandard2.0/PublicAPI.Shipped.txt +++ b/src/NuGet.Core/NuGet.Protocol/PublicAPI/netstandard2.0/PublicAPI.Shipped.txt @@ -721,8 +721,6 @@ NuGet.Protocol.ODataServiceDocumentResourceV2Provider.ODataServiceDocumentCacheI ~NuGet.Protocol.ODataServiceDocumentResourceV2Provider.ODataServiceDocumentCacheInfo.ServiceDocument.get -> NuGet.Protocol.ODataServiceDocumentResourceV2 ~NuGet.Protocol.ODataServiceDocumentResourceV2Provider.ODataServiceDocumentCacheInfo.ServiceDocument.set -> void NuGet.Protocol.ODataServiceDocumentResourceV2Provider.ODataServiceDocumentResourceV2Provider() -> void -NuGet.Protocol.PackageDependencyGroupConverter -NuGet.Protocol.PackageDependencyGroupConverter.PackageDependencyGroupConverter() -> void NuGet.Protocol.PackageDeprecationMetadata ~NuGet.Protocol.PackageDeprecationMetadata.AlternatePackage.get -> NuGet.Protocol.AlternatePackageMetadata ~NuGet.Protocol.PackageDeprecationMetadata.Message.get -> string @@ -1766,10 +1764,6 @@ override NuGet.Protocol.Model.PackageVulnerabilityInfo.GetHashCode() -> int ~override NuGet.Protocol.NuGetVersionConverter.ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) -> object ~override NuGet.Protocol.NuGetVersionConverter.WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) -> void ~override NuGet.Protocol.ODataServiceDocumentResourceV2Provider.TryCreate(NuGet.Protocol.Core.Types.SourceRepository source, System.Threading.CancellationToken token) -> System.Threading.Tasks.Task> -~override NuGet.Protocol.PackageDependencyGroupConverter.CanConvert(System.Type objectType) -> bool -override NuGet.Protocol.PackageDependencyGroupConverter.CanWrite.get -> bool -~override NuGet.Protocol.PackageDependencyGroupConverter.ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) -> object -~override NuGet.Protocol.PackageDependencyGroupConverter.WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) -> void ~override NuGet.Protocol.PackageDetailsUriResourceV3Provider.TryCreate(NuGet.Protocol.Core.Types.SourceRepository source, System.Threading.CancellationToken token) -> System.Threading.Tasks.Task> ~override NuGet.Protocol.PackageMetadataResourceV2Feed.GetMetadataAsync(NuGet.Packaging.Core.PackageIdentity package, NuGet.Protocol.Core.Types.SourceCacheContext sourceCacheContext, NuGet.Common.ILogger log, System.Threading.CancellationToken token) -> System.Threading.Tasks.Task ~override NuGet.Protocol.PackageMetadataResourceV2Feed.GetMetadataAsync(string packageId, bool includePrerelease, bool includeUnlisted, NuGet.Protocol.Core.Types.SourceCacheContext sourceCacheContext, NuGet.Common.ILogger log, System.Threading.CancellationToken token) -> System.Threading.Tasks.Task> diff --git a/src/NuGet.Core/NuGet.Protocol/PublicAPI/netstandard2.0/PublicAPI.Unshipped.txt b/src/NuGet.Core/NuGet.Protocol/PublicAPI/netstandard2.0/PublicAPI.Unshipped.txt index 7dc5c58110b..8cd587c8eaf 100644 --- a/src/NuGet.Core/NuGet.Protocol/PublicAPI/netstandard2.0/PublicAPI.Unshipped.txt +++ b/src/NuGet.Core/NuGet.Protocol/PublicAPI/netstandard2.0/PublicAPI.Unshipped.txt @@ -1 +1,6 @@ #nullable enable +NuGet.Protocol.Converters.NuGetFrameworkConverter +NuGet.Protocol.Converters.NuGetFrameworkConverter.NuGetFrameworkConverter() -> void +~override NuGet.Protocol.Converters.NuGetFrameworkConverter.CanConvert(System.Type objectType) -> bool +~override NuGet.Protocol.Converters.NuGetFrameworkConverter.ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) -> object +~override NuGet.Protocol.Converters.NuGetFrameworkConverter.WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) -> void diff --git a/test/NuGet.Core.Tests/NuGet.Packaging.Test/PackageDependencyGroupTests.cs b/test/NuGet.Core.Tests/NuGet.Packaging.Test/PackageDependencyGroupTests.cs index 074102fb801..9ad4911898d 100644 --- a/test/NuGet.Core.Tests/NuGet.Packaging.Test/PackageDependencyGroupTests.cs +++ b/test/NuGet.Core.Tests/NuGet.Packaging.Test/PackageDependencyGroupTests.cs @@ -1,10 +1,14 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using Newtonsoft.Json; +using System.Collections.Generic; +using System.IO; using NuGet.Frameworks; using NuGet.Packaging.Core; using NuGet.Versioning; using Xunit; +using NuGet.Protocol; namespace NuGet.Packaging.Test { @@ -218,5 +222,108 @@ public void PackageDependencyGroup_GetHashCode_SameVersionAndPackages_DifferentO Assert.Equal(aHashCode, bHashCode); } + + [Fact] + public void PackageDependencyGroup_Deserialize_ReturnsExpected() + { + // Arrange + const string id = "PackageA"; + var version = new VersionRange(new NuGetVersion(1, 4, 1)); + var expectedPackageDependency = new PackageDependency(id, version); + IEnumerable packages = new List() { expectedPackageDependency }; + + var targetFramework = NuGetFramework.Parse(".NETStandard1.0"); + + // Act + using var stringReader = new StringReader(PackageRegistrationDependencyGroupsJson); + using var jsonReader = new JsonTextReader(stringReader); + jsonReader.Read(); + + var serializer = JsonSerializer.Create(JsonExtensions.ObjectSerializationSettings); + PackageDependencyGroup actualPackageDependencies = serializer.Deserialize(jsonReader); + + // Assert + Assert.Equal(packages, actualPackageDependencies.Packages); + Assert.Equal(targetFramework, actualPackageDependencies.TargetFramework); + } + + [Fact] + public void PackageDependencyGroup_NoTargetFramework_Deserialize_ReturnsExpected() + { + // Arrange + const string id = "PackageA"; + var version = new VersionRange(new NuGetVersion(1, 4, 1)); + var expectedPackageDependency = new PackageDependency(id, version); + IEnumerable packages = new List() { expectedPackageDependency }; + + var targetFramework = NuGetFramework.AnyFramework; + + // Act + using var stringReader = new StringReader(PackageRegistrationDependencyGroupsJson_NoTargetFramework); + using var jsonReader = new JsonTextReader(stringReader); + jsonReader.Read(); + + var serializer = JsonSerializer.Create(JsonExtensions.ObjectSerializationSettings); + PackageDependencyGroup actualPackageDependencies = serializer.Deserialize(jsonReader); + + // Assert + Assert.Equal(packages, actualPackageDependencies.Packages); + Assert.Equal(targetFramework, actualPackageDependencies.TargetFramework); + } + + [Fact] + public void PackageDependencyGroup_NoDependencies_Deserialize_ReturnsExpected() + { + // Arrange + const string id = "PackageA"; + var version = new VersionRange(new NuGetVersion(1, 4, 1)); + var expectedPackageDependency = new PackageDependency(id, version); + IEnumerable packages = new List() { }; + + var targetFramework = NuGetFramework.Parse(".NETStandard1.0"); + + // Act + using var stringReader = new StringReader(PackageRegistrationDependencyGroupsJson_NoDependencies); + using var jsonReader = new JsonTextReader(stringReader); + jsonReader.Read(); + + var serializer = JsonSerializer.Create(JsonExtensions.ObjectSerializationSettings); + PackageDependencyGroup actualPackageDependencies = serializer.Deserialize(jsonReader); + + // Assert + Assert.Equal(packages, actualPackageDependencies.Packages); + Assert.Equal(targetFramework, actualPackageDependencies.TargetFramework); + } + + private const string PackageRegistrationDependencyGroupsJson = @"{ + ""targetFramework"": "".NETStandard1.0"", + ""dependencies"": [ + { + ""@id"": ""https://api.nuget.org/v3/catalog0/data/2015.02.01.06.24.00/PackageA.1.6.0.json#dependencygroup/jquery"", + // comment to test that they are ignored + ""@type"": ""PackageDependency"", + ""id"": ""PackageA"", + ""range"": ""[1.4.1, )"", + ""registration"": ""https://api.nuget.org/v3/registration0/PackageA/index.json"" + } + ] + }"; + + private const string PackageRegistrationDependencyGroupsJson_NoTargetFramework = @"{ + ""dependencies"": [ + { + ""@id"": ""https://api.nuget.org/v3/catalog0/data/2015.02.01.06.24.00/PackageA.1.6.0.json#dependencygroup/jquery"", + // comment to test that they are ignored + ""@type"": ""PackageDependency"", + ""id"": ""PackageA"", + ""range"": ""[1.4.1, )"", + ""registration"": ""https://api.nuget.org/v3/registration0/PackageA/index.json"" + } + ] + }"; + + private const string PackageRegistrationDependencyGroupsJson_NoDependencies = @"{ + ""targetFramework"": "".NETStandard1.0"", + }"; } } diff --git a/test/NuGet.Core.Tests/NuGet.Protocol.Tests/Converters/PackageDependencyConverterTests.cs b/test/NuGet.Core.Tests/NuGet.Protocol.Tests/Converters/PackageDependencyConverterTests.cs deleted file mode 100644 index 9f7561cb397..00000000000 --- a/test/NuGet.Core.Tests/NuGet.Protocol.Tests/Converters/PackageDependencyConverterTests.cs +++ /dev/null @@ -1,92 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using Newtonsoft.Json; -using NuGet.Packaging; -using NuGet.Versioning; -using NuGet.Protocol.Plugins; -using Xunit; -using System.Collections.Generic; -using System.IO; - -namespace NuGet.Protocol.Tests -{ - public class PackageDependencyConverterTests - { - private static readonly PackageDependencyGroupConverter _converter = new PackageDependencyGroupConverter(); - - [Fact] - public void CanConvert_ReturnsTrueForPackageDependencyType() - { - var canConvert = _converter.CanConvert(typeof(PackageDependencyGroup)); - - Assert.True(canConvert); - } - - [Fact] - public void ReadJson_ReturnsPackageDependency() - { - const string id = "PackageA"; - var version = new VersionRange(new NuGetVersion(1, 4, 1)); - var expectedPackageDependency = new Packaging.Core.PackageDependency(id, version); - IEnumerable packages = new List() { expectedPackageDependency }; - - using var stringReader = new StringReader(PackageRegistrationDependencyGroupsJson); - using var jsonReader = new JsonTextReader(stringReader); - - jsonReader.Read(); - - PackageDependencyGroup actualPackageDependencies = (PackageDependencyGroup)_converter.ReadJson( - jsonReader, - typeof(PackageDependencyGroup), - existingValue: null, - serializer: JsonSerializationUtilities.Serializer); - - Assert.Equal(packages, actualPackageDependencies.Packages); - } - - [Fact] - public void ReadJson_ReturnsPackageDependencyWithNoRange() - { - const string id = "PackageA"; - var version = VersionRange.All; - var expectedPackageDependency = new Packaging.Core.PackageDependency(id, version); - - IEnumerable packages = new List() { expectedPackageDependency }; - using var stringReader = new StringReader(PackageRegistrationDependencyGroupsNoRangeJson); - using var jsonReader = new JsonTextReader(stringReader); - - jsonReader.Read(); - - PackageDependencyGroup actualPackageDependencies = (PackageDependencyGroup)_converter.ReadJson( - jsonReader, - typeof(PackageDependencyGroup), - existingValue: null, - serializer: JsonSerializationUtilities.Serializer); - Assert.Equal(packages, actualPackageDependencies.Packages); - } - - private const string PackageRegistrationDependencyGroupsJson = @"{ - ""dependencies"": [ - { - ""@id"": ""https://api.nuget.org/v3/catalog0/data/2015.02.01.06.24.00/PackageA.1.6.0.json#dependencygroup/jquery"", - // comment to test that they are ignored - ""@type"": ""PackageDependency"", - ""id"": ""PackageA"", - ""range"": ""[1.4.1, )"", - ""registration"": ""https://api.nuget.org/v3/registration0/PackageA/index.json"" - } - ] - }"; - - private const string PackageRegistrationDependencyGroupsNoRangeJson = @"{""dependencies"": [ - { - ""@id"": ""https://api.nuget.org/v3/catalog0/data/2015.02.01.06.24.00/PackageA.1.6.0.json#dependencygroup/jquery"", - ""@type"": ""PackageDependency"", - ""id"": ""PackageA"", - ""registration"": ""https://api.nuget.org/v3/registration0/PackageA/index.json"" - } - ] - }"; - } -} From f14e6f769612dbaf2e04eb9a702e7b3dcbe8b52b Mon Sep 17 00:00:00 2001 From: Martin Ruiz Date: Mon, 9 Sep 2024 11:57:36 -0700 Subject: [PATCH 15/17] Fix null constructor for target framework --- .../NuGet.Packaging/Core/PackageDependencyGroup.cs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/NuGet.Core/NuGet.Packaging/Core/PackageDependencyGroup.cs b/src/NuGet.Core/NuGet.Packaging/Core/PackageDependencyGroup.cs index 832bbf8c383..4622ad07be3 100644 --- a/src/NuGet.Core/NuGet.Packaging/Core/PackageDependencyGroup.cs +++ b/src/NuGet.Core/NuGet.Packaging/Core/PackageDependencyGroup.cs @@ -21,8 +21,17 @@ public class PackageDependencyGroup : IEquatable, IFrame [JsonConstructor] private PackageDependencyGroup(NuGetFramework targetFramework) - : this(targetFramework, new List()) { + if (targetFramework == null) + { + _targetFramework = NuGetFramework.AnyFramework; + } + else + { + _targetFramework = targetFramework; + } + + _packages = new List(); } /// From 78918c9a7ac0363c26ff93fc9948db3f4914c36a Mon Sep 17 00:00:00 2001 From: Martin Ruiz Date: Tue, 10 Sep 2024 11:55:28 -0700 Subject: [PATCH 16/17] PR comments --- .../NuGet.Protocol/Converters/NuGetFrameworkConverter.cs | 5 ++--- .../NuGet.Protocol/PublicAPI/net472/PublicAPI.Unshipped.txt | 5 ----- .../PublicAPI/netcoreapp5.0/PublicAPI.Unshipped.txt | 5 ----- .../PublicAPI/netstandard2.0/PublicAPI.Unshipped.txt | 5 ----- 4 files changed, 2 insertions(+), 18 deletions(-) diff --git a/src/NuGet.Core/NuGet.Protocol/Converters/NuGetFrameworkConverter.cs b/src/NuGet.Core/NuGet.Protocol/Converters/NuGetFrameworkConverter.cs index 918f62400ce..54461514bdc 100644 --- a/src/NuGet.Core/NuGet.Protocol/Converters/NuGetFrameworkConverter.cs +++ b/src/NuGet.Core/NuGet.Protocol/Converters/NuGetFrameworkConverter.cs @@ -2,13 +2,12 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; -using System.Xml.Linq; using Newtonsoft.Json; using NuGet.Frameworks; namespace NuGet.Protocol.Converters { - public class NuGetFrameworkConverter : JsonConverter + internal class NuGetFrameworkConverter : JsonConverter { public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { @@ -17,7 +16,7 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { - var value = serializer.Deserialize(reader); + var value = reader.Value.ToString(); var framework = NuGetFramework.AnyFramework; if (!string.IsNullOrEmpty(value)) diff --git a/src/NuGet.Core/NuGet.Protocol/PublicAPI/net472/PublicAPI.Unshipped.txt b/src/NuGet.Core/NuGet.Protocol/PublicAPI/net472/PublicAPI.Unshipped.txt index 8cd587c8eaf..7dc5c58110b 100644 --- a/src/NuGet.Core/NuGet.Protocol/PublicAPI/net472/PublicAPI.Unshipped.txt +++ b/src/NuGet.Core/NuGet.Protocol/PublicAPI/net472/PublicAPI.Unshipped.txt @@ -1,6 +1 @@ #nullable enable -NuGet.Protocol.Converters.NuGetFrameworkConverter -NuGet.Protocol.Converters.NuGetFrameworkConverter.NuGetFrameworkConverter() -> void -~override NuGet.Protocol.Converters.NuGetFrameworkConverter.CanConvert(System.Type objectType) -> bool -~override NuGet.Protocol.Converters.NuGetFrameworkConverter.ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) -> object -~override NuGet.Protocol.Converters.NuGetFrameworkConverter.WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) -> void diff --git a/src/NuGet.Core/NuGet.Protocol/PublicAPI/netcoreapp5.0/PublicAPI.Unshipped.txt b/src/NuGet.Core/NuGet.Protocol/PublicAPI/netcoreapp5.0/PublicAPI.Unshipped.txt index 8cd587c8eaf..7dc5c58110b 100644 --- a/src/NuGet.Core/NuGet.Protocol/PublicAPI/netcoreapp5.0/PublicAPI.Unshipped.txt +++ b/src/NuGet.Core/NuGet.Protocol/PublicAPI/netcoreapp5.0/PublicAPI.Unshipped.txt @@ -1,6 +1 @@ #nullable enable -NuGet.Protocol.Converters.NuGetFrameworkConverter -NuGet.Protocol.Converters.NuGetFrameworkConverter.NuGetFrameworkConverter() -> void -~override NuGet.Protocol.Converters.NuGetFrameworkConverter.CanConvert(System.Type objectType) -> bool -~override NuGet.Protocol.Converters.NuGetFrameworkConverter.ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) -> object -~override NuGet.Protocol.Converters.NuGetFrameworkConverter.WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) -> void diff --git a/src/NuGet.Core/NuGet.Protocol/PublicAPI/netstandard2.0/PublicAPI.Unshipped.txt b/src/NuGet.Core/NuGet.Protocol/PublicAPI/netstandard2.0/PublicAPI.Unshipped.txt index 8cd587c8eaf..7dc5c58110b 100644 --- a/src/NuGet.Core/NuGet.Protocol/PublicAPI/netstandard2.0/PublicAPI.Unshipped.txt +++ b/src/NuGet.Core/NuGet.Protocol/PublicAPI/netstandard2.0/PublicAPI.Unshipped.txt @@ -1,6 +1 @@ #nullable enable -NuGet.Protocol.Converters.NuGetFrameworkConverter -NuGet.Protocol.Converters.NuGetFrameworkConverter.NuGetFrameworkConverter() -> void -~override NuGet.Protocol.Converters.NuGetFrameworkConverter.CanConvert(System.Type objectType) -> bool -~override NuGet.Protocol.Converters.NuGetFrameworkConverter.ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) -> object -~override NuGet.Protocol.Converters.NuGetFrameworkConverter.WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) -> void From ce23ff72bcca698c9c94a8ca264044126b02b002 Mon Sep 17 00:00:00 2001 From: Martin Ruiz Date: Wed, 11 Sep 2024 14:40:21 -0700 Subject: [PATCH 17/17] pr comments --- .../Converters/NuGetFrameworkConverter.cs | 10 ++++------ .../PackageDependencyGroupTests.cs | 20 +++++++++---------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/src/NuGet.Core/NuGet.Protocol/Converters/NuGetFrameworkConverter.cs b/src/NuGet.Core/NuGet.Protocol/Converters/NuGetFrameworkConverter.cs index 54461514bdc..c2b766cb7d7 100644 --- a/src/NuGet.Core/NuGet.Protocol/Converters/NuGetFrameworkConverter.cs +++ b/src/NuGet.Core/NuGet.Protocol/Converters/NuGetFrameworkConverter.cs @@ -7,14 +7,14 @@ namespace NuGet.Protocol.Converters { - internal class NuGetFrameworkConverter : JsonConverter + internal class NuGetFrameworkConverter : JsonConverter { - public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) + public override void WriteJson(JsonWriter writer, NuGetFramework value, JsonSerializer serializer) { - serializer.Serialize(writer, value.ToString()); + throw new NotImplementedException(); } - public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) + public override NuGetFramework ReadJson(JsonReader reader, Type objectType, NuGetFramework existingValue, bool hasExistingValue, JsonSerializer serializer) { var value = reader.Value.ToString(); var framework = NuGetFramework.AnyFramework; @@ -26,7 +26,5 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist return framework; } - - public override bool CanConvert(Type objectType) => objectType == typeof(NuGetFramework); } } diff --git a/test/NuGet.Core.Tests/NuGet.Packaging.Test/PackageDependencyGroupTests.cs b/test/NuGet.Core.Tests/NuGet.Packaging.Test/PackageDependencyGroupTests.cs index 9ad4911898d..1b7b396b132 100644 --- a/test/NuGet.Core.Tests/NuGet.Packaging.Test/PackageDependencyGroupTests.cs +++ b/test/NuGet.Core.Tests/NuGet.Packaging.Test/PackageDependencyGroupTests.cs @@ -228,9 +228,9 @@ public void PackageDependencyGroup_Deserialize_ReturnsExpected() { // Arrange const string id = "PackageA"; - var version = new VersionRange(new NuGetVersion(1, 4, 1)); - var expectedPackageDependency = new PackageDependency(id, version); - IEnumerable packages = new List() { expectedPackageDependency }; + var range = new VersionRange(new NuGetVersion(1, 4, 1)); + var expectedPackageDependency = new PackageDependency(id, range); + IEnumerable expectedTargetFramework = new List() { expectedPackageDependency }; var targetFramework = NuGetFramework.Parse(".NETStandard1.0"); @@ -243,7 +243,7 @@ public void PackageDependencyGroup_Deserialize_ReturnsExpected() PackageDependencyGroup actualPackageDependencies = serializer.Deserialize(jsonReader); // Assert - Assert.Equal(packages, actualPackageDependencies.Packages); + Assert.Equal(expectedTargetFramework, actualPackageDependencies.Packages); Assert.Equal(targetFramework, actualPackageDependencies.TargetFramework); } @@ -252,11 +252,11 @@ public void PackageDependencyGroup_NoTargetFramework_Deserialize_ReturnsExpected { // Arrange const string id = "PackageA"; - var version = new VersionRange(new NuGetVersion(1, 4, 1)); - var expectedPackageDependency = new PackageDependency(id, version); - IEnumerable packages = new List() { expectedPackageDependency }; + var range = new VersionRange(new NuGetVersion(1, 4, 1)); + var expectedPackageDependency = new PackageDependency(id, range); + IEnumerable expectedPackages = new List() { expectedPackageDependency }; - var targetFramework = NuGetFramework.AnyFramework; + var expectedTargetFramework = NuGetFramework.AnyFramework; // Act using var stringReader = new StringReader(PackageRegistrationDependencyGroupsJson_NoTargetFramework); @@ -267,8 +267,8 @@ public void PackageDependencyGroup_NoTargetFramework_Deserialize_ReturnsExpected PackageDependencyGroup actualPackageDependencies = serializer.Deserialize(jsonReader); // Assert - Assert.Equal(packages, actualPackageDependencies.Packages); - Assert.Equal(targetFramework, actualPackageDependencies.TargetFramework); + Assert.Equal(expectedPackages, actualPackageDependencies.Packages); + Assert.Equal(expectedTargetFramework, actualPackageDependencies.TargetFramework); } [Fact]