Skip to content

Commit 908f9cd

Browse files
committed
Cleanup
1 parent 17ba725 commit 908f9cd

9 files changed

Lines changed: 69 additions & 23 deletions

File tree

src/NuGet.Core/NuGet.Protocol/Converters/MetadataStringOrArrayStjConverter.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@ internal sealed class MetadataStringOrArrayStjConverter : JsonConverter<IReadOnl
1717
{
1818
public override IReadOnlyList<string>? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
1919
{
20-
if (reader.TokenType == JsonTokenType.Null)
21-
{
22-
return null;
23-
}
24-
2520
if (reader.TokenType == JsonTokenType.String)
2621
{
2722
var str = reader.GetString();
2823
return string.IsNullOrWhiteSpace(str) ? null : new[] { str! };
2924
}
3025

26+
if (reader.TokenType != JsonTokenType.StartArray)
27+
{
28+
throw new JsonException(string.Format(System.Globalization.CultureInfo.CurrentCulture, Strings.Error_UnexpectedJsonToken, reader.TokenType));
29+
}
30+
3131
var values = new List<string>();
3232
while (reader.Read() && reader.TokenType != JsonTokenType.EndArray)
3333
{

src/NuGet.Core/NuGet.Protocol/Converters/NuGetVersionStjConverter.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,6 @@ internal sealed class NuGetVersionStjConverter : JsonConverter<NuGetVersion>
1313
{
1414
public override NuGetVersion? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
1515
{
16-
if (reader.TokenType == JsonTokenType.Null)
17-
{
18-
return null;
19-
}
20-
2116
var str = reader.GetString();
2217
return str is null ? null : NuGetVersion.Parse(str);
2318
}

src/NuGet.Core/NuGet.Protocol/Converters/PackageDependencyGroupStjConverter.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@
1111

1212
namespace NuGet.Protocol.Converters
1313
{
14-
/// <remarks>
15-
/// No explicit NSJ equivalent — NSJ relies on <c>[JsonConstructor]</c> and <c>[JsonProperty]</c> attributes
16-
/// on <see cref="PackageDependencyGroup"/> in NuGet.Packaging. STJ ignores those attributes, requiring this converter.
17-
/// </remarks>
14+
/// <remarks>No NSJ equivalent.</remarks>
1815
internal sealed class PackageDependencyGroupStjConverter : JsonConverter<PackageDependencyGroup>
1916
{
2017
private static readonly PackageDependencyStjConverter _dependencyConverter = new();

src/NuGet.Core/NuGet.Protocol/Converters/PackageDependencyStjConverter.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,15 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using System.Globalization;
56
using System.Text.Json;
67
using System.Text.Json.Serialization;
78
using NuGet.Packaging.Core;
89
using NuGet.Versioning;
910

1011
namespace NuGet.Protocol.Converters
1112
{
12-
/// <remarks>
13-
/// No explicit NSJ equivalent — NSJ relies on <c>[JsonConstructor]</c> and <c>[JsonProperty]</c> attributes
14-
/// on <see cref="PackageDependency"/> in NuGet.Packaging. STJ ignores those attributes, requiring this converter.
15-
/// </remarks>
13+
/// <remarks>No NSJ equivalent.</remarks>
1614
internal sealed class PackageDependencyStjConverter : JsonConverter<PackageDependency>
1715
{
1816
private static readonly VersionRangeStjConverter _versionRangeConverter = new();
@@ -54,6 +52,11 @@ public override PackageDependency Read(ref Utf8JsonReader reader, Type typeToCon
5452
}
5553
}
5654

55+
if (string.IsNullOrEmpty(id))
56+
{
57+
throw new JsonException(string.Format(System.Globalization.CultureInfo.CurrentCulture, Strings.Error_RequiredJsonPropertyMissing, JsonProperties.PackageId));
58+
}
59+
5760
return new PackageDependency(id!, range);
5861
}
5962

src/NuGet.Core/NuGet.Protocol/Converters/SafeUriStjConverter.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@ internal sealed class SafeUriStjConverter : JsonConverter<Uri>
1212
{
1313
public override Uri? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
1414
{
15-
if (reader.TokenType == JsonTokenType.Null)
16-
{
17-
return null;
18-
}
19-
2015
if (reader.TokenType == JsonTokenType.String)
2116
{
2217
Uri.TryCreate(reader.GetString()?.Trim(), UriKind.Absolute, out Uri? uri);

src/NuGet.Core/NuGet.Protocol/Converters/VersionInfoStjConverter.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using System.Globalization;
56
using System.Text.Json;
67
using System.Text.Json.Serialization;
78
using NuGet.Protocol.Core.Types;
@@ -46,6 +47,11 @@ public override VersionInfo Read(ref Utf8JsonReader reader, Type typeToConvert,
4647
}
4748
}
4849

50+
if (string.IsNullOrEmpty(version))
51+
{
52+
throw new JsonException(string.Format(CultureInfo.CurrentCulture, Strings.Error_RequiredJsonPropertyMissing, JsonProperties.Version));
53+
}
54+
4955
return new VersionInfo(NuGetVersion.Parse(version!), downloads);
5056
}
5157

src/NuGet.Core/NuGet.Protocol/Strings.Designer.cs

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/NuGet.Core/NuGet.Protocol/Strings.resx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,4 +543,11 @@ The "s" should be localized to the abbreviation for seconds.</comment>
543543
<value>Expected package {0} {1}, but got package {2} {3}</value>
544544
<comment>0 and 2 are package names, and 1 and 3 are version numbers</comment>
545545
</data>
546+
<data name="Error_UnexpectedJsonToken" xml:space="preserve">
547+
<value>Unexpected JSON token '{0}'.</value>
548+
</data>
549+
<data name="Error_RequiredJsonPropertyMissing" xml:space="preserve">
550+
<value>Required JSON property '{0}' is missing.</value>
551+
<comment>0 - property name</comment>
552+
</data>
546553
</root>

test/NuGet.Core.Tests/NuGet.Protocol.Tests/Converters/StjConverterTests.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,19 @@ public void MetadataStringOrArrayStjConverter_OnNullOrWhitespace_ReturnsNull(str
119119
actual.Should().BeNull();
120120
}
121121

122+
[Theory]
123+
[InlineData("42")]
124+
[InlineData("true")]
125+
[InlineData("{}")]
126+
public void MetadataStringOrArrayStjConverter_OnUnexpectedTokenType_ThrowsJsonException(string json)
127+
{
128+
// Act
129+
var act = () => Deserialize<IReadOnlyList<string>>(json, new MetadataStringOrArrayStjConverter());
130+
131+
// Assert
132+
act.Should().Throw<JsonException>();
133+
}
134+
122135
[Theory]
123136
[InlineData("""{"version":"1.0.0","downloads":12345}""", "1.0.0", 12345L)]
124137
[InlineData("""{"version":"2.0.0-beta"}""", "2.0.0-beta", null)]
@@ -166,6 +179,18 @@ public void PackageDependencyStjConverter_OnObject_ReturnsCorrectDependency(stri
166179
actual.VersionRange.Should().Be(expectedVersionRange);
167180
}
168181

182+
[Theory]
183+
[InlineData("{}")]
184+
[InlineData("""{"range":"[1.0.0, )"}""")]
185+
public void PackageDependencyStjConverter_OnMissingId_ThrowsJsonException(string json)
186+
{
187+
// Act
188+
var act = () => Deserialize<PackageDependency>(json, new PackageDependencyStjConverter());
189+
190+
// Assert
191+
act.Should().Throw<JsonException>();
192+
}
193+
169194
[Fact]
170195
public void PackageDependencyStjConverter_OnRoundTrip_PreservesValues()
171196
{

0 commit comments

Comments
 (0)