Skip to content

Commit 37587df

Browse files
authored
feat: swagger widget (openapi) (#169)
1 parent cc4f766 commit 37587df

20 files changed

+170
-1
lines changed

src/Lib/StatCan.OrchardCore.Application.Targets/StatCan.OrchardCore.Application.Targets.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<ProjectReference Include="..\..\Modules\StatCan.OrchardCore.Hackathon\StatCan.OrchardCore.Hackathon.csproj" PrivateAssets="none" />
2525
<ProjectReference Include="..\..\Modules\StatCan.OrchardCore.LocalizedText\StatCan.OrchardCore.LocalizedText.csproj" PrivateAssets="none" />
2626
<ProjectReference Include="..\..\Modules\StatCan.OrchardCore.Matomo\StatCan.OrchardCore.Matomo.csproj" PrivateAssets="none" />
27+
<ProjectReference Include="..\..\Modules\StatCan.OrchardCore.OpenAPI\StatCan.OrchardCore.OpenAPI.csproj" PrivateAssets="none" />
2728
<ProjectReference Include="..\..\Modules\StatCan.OrchardCore.Scripting\StatCan.OrchardCore.Scripting.csproj" PrivateAssets="none" />
2829
<ProjectReference Include="..\..\Modules\StatCan.OrchardCore.VueForms\StatCan.OrchardCore.VueForms.csproj" PrivateAssets="none" />
2930

src/Lib/StatCan.OrchardCore.Security/SecurityExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public static IApplicationBuilder UseStatCanSecurityHeaders(this IApplicationBui
3434
.From("unpkg.com")
3535
.From("cdnjs.cloudflare.com")
3636
.From("stackpath.bootstrapcdn.com");
37-
builder.AddConnectSrc().Self().From("cdn.jsdelivr.net").From("dpm.demdex.net").From("canada.sc.omtrdc.net"); // adobe analytics
37+
builder.AddConnectSrc().Self().From("*.statcan.ca").From("*.statcan.gc.ca").From("cdn.jsdelivr.net").From("dpm.demdex.net").From("canada.sc.omtrdc.net"); // adobe analytics
3838

3939
builder.AddScriptSrc()
4040
.UnsafeEval() // for vue-js in oc admin
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace StatCan.OrchardCore.OpenAPI
2+
{
3+
public static class FeatureIds
4+
{
5+
public const string OpenAPI = "StatCan.OrchardCore.OpenAPI";
6+
}
7+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using OrchardCore.Modules.Manifest;
2+
using static StatCan.OrchardCore.OpenAPI.FeatureIds;
3+
4+
[assembly: Module(
5+
Name = "StatCan OpenAPI",
6+
Author = "Digital Innovation Team",
7+
Website = "https://digital.statcan.gc.ca",
8+
Version = "1.0.0"
9+
)]
10+
11+
[assembly: Feature(
12+
Id = OpenAPI,
13+
Name = "StatCan.OpenAPI - Widgets",
14+
Category = "Content",
15+
Description = "Adds a widget that displays the Swagger UI",
16+
Dependencies = new[]
17+
{
18+
"OrchardCore.Widgets",
19+
"OrchardCore.Title"
20+
}
21+
)]
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System.ComponentModel;
2+
using OrchardCore.ContentManagement.Metadata;
3+
using OrchardCore.ContentManagement.Metadata.Settings;
4+
using OrchardCore.Data.Migration;
5+
using StatCan.OrchardCore.Extensions;
6+
7+
namespace StatCan.OrchardCore.OpenAPI
8+
{
9+
public class Migrations : DataMigration
10+
{
11+
private readonly IContentDefinitionManager _contentDefinitionManager;
12+
public Migrations(IContentDefinitionManager contentDefinitionManager)
13+
{
14+
_contentDefinitionManager = contentDefinitionManager;
15+
}
16+
17+
public int Create()
18+
{
19+
_contentDefinitionManager.AlterTypeDefinition("OpenAPI", type => type
20+
.DisplayedAs("OpenAPI")
21+
.Stereotype("Widget")
22+
.WithPart("TitlePart", part => part
23+
.WithSettings(new TitlePartSettings
24+
{
25+
RenderTitle = false,
26+
})
27+
)
28+
.WithPart("OpenAPI")
29+
);
30+
31+
_contentDefinitionManager.AlterPartDefinition("OpenAPI", part => part
32+
.WithField("Spec", field => field
33+
.OfType("TextField")
34+
.WithDisplayName("Spec")
35+
.WithEditor("CodeMirror")
36+
)
37+
);
38+
return 1;
39+
}
40+
41+
internal class TitlePartSettings
42+
{
43+
public int Options { get; set; }
44+
45+
public string Pattern { get; set; }
46+
47+
[DefaultValue(true)]
48+
public bool RenderTitle { get; set; }
49+
}
50+
}
51+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
using OrchardCore.Modules;
3+
using OrchardCore.ResourceManagement;
4+
using OrchardCore.Data.Migration;
5+
6+
namespace StatCan.OrchardCore.OpenAPI
7+
{
8+
[Feature(FeatureIds.OpenAPI)]
9+
public class Startup : StartupBase
10+
{
11+
public override void ConfigureServices(IServiceCollection serviceCollection) => serviceCollection.AddScoped<IDataMigration, Migrations>();
12+
}
13+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Razor">
2+
3+
<PropertyGroup>
4+
<TargetFramework>$(AspNetCoreTargetFramework)</TargetFramework>
5+
<AddRazorSupportForMvc>true</AddRazorSupportForMvc>
6+
<DefaultItemExcludes>$(DefaultItemExcludes);.git*;node_modules\**</DefaultItemExcludes>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<FrameworkReference Include="Microsoft.AspNetCore.App" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<PackageReference Include="OrchardCore.ContentManagement" Version="$(OrchardCoreVersion)" />
15+
<PackageReference Include="OrchardCore.Contents" Version="$(OrchardCoreVersion)" />
16+
<PackageReference Include="OrchardCore.DisplayManagement" Version="$(OrchardCoreVersion)" />
17+
<PackageReference Include="OrchardCore.ResourceManagement" Version="$(OrchardCoreVersion)" />
18+
</ItemGroup>
19+
20+
<ItemGroup>
21+
<ProjectReference Include="..\..\Lib\StatCan.OrchardCore.Extensions\StatCan.OrchardCore.Extensions.csproj" />
22+
</ItemGroup>
23+
24+
</Project>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{% style name:"swagger-style", src:"~/StatCan.OrchardCore.OpenAPI/Styles/swagger-ui.css", debug_src:"~/StatCan.OrchardCore.OpenAPI/Styles/swagger-ui.css" %}
2+
{% script name:"swagger", src:"~/StatCan.OrchardCore.OpenAPI/Scripts/swagger-ui-bundle.js", debug_src:"~/StatCan.OrchardCore.OpenAPI/Scripts/swagger-ui-bundle.js", at:"Foot" %}
3+
{% script name:"swagger-preset", src:"~/StatCan.OrchardCore.OpenAPI/Scripts/swagger-ui-standalone-preset.js", debug_src:"~/StatCan.OrchardCore.OpenAPI/Scripts/swagger-ui-standalone-preset.js", at:"Foot" %}
4+
{% assign generatedId = "swagger-" | append: Model.ContentItem.ContentItemId %}
5+
6+
<div id="{{ generatedId }}"></div>
7+
{% block "script", at: "Foot", depends-on:"swagger" %}
8+
window.onload = function() {
9+
// Begin Swagger UI call region
10+
const ui = SwaggerUIBundle({
11+
spec: {{ Model.ContentItem.Content.OpenAPI.Spec.Text | raw }},
12+
dom_id: '#{{generatedId}}',
13+
deepLinking: true,
14+
presets: [
15+
SwaggerUIBundle.presets.apis,
16+
SwaggerUIStandalonePreset
17+
],
18+
plugins: [
19+
SwaggerUIBundle.plugins.DownloadUrl
20+
],
21+
layout: "StandaloneLayout"
22+
})
23+
// End Swagger UI call region
24+
25+
window.ui = ui
26+
}
27+
{% endblock%}

src/Modules/StatCan.OrchardCore.OpenAPI/wwwroot/Scripts/swagger-ui-bundle.js

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Modules/StatCan.OrchardCore.OpenAPI/wwwroot/Scripts/swagger-ui-bundle.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)