servicestackopenapi

Limiting models that are returned in OpenAPI specification


Using ApiDeclarationFilter we can remove paths from openapi specification without removing it from MetaData (/types/typescript for example).

SchemaFilter allows going through the models that are in api.Definitions on the openapi spec.

ApiDeclarationFilter also has access to these deffinitions. However, there does not seem to be a way to tag them in anyway to be unique/specific type. We wish to hide certain DTOs from the OpenApi documentation specification but not from the metadata (/types/typescript)

Is there another way to inject into the generation higher up or is there a tag we can tag DTOs with that we can match on to then remove them in ApiDeclarationFilter ? Imporantly we need to keep the models in the meta data but not in the openapi specification


Solution

  • The [ExcludeMetadata] or [Exclude(Feature.Metadata)] attributes can be used to hide an API from all metadata services, including Open API, API Explorer and generated DTOs.

    There's no attribute to just hide them from Open API / Swagger UI so the only solution would be to remove them manually using the ApiDeclarationFilter.

    I've just added another a IgnoreRequest filter you can use to specify which Request DTOs should be ignored in the latest v8.2.3+ that's now available in the latest Pre Release packages where you could define your own attribute to mark which Request DTOs you want to hide:

    [AttributeUsage(AttributeTargets.Class)]
    public class HideFromOpenApi() : Attribute {}
    

    Then ignore them with:

    Plugins.Add(new OpenApiFeature {
        IgnoreRequest = dto => dto.HasAttribute<HideFromOpenApi>()
    });