Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add links in description to other operations in Swagger (through Swashbuckle)

According to the documentation for Swashbuckle, only a few XML comments are supported in the latest version. It seems like XML comments such as <example> or <see> are not currently supported but will be implemented in Swashbuckle v6.

Until then, is there a workaround I can do to mimick the behavior of <example> or <see>?

I'd like to somehow add a link (using <see> with cref) in the <summary> of an enum, which is listed under the model of an endpoint, to point to the enum's corresponding endpoint (a different endpoint in Swagger that gets the list of types of that enum).

Edit (not sure how to format in comment):

I'd like Swagger to detect <see> and display a link in the enum's description to a different endpoint

/// <summary>
/// Generic description. 
/// Find enum types <see cref="ContactEntityType">here</see>
/// </summary>
[PropertyRequired, PropertyStringAsEnum(typeof(ContactEntityType))]
[DataMember(Name = "entityType")]
public NamedReference EntityType { get; set; }
like image 492
Emily Siu Avatar asked Jul 19 '17 18:07

Emily Siu


People also ask

What is difference between NSwag and swashbuckle?

NSwag not only provides the functionality of Swashbuckle (Swagger generation) but also code generators. This way we can avoid incompatibilities and offer more features and a more streamlined toolchain.

How do you copy a Swagger URL?

Right-click on the swagger file and select the Copy option. Select the folder in which you want to paste the swagger file. Right-click and select the Paste BW option. The swagger file is copied to the selected folder.

What is swashbuckle AspNetCore SwaggerGen?

AspNetCore. Swagger: a Swagger object model and middleware to expose SwaggerDocument objects as JSON endpoints. Swashbuckle. AspNetCore. SwaggerGen: a Swagger generator that builds SwaggerDocument objects directly from your routes, controllers, and models.


1 Answers

You can use an ISchemaFilter or an IDocumentFilter to modify the resulting SwaggerDoc.

Here are some samples:

    private class ApplySchemaVendorExtensions : ISchemaFilter
    {
        public void Apply(Schema schema, SchemaRegistry schemaRegistry, Type type)
        {
            // Modify the example values in the final SwaggerDocument
            //
            if (schema.properties != null)
            {
                foreach (var p in schema.properties)
                {
                    switch (p.Value.format)
                    {
                        case "int32":
                            p.Value.example = 123;
                            break;
                        case "double":
                            p.Value.example = 9858.216;
                            break;
                    }
                }
            }
        }
    }

_

    private class ApplyDocumentVendorExtensions : IDocumentFilter
    {
        public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer)
        {
            schemaRegistry.GetOrRegister(typeof(ExtraType));
            //schemaRegistry.GetOrRegister(typeof(BigClass));

            var paths = new Dictionary<string, PathItem>(swaggerDoc.paths);
            swaggerDoc.paths.Clear();
            foreach (var path in paths)
            {
                if (path.Key.Contains("foo"))
                    swaggerDoc.paths.Add(path);
            }
        }
    }

And to add a link just use the anchor tag :

/// <summary>Details - testing anchor: <a href="?filter=TestPost">TestPost</a></summary>
like image 157
Helder Sepulveda Avatar answered Sep 27 '22 19:09

Helder Sepulveda