Skip to content

Commit 55d960a

Browse files
Remove suppression and Fix trim warnings (#7488)
1 parent 23a043f commit 55d960a

3 files changed

Lines changed: 103 additions & 10 deletions

File tree

src/NuGet.Core/NuGet.Credentials/PluginCredentialProvider.cs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,13 @@
55

66
using System;
77
using System.Diagnostics;
8-
#if NET5_0_OR_GREATER
9-
using System.Diagnostics.CodeAnalysis;
10-
#endif
118
using System.Globalization;
129
using System.Linq;
1310
using System.Net;
1411
using System.Text;
1512
using System.Threading;
1613
using System.Threading.Tasks;
17-
using Newtonsoft.Json;
14+
using System.Text.Json;
1815
using NuGet.Common;
1916
using NuGet.Configuration;
2017

@@ -173,10 +170,6 @@ public Task<CredentialResponse> GetAsync(
173170
/// </summary>
174171
public int TimeoutSeconds { get; }
175172

176-
#if NET5_0_OR_GREATER
177-
[UnconditionalSuppressMessage("AOT", "IL2026", Justification = "Legacy command-line credential provider infrastructure; Newtonsoft.Json deserializes the concrete PluginCredentialResponse type, which is preserved here.")]
178-
[UnconditionalSuppressMessage("AOT", "IL3050", Justification = "Legacy command-line credential provider infrastructure; Newtonsoft.Json deserializes the concrete PluginCredentialResponse type, which is preserved here.")]
179-
#endif
180173
private PluginCredentialResponse GetPluginResponse(PluginCredentialRequest request,
181174
CancellationToken cancellationToken)
182175
{
@@ -216,8 +209,10 @@ private PluginCredentialResponse GetPluginResponse(PluginCredentialRequest reque
216209
try
217210
{
218211
// Mono will add utf-16 byte order mark to the start of stdOut, remove it here.
219-
credentialResponse =
220-
JsonConvert.DeserializeObject<PluginCredentialResponse>(stdOut.Trim(new char[] { '\uFEFF' }))
212+
var trimmedOutput = stdOut.Trim(new char[] { '\uFEFF' });
213+
credentialResponse = string.IsNullOrWhiteSpace(trimmedOutput)
214+
? new PluginCredentialResponse()
215+
: JsonSerializer.Deserialize(trimmedOutput, PluginCredentialResponseJsonContext.Default.PluginCredentialResponse)
221216
?? new PluginCredentialResponse();
222217
}
223218
catch (Exception)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System.Text.Json.Serialization;
5+
6+
namespace NuGet.Credentials
7+
{
8+
[JsonSourceGenerationOptions(PropertyNameCaseInsensitive = true)]
9+
[JsonSerializable(typeof(PluginCredentialResponse))]
10+
internal partial class PluginCredentialResponseJsonContext : JsonSerializerContext
11+
{
12+
}
13+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using Newtonsoft.Json;
5+
using Xunit;
6+
7+
namespace NuGet.Credentials.Test
8+
{
9+
public class PluginCredentialResponseDeserializationTests
10+
{
11+
private static PluginCredentialResponse? DeserializeWithStj(string json)
12+
{
13+
return System.Text.Json.JsonSerializer.Deserialize(json, PluginCredentialResponseJsonContext.Default.PluginCredentialResponse);
14+
}
15+
16+
private static PluginCredentialResponse? DeserializeWithNsj(string json)
17+
{
18+
return JsonConvert.DeserializeObject<PluginCredentialResponse>(json);
19+
}
20+
21+
private static void AssertResponsesEqual(PluginCredentialResponse? nsj, PluginCredentialResponse? stj)
22+
{
23+
Assert.Equal(nsj?.Username, stj?.Username);
24+
Assert.Equal(nsj?.Password, stj?.Password);
25+
Assert.Equal(nsj?.Message, stj?.Message);
26+
Assert.Equal(nsj?.AuthTypes, stj?.AuthTypes);
27+
Assert.Equal(nsj?.IsValid, stj?.IsValid);
28+
}
29+
30+
[Theory]
31+
[InlineData(@"{""Username"":""user"",""Password"":""pass"",""Message"":""msg"",""AuthTypes"":[""basic"",""digest""]}")]
32+
[InlineData(@"{""username"":""u"",""password"":""p"",""message"":""m"",""authTypes"":[""basic""]}")]
33+
[InlineData(@"{""USERNAME"":""u"",""PassWord"":""p""}")]
34+
[InlineData(@"{""Username"":""u""}")]
35+
[InlineData(@"{}")]
36+
[InlineData(@"{""Username"":null,""Password"":null,""AuthTypes"":null}")]
37+
[InlineData(@"{""Username"":""u"",""Password"":""p"",""AuthTypes"":[]}")]
38+
[InlineData(@"{""Username"":""u"",""Password"":""p"",""ExtraProp"":""val"",""Another"":123}")]
39+
[InlineData(@"{""Username"":"""",""Password"":"""",""Message"":""""}")]
40+
[InlineData(@"{""Username"":"" user "",""Password"":"" pass ""}")]
41+
[InlineData(@"{""Username"":""\u0041\u0042"",""Password"":""\u00e9""}")]
42+
[InlineData(@"{""Username"":""user"",""Password"":""p@$$w0rd!#%&*""}")]
43+
[InlineData(@"{""Username"":""u"",""Password"":""p"",""Message"":""""}")]
44+
[InlineData(@" {""Username"":""u"",""Password"":""p""} ")]
45+
[InlineData(@"{""Message"":""Extra message.""}")]
46+
[InlineData(@"{""username"":""u"", ""password"":""p"", ""Message"":""""}")]
47+
[InlineData(@"{""Username"":""u"",""Password"":""p"",""AuthTypes"":[""basic"",""digest"",""negotiate"",""ntlm""]}")]
48+
public void StjAndNsjProduceSameResult(string json)
49+
{
50+
PluginCredentialResponse? nsjResult = DeserializeWithNsj(json);
51+
PluginCredentialResponse? stjResult = DeserializeWithStj(json);
52+
53+
AssertResponsesEqual(nsjResult, stjResult);
54+
}
55+
56+
[Theory]
57+
[InlineData("null")]
58+
public void StjAndNsjBothReturnNull(string json)
59+
{
60+
PluginCredentialResponse? nsjResult = DeserializeWithNsj(json);
61+
PluginCredentialResponse? stjResult = DeserializeWithStj(json);
62+
63+
Assert.Null(nsjResult);
64+
Assert.Null(stjResult);
65+
}
66+
67+
[Theory]
68+
[InlineData("not json")]
69+
[InlineData("{invalid}")]
70+
public void StjAndNsjBothThrowOnInvalidJson(string json)
71+
{
72+
Assert.ThrowsAny<JsonReaderException>(() => DeserializeWithNsj(json));
73+
Assert.ThrowsAny<System.Text.Json.JsonException>(() => DeserializeWithStj(json));
74+
}
75+
76+
[Fact]
77+
public void StjAndNsjBothThrowOnJsonArrayInput()
78+
{
79+
var json = "[1,2,3]";
80+
81+
Assert.ThrowsAny<JsonSerializationException>(() => DeserializeWithNsj(json));
82+
Assert.ThrowsAny<System.Text.Json.JsonException>(() => DeserializeWithStj(json));
83+
}
84+
}
85+
}

0 commit comments

Comments
 (0)