Skip to content

Commit 341ebb7

Browse files
authored
Merge pull request #9 from dotarj/refactor/code-smells
fix SonarCloud code smells
2 parents d41a0cc + 49bd322 commit 341ebb7

5 files changed

Lines changed: 35 additions & 51 deletions

File tree

samples/PartialResponse.AspNetCore.Mvc.Formatters.Json.Samples/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace PartialResponse.AspNetCore.Mvc.Formatters.Json.Samples
77
{
8-
public class Program
8+
public static class Program
99
{
1010
public static void Main(string[] args)
1111
{

src/PartialResponse.AspNetCore.Mvc.Formatters.Json/Internal/MediaTypeHeaderValues.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace PartialResponse.AspNetCore.Mvc.Formatters.Json.Internal
66
{
7-
internal class MediaTypeHeaderValues
7+
internal static class MediaTypeHeaderValues
88
{
99
public static readonly MediaTypeHeaderValue ApplicationJson = MediaTypeHeaderValue.Parse("application/json").CopyAsReadOnly();
1010

src/PartialResponse.AspNetCore.Mvc.Formatters.Json/Internal/MvcPartialJsonLoggerExtensions.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,7 @@ namespace PartialResponse.AspNetCore.Mvc.Formatters.Json.Internal
77
{
88
internal static class MvcPartialJsonLoggerExtensions
99
{
10-
private static readonly Action<ILogger, string, Exception> LogMessage;
11-
12-
static MvcPartialJsonLoggerExtensions()
13-
{
14-
LogMessage = LoggerMessage.Define<string>(LogLevel.Information, 1, "Executing PartialJsonResult, writing value {Value}.");
15-
}
10+
private static readonly Action<ILogger, string, Exception> LogMessage = LogMessage = LoggerMessage.Define<string>(LogLevel.Information, 1, "Executing PartialJsonResult, writing value {Value}.");
1611

1712
public static void PartialJsonResultExecuting(this ILogger logger, object value)
1813
{

src/PartialResponse.AspNetCore.Mvc.Formatters.Json/Internal/MvcPartialJsonMvcOptionsSetup.cs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,18 @@ namespace PartialResponse.AspNetCore.Mvc.Formatters.Json.Internal
1717
/// </summary>
1818
public class MvcPartialJsonMvcOptionsSetup : IConfigureOptions<MvcOptions>
1919
{
20-
private readonly ILoggerFactory loggerFactory;
2120
private readonly IFieldsParser fieldsParser;
2221
private readonly MvcPartialJsonOptions partialJsonOptions;
2322
private readonly ArrayPool<char> charPool;
24-
private readonly ObjectPoolProvider objectPoolProvider;
2523

2624
/// <summary>
2725
/// Initializes a new instance of the <see cref="MvcPartialJsonMvcOptionsSetup"/> class.
2826
/// </summary>
29-
/// <param name="loggerFactory">The logger factory.</param>
3027
/// <param name="fieldsParser">The fields parser.</param>
3128
/// <param name="partialJsonOptions">The options.</param>
3229
/// <param name="charPool">The character array pool.</param>
33-
/// <param name="objectPoolProvider">The object pool provider.</param>
34-
public MvcPartialJsonMvcOptionsSetup(ILoggerFactory loggerFactory, IFieldsParser fieldsParser, IOptions<MvcPartialJsonOptions> partialJsonOptions, ArrayPool<char> charPool, ObjectPoolProvider objectPoolProvider)
30+
public MvcPartialJsonMvcOptionsSetup(IFieldsParser fieldsParser, IOptions<MvcPartialJsonOptions> partialJsonOptions, ArrayPool<char> charPool)
3531
{
36-
if (loggerFactory == null)
37-
{
38-
throw new ArgumentNullException(nameof(loggerFactory));
39-
}
40-
4132
if (fieldsParser == null)
4233
{
4334
throw new ArgumentNullException(nameof(fieldsParser));
@@ -53,16 +44,9 @@ public MvcPartialJsonMvcOptionsSetup(ILoggerFactory loggerFactory, IFieldsParser
5344
throw new ArgumentNullException(nameof(charPool));
5445
}
5546

56-
if (objectPoolProvider == null)
57-
{
58-
throw new ArgumentNullException(nameof(objectPoolProvider));
59-
}
60-
61-
this.loggerFactory = loggerFactory;
6247
this.fieldsParser = fieldsParser;
6348
this.partialJsonOptions = partialJsonOptions.Value;
6449
this.charPool = charPool;
65-
this.objectPoolProvider = objectPoolProvider;
6650
}
6751

6852
/// <summary>

src/PartialResponse.AspNetCore.Mvc.Formatters.Json/PartialJsonOutputFormatter.cs

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public PartialJsonOutputFormatter(JsonSerializerSettings serializerSettings, IFi
8383
protected JsonSerializerSettings SerializerSettings { get; }
8484

8585
/// <inheritdoc />
86-
public override async Task WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding)
86+
public override Task WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding)
8787
{
8888
if (context == null)
8989
{
@@ -95,31 +95,7 @@ public override async Task WriteResponseBodyAsync(OutputFormatterWriteContext co
9595
throw new ArgumentNullException(nameof(selectedEncoding));
9696
}
9797

98-
var response = context.HttpContext.Response;
99-
100-
FieldsParserResult fieldsParserResult = default;
101-
102-
if (!this.ShouldBypassPartialResponse(context.HttpContext))
103-
{
104-
fieldsParserResult = this.fieldsParser.Parse(context.HttpContext.Request);
105-
106-
if (fieldsParserResult.HasError && !this.options.IgnoreParseErrors)
107-
{
108-
response.StatusCode = 400;
109-
110-
return;
111-
}
112-
}
113-
114-
using (var writer = context.WriterFactory(response.Body, selectedEncoding))
115-
{
116-
this.WriteObject(writer, context.Object, fieldsParserResult);
117-
118-
// Perf: call FlushAsync to call WriteAsync on the stream with any content left in the TextWriter's
119-
// buffers. This is better than just letting dispose handle it (which would result in a synchronous
120-
// write).
121-
await writer.FlushAsync();
122-
}
98+
return this.WriteResponseBodyImplAsync(context, selectedEncoding);
12399
}
124100

125101
/// <summary>
@@ -153,6 +129,35 @@ protected virtual JsonSerializer CreateJsonSerializer()
153129
return this.serializer;
154130
}
155131

132+
private async Task WriteResponseBodyImplAsync(OutputFormatterWriteContext context, Encoding selectedEncoding)
133+
{
134+
var response = context.HttpContext.Response;
135+
136+
FieldsParserResult fieldsParserResult = default;
137+
138+
if (!this.ShouldBypassPartialResponse(context.HttpContext))
139+
{
140+
fieldsParserResult = this.fieldsParser.Parse(context.HttpContext.Request);
141+
142+
if (fieldsParserResult.HasError && !this.options.IgnoreParseErrors)
143+
{
144+
response.StatusCode = 400;
145+
146+
return;
147+
}
148+
}
149+
150+
using (var writer = context.WriterFactory(response.Body, selectedEncoding))
151+
{
152+
this.WriteObject(writer, context.Object, fieldsParserResult);
153+
154+
// Perf: call FlushAsync to call WriteAsync on the stream with any content left in the TextWriter's
155+
// buffers. This is better than just letting dispose handle it (which would result in a synchronous
156+
// write).
157+
await writer.FlushAsync();
158+
}
159+
}
160+
156161
private bool ShouldBypassPartialResponse(HttpContext httpContext)
157162
{
158163
if (httpContext.Items.ContainsKey(BypassPartialResponseKey))

0 commit comments

Comments
 (0)