Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DIFF #207 [FEATURE] Generate classes based on AsyncAPI document #3

Draft
wants to merge 11 commits into
base: #197-more-logging
Choose a base branch
from
Draft
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
4 changes: 3 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ jobs:
uses: ./.github/npm
- name: Run dotnet build src
run: dotnet build ./src/Saunter-src.slnf --configuration Debug
- name: Run dotnet restore
run: dotnet restore
- name: Run dotnet build all
run: dotnet build --configuration Debug
run: dotnet build --configuration Debug --no-restore
- name: unit test
run: dotnet test --no-build --configuration Debug

Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -598,3 +598,5 @@ fabric.properties

# End of https://www.gitignore.io/api/csharp,jetbrains,visualstudio
/examples/StreetlightsAPI/Properties/launchSettings.json

*.g.cs
51 changes: 51 additions & 0 deletions Saunter.sln
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,23 @@ EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AsyncAPI.Saunter.Generator.Cli.Tests", "test\AsyncAPI.Saunter.Generator.Cli.Tests\AsyncAPI.Saunter.Generator.Cli.Tests.csproj", "{18AD0249-0436-4A26-9972-B97BA6905A54}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StreetlightsAPI.TopLevelStatement", "examples\StreetlightsAPI.TopLevelStatement\StreetlightsAPI.TopLevelStatement.csproj", "{6F6B8B03-9045-46EC-AE12-E7ADA492F9FA}"
ProjectSection(ProjectDependencies) = postProject
{A320E670-5CB0-4815-AF67-D8D09FC92A2A} = {A320E670-5CB0-4815-AF67-D8D09FC92A2A}
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AsyncAPI.Saunter.Generator.Build", "src\AsyncAPI.Saunter.Generator.Build\AsyncAPI.Saunter.Generator.Build.csproj", "{A320E670-5CB0-4815-AF67-D8D09FC92A2A}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AsyncAPI.Saunter.Generator.Build.Tests", "test\AsyncAPI.Saunter.Generator.Build.Tests\AsyncAPI.Saunter.Generator.Build.Tests.csproj", "{61142B10-7B49-436E-AE32-2737658BD1E5}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StreetlightsAPI.AsyncApiSpecFirst", "examples\StreetlightsAPI.AsyncApiSpecFirst\StreetlightsAPI.AsyncApiSpecFirst.csproj", "{19A30A6D-1E91-44FD-BB5D-428D12D0160D}"
ProjectSection(ProjectDependencies) = postProject
{A320E670-5CB0-4815-AF67-D8D09FC92A2A} = {A320E670-5CB0-4815-AF67-D8D09FC92A2A}
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AsyncAPI.Saunter.Generator.SourceGenerator", "src\AsyncAPI.Saunter.Generator.SourceGenerator\AsyncAPI.Saunter.Generator.SourceGenerator.csproj", "{6ADD07FE-B6D1-42A9-A618-E7AA102E38A0}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AsyncAPI.Saunter.Generator", "src\AsyncAPI.Saunter.Generator\AsyncAPI.Saunter.Generator.csproj", "{47763E73-0B9B-4B11-BB03-5FDB0B8A0C5E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -196,6 +208,42 @@ Global
{61142B10-7B49-436E-AE32-2737658BD1E5}.Release|x64.Build.0 = Release|Any CPU
{61142B10-7B49-436E-AE32-2737658BD1E5}.Release|x86.ActiveCfg = Release|Any CPU
{61142B10-7B49-436E-AE32-2737658BD1E5}.Release|x86.Build.0 = Release|Any CPU
{19A30A6D-1E91-44FD-BB5D-428D12D0160D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{19A30A6D-1E91-44FD-BB5D-428D12D0160D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{19A30A6D-1E91-44FD-BB5D-428D12D0160D}.Debug|x64.ActiveCfg = Debug|Any CPU
{19A30A6D-1E91-44FD-BB5D-428D12D0160D}.Debug|x64.Build.0 = Debug|Any CPU
{19A30A6D-1E91-44FD-BB5D-428D12D0160D}.Debug|x86.ActiveCfg = Debug|Any CPU
{19A30A6D-1E91-44FD-BB5D-428D12D0160D}.Debug|x86.Build.0 = Debug|Any CPU
{19A30A6D-1E91-44FD-BB5D-428D12D0160D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{19A30A6D-1E91-44FD-BB5D-428D12D0160D}.Release|Any CPU.Build.0 = Release|Any CPU
{19A30A6D-1E91-44FD-BB5D-428D12D0160D}.Release|x64.ActiveCfg = Release|Any CPU
{19A30A6D-1E91-44FD-BB5D-428D12D0160D}.Release|x64.Build.0 = Release|Any CPU
{19A30A6D-1E91-44FD-BB5D-428D12D0160D}.Release|x86.ActiveCfg = Release|Any CPU
{19A30A6D-1E91-44FD-BB5D-428D12D0160D}.Release|x86.Build.0 = Release|Any CPU
{6ADD07FE-B6D1-42A9-A618-E7AA102E38A0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6ADD07FE-B6D1-42A9-A618-E7AA102E38A0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6ADD07FE-B6D1-42A9-A618-E7AA102E38A0}.Debug|x64.ActiveCfg = Debug|Any CPU
{6ADD07FE-B6D1-42A9-A618-E7AA102E38A0}.Debug|x64.Build.0 = Debug|Any CPU
{6ADD07FE-B6D1-42A9-A618-E7AA102E38A0}.Debug|x86.ActiveCfg = Debug|Any CPU
{6ADD07FE-B6D1-42A9-A618-E7AA102E38A0}.Debug|x86.Build.0 = Debug|Any CPU
{6ADD07FE-B6D1-42A9-A618-E7AA102E38A0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6ADD07FE-B6D1-42A9-A618-E7AA102E38A0}.Release|Any CPU.Build.0 = Release|Any CPU
{6ADD07FE-B6D1-42A9-A618-E7AA102E38A0}.Release|x64.ActiveCfg = Release|Any CPU
{6ADD07FE-B6D1-42A9-A618-E7AA102E38A0}.Release|x64.Build.0 = Release|Any CPU
{6ADD07FE-B6D1-42A9-A618-E7AA102E38A0}.Release|x86.ActiveCfg = Release|Any CPU
{6ADD07FE-B6D1-42A9-A618-E7AA102E38A0}.Release|x86.Build.0 = Release|Any CPU
{47763E73-0B9B-4B11-BB03-5FDB0B8A0C5E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{47763E73-0B9B-4B11-BB03-5FDB0B8A0C5E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{47763E73-0B9B-4B11-BB03-5FDB0B8A0C5E}.Debug|x64.ActiveCfg = Debug|Any CPU
{47763E73-0B9B-4B11-BB03-5FDB0B8A0C5E}.Debug|x64.Build.0 = Debug|Any CPU
{47763E73-0B9B-4B11-BB03-5FDB0B8A0C5E}.Debug|x86.ActiveCfg = Debug|Any CPU
{47763E73-0B9B-4B11-BB03-5FDB0B8A0C5E}.Debug|x86.Build.0 = Debug|Any CPU
{47763E73-0B9B-4B11-BB03-5FDB0B8A0C5E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{47763E73-0B9B-4B11-BB03-5FDB0B8A0C5E}.Release|Any CPU.Build.0 = Release|Any CPU
{47763E73-0B9B-4B11-BB03-5FDB0B8A0C5E}.Release|x64.ActiveCfg = Release|Any CPU
{47763E73-0B9B-4B11-BB03-5FDB0B8A0C5E}.Release|x64.Build.0 = Release|Any CPU
{47763E73-0B9B-4B11-BB03-5FDB0B8A0C5E}.Release|x86.ActiveCfg = Release|Any CPU
{47763E73-0B9B-4B11-BB03-5FDB0B8A0C5E}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -213,6 +261,9 @@ Global
{6F6B8B03-9045-46EC-AE12-E7ADA492F9FA} = {6ABD4842-47AF-49A5-B057-0EBA64416789}
{A320E670-5CB0-4815-AF67-D8D09FC92A2A} = {28D4C365-FDED-49AE-A97D-36202E24A55A}
{61142B10-7B49-436E-AE32-2737658BD1E5} = {6491E321-2D02-44AB-9116-D722FE169595}
{19A30A6D-1E91-44FD-BB5D-428D12D0160D} = {6ABD4842-47AF-49A5-B057-0EBA64416789}
{6ADD07FE-B6D1-42A9-A618-E7AA102E38A0} = {28D4C365-FDED-49AE-A97D-36202E24A55A}
{47763E73-0B9B-4B11-BB03-5FDB0B8A0C5E} = {28D4C365-FDED-49AE-A97D-36202E24A55A}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {2F85D9DA-DBCF-4F13-8C42-5719F1469B2E}
Expand Down
57 changes: 57 additions & 0 deletions examples/StreetlightsAPI.AsyncApiSpecFirst/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System.Linq;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using NLog;
using NLog.Web;
using Saunter;
using Saunter.Streetlights.Api;

Check failure on line 11 in examples/StreetlightsAPI.AsyncApiSpecFirst/Program.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'Streetlights' does not exist in the namespace 'Saunter' (are you missing an assembly reference?)

Check failure on line 11 in examples/StreetlightsAPI.AsyncApiSpecFirst/Program.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'Streetlights' does not exist in the namespace 'Saunter' (are you missing an assembly reference?)
using StreetlightsAPI;

LogManager.Setup().LoadConfigurationFromAppSettings();

var builder = WebApplication.CreateBuilder(args);
builder.Logging.AddSimpleConsole(console => console.SingleLine = true);
builder.Host.UseNLog();

// Add Saunter to the application services.
builder.Services.AddAsyncApiStreetlights();

builder.Services.AddScoped<IStreetlightMessageBus, StreetlightMessageBus>();
builder.Services.AddControllers();

var app = builder.Build();

app.UseDeveloperExceptionPage();

app.UseRouting();
app.UseCors(configure => configure.AllowAnyOrigin().AllowAnyMethod());

// to be fixed with issue #173
#pragma warning disable ASP0014 // Suggest using top level route registrations instead of UseEndpoints
app.UseEndpoints(endpoints =>
{
endpoints.MapAsyncApiDocuments();
endpoints.MapAsyncApiUi();

endpoints.MapControllers();
});
#pragma warning restore ASP0014 // Suggest using top level route registrations instead of UseEndpoints

await app.StartAsync();

// Print the AsyncAPI doc location
var logger = app.Services.GetService<ILoggerFactory>().CreateLogger<Program>();
var options = app.Services.GetService<IOptions<AsyncApiOptions>>();
var addresses = app.Urls;
logger.LogInformation("AsyncAPI doc available at: {URL}", $"{addresses.FirstOrDefault()}{options.Value.Middleware.Route}");
logger.LogInformation("AsyncAPI UI available at: {URL}", $"{addresses.FirstOrDefault()}{options.Value.Middleware.UiBaseRoute}");

// Redirect base url to AsyncAPI UI
app.Map("/", () => Results.Redirect("index.html"));
app.Map("/index.html", () => Results.Redirect(options.Value.Middleware.UiBaseRoute));

await app.WaitForShutdownAsync();
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<IsPackable>false</IsPackable>

<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>Generated</CompilerGeneratedFilesOutputPath>
</PropertyGroup>

<ItemGroup>
<!-- Exclude the output of source generators from the compilation, but include into the project -->
<Compile Remove="$(CompilerGeneratedFilesOutputPath)/**/*.g.cs" />
<None Include="$(CompilerGeneratedFilesOutputPath)/**/*.g.cs" />
</ItemGroup>

<ItemGroup>
<!-- Instruct "AsyncAPI.Saunter.Generator.SourceGenerator" to generate classes for this AsyncAPI specifiction -->
<AdditionalFiles Include="specs/streetlights.json" Namespace="Saunter" />
<AdditionalFiles Include="specs/streetlights.yml" Namespace="Saunter.Yml" />
</ItemGroup>

<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<DocumentationFile>bin\Debug\StreetlightsAPI.TopLevelStatement.xml</DocumentationFile>
<NoWarn>1701;1702;1591</NoWarn>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<DocumentationFile>bin\Release\StreetlightsAPI.TopLevelStatement.xml</DocumentationFile>
<NoWarn>1701;1702;1591</NoWarn>
</PropertyGroup>

<!-- Get the latest local build nuget package from source code, from the local nuget packages source as configured in nuget.config
999.* are the 'special' version number for local builds -->
<ItemGroup Condition=" '$(Configuration)' == 'Debug' ">
<!-- <ProjectReference Include="..\..\src\AsyncAPI.Saunter.Generator.SourceGenerator\AsyncAPI.Saunter.Generator.SourceGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
-->
</ItemGroup>

<ItemGroup Condition=" '$(Configuration)' == 'Debug' ">
<PackageReference Include="AsyncAPI.Saunter.Generator.SourceGenerator" Version="999.*">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<!-- For release: use a real published nuget package -->
<ItemGroup Condition=" '$(Configuration)' == 'Release' ">
<PackageReference Include="AsyncAPI.Saunter.Generator.SourceGenerator" Version="*-*">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Saunter\Saunter.csproj" />
</ItemGroup>

<ItemGroup>
<Compile Include="../StreetlightsAPI/API.cs" />
<Compile Include="../StreetlightsAPI/Messaging.cs" />

<None Include="../StreetlightsAPI/nlog.config">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

<ItemGroup>
<PackageReference Include="NLog" Version="5.3.2" />
<PackageReference Include="NLog.Web.AspNetCore" Version="5.3.11" />
</ItemGroup>

</Project>
19 changes: 19 additions & 0 deletions examples/StreetlightsAPI.AsyncApiSpecFirst/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},

"AllowedHosts": "*",

"Kestrel": {
"EndPoints": {
"Http": {
"Url": "http://localhost:5001"
}
}
}
}
94 changes: 94 additions & 0 deletions examples/StreetlightsAPI.AsyncApiSpecFirst/specs/streetlights.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
{
"asyncapi": "2.6.0",
"info": {
"title": "Streetlights API",
"version": "1.0.0",
"description": "The Smartylighting Streetlights API allows you to remotely manage the city lights.",
"license": {
"name": "Apache 2.0",
"url": "https://www.apache.org/licenses/LICENSE-2.0"
}
},
"servers": {
"mosquitto": {
"url": "test.mosquitto.org",
"protocol": "mqtt"
},
"webapi": {
"url": "localhost:5000",
"protocol": "http"
}
},
"defaultContentType": "application/json",
"channels": {
"publish/light/measured": {
"servers": [
"webapi"
],
"publish": {
"operationId": "MeasureLight",
"summary": "Inform about environmental lighting conditions for a particular streetlight.",
"tags": [
{
"name": "Light"
}
],
"message": {
"$ref": "#/components/messages/lightMeasuredEvent"
}
}
},
"subscribe/light/measured": {
"servers": [
"mosquitto"
],
"subscribe": {
"operationId": "PublishLightMeasurement",
"summary": "Subscribe to environmental lighting conditions for a particular streetlight.",
"tags": [
{
"name": "Light"
}
],
"message": {
"payload": {
"$ref": "#/components/schemas/lightMeasuredEvent"
}
}
}
}
},
"components": {
"schemas": {
"lightMeasuredEvent": {
"type": "object",
"properties": {
"id": {
"type": "integer",
"format": "int32",
"description": "Id of the streetlight."
},
"lumens": {
"type": "integer",
"format": "int32",
"description": "Light intensity measured in lumens."
},
"sentAt": {
"type": "string",
"format": "date-time",
"description": "Light intensity measured in lumens."
}
},
"additionalProperties": false
}
},
"messages": {
"lightMeasuredEvent": {
"payload": {
"$ref": "#/components/schemas/lightMeasuredEvent"
},
"name": "lightMeasuredEvent"
}
}
}
}
Loading
Loading