Created
March 3, 2018 14:39
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
[AttributeUsageAttribute(AttributeTargets.Class, Inherited = true, AllowMultiple = false)] | |
public class ContractAttribute : Attribute {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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