Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failed to load API definition

I have a simple ASP.NET Core API where I have a controller which was supposed to work with an input parameter of HttpRequestMessage. I tried the standard formatters and they did not work:

  services.AddMvc(options =>
        {

             options.RespectBrowserAcceptHeader = true;

             options.InputFormatters.Add(new XmlSerializerInputFormatter());
             options.OutputFormatters.Add(new XmlSerializerOutputFormatter());

        }

Also tried this formatter:

XmlDataContractSerializerOutputFormatter

And it did not work neither. So as result, I end up with a custom formatter:

public class RawInputFormatter : InputFormatter
{
    public override Boolean CanRead(InputFormatterContext context)
    {
        return true;
    }

    public override async Task<InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context)
    {
        if (context == null)
            throw new ArgumentNullException(nameof(context));

        var request = context.HttpContext.Request;
        using (var reader = new StreamReader(request.Body))
        {
            try
            {
                var content = await reader.ReadToEndAsync();
                return await InputFormatterResult.SuccessAsync(content);
            }
            catch
            {
                return await InputFormatterResult.FailureAsync();
            }
        }
    }
}

And consequently had to change the input parameter type from HttpRequestMessage to [FromBody]string

And I am being able to read the input xml after this change.

BUT

Now when I start the API (and I use Swagger), the Swagger's screen contains the error message: Failed to load API definition

And it's for all the controllers in the API I have.

I googled but did not find anything valuable. Please advise.

like image 810
Joe Doe Avatar asked Apr 09 '18 13:04

Joe Doe


People also ask

Where can I find swagger error?

Simply navigate to https://localhost:{PortNo}/swagger/v1/swagger.json and get much more details about the error message.

What is the use of swagger API?

Swagger helps users build, document, test and consume RESTful web services. It can be used with both a top-down and bottom-up API development approach. In the top-down, or design-first, method, Swagger can be used to design an API before any code is written.

Where is swagger v1 swagger JSON?

Launch the app and navigate to https://localhost:<port>/swagger/v1/swagger.json . The generated document describing the endpoints appears as shown in OpenAPI specification (openapi. json). The Swagger UI can be found at https://localhost:<port>/swagger .


1 Answers

It would be more easy to address this problem if you would have shared your swagger configuration.

Try adding this to your swagger configuration:

options.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
like image 195
Tarik Tutuncu Avatar answered Oct 22 '22 06:10

Tarik Tutuncu