Swashbuckle.webapi: Adding custom operations

Created on 16 May 2015  路  6Comments  路  Source: domaindrivendev/Swashbuckle.WebApi

I'm trying to add Owin OAuth authorization server's token endpoint to the swagger api docs, but Swashbuckle can only read operations from the IApiExplorer. Owin uses a custom middleware to create the OAuth token endpoint with app.UseOAuthAuthorizationServer(options) call, so it won't appear on the IApiExplorer.

I need something like this:

swaggerConfig.operations.Add(new Operation() {
    operationId = "auth/token",
    consumes = new List<string>
    {
        "application/x-www-form-urlencoded"
    },
    parameters = new List<Parameter> {
        new Parameter
        {
            type = "string",
            name = "grant_type",
            required = true,
            @in = "formData"
        },
        new Parameter
        {
            type = "string",
            name = "username",
            required = false,
            @in = "formData"
        },
        new Parameter
        {
            type = "string",
            name = "password",
            required = false,
            @in = "formData"
        }
    }
});

How could I manually add custom operations to the docs?

Most helpful comment

Thank you!

I was able to add the custom operation like this:

class AuthTokenOperation : IDocumentFilter
{
    public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer)
    {
        swaggerDoc.paths.Add("/auth/token", new PathItem
        {
            post = new Operation
            {
                tags = new List<string> { "Auth" },
                consumes = new List<string>
                {
                    "application/x-www-form-urlencoded"
                },
                parameters = new List<Parameter> {
                    new Parameter
                    {
                        type = "string",
                        name = "grant_type",
                        required = true,
                        @in = "formData"
                    },
                    new Parameter
                    {
                        type = "string",
                        name = "username",
                        required = false,
                        @in = "formData"
                    },
                    new Parameter
                    {
                        type = "string",
                        name = "password",
                        required = false,
                        @in = "formData"
                    }
                }
            }
        });
    }
}

httpConfig.EnableSwagger(c =>
{
    c.DocumentFilter<AuthTokenOperation>();
});

All 6 comments

For this you should wire up a custom IDocumentFilter (see readme). This gives you access to the paths collection where you can manually add operations

Thank you!

I was able to add the custom operation like this:

class AuthTokenOperation : IDocumentFilter
{
    public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer)
    {
        swaggerDoc.paths.Add("/auth/token", new PathItem
        {
            post = new Operation
            {
                tags = new List<string> { "Auth" },
                consumes = new List<string>
                {
                    "application/x-www-form-urlencoded"
                },
                parameters = new List<Parameter> {
                    new Parameter
                    {
                        type = "string",
                        name = "grant_type",
                        required = true,
                        @in = "formData"
                    },
                    new Parameter
                    {
                        type = "string",
                        name = "username",
                        required = false,
                        @in = "formData"
                    },
                    new Parameter
                    {
                        type = "string",
                        name = "password",
                        required = false,
                        @in = "formData"
                    }
                }
            }
        });
    }
}

httpConfig.EnableSwagger(c =>
{
    c.DocumentFilter<AuthTokenOperation>();
});

hi, how to use https only for this operation?

I am using .Net core Api and having Swashbuckle.AspNetCore 1.0.0-rc1 installed in my project. I do not see the Parameter class available there. I see parameters (Parameters there) is of type IParameter but I am not sure what type implementation should i provide since IParameter is an interface. Any help?

Also IDocumentFilter has method void Apply(SwaggerDocument swaggerDoc, DocumentFilterContext context); NOT void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer);

In future, please post Core related issues to the Swashbuckle.AspNetCore repo.

But to answer your, you can use the BodyParameter or NonBodyParameter concrete classes

Hey
Is it possible to make a custom operation with raw URL input? I need it to document some OData endpoints.

For example:
/odata/People?$select=Id,Name&$expand=Passport($select=IDNP)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nf17 picture nf17  路  4Comments

joseph-ortiz picture joseph-ortiz  路  4Comments

raaga123 picture raaga123  路  4Comments

niemyjski picture niemyjski  路  3Comments

josephearl picture josephearl  路  4Comments