ikyriak / IdempotentAPI

A .NET library that handles the HTTP write operations (POST and PATCH) that can affect only once for the given request data and idempotency-key by using an ASP.NET Core attribute (filter).
MIT License
258 stars 39 forks source link

The same IdempotencyKey cannot return a custom error message #24

Open apchenjun opened 2 years ago

apchenjun commented 2 years ago

The first request can normally receive the returned custom format message b5853d2d4c3640cbba64a2196f5b5c3 The second request cannot receive the custom format message 80bfb593fd5f531634c8f33693baef6 The second request with postman return 500 The first f50258b8a1651a2204c168da36a9740 The second 8d89a1d02f6d3bfd53625184cfaa6e0

apchenjun commented 2 years ago

I hope the same IdempotencyKey throws an error and I can return a custom formatted error message image like this image

william-keller commented 2 years ago

Hi @apchenjun

This error happened with me too

In my case, i am using swagger to generate openapi documentation

It occurs by a validation of swagger filter..

In your method or controller class you must add a (ProducessAtributte("application/json"))

Hope that it can help you

ikyriak commented 2 years ago

Hello @apchenjun,

As @william-keller mentioned, the No output formatter was found for content types 'application/json; charset=utf-8, application/json; charset=utf-8' to write the response. warning is shown when we have not defined the Consumes and Produces media type.

Can you try to define it at your controller? For example, see the following sample code:

[ApiController]
[Route("[controller]")]
[Consumes("application/json")]
[Produces("application/json")]
[Idempotent(Enabled = true)]
public class SimpleController : ControllerBase
{
    // ...
}

In general, the concept of the library and Idempotency as described in the README file is to return the cached response (generated on the first try) in the subsequent requests (i.e., second, third, etc.) when using the same IdempotencyKey. That means that the actual code of the controller (action) will not be executed multiple times.