-
Notifications
You must be signed in to change notification settings - Fork 677
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
What's the easiest way to retrieve swagger.json documentation file? #559
Comments
And to have this |
+1 Would like to know this also! |
If you are using OWIN/Katana with your WebAPI project, then it is possible to generate a http://www.strathweb.com/2013/12/owin-memory-integration-testing/ For example, I have a Startup.cs file with something like the following in my WebAPI project (I'm including Autofac to show how I also handle dependency injection, but you can ignore it if you like): public class Startup
{
public void Configuration(IAppBuilder app)
{
var config = new HttpConfiguration();
ConfigureAutofac(app, config);
config.MapHttpAttributeRoutes();
config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
// Configures Swagger using Swashbuckle here. For this example, the
// API version is "v1"
SwaggerConfig.Register(config);
app.UseWebApi(config);
}
public virtual IContainer ConfigureAutofac(IAppBuilder app, HttpConfiguration config)
{
var builder = new ContainerBuilder();
builder.RegisterAssemblyModules(typeof(Startup).Assembly);
var container = builder.Build();
// Allows the Autofac container to be accessed from the OWIN context.
app.UseAutofacMiddleware(container);
// Informs ASP.NET WebApi 2 to use Autofac when resolving dependencies
// (e.g. dependencies in Controllers).
config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
return container;
}
} Then I created a new Console project called public class Program
{
public static void Main(string[] args)
{
using (var server = TestServer.Create<ApiStartup>())
{
var response = server.CreateRequest("/swagger/docs/v1").GetAsync().Result;
var content = response.Content.ReadAsStringAsync().Result;
File.WriteAllText("swagger.json", content);
}
}
} I then run the console app and a file called With this solution, you don't need to worry about exposing your WebAPI to the public or a potential port conflict because it is all hosted in memory without requiring a port for listening to incoming requests. If you are not using OWIN/Katana, then it might still be possible, but you would have the Hope that helps! I've successfully used this method along with the AutoRest project to generate .NET client code for my WebAPI project every time I build my solution. |
I didn't ask the original question but thank you for the detailed response. Will try this out tomorrow. |
@chartek Thanks for the answer! My code is pretty similar haha, except I don't use Actually I missed this thing about Initially what I wanted to know - if it's possible to generate It seems the final, full and correct answer is: No, it's not possible to generate I also provide my code below, although it's pretty similar to @chartek code above. Hope it would help somebody. Created the separate project to run Self-Hosted Web API using OWIN. namespace MyWebApi.SelfHosted
{
public class WebApiHostStartup
{
public void Configuration(IAppBuilder appBuilder)
{
// Configure Web API for self-host.
var config = new HttpConfiguration();
WebApiConfig.Register(config);
ConfigureSwagger(config);
//ConfigureUnity(config);
appBuilder.UseWebApi(config);
}
private static void ConfigureSwagger(HttpConfiguration httpConfiguration)
{
httpConfiguration
.EnableSwagger(MyWebApi.SwaggerConfig.ConfigureSwagger)
.EnableSwaggerUi(MyWebApi.SwaggerConfig.ConfigureSwaggerUi);
}
}
} Swagger config is still stored in original WebAPI project. namespace MyWebApi
{
public class SwaggerConfig
{
public static void Register()
{
GlobalConfiguration.Configuration
.EnableSwagger(ConfigureSwagger)
.EnableSwaggerUi(ConfigureSwaggerUi);
}
public static void ConfigureSwagger(SwaggerDocsConfig c)
{
c.SingleApiVersion("v1", "MyWebApi");
}
public static void ConfigureSwaggerUi(SwaggerUiConfig c)
{
//c.InjectStylesheet(containingAssembly, "Swashbuckle.Dummy.SwaggerExtensions.testStyles1.css");
//c.InjectJavaScript(thisAssembly, "Swashbuckle.Dummy.SwaggerExtensions.testScript1.js");
//c.BooleanValues(new[] { "0", "1" });
//c.SetValidatorUrl("http://localhost/validator");
//c.DisableValidator();
//c.DocExpansion(DocExpansion.List);
//c.CustomAsset("index", containingAssembly, "YourWebApiProject.SwaggerExtensions.index.html");
//c.EnableDiscoveryUrlSelector();
//c.EnableOAuth2Support("test-client-id", "test-realm", "Swagger UI");
}
}
} And running the self-hosted project from the console app entry-point. namespace MyWebApi.SelfHosted
{
public class Program
{
private const string BaseAddress = "http://localhost:8080";
public static void Main(string[] args)
{
// Start OWIN host
using (WebApp.Start<WebApiHostStartup>(BaseAddress))
{
using (var client = new HttpClient { BaseAddress = new Uri(BaseAddress) })
{
HttpResponseMessage response = client.GetAsync("swagger/docs/v1").Result;
string content = response.Content.ReadAsStringAsync().Result;
File.WriteAllText("swagger.json", content);
}
}
}
}
} |
If you don't mind dropping down to a lower level, forfiting some of the fluent config, you may find the following approach a bit easier:
|
@domaindrivendev Thanks for the answer! I already use self-hosted approach, but maybe I will reconsider it in future. |
@domaindrivendev - thank you very much for this succinct, reusable snippet. We have been able to add more options like the Is this doable? And if it is, can you please show how? |
Hello @domaindrivendev, |
To add xml comments using the solution @domaindrivendev gave above there is an operation filter called new Swashbuckle.Swagger.XmlComments.ApplyXmlActionComments("location of xml file")); |
Hi, I'm not sure whether this question sounds weird. But I was wondering when we can create a swagger.json file from our web api controller. So, is there a way where we can generate controller and all action methods automatically in my web api controller locally using the swagger.json file. I apologize if this question sounds irrelevant. I'm just learning. Thank you! |
Here is the implementation we came up with. The
|
Serously? The answer is 100+ lines of code? This is not maintainable: if this ends up breaking in the future, how would anyone know how to fix it without having to basically grock the entire thing and re-write it? This should have a solution in Swashbuckle project (some simple XML in .csproj to write out .json file to a location at build time). |
FYI: I know this isn't exactly the answer many people are looking for, but if you happen to be using Swashbuckle.AspNetCore then you have the option of generating the .json file from the command line without deploying https://github.com/domaindrivendev/Swashbuckle.AspNetCore#swashbuckleaspnetcorecli This solution was discussed in this thread domaindrivendev/Swashbuckle.AspNetCore#541 (again, it is specific to dot net core, but there might be some helpful information there as well) |
This code to support on .NET Framework, but it's not support .NET Core |
Hello!
I'm trying to understand if it's possible to generate
swagger.json
documentation file manually from code? Or I need to run my WebAPI anyway?If I need to run WebAPI anyway, is it easier just to request swagger endpoint rather than trying to generate this
json
manually from code?By
manually from code
I mean something like this:However, I guess this code still requires my WebAPI to be running? Because
HttpConfiguration
should be initialized first.So finally what's the easiest way to retrieve
swagger.json
documentation file generated automatically from myWebAPI
bySwagger/Swashbuckle
?To run self-hosted WebAPI, configure
Swagger/Swashbuckle
and requestapi/swagger/docs/v1
?Thanks,
Gordey
The text was updated successfully, but these errors were encountered: