title: How can a filter be applied to a single swagger document with Swashbuckle
body: <p>I am generating multiple swagger documents with my ASP.NET Core web service. I have some filters that I want to only apply to one document, but it appears that they apply to all documents.</p>
  <p>I have an <code>IDocumentFilter</code>, <code>IOperationFilter</code>, <code>IParameterFilter</code>, and <code>ISchemaFilter</code> that I only want to apply in one case. When I load the swagger pages for the other document types, my filters are applied. I can debug and see that the filters are appended (probably, the debugger won't show me the types but the number of filters go up) onto a single <code>SwaggerGenOptions</code> in <code>AddSwaggerGen</code>.</p>
  <p>Below is what I use in my startup code. The <code>OpenApiConfigOptions</code> is just a singleton set up elsewhere that has some info like the API name and description.</p>
  <p>I thought since the <code>SwaggerDoc</code> extension method does set up multiple documents that it would be using a new <code>SwaggerGenOptions</code> for each document. Is there something I'm missing in my setup?</p>
  <p>I also don't see a way to, within the filter itself, check which document is being loaded and skip the filter if it isn't the right document. That would not be an ideal solution but could be a workaround if I could find that value inside of the filter.</p>
  <pre class="lang-cs prettyprint-override"><code>using Microsoft.Extensions.DependencyInjection;
  using Microsoft.OpenApi.Models;
  
  namespace Microsoft.Extensions.DependencyInjection
  {
      public static class ServiceCollectionExtensions
      {
          public static IServiceCollection RegisterMyOpenApi&lt;TExamplesAssembly&gt;(this IServiceCollection services, OpenApiConfigOptions options)
          {
              services.RegisterOpenApiDocument(options, &quot;BASIC&quot;);
              var scheme = new OpenApiSecurityScheme()
              {
                  Description = &quot;basic authentication&quot;,
                  Type = SecuritySchemeType.Http,
                  Scheme = &quot;basic&quot;
              };
              services.AddFilteredSecurityScheme(&quot;BASIC&quot;, &quot;basic&quot;, scheme);
  
              services.RegisterOpenApiDocument(options, &quot;ANONYMOUS&quot;);
  
              services.RegisterOpenApiDocument(options, &quot;MYSPECIALTYPE&quot;);
  
              return services;
          }
  
          private static void RegisterOpenApiDocument(this IServiceCollection services, OpenApiConfigOptions options, string type)
          {
              services.AddSwaggerGen(c =&gt;
              {
                  var info = new OpenApiInfo
                  {
                      Title = options.ServiceTitle,
                      Description = options.ServiceDescription,
                      Version = options.Version
                  };
                  c.SwaggerDoc(type.ToLowerInvariant(), info);
  
                  if (type == &quot;MYSPECIALTYPE&quot;)
                  {
                      // Azure APIM does not understand reference types for enums
                      c.UseInlineDefinitionsForEnums();
  
                      c.DocumentFilter&lt;MySpecialDocumentFilter&gt;(options);
                      c.OperationFilter&lt;MySpecialOperationFilter&gt;();
                      c.ParameterFilter&lt;MySpecialParameterFilter&gt;();
                      c.SchemaFilter&lt;MySpecialSchemaFilter&gt;();
                  }
              });
          }
      }
  }
  </code></pre>