Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding endpoints to an OpenAPI specification #222

Closed
eminencegrs opened this issue Feb 6, 2024 · 2 comments
Closed

Adding endpoints to an OpenAPI specification #222

eminencegrs opened this issue Feb 6, 2024 · 2 comments

Comments

@eminencegrs
Copy link

Hello there.

There is a question related to OpenAPI/Swagger.
I have seen the issue #95, but I wonder if the tusdotnet library supports adding exposed endpoints to an OpenAPI specification?

For example, my service looks like this:

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddSwagger();

var app = builder.Build();
app.UseSwagger();
app.UseSwaggerUI(options => options.SwaggerEndpoint("/swagger/v1/swagger.json", "My Service v1"));
app.MapControllers().WithOpenApi();
app.MapTus("/api/files", httpContext => ...);
app.Run();

I'd like to have something like:

app.MapTus("/api/files", httpContext => ...).WithOpenApi();
@smatsson
Copy link
Collaborator

smatsson commented Feb 9, 2024

Hi!

Unfortunately not out of the box. You can work around this by adding the definitions from https://raw.githubusercontent.com/tus/tus-resumable-upload-protocol/main/OpenAPI/openapi3.yaml to your code.

Install packages:

<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.1" />
<PackageReference Include="Microsoft.OpenApi.Readers" Version="1.6.13" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />

Setup a document filter:

using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Readers;
using Swashbuckle.AspNetCore.SwaggerGen;

public class TusOperationsFilter : IDocumentFilter
{
    public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
    {
        using var stream = File.OpenRead(@"<path-to-openapi3.yaml>");
        var openApiDocument = new OpenApiStreamReader().Read(stream, out var diagnostic);

        foreach (var path in openApiDocument.Paths)
        {
            swaggerDoc.Paths.Add(path.Key, path.Value);
            foreach (var op in path.Value.Operations)
            {
                op.Value.Tags = [new OpenApiTag()
                {
                    Name = swaggerDoc.Info.Title
                }];
            }
        }

        foreach (var schema in openApiDocument.Components.Schemas)
        {
            swaggerDoc.Components.Schemas.Add(schema);
        }
    }
}

Hook it all up in Program/Startup:

builder.Services.AddSwaggerGen(c =>
{
    c.DocumentFilter<TusOperationsFilter>();
});

@eminencegrs
Copy link
Author

Hi @smatsson,

Thank you for being so helpful!

I've played around with OpenAPI and finally received an acceptable result based on your suggestion.
Although it is not an ideal solution to my problem and looks like a workaround, it allowed me to get what I needed.

Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants