Skip to content

OpenAPI: Previously used application/vnd.api+json returns 415 - Unsupported Media Type #1729

Open
@JohnStrim

Description

@JohnStrim

SUMMARY

First of all Great work with Integrating Open.Api and forming Swagger UI. I have a question concerning the supported media type we used so far: application/vnd.api+json.

DETAILS

I noticed that when I add the Swashbuckle prerelease package and I include it in the UI I have a problem with all my existing integrations. The reason appears to be the Content-Type of the request.
In all my current integrations I used this request Header:
Content-Type: application/vnd.api+json
When I Swashbuckle in API I get Status Code: 415 - Unsupported Media Type
The only way it works is if I set:
Content-Type: application/vnd.api+json; ext=openapi
Am I missing any configuration in order not to break compatibility with previous integrations while using Swagger?

STEPS TO REPRODUCE

  1. Updated to JsonApiDotNetCore and JsonApiDotNetCore.OpenApi.Swashbuckle.
  2. Include it in the code as mentioned in the documentation (I will provide the full Extension Method I created)
  3. Run Solution, open Swagger Doc, try request (Executes successfully)
  4. Open Postman with the same request and authentication but use the following header: Content-Type: application/vnd.api+json
  5. Error Response: 415 - Unsupported Media Type
internal static IServiceCollection AddSwaggerDocumentation(this IServiceCollection services, IConfiguration configuration)
{
    services.AddOpenApiForJsonApi(options =>
    {
        var currentNs = configuration.GetCurrentNamespace();
        currentNs = string.IsNullOrEmpty(currentNs) ? currentNs : $"/{currentNs}";
        options.SwaggerDoc("v1", new OpenApiInfo { Title = "External Services", Version = "v1" });
        options.AddServer(new OpenApiServer { Url = $"{currentNs}/api/externalservices" });
        options.AddSwaggerAuth();
        options.TagActionsBy(apiDesc =>
        {

            var controllerType = apiDesc.ActionDescriptor.EndpointMetadata
             .OfType<DisplayNameAttribute>()
             .FirstOrDefault()?.DisplayName;

            return new List<string> { controllerType ?? apiDesc.ActionDescriptor.RouteValues["controller"] };
        });

        options.DocInclusionPredicate((name, api) => true);

        options.OperationFilter<DefaultApiVersionFilter>();
    });

    return services;
}

internal static SwaggerGenOptions AddSwaggerAuth(this SwaggerGenOptions options)
{
    options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
    {
        Type = SecuritySchemeType.Http,
        Scheme = "bearer",
        BearerFormat = "JWT",
        Description = "Use JWT provided by Identity Service."
    });

    options.AddSecurityRequirement(new OpenApiSecurityRequirement
    {
        {
            new OpenApiSecurityScheme
            {
                Reference = new OpenApiReference
                {
                    Type = ReferenceType.SecurityScheme,
                    Id = "Bearer"
                }
            },
            Array.Empty<string>()
        }
    });

    return options;
}

VERSIONS USED

  • JsonApiDotNetCore version: 5.7.1
  • JsonApiDotNetCore.OpenApi.Swashbuckle version: 5.7.1-preview.2
  • .NET version: 8.0

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions