Skip to content

Commit

Permalink
fix patch
Browse files Browse the repository at this point in the history
  • Loading branch information
oskogstad committed Jan 7, 2025
1 parent 0fff9b7 commit 394a061
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 3 deletions.
7 changes: 6 additions & 1 deletion docs/schema/V1/swagger.verified.json
Original file line number Diff line number Diff line change
Expand Up @@ -6086,7 +6086,12 @@
},
"responses": {
"204": {
"description": "Patch was successfully applied."
"description": "Patch was successfully applied.",
"headers": {
"Etag": {
"description": "The new UUID ETag of the dialog"
}
}
},
"400": {
"content": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using Digdir.Domain.Dialogporten.WebApi.Common.Authorization;
using Digdir.Domain.Dialogporten.WebApi.Common.Extensions;
using Digdir.Domain.Dialogporten.WebApi.Endpoints.V1.Common.Extensions;
using Digdir.Domain.Dialogporten.WebApi.Endpoints.V1.Common.Headers;
using Digdir.Domain.Dialogporten.WebApi.Endpoints.V1.ServiceOwner.DialogTransmissions.Get;
using Digdir.Library.Entity.Abstractions.Features.Identifiable;
using FastEndpoints;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public PatchDialogsController(ISender sender, IMapper mapper)
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status412PreconditionFailed)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status422UnprocessableEntity)]
[ProducesResponseHeader(StatusCodes.Status204NoContent, Constants.ETag, "The new UUID ETag of the dialog")]
public async Task<IActionResult> Patch(
[FromRoute] Guid dialogId,
[FromHeader(Name = Constants.IfMatch)] Guid? etag,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace Digdir.Domain.Dialogporten.WebApi.Endpoints.V1.ServiceOwner.Dialogs.Patch;

[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public sealed class ProducesResponseHeaderAttribute : Attribute
{
public ProducesResponseHeaderAttribute(int statusCode, string headerName, string description)
{
HeaderName = headerName;
StatusCode = statusCode;
Description = description;
}

public string HeaderName { get; }
public int StatusCode { get; }
public string Description { get; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Globalization;
using System.Reflection;
using NSwag;
using NSwag.Generation.Processors;
using NSwag.Generation.Processors.Contexts;

namespace Digdir.Domain.Dialogporten.WebApi.Endpoints.V1.ServiceOwner.Dialogs.Patch;

public sealed class ProducesResponseHeaderOperationProcessor : IOperationProcessor
{
public bool Process(OperationProcessorContext context)
{
var headerAttribute = context.MethodInfo.GetCustomAttribute<ProducesResponseHeaderAttribute>();
if (headerAttribute == null)
{
return true;
}

var statusCode = headerAttribute.StatusCode.ToString(CultureInfo.InvariantCulture);
var response = context.OperationDescription.Operation.Responses[statusCode];
var header = new OpenApiHeader
{
Description = headerAttribute.Description,
};

response.Headers.Add(headerAttribute.HeaderName, header);
return true;
}
}
5 changes: 4 additions & 1 deletion src/Digdir.Domain.Dialogporten.WebApi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
using Digdir.Domain.Dialogporten.Application.Externals.Presentation;
using Digdir.Domain.Dialogporten.WebApi.Common.Extensions;
using Digdir.Domain.Dialogporten.Infrastructure;
using Digdir.Domain.Dialogporten.WebApi;
using Digdir.Domain.Dialogporten.WebApi.Common;
using Digdir.Domain.Dialogporten.WebApi.Common.Authentication;
using Digdir.Domain.Dialogporten.WebApi.Common.Authorization;
using Digdir.Domain.Dialogporten.WebApi.Common.Json;
using Digdir.Domain.Dialogporten.WebApi.Common.Swagger;
using Digdir.Domain.Dialogporten.WebApi.Endpoints.V1.ServiceOwner.Dialogs.Patch;
using Digdir.Library.Utils.AspNet;
using FastEndpoints;
using FastEndpoints.Swagger;
Expand Down Expand Up @@ -125,6 +125,9 @@ static void BuildAndRun(string[] args, TelemetryConfiguration telemetryConfigura
// generic "2" suffix duplicate names get, so we add a "SO" suffix to the serviceowner specific schemas.
// This should match the operationIds used for service owners.
s.AddServiceOwnerSuffixToSchemas();

// Adding ResponseHeaders for PATCH MVC controller
s.OperationProcessors.Add(new ProducesResponseHeaderOperationProcessor());
};
})
.AddControllers(options => options.InputFormatters.Insert(0, JsonPatchInputFormatter.Get()))
Expand Down

0 comments on commit 394a061

Please sign in to comment.