Reputation: 209
I have been looking for some clues about this without much luck. It looks like mvc core 2 stopped using IApiExplorer that I could find some examples of, so I am not really sure where to start.
In my core asp.net api app I have generic handlers that deal with many api calls. So rather than reading properties of MVC controller I need to generate swagger documentation from classes that represent api queries and commands.
I have classes that are decorated by custom attributes like these (simplified):
[ApiDriver("GetSomeResult",ApiType.Query, ApiHttpMethod.Get)]
public class MyQueryClass
{
public string MyProperty{ get; set; }
}
where attribute is defined as:
[AttributeUsage(AttributeTargets.Class)]
public class ApiDriverAttribute: Attribute
{
public ApiDriverAttribute(string apiName, ApiType apiType, ApiHttpMethod httpMethod)
{
ApiName = apiName;
ApiType = apiType;
ApiHttpMethod = httpMethod;
}
public string ApiName { get; set; }
public ApiType ApiType { get; set; }
public ApiHttpMethod ApiHttpMethod { get; set; }
}
public enum ApiType { Command, Query}
public enum ApiHttpMethod { Post, Get }
So I need swagger to target (or I somehow need to provide data about) classes attributed in this way rather than going to MVC controllers.
Many thanks
Upvotes: 3
Views: 1211
Reputation: 209
I have now integrated answer to this question in Routable Dto solution, see RoutableDtoSwaggerGenerator class.
The answer was relatively simple. I had to create an implementation of IDocumentFilter and populate swagger document paths, operations, parameters and responses from my own routing repository.
All I had to do then is register my filter within overall SwaggerGen registration in StartUp.
Hope this helps someone and saves some time.
Upvotes: 1
Reputation: 17614
Here is how I inject models into the documentation:
private class ApplyDocumentVendorExtensions : IDocumentFilter
{
public void Apply(SwaggerDocument sd, SchemaRegistry sr, IApiExplorer ae)
{
sr.GetOrRegister(typeof(ExtraType));
//sr.GetOrRegister(typeof(BigClass));
}
}
Here is the full code on GitHub SwaggerConfig.cs
It might be slightly different for you because I'm not net-core
Upvotes: 2