Skip to content

Commit

Permalink
Use 'is [not] null' instead of ==/!= (#302)
Browse files Browse the repository at this point in the history
  • Loading branch information
satano committed Jul 18, 2024
1 parent 61488d4 commit 3066153
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 40 deletions.
11 changes: 5 additions & 6 deletions demo/OrderService/SwaggerDefaultValues.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Swashbuckle.AspNetCore.Swagger;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using System.Linq;
using Microsoft.OpenApi.Models;

namespace OrderService
{
Expand All @@ -24,7 +23,7 @@ public void Apply(OpenApiOperation operation, OperationFilterContext context)

operation.Deprecated = apiDescription.IsDeprecated();

if (operation.Parameters == null)
if (operation.Parameters is null)
{
return;
}
Expand All @@ -35,12 +34,12 @@ public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
ApiParameterDescription description = apiDescription.ParameterDescriptions.First(p => p.Name == parameter.Name);

if (parameter.Description == null)
if (parameter.Description is null)
{
parameter.Description = description.ModelMetadata?.Description;
}

if (parameter.Default == null)
if (parameter.Default is null)
{
parameter.Default = description.DefaultValue;
}
Expand All @@ -49,4 +48,4 @@ public void Apply(OpenApiOperation operation, OperationFilterContext context)
}
}
}
}
}
2 changes: 1 addition & 1 deletion demo/ProjectService/Controllers/ProjectsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public IActionResult Get(int id)
{
Project project = _projects.Value.FirstOrDefault(p => p.Id == id);

if (project != null)
if (project is not null)
{
return Ok(project);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ private Response CreateResponseSchema(
OpenApiDocument openApiDocument)
{
AggregateResponseAttribute attribute = GetAggregatorAttribute(aggregateRoute);
if (attribute != null)
if (attribute is not null)
{
OpenApiSchema reference = _schemaGenerator.GenerateSchema(attribute.ResponseType, _schemaRepository);
var response = new Response()
Expand All @@ -106,7 +106,7 @@ private Response CreateResponseSchema(
openApiDocument.Components.Schemas.Add(item.Key, item.Value);
}

if (reference.Reference != null)
if (reference.Reference is not null)
{
response.Schema = _schemaRepository.Schemas[reference.Reference.Id];
}
Expand Down
6 changes: 3 additions & 3 deletions src/MMLib.SwaggerForOcelot/Aggregates/RouteDocs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ private IEnumerable<OpenApiParameter> GetParameters()
if (Docs.ContainsKey(PathKey))
{
JToken parameters = Docs.SelectToken($"{PathKey}.{ParametersKey}");
if (parameters != null)
if (parameters is not null)
{
return parameters.ToObject<IEnumerable<OpenApiParameter>>()
.Select(d =>
{
KeyValuePair<string, string>? map =
ParametersMap?.FirstOrDefault(p => p.Value.Equals(d.Name, StringComparison.OrdinalIgnoreCase));
if (map != null && map.HasValue && map.Value.Key != null)
if (map is not null && map.HasValue && map.Value.Key is not null)
{
d.Name = map.Value.Key;
}
Expand Down Expand Up @@ -141,7 +141,7 @@ private Dictionary<string, OpenApiMediaType> CreateContent(Response response)
}

OpenApiMediaTypeEx content = response.Content[MediaTypeNames.Application.Json];
if (content == null)
if (content is null)
{
return ret;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private RouteDocs GetRouteDocs(RouteOptions route)
private static JObject CreateDocs(JObject docs, JToken paths, JProperty path)
{
var retDocs = new JObject();
if (path != null)
if (path is not null)
{
retDocs[RouteDocs.PathKey] = paths.SelectToken($"{path.Name}.get");
}
Expand All @@ -74,7 +74,7 @@ private static string GetDownstreamPath(RouteOptions route)
{
var downstreamPath = new StringBuilder(route.DownstreamPathWithSlash);

if (route.ParametersMap != null)
if (route.ParametersMap is not null)
{
foreach (KeyValuePair<string, string> map in route.ParametersMap)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Net;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Net;

namespace MMLib.SwaggerForOcelot.Configuration
{
Expand Down Expand Up @@ -59,6 +59,6 @@ public class SwaggerEndPointOptions
/// </value>
public bool TakeServersFromDownstreamService { get; set; } = false;

internal bool IsGatewayItSelf => Config != null && Config.Any(c => c.IsGatewayItSelf);
internal bool IsGatewayItSelf => Config is not null && Config.Any(c => c.IsGatewayItSelf);
}
}
26 changes: 13 additions & 13 deletions src/MMLib.SwaggerForOcelot/Middleware/SwaggerForOcelotMiddleware.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
using Kros.Utils;
using Kros.Utils;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Writers;
using MMLib.SwaggerForOcelot.Configuration;
using MMLib.SwaggerForOcelot.Repositories;
using MMLib.SwaggerForOcelot.ServiceDiscovery;
using MMLib.SwaggerForOcelot.Transformation;
using Swashbuckle.AspNetCore.Swagger;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MMLib.SwaggerForOcelot.Repositories;
using Swashbuckle.AspNetCore.Swagger;
using System.IO;
using System.Globalization;
using Microsoft.OpenApi.Writers;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.OpenApi.Models;
using MMLib.SwaggerForOcelot.ServiceDiscovery;
using System.Threading.Tasks;

namespace MMLib.SwaggerForOcelot.Middleware
{
Expand Down Expand Up @@ -71,7 +71,7 @@ public async Task Invoke(HttpContext context,
{
(string version, SwaggerEndPointOptions endPoint) = GetEndPoint(context.Request.Path, swaggerEndPointRepository);

if (_downstreamInterceptor != null &&
if (_downstreamInterceptor is not null &&
!_downstreamInterceptor.DoDownstreamSwaggerEndpoint(context, version, endPoint))
{
return;
Expand Down Expand Up @@ -137,18 +137,18 @@ private string GetServerName(HttpContext context, SwaggerEndPointOptions endPoin

private async Task<string> ReconfigureUpstreamSwagger(HttpContext context, string swaggerJson)
{
if (_options.ReConfigureUpstreamSwaggerJson != null && _options.ReConfigureUpstreamSwaggerJsonAsync != null)
if (_options.ReConfigureUpstreamSwaggerJson is not null && _options.ReConfigureUpstreamSwaggerJsonAsync is not null)
{
throw new Exception(
"Both ReConfigureUpstreamSwaggerJson and ReConfigureUpstreamSwaggerJsonAsync cannot have a value. Only use one method.");
}

if (_options.ReConfigureUpstreamSwaggerJson != null)
if (_options.ReConfigureUpstreamSwaggerJson is not null)
{
return _options.ReConfigureUpstreamSwaggerJson(context, swaggerJson);
}

if (_options.ReConfigureUpstreamSwaggerJsonAsync != null)
if (_options.ReConfigureUpstreamSwaggerJsonAsync is not null)
{
return await _options.ReConfigureUpstreamSwaggerJsonAsync(context, swaggerJson);
}
Expand Down
2 changes: 1 addition & 1 deletion src/MMLib.SwaggerForOcelot/RouteOptionsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static IEnumerable<RouteOptions> GroupByPaths(this IEnumerable<RouteOptio
route.DownstreamPathTemplate,
p.Key.VirtualDirectory,
route.DangerousAcceptAnyServerCertificateValidator,
p.Where(r => r.UpstreamHttpMethod != null).SelectMany(r => r.UpstreamHttpMethod))
p.Where(r => r.UpstreamHttpMethod is not null).SelectMany(r => r.UpstreamHttpMethod))
{
DownstreamHttpVersion = route.DownstreamHttpVersion,
DownstreamScheme = route.DownstreamScheme,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ private async Task<Uri> GetSwaggerUri(SwaggerEndPointConfig endPoint, RouteOptio
}

private string GetScheme(ServiceHostAndPort service, RouteOptions route)
=> (route != null && !route.DownstreamScheme.IsNullOrEmpty())
=> (route is not null && !route.DownstreamScheme.IsNullOrEmpty())
? route.DownstreamScheme
: !service.Scheme.IsNullOrEmpty()
? service.Scheme
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ private string TransformSwagger(
AddHost(swagger, hostOverride);
}

if (paths != null)
if (paths is not null)
{
RenameAndRemovePaths(routes, paths, basePath);

Expand All @@ -100,7 +100,7 @@ private string TransformSwagger(
i => $"$..[?(@*.*.allOf[?(@.$ref == '#/{SwaggerProperties.Definitions}/{i.Name}')])]",
i => $"$..allOf[?(@.$ref == '#/{SwaggerProperties.Definitions}/{i.Name}')]",
i => $"$..[?(@*.*.oneOf[?(@.$ref == '#/{SwaggerProperties.Definitions}/{i.Name}')])]");
if (swagger["tags"] != null)
if (swagger["tags"] is not null)
{
RemoveItems<JObject>(
swagger[SwaggerProperties.Tags],
Expand Down Expand Up @@ -136,12 +136,12 @@ private string TransformOpenApi(
}

JToken paths = openApi[OpenApiProperties.Paths];
if (paths != null)
if (paths is not null)
{
RenameAndRemovePaths(routes, paths, downstreamBasePath);

JToken schemaToken = openApi[OpenApiProperties.Components][OpenApiProperties.Schemas];
if (endPointOptions.RemoveUnusedComponentsFromScheme && schemaToken != null)
if (endPointOptions.RemoveUnusedComponentsFromScheme && schemaToken is not null)
{
RemoveItems<JProperty>(schemaToken,
paths,
Expand All @@ -154,7 +154,7 @@ private string TransformOpenApi(
i => $"$..[?(@*.*.oneOf[?(@.$ref == '#/{OpenApiProperties.Components}/{OpenApiProperties.Schemas}/{i.Name}')])]");
}

if (endPointOptions.RemoveUnusedComponentsFromScheme && openApi["tags"] != null)
if (endPointOptions.RemoveUnusedComponentsFromScheme && openApi["tags"] is not null)
{
RemoveItems<JObject>(
openApi[OpenApiProperties.Tags],
Expand Down Expand Up @@ -348,7 +348,7 @@ private static void TransformServerPaths(JObject openApi, string serverOverride,

foreach (JToken server in openApi.GetValue(OpenApiProperties.Servers))
{
if (server[OpenApiProperties.Url] != null)
if (server[OpenApiProperties.Url] is not null)
{
server[OpenApiProperties.Url] = serverOverride.RemoveSlashFromEnd();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using Swashbuckle.AspNetCore.SwaggerGen;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mime;
using Xunit;

namespace MMLib.SwaggerForOcelot.Tests.Aggregates
Expand Down Expand Up @@ -284,7 +283,7 @@ private JObject CreateDocs(string summary, JArray parameters = null, JObject res
{
var path = new JObject(new JProperty(RouteDocs.SummaryKey, summary));

if (parameters != null)
if (parameters is not null)
{
path.Add(RouteDocs.ParametersKey, parameters);
}
Expand Down

0 comments on commit 3066153

Please sign in to comment.