Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions src/Simplify.Web.Swagger/ControllerAction.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
using System;
using System.Collections.Generic;
#if NET10_0
using Microsoft.OpenApi;
using NetHttpMethod = System.Net.Http.HttpMethod;
#else
using Microsoft.OpenApi.Models;
#endif
using Simplify.Web.Controllers.Meta.Routing;

namespace Simplify.Web.Swagger;
Expand Down Expand Up @@ -46,25 +42,16 @@ public class ControllerAction
/// <value>
/// The type.
/// </value>
#if NET10_0
public NetHttpMethod Type { get; set; } = NetHttpMethod.Get;
#else
public OperationType Type { get; set; }
#endif

/// <summary>
/// Gets the path.
/// </summary>
/// <value>
/// The path.
/// </value>
#if NETSTANDARD2_0
public string Path => ControllerRoute.Path.StartsWith("/") ? ControllerRoute.Path : "/" + ControllerRoute.Path;
#else
public string Path => ControllerRoute.Path.StartsWith('/') ? ControllerRoute.Path : "/" + ControllerRoute.Path;

#endif

/// <summary>
/// Gets or sets the controller route.
/// </summary>
Expand Down
20 changes: 0 additions & 20 deletions src/Simplify.Web.Swagger/ControllerActionsFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,8 @@
using Simplify.Web.Controllers.Meta.Routing;
using Simplify.Web.Http;
using Swashbuckle.AspNetCore.SwaggerGen;
#if NET10_0
using Microsoft.OpenApi;
using NetHttpMethod = System.Net.Http.HttpMethod;
#else
using Microsoft.OpenApi.Models;
#endif

namespace Simplify.Web.Swagger;

Expand Down Expand Up @@ -127,7 +123,6 @@ private static string FormatNameSource(string str)
return str;
}

#if NET10_0
private static NetHttpMethod HttpMethodToOperationType(HttpMethod method) =>
method switch
{
Expand All @@ -139,19 +134,6 @@ private static NetHttpMethod HttpMethodToOperationType(HttpMethod method) =>
HttpMethod.Options => NetHttpMethod.Options,
_ => NetHttpMethod.Get,
};
#else
private static OperationType HttpMethodToOperationType(HttpMethod method) =>
method switch
{
HttpMethod.Get => OperationType.Get,
HttpMethod.Post => OperationType.Post,
HttpMethod.Put => OperationType.Put,
HttpMethod.Patch => OperationType.Patch,
HttpMethod.Delete => OperationType.Delete,
HttpMethod.Options => OperationType.Options,
_ => OperationType.Get,
};
#endif

private static OpenApiRequestBody CreateRequestBody(
Type controllerType,
Expand Down Expand Up @@ -205,9 +187,7 @@ DocumentFilterContext context

foreach (var item in producesResponse.ContentTypes.Distinct())
{
#if NET10_0
response.Content ??= new Dictionary<string, OpenApiMediaType>();
#endif
response.Content.Add(
item,
producesResponse.Type is null
Expand Down
27 changes: 2 additions & 25 deletions src/Simplify.Web.Swagger/EnumNamesSchemaFilter.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
using System;
using System.Linq;
using Swashbuckle.AspNetCore.SwaggerGen;
#if NET10_0
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Nodes;
using Microsoft.OpenApi;
#else
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
#endif
using Swashbuckle.AspNetCore.SwaggerGen;

namespace Simplify.Web.Swagger;

Expand All @@ -18,7 +13,6 @@ namespace Simplify.Web.Swagger;
public class EnumNamesSchemaFilter : ISchemaFilter
{
/// <inheritdoc />
#if NET10_0
public void Apply(IOpenApiSchema schema, SchemaFilterContext context)
{
if (!context.Type.IsEnum)
Expand All @@ -38,23 +32,6 @@ public void Apply(IOpenApiSchema schema, SchemaFilterContext context)
concreteSchema.Extensions["x-varnames"] = new JsonNodeExtension(varnames);
concreteSchema.Description = BuildDescription(names, values);
}
#else
public void Apply(OpenApiSchema schema, SchemaFilterContext context)
{
if (!context.Type.IsEnum)
return;

var names = Enum.GetNames(context.Type);
var values = Enum.GetValues(context.Type).Cast<object>().ToArray();

var varnames = new OpenApiArray();
foreach (var name in names)
varnames.Add(new OpenApiString(name));

schema.Extensions["x-varnames"] = varnames;
schema.Description = BuildDescription(names, values);
}
#endif

private static string BuildDescription(string[] names, object[] values) =>
string.Join(", ", names.Select((name, i) => $"{Convert.ToInt64(values[i])} = {name}"));
Expand Down
7 changes: 1 addition & 6 deletions src/Simplify.Web.Swagger/Simplify.Web.Swagger.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,10 @@

<PackageReleaseNotes>See https://github.com/SimplifyNet/Simplify.Web.Swagger/tree/master/src/Simplify.Web.Swagger/CHANGELOG.md for details</PackageReleaseNotes>
</PropertyGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net8.0' Or '$(TargetFramework)' == 'net9.0' Or '$(TargetFramework)' == 'net10.0' ">
<ItemGroup>
<PackageReference Include="Asp.Versioning.Mvc" Version="8.1.1" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net10.0' ">
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="10.2.1" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net8.0' Or '$(TargetFramework)' == 'net9.0' ">
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="9.0.6" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Simplify.Web" Version="5.2" />
</ItemGroup>
Expand Down
53 changes: 0 additions & 53 deletions src/Simplify.Web.Swagger/SimplifyWebDocumentFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@
using System.Linq;
using Simplify.Web.Controllers.Meta.Routing;
using Swashbuckle.AspNetCore.SwaggerGen;
#if NET10_0
using Microsoft.OpenApi;
using JsonNode = System.Text.Json.Nodes.JsonNode;
#else
using Microsoft.OpenApi.Models;
#endif

namespace Simplify.Web.Swagger;

Expand Down Expand Up @@ -55,19 +51,11 @@ var item in controllerActions
PopulateDocumentTags(swaggerDoc, controllerActions);
}

#if NET10_0
private static IList<IOpenApiParameter> CreateParameters(ControllerAction item, DocumentFilterContext context) =>
item.ControllerRoute.Items
.OfType<PathParameter>()
.Select(x => (IOpenApiParameter)CreatePathParameter(x, item, context))
.ToList();
#else
private static IList<OpenApiParameter> CreateParameters(ControllerAction item, DocumentFilterContext context) =>
item.ControllerRoute.Items
.OfType<PathParameter>()
.Select(x => CreatePathParameter(x, item, context))
.ToList();
#endif

private static OpenApiParameter CreatePathParameter(PathParameter pathParam, ControllerAction item, DocumentFilterContext context)
{
Expand Down Expand Up @@ -103,21 +91,15 @@ private OpenApiOperation CreateOperation(ControllerAction item, OpenApiDocument
{
var operation = new OpenApiOperation();

#if NET10_0
operation.Tags ??= new HashSet<OpenApiTagReference>();
operation.Tags.Add(new OpenApiTagReference(item.Names.GroupName));
#else
operation.Tags.Add(new OpenApiTag { Name = item.Names.GroupName });
#endif

if (item.Names.Summary != null)
operation.Summary = item.Names.Summary;

foreach (var response in item.Responses)
{
#if NET10_0
operation.Responses ??= new OpenApiResponses();
#endif
operation.Responses.Add(response.Key.ToString(), response.Value);
}

Expand All @@ -130,33 +112,13 @@ private OpenApiOperation CreateOperation(ControllerAction item, OpenApiDocument
{
var schemeNames = ResolveSecuritySchemeNames(swaggerDoc);

#if NET10_0
if (schemeNames.Count > 0)
operation.Security = schemeNames
.Select(name => new OpenApiSecurityRequirement
{
[new OpenApiSecuritySchemeReference(name, swaggerDoc)] = [],
})
.ToList();
#else
if (schemeNames.Count > 0)
operation.Security = schemeNames
.Select(name => new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = name,
},
},
new List<string>()
},
})
.ToList();
#endif
}

if (_args == null)
Expand Down Expand Up @@ -190,21 +152,12 @@ private static OpenApiParameter CreateAcceptLanguageParameter(AcceptLanguageHead
AllowEmptyValue = true,
};

#if NET10_0
param.Example = args.Default;
param.Schema = new OpenApiSchema
{
Default = args.Default,
Enum = args.Languages.Select(l => (JsonNode)l).ToList()
};
#else
param.Example = new Microsoft.OpenApi.Any.OpenApiString(args.Default);
param.Schema = new OpenApiSchema
{
Default = new Microsoft.OpenApi.Any.OpenApiString(args.Default),
Enum = args.Languages.Select(l => (Microsoft.OpenApi.Any.IOpenApiAny)new Microsoft.OpenApi.Any.OpenApiString(l)).ToList()
};
#endif

return param;
}
Expand All @@ -214,12 +167,8 @@ private static void PopulateDocumentTags(
IEnumerable<ControllerAction> actions
)
{
#if NET10_0
var existingNames =
swaggerDoc.Tags?.Select(t => t.Name).ToHashSet(StringComparer.Ordinal) ?? [];
#else
var existingNames = swaggerDoc.Tags.Select(t => t.Name).ToHashSet(StringComparer.Ordinal);
#endif

foreach (
var name in actions
Expand All @@ -228,9 +177,7 @@ var name in actions
.Where(n => !existingNames.Contains(n))
)
{
#if NET10_0
swaggerDoc.Tags ??= new HashSet<OpenApiTag>();
#endif
swaggerDoc.Tags.Add(new OpenApiTag { Name = name });
}
}
Expand Down
4 changes: 0 additions & 4 deletions src/Simplify.Web.Swagger/SimplifyWebSwaggerArgs.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
using System.Collections.Generic;
#if NET10_0
using Microsoft.OpenApi;
#else
using Microsoft.OpenApi.Models;
#endif

namespace Simplify.Web.Swagger;

Expand Down
6 changes: 1 addition & 5 deletions src/TesterApp/Program.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
using Microsoft.OpenApi;
using Simplify.DI;
using Simplify.Web;
using Simplify.Web.Swagger;
using TesterApp.Setup;
#if NET10_0
using Microsoft.OpenApi;
#else
using Microsoft.OpenApi.Models;
#endif

var builder = WebApplication.CreateBuilder(args);

Expand Down
Loading