Skip to content

Commit 6728762

Browse files
Adds support for comments in config files. Closes #600 (#602)
1 parent a97ec84 commit 6728762

33 files changed

+55
-139
lines changed
Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,18 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
using System.Text.Json.Serialization;
5-
64
namespace Microsoft.DevProxy.Abstractions;
75

86
public class GraphBatchRequestPayload
97
{
10-
[JsonPropertyName("requests")]
118
public GraphBatchRequestPayloadRequest[] Requests { get; set; } = Array.Empty<GraphBatchRequestPayloadRequest>();
129
}
1310

1411
public class GraphBatchRequestPayloadRequest
1512
{
16-
[JsonPropertyName("id")]
1713
public string Id { get; set; } = string.Empty;
18-
[JsonPropertyName("method")]
1914
public string Method { get; set; } = string.Empty;
20-
[JsonPropertyName("url")]
2115
public string Url { get; set; } = string.Empty;
22-
[JsonPropertyName("headers")]
2316
public Dictionary<string, string>? Headers { get; set; } = new Dictionary<string, string>();
24-
[JsonPropertyName("body")]
2517
public object? Body { get; set; }
2618
}
Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,28 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
using System.Text.Json.Serialization;
5-
64
namespace Microsoft.DevProxy.Abstractions;
75

86
public class GraphBatchResponsePayload
97
{
10-
[JsonPropertyName("responses")]
118
public GraphBatchResponsePayloadResponse[] Responses { get; set; } = Array.Empty<GraphBatchResponsePayloadResponse>();
129
}
1310

1411
public class GraphBatchResponsePayloadResponse
1512
{
16-
[JsonPropertyName("id")]
1713
public string Id { get; set; } = string.Empty;
18-
[JsonPropertyName("status")]
1914
public int Status { get; set; } = 200;
20-
[JsonPropertyName("body")]
2115
public dynamic? Body { get; set; }
22-
[JsonPropertyName("headers")]
2316
public Dictionary<string, string>? Headers { get; set; }
2417
}
2518

2619
public class GraphBatchResponsePayloadResponseBody
2720
{
28-
[JsonPropertyName("error")]
2921
public GraphBatchResponsePayloadResponseBodyError? Error { get; set; }
3022
}
3123

3224
public class GraphBatchResponsePayloadResponseBodyError
3325
{
34-
[JsonPropertyName("code")]
3526
public string Code { get; set; } = string.Empty;
36-
[JsonPropertyName("message")]
3727
public string Message { get; set; } = string.Empty;
3828
}

dev-proxy-abstractions/MockResponse.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,31 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
using System.Text.Json.Serialization;
5-
64
namespace Microsoft.DevProxy.Abstractions;
75

86
public class MockResponse
97
{
10-
[JsonPropertyName("request")]
118
public MockResponseRequest? Request { get; set; }
12-
[JsonPropertyName("response")]
139
public MockResponseResponse? Response { get; set; }
1410
}
1511

1612
public class MockResponseRequest
1713
{
18-
[JsonPropertyName("url")]
1914
public string Url { get; set; } = string.Empty;
20-
[JsonPropertyName("method")]
2115
public string Method { get; set; } = "GET";
22-
[JsonPropertyName("nth"), JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
2316
public int? Nth { get; set; }
2417
}
2518

2619
public class MockResponseResponse
2720
{
28-
[JsonPropertyName("statusCode")]
2921
public int? StatusCode { get; set; } = 200;
30-
[JsonPropertyName("body")]
3122
public dynamic? Body { get; set; }
32-
[JsonPropertyName("headers")]
3323
public List<MockResponseHeader>? Headers { get; set; }
3424
}
3525

3626
public class MockResponseHeader
3727
{
38-
[JsonPropertyName("name")]
3928
public string Name { get; set; } = string.Empty;
40-
[JsonPropertyName("value")]
4129
public string Value { get; set; } = string.Empty;
4230

4331
public MockResponseHeader()

dev-proxy-abstractions/ProxyUtils.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// Licensed under the MIT License.
33

44
using System.Reflection;
5+
using System.Text.Json;
6+
using System.Text.Json.Serialization;
57
using System.Text.RegularExpressions;
68
using Titanium.Web.Proxy.Http;
79

@@ -23,6 +25,14 @@ public static class ProxyUtils
2325
private static readonly Regex allAlphaRegex = new Regex("^[a-z]+$", RegexOptions.IgnoreCase);
2426
private static readonly Regex deprecationRegex = new Regex("^[a-z]+_v2$", RegexOptions.IgnoreCase);
2527
private static readonly Regex functionCallRegex = new Regex(@"^[a-z]+\(.*\)$", RegexOptions.IgnoreCase);
28+
private static readonly JsonSerializerOptions jsonSerializerOptions = new JsonSerializerOptions
29+
{
30+
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
31+
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
32+
PropertyNameCaseInsensitive = true,
33+
ReadCommentHandling = JsonCommentHandling.Skip,
34+
WriteIndented = true
35+
};
2636

2737
// doesn't end with a path separator
2838
public static string? AppFolder => Path.GetDirectoryName(AppContext.BaseDirectory);
@@ -326,4 +336,6 @@ public static void MergeHeaders(IList<MockResponseHeader> allHeaders, IList<Mock
326336
allHeaders.Add(header);
327337
}
328338
}
339+
340+
public static JsonSerializerOptions JsonSerializerOptions => jsonSerializerOptions;
329341
}

dev-proxy-plugins/Behavior/RateLimitingCustomResponseLoader.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using Microsoft.Extensions.Logging;
55
using Microsoft.DevProxy.Abstractions;
6-
using Microsoft.DevProxy.Plugins.Mocks;
76
using System.Text.Json;
87

98
namespace Microsoft.DevProxy.Plugins.Behavior;
@@ -37,7 +36,7 @@ public void LoadResponse()
3736
using (StreamReader reader = new StreamReader(stream))
3837
{
3938
var responseString = reader.ReadToEnd();
40-
var response = JsonSerializer.Deserialize<MockResponseResponse>(responseString);
39+
var response = JsonSerializer.Deserialize<MockResponseResponse>(responseString, ProxyUtils.JsonSerializerOptions);
4140
if (response is not null)
4241
{
4342
_configuration.CustomResponse = response;

dev-proxy-plugins/Behavior/RateLimitingPlugin.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ private void UpdateProxyResponse(ProxyHttpEventArgsBase e, HttpStatusCode errorS
8383
RequestId = requestId,
8484
Date = requestDate
8585
}
86-
})
86+
}),
87+
ProxyUtils.JsonSerializerOptions
8788
);
8889
}
8990

@@ -242,7 +243,7 @@ _urlsToWatch is null ||
242243
}
243244

244245
string body = _configuration.CustomResponse.Body is not null ?
245-
JsonSerializer.Serialize(_configuration.CustomResponse.Body, new JsonSerializerOptions { WriteIndented = true }) :
246+
JsonSerializer.Serialize(_configuration.CustomResponse.Body, ProxyUtils.JsonSerializerOptions) :
246247
"";
247248
e.Session.GenericResponse(body, responseCode, headers);
248249
state.HasBeenSet = true;

dev-proxy-plugins/Behavior/RetryAfterPlugin.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ private void UpdateProxyResponse(ProxyRequestArgs e, ThrottlingInfo throttlingIn
103103
RequestId = requestId,
104104
Date = requestDate
105105
}
106-
})
106+
}),
107+
ProxyUtils.JsonSerializerOptions
107108
);
108109
}
109110
else
Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
using System.Text.Json.Serialization;
54
using Microsoft.DevProxy.Abstractions;
65

76
namespace Microsoft.DevProxy.Plugins;
87

98
public class GenericErrorResponse
109
{
11-
[JsonPropertyName("statusCode")]
1210
public int StatusCode { get; set; }
13-
[JsonPropertyName("headers")]
1411
public List<MockResponseHeader>? Headers { get; set; }
15-
[JsonPropertyName("body")]
1612
public dynamic? Body { get; set; }
1713
}

dev-proxy-plugins/Guidance/ODataPagingGuidancePlugin.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ private string GetNextLinkFromJson(string responseBody)
8888

8989
try
9090
{
91-
var response = JsonSerializer.Deserialize<JsonElement>(responseBody);
91+
var response = JsonSerializer.Deserialize<JsonElement>(responseBody, ProxyUtils.JsonSerializerOptions);
9292
JsonElement nextLinkProperty = new JsonElement();
9393
if (response.TryGetProperty("@odata.nextLink", out nextLinkProperty))
9494
{

dev-proxy-plugins/Inspection/DevToolsPlugin.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ private void SocketMessageReceived(string msg)
114114

115115
try
116116
{
117-
var message = JsonSerializer.Deserialize<GetResponseBodyMessage>(msg);
117+
var message = JsonSerializer.Deserialize<GetResponseBodyMessage>(msg, ProxyUtils.JsonSerializerOptions);
118118
if (message?.Method == "Network.getResponseBody")
119119
{
120120
var requestId = message.Params?.RequestId;

0 commit comments

Comments
 (0)