Skip to content

Commit b669fd2

Browse files
committed
This is gonna be alot of work
1 parent 35b530d commit b669fd2

19 files changed

Lines changed: 125 additions & 378 deletions

‎API/Program.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
using OpenShock.Common;
1212
using OpenShock.Common.Extensions;
1313
using OpenShock.Common.Hubs;
14+
using OpenShock.Common.OpenAPI;
1415
using OpenShock.Common.Services;
1516
using OpenShock.Common.Services.Device;
1617
using OpenShock.Common.Services.LCGNodeProvisioner;
1718
using OpenShock.Common.Services.Ota;
18-
using OpenShock.Common.Swagger;
1919
using Serilog;
2020
using OAuthConstants = OpenShock.API.OAuth.OAuthConstants;
2121

@@ -103,7 +103,7 @@ static void DefaultOptions(RemoteAuthenticationOptions options, string provider)
103103
builder.Services.AddScoped<IUserService, UserService>();
104104
builder.Services.AddScoped<ILCGNodeProvisioner, LCGNodeProvisioner>();
105105

106-
builder.AddSwaggerExt<Program>();
106+
builder.Services.AddOpenApiExt<Program>();
107107

108108
builder.AddCloudflareTurnstileService();
109109
builder.AddEmailService();

‎Common/Common.csproj‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
</PackageReference>
1414
<PackageReference Include="MessagePack" />
1515
<PackageReference Include="Microsoft.AspNetCore.DataProtection.EntityFrameworkCore" />
16+
<PackageReference Include="Microsoft.AspNetCore.OpenApi" />
1617
<PackageReference Include="Microsoft.Extensions.Caching.Hybrid" />
1718
<PackageReference Include="Microsoft.IO.RecyclableMemoryStream" />
1819
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" />
@@ -31,7 +32,6 @@
3132
<PackageReference Include="OneOf" />
3233
<PackageReference Include="Serilog.AspNetCore" />
3334
<PackageReference Include="Serilog.Sinks.OpenTelemetry" />
34-
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" />
3535
<PackageReference Include="Z.EntityFramework.Plus.EFCore" />
3636
</ItemGroup>
3737

‎Common/DataAnnotations/EmailAddressAttribute.cs‎

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using System.ComponentModel.DataAnnotations;
22
using System.Net.Mail;
3-
using Microsoft.OpenApi;
43
using OpenShock.Common.Constants;
5-
using OpenShock.Common.DataAnnotations.Interfaces;
64

75
namespace OpenShock.Common.DataAnnotations;
86

@@ -13,7 +11,7 @@ namespace OpenShock.Common.DataAnnotations;
1311
/// Inherits from <see cref="ValidationAttribute"/>.
1412
/// </remarks>
1513
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter)]
16-
public sealed class EmailAddressAttribute : ValidationAttribute, IParameterAttribute
14+
public sealed class EmailAddressAttribute : ValidationAttribute
1715
{
1816
/// <summary>
1917
/// Example value used to generate OpenApi documentation.
@@ -54,15 +52,4 @@ public sealed class EmailAddressAttribute : ValidationAttribute, IParameterAttri
5452

5553
return ValidationResult.Success;
5654
}
57-
58-
/// <inheritdoc/>
59-
public void Apply(OpenApiSchema schema)
60-
{
61-
//if (ShouldValidate) schema.Pattern = ???;
62-
63-
schema.Example = ExampleValue;
64-
}
65-
66-
/// <inheritdoc/>
67-
public void Apply(OpenApiParameter parameter) => Apply(parameter.Schema);
6855
}

‎Common/DataAnnotations/Interfaces/IOperationAttribute.cs‎

Lines changed: 0 additions & 15 deletions
This file was deleted.

‎Common/DataAnnotations/Interfaces/IParameterAttribute.cs‎

Lines changed: 0 additions & 21 deletions
This file was deleted.

‎Common/DataAnnotations/OpenApiSchemas.cs‎

Lines changed: 0 additions & 32 deletions
This file was deleted.

‎Common/DataAnnotations/PasswordAttribute.cs‎

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
using System.ComponentModel.DataAnnotations;
2-
using Microsoft.OpenApi;
32
using OpenShock.Common.Constants;
4-
using OpenShock.Common.DataAnnotations.Interfaces;
53

64
namespace OpenShock.Common.DataAnnotations;
75

@@ -12,7 +10,7 @@ namespace OpenShock.Common.DataAnnotations;
1210
/// Inherits from <see cref="ValidationAttribute"/>.
1311
/// </remarks>
1412
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter)]
15-
public sealed class PasswordAttribute : ValidationAttribute, IParameterAttribute
13+
public sealed class PasswordAttribute : ValidationAttribute
1614
{
1715
/// <summary>
1816
/// Example value used to generate OpenApi documentation.
@@ -53,15 +51,4 @@ public sealed class PasswordAttribute : ValidationAttribute, IParameterAttribute
5351

5452
return ValidationResult.Success;
5553
}
56-
57-
/// <inheritdoc/>
58-
public void Apply(OpenApiSchema schema)
59-
{
60-
//if (ShouldValidate) schema.Pattern = ???;
61-
62-
schema.Example = ExampleValue;
63-
}
64-
65-
/// <inheritdoc/>
66-
public void Apply(OpenApiParameter parameter) => Apply(parameter.Schema);
6754
}

‎Common/DataAnnotations/StringCollectionItemMaxLengthAttribute.cs‎

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

33
namespace OpenShock.Common.DataAnnotations;
44

5-
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false, Inherited = true)]
5+
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
66
public sealed class StringCollectionItemMaxLengthAttribute : ValidationAttribute
77
{
88
public StringCollectionItemMaxLengthAttribute(int maxLength)

‎Common/DataAnnotations/UsernameAttribute.cs‎

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
using System.ComponentModel.DataAnnotations;
2-
using Microsoft.OpenApi;
3-
using OpenShock.Common.DataAnnotations.Interfaces;
42
using OpenShock.Common.Validation;
53

64
namespace OpenShock.Common.DataAnnotations;
@@ -12,7 +10,7 @@ namespace OpenShock.Common.DataAnnotations;
1210
/// Inherits from <see cref="ValidationAttribute"/>.
1311
/// </remarks>
1412
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter)]
15-
public sealed class UsernameAttribute : ValidationAttribute, IParameterAttribute
13+
public sealed class UsernameAttribute : ValidationAttribute
1614
{
1715
/// <summary>
1816
/// Example value used to generate OpenApi documentation.
@@ -49,15 +47,4 @@ public sealed class UsernameAttribute : ValidationAttribute, IParameterAttribute
4947
error => new ValidationResult($"{error.Type} - {error.Message}")
5048
);
5149
}
52-
53-
/// <inheritdoc/>
54-
public void Apply(OpenApiSchema schema)
55-
{
56-
//if (ShouldValidate) schema.Pattern = ???;
57-
58-
schema.Example = ExampleValue;
59-
}
60-
61-
/// <inheritdoc/>
62-
public void Apply(OpenApiParameter parameter) => Apply(parameter.Schema);
6350
}

‎Common/OpenAPI/DocumentDefaults.cs‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using Microsoft.AspNetCore.OpenApi;
2+
using Microsoft.OpenApi;
3+
4+
namespace OpenShock.Common.OpenAPI;
5+
6+
public static class DocumentDefaults
7+
{
8+
public static Func<OpenApiDocument, OpenApiDocumentTransformerContext, CancellationToken, Task> GetDocumentTransformer(string version)
9+
{
10+
return (document, context, _) =>
11+
{
12+
var env = context.ApplicationServices.GetRequiredService<IHostEnvironment>();
13+
14+
document.Info = new OpenApiInfo
15+
{
16+
Title = "OpenShock API",
17+
// Summary = ...
18+
// Description = ...
19+
Version = version,
20+
// TermsOfService = ...
21+
// Contact = ...
22+
// License = ...
23+
};
24+
25+
document.Servers =
26+
[
27+
new OpenApiServer { Url = "https://api.openshock.app" },
28+
new OpenApiServer { Url = "https://api.openshock.dev" }
29+
];
30+
if (env.IsDevelopment())
31+
{
32+
document.Servers.Add(new OpenApiServer { Url = "https://localhost" });
33+
}
34+
35+
document.Components ??= new OpenApiComponents();
36+
document.Components.SecuritySchemes = new Dictionary<string, IOpenApiSecurityScheme>
37+
{
38+
{
39+
"ApiToken",
40+
new OpenApiSecurityScheme
41+
{
42+
Name = "OpenShockToken",
43+
Description = "Enter API Token",
44+
Type = SecuritySchemeType.ApiKey,
45+
In = ParameterLocation.Header
46+
}
47+
},
48+
{
49+
"HubToken",
50+
new OpenApiSecurityScheme
51+
{
52+
Name = "DeviceToken",
53+
Description = "Enter hub token",
54+
Type = SecuritySchemeType.ApiKey,
55+
In = ParameterLocation.Header
56+
}
57+
},
58+
{
59+
"UserSessionCookie",
60+
new OpenApiSecurityScheme
61+
{
62+
Name = "openShockSession",
63+
Description = "Enter user session cookie",
64+
Type = SecuritySchemeType.ApiKey,
65+
In = ParameterLocation.Cookie
66+
}
67+
}
68+
};
69+
70+
return Task.CompletedTask;
71+
};
72+
}
73+
}

0 commit comments

Comments
 (0)