Skip to content

Instantly share code, notes, and snippets.

@hartmannr76
Created March 3, 2018 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hartmannr76/33e8e6861fcaf282083a7f82a9db9669 to your computer and use it in GitHub Desktop.
Save hartmannr76/33e8e6861fcaf282083a7f82a9db9669 to your computer and use it in GitHub Desktop.
Swashbuckle IDocumentFilter that only includes attributed classes. This is good for documenting message bus messages
using System;
[AttributeUsageAttribute(AttributeTargets.Class, Inherited = true, AllowMultiple = false)]
public class ContractAttribute : Attribute {}
public class ContractOnlyFilter : IDocumentFilter
{
public void Apply(SwaggerDocument swaggerDoc, DocumentFilterContext context)
{
// however you identify the version of your messages
if (swaggerDoc.Info.Version.Contains("message"))
{
swaggerDoc.Paths.Clear();
swaggerDoc.Definitions.Clear();
var contracts = Assembly.GetExecutingAssembly().GetTypes().Where(x => x.GetCustomAttributes(true).Any(a => a.GetType() == typeof(ContractAttribute)));
foreach (var contract in contracts)
{
context.SchemaRegistry.GetOrRegister(contract);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment