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

ensuring more detailed swagger #260

Merged
merged 2 commits into from
Oct 5, 2023
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
2 changes: 2 additions & 0 deletions src/Altinn.Notifications/Altinn.Notifications.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
<PackageReference Include="Azure.Security.KeyVault.Secrets" Version="4.5.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.5.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Filters" Version="7.0.11" />
<PackageReference Include="System.Drawing.Common" Version="7.0.0" />
<PackageReference Include="System.Text.RegularExpressions" Version="4.3.1" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

using Swashbuckle.AspNetCore.Annotations;
using Swashbuckle.AspNetCore.Filters;

namespace Altinn.Notifications.Controllers;

/// <summary>
Expand All @@ -19,6 +22,9 @@ namespace Altinn.Notifications.Controllers;
[Route("notifications/api/v1/orders/email")]
[ApiController]
[Authorize]
[SwaggerResponse(401, "Caller is unauthorized")]
[SwaggerResponse(403, "Caller is not authorized to access the requested resource")]

public class EmailNotificationOrdersController : ControllerBase
{
private readonly IValidator<EmailNotificationOrderRequestExt> _validator;
Expand All @@ -38,10 +44,14 @@ public EmailNotificationOrdersController(IValidator<EmailNotificationOrderReques
/// The API will accept the request after som basic validation of the request.
/// The system will also attempt to verify that it will be possible to fulfill the order.
/// </summary>
/// <returns>The registered notification order</returns>
/// <returns>The id of the registered notification order</returns>
[HttpPost]
[Consumes("application/json")]
public async Task<ActionResult<NotificationOrderExt>> Post(EmailNotificationOrderRequestExt emailNotificationOrderRequest)
[Produces("application/json")]
[SwaggerResponse(202, "The notification order was accepted", typeof(OrderIdExt))]
[SwaggerResponse(400, "The notification order is invalid", typeof(ValidationProblemDetails))]
[SwaggerResponseHeader(202, "Location", "string", "Link to access the newly created notification order.")]
public async Task<ActionResult<OrderIdExt>> Post(EmailNotificationOrderRequestExt emailNotificationOrderRequest)
{
var validationResult = _validator.Validate(emailNotificationOrderRequest);
if (!validationResult.IsValid)
Expand Down
15 changes: 14 additions & 1 deletion src/Altinn.Notifications/Controllers/OrdersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding;

using Swashbuckle.AspNetCore.Annotations;

namespace Altinn.Notifications.Controllers;

Expand All @@ -14,6 +17,8 @@ namespace Altinn.Notifications.Controllers;
[Route("notifications/api/v1/orders")]
[ApiController]
[Authorize]
[SwaggerResponse(401, "Caller is unauthorized")]
[SwaggerResponse(403, "Caller is not authorized to access the requested resource")]
public class OrdersController : ControllerBase
{
private readonly IGetOrderService _getOrderService;
Expand All @@ -33,6 +38,9 @@ public OrdersController(IGetOrderService getOrderService)
/// <returns>The order that correspons to the provided id</returns>
[HttpGet]
[Route("{id}")]
[Produces("application/json")]
[SwaggerResponse(200, "The notification order matching the provided id was retrieved successfully", typeof(NotificationOrderExt))]
[SwaggerResponse(404, "No order with the provided id was found")]
public async Task<ActionResult<NotificationOrderExt>> GetById([FromRoute] Guid id)
{
string? expectedCreator = User.GetOrg();
Expand All @@ -57,7 +65,9 @@ public async Task<ActionResult<NotificationOrderExt>> GetById([FromRoute] Guid i
/// <param name="sendersReference">The senders reference</param>
/// <returns>The order that correspons to the provided senders reference</returns>
[HttpGet]
public async Task<ActionResult<NotificationOrderListExt>> GetBySendersRef([FromQuery] string sendersReference)
[Produces("application/json")]
[SwaggerResponse(200, "The list of notification orders matching the provided senders ref was retrieved successfully", typeof(NotificationOrderListExt))]
public async Task<ActionResult<NotificationOrderListExt>> GetBySendersRef([FromQuery, BindRequired] string sendersReference)
{
string? expectedCreator = User.GetOrg();
if (expectedCreator == null)
Expand All @@ -82,6 +92,9 @@ public async Task<ActionResult<NotificationOrderListExt>> GetBySendersRef([FromQ
/// <returns>The order that correspons to the provided id</returns>
[HttpGet]
[Route("{id}/status")]
[Produces("application/json")]
[SwaggerResponse(200, "The notification order matching the provided id was retrieved successfully", typeof(NotificationOrderExt))]
[SwaggerResponse(404, "No order with the provided id was found")]
public async Task<ActionResult<NotificationOrderWithStatusExt>> GetWithStatusById([FromRoute] Guid id)
{
string? expectedCreator = User.GetOrg();
Expand Down
25 changes: 24 additions & 1 deletion src/Altinn.Notifications/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#nullable disable

Check warning on line 1 in src/Altinn.Notifications/Program.cs

View workflow job for this annotation

GitHub Actions / Build, test & analyze

Refactor this top-level file to reduce its Cognitive Complexity from 17 to the 15 allowed. (https://rules.sonarsource.com/csharp/RSPEC-3776)

Check warning on line 1 in src/Altinn.Notifications/Program.cs

View workflow job for this annotation

GitHub Actions / Build, test & analyze

Refactor this top-level file to reduce its Cognitive Complexity from 17 to the 15 allowed. (https://rules.sonarsource.com/csharp/RSPEC-3776)
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Serialization;

Expand All @@ -24,6 +25,9 @@
using Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel;
using Microsoft.IdentityModel.Tokens;

using Swashbuckle.AspNetCore.Filters;
using Swashbuckle.AspNetCore.SwaggerGen;

ILogger logger;

string vaultApplicationInsightsKey = "ApplicationInsights--InstrumentationKey";
Expand All @@ -38,7 +42,12 @@
ConfigureServices(builder.Services, builder.Configuration);

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddSwaggerGen(c =>
{
IncludeXmlComments(c);
c.EnableAnnotations();
c.OperationFilter<AddResponseHeadersFilter>();
});

var app = builder.Build();
app.SetUpPostgreSql(builder.Environment.IsDevelopment(), builder.Configuration);
Expand All @@ -62,7 +71,7 @@
{
var logFactory = LoggerFactory.Create(builder =>
{
builder

Check warning on line 74 in src/Altinn.Notifications/Program.cs

View workflow job for this annotation

GitHub Actions / Build, test & analyze

Make sure that this logger's configuration is safe. (https://rules.sonarsource.com/csharp/RSPEC-4792)

Check warning on line 74 in src/Altinn.Notifications/Program.cs

View workflow job for this annotation

GitHub Actions / Build, test & analyze

Make sure that this logger's configuration is safe. (https://rules.sonarsource.com/csharp/RSPEC-4792)
.AddFilter("Altinn.Platform.Notifications.Program", LogLevel.Debug)
.AddConsole();
});
Expand Down Expand Up @@ -101,7 +110,7 @@
// If not application insight is available log to console
logging.AddFilter("Microsoft", LogLevel.Warning);
logging.AddFilter("System", LogLevel.Warning);
logging.AddConsole();

Check warning on line 113 in src/Altinn.Notifications/Program.cs

View workflow job for this annotation

GitHub Actions / Build, test & analyze

Make sure that this logger's configuration is safe. (https://rules.sonarsource.com/csharp/RSPEC-4792)
}
}

Expand All @@ -122,7 +131,7 @@
services.AddSingleton(config);
if (!string.IsNullOrEmpty(applicationInsightsConnectionString))
{
services.AddSingleton(typeof(ITelemetryChannel), new ServerTelemetryChannel() { StorageFolder = "/tmp/logtelemetry" });

Check warning on line 134 in src/Altinn.Notifications/Program.cs

View workflow job for this annotation

GitHub Actions / Build, test & analyze

Make sure publicly writable directories are used safely here. (https://rules.sonarsource.com/csharp/RSPEC-5443)

services.AddApplicationInsightsTelemetry(new ApplicationInsightsServiceOptions
{
Expand Down Expand Up @@ -214,3 +223,17 @@
ValidatorOptions.Global.LanguageManager.Enabled = false;
services.AddSingleton<IValidator<EmailNotificationOrderRequestExt>, EmailNotificationOrderRequestValidator>();
}

void IncludeXmlComments(SwaggerGenOptions swaggerGenOptions)
{
try
{
string xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
string xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
swaggerGenOptions.IncludeXmlComments(xmlPath);
}
catch (Exception e)
{
logger.LogWarning(e, "Program // Exception when attempting to include the XML comments file(s).");
}
}
Loading