Skip to content

Commit 0128f42

Browse files
committed
Update NSJ version and Fix trim warnings
1 parent 97c0fe1 commit 0128f42

3 files changed

Lines changed: 105 additions & 3 deletions

File tree

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
using System.Text;
1515
using System.Threading;
1616
using System.Threading.Tasks;
17-
using Newtonsoft.Json;
17+
using System.Text.Json;
1818
using NuGet.Common;
1919
using NuGet.Configuration;
2020

@@ -216,8 +216,10 @@ private PluginCredentialResponse GetPluginResponse(PluginCredentialRequest reque
216216
try
217217
{
218218
// 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' }))
219+
var trimmedOutput = stdOut.Trim(new char[] { '\uFEFF' });
220+
credentialResponse = string.IsNullOrWhiteSpace(trimmedOutput)
221+
? new PluginCredentialResponse()
222+
: JsonSerializer.Deserialize(trimmedOutput, PluginCredentialResponseJsonContext.Default.PluginCredentialResponse)
221223
?? new PluginCredentialResponse();
222224
}
223225
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: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
#nullable disable
5+
6+
using Newtonsoft.Json;
7+
using Xunit;
8+
9+
namespace NuGet.Credentials.Test
10+
{
11+
public class PluginCredentialResponseDeserializationTests
12+
{
13+
private static PluginCredentialResponse DeserializeWithStj(string json)
14+
{
15+
return System.Text.Json.JsonSerializer.Deserialize(json, PluginCredentialResponseJsonContext.Default.PluginCredentialResponse);
16+
}
17+
18+
private static PluginCredentialResponse DeserializeWithNsj(string json)
19+
{
20+
return JsonConvert.DeserializeObject<PluginCredentialResponse>(json);
21+
}
22+
23+
private static void AssertResponsesEqual(PluginCredentialResponse nsj, PluginCredentialResponse stj)
24+
{
25+
Assert.Equal(nsj?.Username, stj?.Username);
26+
Assert.Equal(nsj?.Password, stj?.Password);
27+
Assert.Equal(nsj?.Message, stj?.Message);
28+
Assert.Equal(nsj?.AuthTypes, stj?.AuthTypes);
29+
Assert.Equal(nsj?.IsValid, stj?.IsValid);
30+
}
31+
32+
[Theory]
33+
[InlineData(@"{""Username"":""user"",""Password"":""pass"",""Message"":""msg"",""AuthTypes"":[""basic"",""digest""]}")]
34+
[InlineData(@"{""username"":""u"",""password"":""p"",""message"":""m"",""authTypes"":[""basic""]}")]
35+
[InlineData(@"{""USERNAME"":""u"",""PassWord"":""p""}")]
36+
[InlineData(@"{""Username"":""u""}")]
37+
[InlineData(@"{}")]
38+
[InlineData(@"{""Username"":null,""Password"":null,""AuthTypes"":null}")]
39+
[InlineData(@"{""Username"":""u"",""Password"":""p"",""AuthTypes"":[]}")]
40+
[InlineData(@"{""Username"":""u"",""Password"":""p"",""ExtraProp"":""val"",""Another"":123}")]
41+
[InlineData(@"{""Username"":"""",""Password"":"""",""Message"":""""}")]
42+
[InlineData(@"{""Username"":"" user "",""Password"":"" pass ""}")]
43+
[InlineData(@"{""Username"":""\u0041\u0042"",""Password"":""\u00e9""}")]
44+
[InlineData(@"{""Username"":""user"",""Password"":""p@$$w0rd!#%&*""}")]
45+
[InlineData(@"{""Username"":""u"",""Password"":""p"",""Message"":""""}")]
46+
[InlineData(@" {""Username"":""u"",""Password"":""p""} ")]
47+
[InlineData(@"{""Message"":""Extra message.""}")]
48+
[InlineData(@"{""username"":""u"", ""password"":""p"", ""Message"":""""}")]
49+
[InlineData(@"{""Username"":""u"",""Password"":""p"",""AuthTypes"":[""basic"",""digest"",""negotiate"",""ntlm""]}")]
50+
public void StjAndNsjProduceSameResult(string json)
51+
{
52+
var nsjResult = DeserializeWithNsj(json);
53+
var stjResult = DeserializeWithStj(json);
54+
55+
AssertResponsesEqual(nsjResult, stjResult);
56+
}
57+
58+
[Theory]
59+
[InlineData("null")]
60+
public void StjAndNsjBothReturnNull(string json)
61+
{
62+
var nsjResult = DeserializeWithNsj(json);
63+
var stjResult = DeserializeWithStj(json);
64+
65+
Assert.Null(nsjResult);
66+
Assert.Null(stjResult);
67+
}
68+
69+
[Theory]
70+
[InlineData("not json")]
71+
[InlineData("{invalid}")]
72+
public void StjAndNsjBothThrowOnInvalidJson(string json)
73+
{
74+
Assert.ThrowsAny<JsonReaderException>(() => DeserializeWithNsj(json));
75+
Assert.ThrowsAny<System.Text.Json.JsonException>(() => DeserializeWithStj(json));
76+
}
77+
78+
[Fact]
79+
public void StjAndNsjBothThrowOnJsonArrayInput()
80+
{
81+
var json = "[1,2,3]";
82+
83+
Assert.ThrowsAny<JsonSerializationException>(() => DeserializeWithNsj(json));
84+
Assert.ThrowsAny<System.Text.Json.JsonException>(() => DeserializeWithStj(json));
85+
}
86+
}
87+
}

0 commit comments

Comments
 (0)