DEV Community

Gabriel Weidmann
Gabriel Weidmann

Posted on

Display and test openapi.yaml file

From a partner company I got a openapi.yaml file to evaluate their api for our use cases. Mostly I know those files from working with ASP.NET Core Apis + Swagger UI, but this time it was just a standalone file.

I got the file and an api key for the sandbox.

Did not work: Use via ASP.NET Core swagger

The first thing I was trying was to use the already known swagger ui as it's the same format.

So:

  1. Setting up a new ASP.NET Core Api project in Visual Studio
  2. Including the latest Swashbuckle.AspNetCore nuget
  3. Putting the openapi.yaml file in the wwwroot folder
  4. Setting up the Program.cs
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.UseHttpsRedirection();

// Allow to serve the unknown-by-default file type .yaml
var provider = new FileExtensionContentTypeProvider();
provider.Mappings[".yaml"] = "application/yaml";

app.UseStaticFiles(new StaticFileOptions
{
    ContentTypeProvider = provider,
});

app.UseSwaggerUI(c =>
{
    c.SwaggerEndpoint("/openapi.yaml", "Imported API");
    c.RoutePrefix = "swagger";
});

app.Run();
Enter fullscreen mode Exit fullscreen mode
  1. Accessing the api via https://localhost:7298/swagger

This worked almost, but then CORS came around :-(

Access to fetch at 'https://sandbox.***.com/api/...' from origin 'https://localhost:7298' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

The problem seems to be the browser, so let's look for a non-browser-solution.

Did work: Import in Postman

Then I noticed that Postman got the functionality to import openapi spec files:

Importing openapi spec in Postman

From here it's possible to simulate the api requests just as good (or better?) as in swagger UI.

Top comments (0)