Skip to content

Commit

Permalink
feat: add documentation for open api generation
Browse files Browse the repository at this point in the history
Refs: #1021
  • Loading branch information
Phil91 committed Sep 24, 2024
1 parent 4eab2bc commit 540ed00
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 11 deletions.
6 changes: 6 additions & 0 deletions .tractusx
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,9 @@
###############################################################

leadingRepository: "https://github.com/eclipse-tractusx/portal"
openApiSpecs:
- "https://github.com/eclipse-tractusx/portal-backend/tree/v2.2.0/docs/api/Org.Eclipse.TractusX.Portal.Backend.Administration.Service.yaml"
- "https://github.com/eclipse-tractusx/portal-backend/tree/v2.2.0/docs/api/Org.Eclipse.TractusX.Portal.Backend.Apps.Service.yaml"
- "https://github.com/eclipse-tractusx/portal-backend/tree/v2.2.0/docs/api/Org.Eclipse.TractusX.Portal.Backend.Notifications.Service.yaml"
- "https://github.com/eclipse-tractusx/portal-backend/tree/v2.2.0/docs/api/Org.Eclipse.TractusX.Portal.Backend.Registration.Service.yaml"
- "https://github.com/eclipse-tractusx/portal-backend/tree/v2.2.0/docs/api/Org.Eclipse.TractusX.Portal.Backend.Services.Service.yaml"
45 changes: 45 additions & 0 deletions docs/api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# OpenAPI Provisioning

The provisioning of the open api files for each service of the portal is done via the ms build process of each Service project.

## Setup to generate open api files

The following setup was done for all existing services of the portal which currently are:

- `Adminstiration Service`
- `Apps Service`
- `Notification Service`
- `Registration Service`
- `Services Service`

### Setup dotnet tool

To be able to run the `dotnet tool run swagger tofile` command it needs to be installed for the project. Therefor a `dotnet-tools.json` file was created within a `.config` directory. This file includes a reference to a nuget package which is needed to execute the command.

### Setup csproj

To execute the generation of the open api document the .csproj file of the project was adjusted as follows:

```xml
<Target Name="openapi" AfterTargets="Build">
<Message Text="generating openapi v$(Version)" Importance="high" />
<Exec Command="dotnet tool restore" />
<Exec Command="dotnet tool run swagger tofile --yaml --output ../../../docs/api/$(AssemblyName).yaml $(OutputPath)$(AssemblyName).dll v$(Version)" EnvironmentVariables="DOTNET_ROLL_FORWARD=LatestMajor;ASPNETCORE_ENVIRONMENT=Swagger;MVC_ROUTING_BASEPATH=api/administration" />
</Target>
```

The configuration runs after the build of the project, it executes a `dotnet tool restore` which is needed to than run the command to generate the open api file.

The `dotnet tool run swagger tofile` is executed with the following parameters:

1. `--yaml` sets the filetype to yaml, an alternative would be to remove the parameter, the file would than be generated as a json

2. `--ouput ../../../docs/api/$(AssemblyName).yaml $(OutputPath)$(AssemblyName).dll v$(Version)` sets the ouput path for the file and specifies the version of the swagger file that should be taken

> **IMPORTANT**: the version set for the output must(!) match with the version which is specified in the `Program.cs` more under [Setup Program](#setup-program)
3. `EnvironmentVariables="DOTNET_ROLL_FORWARD=LatestMajor;ASPNETCORE_ENVIRONMENT=Swagger"` sets the environment variables to start up the program. The environment of the application is set to swagger to skip the application configuration validation

## Setup Program

To get the version of the assembly which is used in the csproj file with `$(Version)` an extension was introduced. By calling `AssemblyExtension.GetApplicationVersion()` in the Program.cs the entry assembly will be taken, and the `InformationalVersion` will be taken.
4 changes: 2 additions & 2 deletions src/administration/Administration.Service/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
using Org.Eclipse.TractusX.Portal.Backend.Bpdm.Library.DependencyInjection;
using Org.Eclipse.TractusX.Portal.Backend.Framework.ErrorHandling.Service;
using Org.Eclipse.TractusX.Portal.Backend.Framework.Models;
using Org.Eclipse.TractusX.Portal.Backend.Framework.Models.Extensions;
using Org.Eclipse.TractusX.Portal.Backend.IssuerComponent.Library.DependencyInjection;
using Org.Eclipse.TractusX.Portal.Backend.Notifications.Library;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess;
Expand All @@ -36,9 +37,8 @@
using Org.Eclipse.TractusX.Portal.Backend.Provisioning.Library.Service;
using Org.Eclipse.TractusX.Portal.Backend.Web.Initialization;
using Org.Eclipse.TractusX.Portal.Backend.Web.PublicInfos.DependencyInjection;
using System.Reflection;

var version = $"v{Assembly.GetExecutingAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion.Split("+")[0]}";
var version = AssemblyExtension.GetApplicationVersion();

await WebAppHelper
.BuildAndRunWebApplicationAsync<Program>(args, "administration", version, builder =>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Reflection;

namespace Org.Eclipse.TractusX.Portal.Backend.Framework.Models.Extensions;

public static class AssemblyExtension
{
public static string GetApplicationVersion() =>
$"v{Assembly.GetCallingAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion.Split("+")[0]}";
}
4 changes: 2 additions & 2 deletions src/marketplace/Apps.Service/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using Org.Eclipse.TractusX.Portal.Backend.Dim.Library.DependencyInjection;
using Org.Eclipse.TractusX.Portal.Backend.Framework.ErrorHandling.Service;
using Org.Eclipse.TractusX.Portal.Backend.Framework.Models;
using Org.Eclipse.TractusX.Portal.Backend.Framework.Models.Extensions;
using Org.Eclipse.TractusX.Portal.Backend.Notifications.Library;
using Org.Eclipse.TractusX.Portal.Backend.Offers.Library.DependencyInjection;
using Org.Eclipse.TractusX.Portal.Backend.Offers.Library.Service;
Expand All @@ -31,9 +32,8 @@
using Org.Eclipse.TractusX.Portal.Backend.Provisioning.Library;
using Org.Eclipse.TractusX.Portal.Backend.Web.Initialization;
using Org.Eclipse.TractusX.Portal.Backend.Web.PublicInfos.DependencyInjection;
using System.Reflection;

var version = $"v{Assembly.GetExecutingAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion.Split("+")[0]}";
var version = AssemblyExtension.GetApplicationVersion();

await WebAppHelper
.BuildAndRunWebApplicationAsync<Program>(args, "apps", version, builder =>
Expand Down
4 changes: 2 additions & 2 deletions src/marketplace/Services.Service/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using Org.Eclipse.TractusX.Portal.Backend.Dim.Library.DependencyInjection;
using Org.Eclipse.TractusX.Portal.Backend.Framework.ErrorHandling.Service;
using Org.Eclipse.TractusX.Portal.Backend.Framework.Models;
using Org.Eclipse.TractusX.Portal.Backend.Framework.Models.Extensions;
using Org.Eclipse.TractusX.Portal.Backend.Notifications.Library;
using Org.Eclipse.TractusX.Portal.Backend.Offers.Library.DependencyInjection;
using Org.Eclipse.TractusX.Portal.Backend.Offers.Library.Web.DependencyInjection;
Expand All @@ -31,9 +32,8 @@
using Org.Eclipse.TractusX.Portal.Backend.Services.Service.DependencyInjection;
using Org.Eclipse.TractusX.Portal.Backend.Web.Initialization;
using Org.Eclipse.TractusX.Portal.Backend.Web.PublicInfos.DependencyInjection;
using System.Reflection;

var version = $"v{Assembly.GetExecutingAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion.Split("+")[0]}";
var version = AssemblyExtension.GetApplicationVersion();

await WebAppHelper
.BuildAndRunWebApplicationAsync<Program>(args, "services", version, builder =>
Expand Down
4 changes: 2 additions & 2 deletions src/notifications/Notifications.Service/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/

using Org.Eclipse.TractusX.Portal.Backend.Framework.Models.Extensions;
using Org.Eclipse.TractusX.Portal.Backend.Notifications.Service.BusinessLogic;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess;
using Org.Eclipse.TractusX.Portal.Backend.Web.Initialization;
using Org.Eclipse.TractusX.Portal.Backend.Web.PublicInfos.DependencyInjection;
using System.Reflection;

var version = $"v{Assembly.GetExecutingAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion.Split("+")[0]}";
var version = AssemblyExtension.GetApplicationVersion();

await WebAppHelper
.BuildAndRunWebApplicationAsync<Program>(args, "notification", version, builder =>
Expand Down
5 changes: 2 additions & 3 deletions src/registration/Registration.Service/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
********************************************************************************/

using Org.Eclipse.TractusX.Portal.Backend.Bpdm.Library.DependencyInjection;
using Org.Eclipse.TractusX.Portal.Backend.Framework.ErrorHandling;
using Org.Eclipse.TractusX.Portal.Backend.Framework.Models.Extensions;
using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess;
using Org.Eclipse.TractusX.Portal.Backend.Processes.ApplicationChecklist.Config;
using Org.Eclipse.TractusX.Portal.Backend.Processes.Mailing.Library.DependencyInjection;
Expand All @@ -28,9 +28,8 @@
using Org.Eclipse.TractusX.Portal.Backend.Registration.Service.BusinessLogic;
using Org.Eclipse.TractusX.Portal.Backend.Web.Initialization;
using Org.Eclipse.TractusX.Portal.Backend.Web.PublicInfos.DependencyInjection;
using System.Reflection;

var version = $"v{Assembly.GetExecutingAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion.Split("+")[0]}";
var version = AssemblyExtension.GetApplicationVersion();

await WebAppHelper
.BuildAndRunWebApplicationAsync<Program>(args, "registration", version, builder =>
Expand Down

0 comments on commit 540ed00

Please sign in to comment.