-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
[Feature] Generate Swagger file at postbuild without running the app #541
Comments
@poulad-rbc - this is definitely a feature that's long overdue. I'm working on some significant refactors to make the overall architecture more flexible and ultimately allow this feature to "plug-in" more seamlessly. So, you might want to hold off on your PR until then. In the meantime you could certainly start spiking into what it would take to generate the Swagger outside of the running application from a command line tool. The tricky part will be getting the complete ApiExplorer infrastructure wired up as Swashbuckle is heavily dependent on it. Easiest thing might be an in memory/test server - you could look at the current IntegrationTests for some inspiration. When you have a design in mind, it may be prudent to talk through it before you start knocking out the PR, just so we're on the same page. Thanks |
I did a very small experiment to get swagger in test/WebSites/Basic project. Here is the code in the main() function:
As you can see from my comments this approach currently does not work. GetService() call breaks something and both sw.GetSwagger and host.Run() fails. If I remove GetSwagger() failure remains. |
@PaulDotNet Your code actually works for me; and here's how I serialize the swagger documentation: // Serialize
var swaggerString = JsonConvert.SerializeObject(
swaggerDoc,
Formatting.Indented,
new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
ContractResolver = new SwaggerContractResolver(new JsonSerializerSettings())
});
File.WriteAllText("swagger.json", swaggerString); |
Actually, the code fails on .NET Core SDK 2.1.3 and works with .NET Core SDK 2.1.4. So it looks you'd need .NET Core SDK 2.1.4 for this to work 😄 . |
I came across this thread looking for a similar solution, specifically to write the doc statically during a build rather than only dynamically at runtime. I used the ideas here to update my Main method to this.
Now I can run Edit: I should mention this is using the newer aspnetcore2.0 template that has the separate |
private static string GenerateSwagger(IWebHost host, string docName)
{
var sw = (ISwaggerProvider)host.Services.GetService(typeof(ISwaggerProvider));
var doc = sw.GetSwagger(docName, null, "/");
return JsonConvert.SerializeObject(
doc,
Formatting.Indented,
new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
ContractResolver = new SwaggerContractResolver(new JsonSerializerSettings())
});
} GenerateSwagger function after some cleanup. Just could not look at args parameter... |
Preview of CLI tool is now available. See docs for more info - https://github.com/domaindrivendev/Swashbuckle.AspNetCore#dotnet-swagger-cli-tool. Please try out and provide feedback here |
It'd be nice if the tool dumped to Console.Out by default or allowed a |
@domaindrivendev this is exactly what I want, thanks for your work on it - but could you please add a link to https://myget.org/feed/domaindrivendev/package/nuget/dotnet-swagger to make it easier to find? |
Now available on Nuget - dotnet-swagger.1.2.0-beta1 |
|
This is something I want to use, but right now I don't think it can work for me. My use case is to generate a swagger document with some extensions required to deploy to/update an api gateway as part of a deployment pipeline. These extensions contain environment configuration details which I definitely do not want to expose to end users. I don't have to use swagger for this, but all of my other options are a lot more work. I'm doing this today with Document and Operation filters. I toggle registration of the filters and provide the extra details via command line parameters; I could use appsettings/environment variables instead. My pipeline starts the application, downloads the swagger.json, then tears it down. Doing this with a cli tool would simplify my build and probably make it a little faster. |
This tool still uses your |
I may be mistaken. I saw no discussion of or mention of ways to pass additional command line parameters, and when I run 'dotnet swagger' I get an exception about the assembly version; it looks like this project is still on an older version of Swashbuckle. I'll try upgrading. EDIT: I do get a failure after upgrading to 2.2.0 and trying to generate a swagger document.
Without So this is going to work for me, but right now I'm either still running the tool incorrectly, I need to make other adjustments with the upgrade to 2.2.0, or there is a bug with the tool. |
I assume PS has a way to pass environment variables to any command but you’ll have to Google the exact syntax. On Linux, you would do the following: FOO=BAR dot net swagger Re the exception, can you provide the stack trace? |
Looks like you’re close - let me know how it goes? |
I updated my comment with the stack trace. It looks like it's coming from my code, but it only occurs when running dotnet swagger. |
What this boils down to is that CreateSwaggerProvider() is getting called twice, once during BuildWebHost() and again by GetRequiredService() in the Cli tool. I'm not sure what the correct fix for this really is - I am not very experienced with these DI frameworks - but I will send a PR with a potential fix. |
@domaindrivendev is this still the right place to report issues with the command line tools? I have a space in my profile path (
I think this is from the call to dotnet exec not wrapping the file path in double-quotes. I can verify this by running |
Love your work @domaindrivendev, my plan is to use this tool in conjunction with Atlassian's openapi-diff tool as sort of sanity test. During CI builds, my frontend SPA can compare the current API with a pre-generated version it has - thus ensuring that there are no breaking changes that my frontend might not be handling. As for feedback, the main issue I'm having right now is getting it to work with XML comments enabled. Regular Swashbuckle.AspNetCore running on the server with comments works fine. EDIT: My issue was self inflicted - I was trying to get the name of the file from the |
We're using ASP.NET Core with .NET Framework runtime, is there a plan to make this tool compatible with projects targeting .NET Framework? |
@fiksen99 if you look at the https://github.com/domaindrivendev/Swashbuckle project, it looks like Richard doesn't have the time to maintain both versions. I'm sure he'd appreciate a Pull Request for someone willing to port it though ;) |
I'm actually using the Swashbuckle.AspNetCore project right now, which is
compatible with .Net Framework as it conforms to .Net standard. However
from what I can see currently there isn't an equivalent of the
DotNetCliReference tool in the net framework world.
I've tried to fork the current project, and add a net462 target framework
(with conditional compilation around the assembly loader), I have a
successful build, but alas it wasn't as easy as that and I'm unsure how to
actually run the net framework app correctly (if it's even possible!) to
output the swagger.
…On Tue, Mar 27, 2018, 1:36 AM David Kemp ***@***.***> wrote:
@fiksen99 <https://github.com/fiksen99> if you look at the
https://github.com/domaindrivendev/Swashbuckle project, it looks like
Richard doesn't have the time to maintain both versions.
I'm sure he'd appreciate a Pull Request for someone willing to port it
though ;)
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#541 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ACfI6HW2ic1gXQdtAgGoOAY3OwqxGk5Cks5tifoKgaJpZM4Qohcc>
.
|
It might be worth looking at how the dotnet-ef tool deals with this (although it does compilation too), as it has the ability to boostrap from aspnet core configuration/BuildWebHost the same as this cli would need to. |
Looks like https://www.natemcmaster.com/blog/2017/11/11/build-tools-in-nuget/ may provide insight into how we can get it working as a .Net Framework build tool. Will see if I can figure anything out here |
I am having a problem getting XML comments into the swagger generated by the CLI command. When I load the swagger via the HTTP endpoint it all works great but when I generate the swagger via the command line no annotations from the XML comments. I see a comment above that my Startup class gets called but I don't understand how that happens given how the builder is created but I am just learning .Net Core so my knowledge is limited. Any help would be appreciated. |
I'm having an issue when already reading a configuration section in the
I get a The section Is this use-case supported or am I missing something? |
This also seems to work: |
With version 4.0.1 of the tool I was not able to make the --basepath option work. It generated with the value of ApplicationPath from the appsettings.json file instead. |
After upgrading from 5.0.0-beta to 5.0.0-RC2 we started getting the following error in jenkins:
If I revert back to the beta it starts working again. |
@KasperHoldum This issue is also logged here with some more details: #1103 |
Hello, We are having issue when trying to generate swagger.json:
After some research, I think it's because our projet is targeting the .net Framework ( What are our options to be able to generate the swagger json file from command line? Is this possible? Thanks a lot for your help. |
@Indigo744 I can share our solution to this shortly |
@fiksen99 We went with the route proposed by @PaulDotNet (#541 (comment)) and we simply call our generated Exe app with the relevant args to get the Swagger Json file. However, I'm interested in hearing your solution. |
Essentially exactly what we did. Separate codepath to output the swagger
when a specific arg was passed in to the exe. We also added an msbuild task
as we needed the swagger as part of our build output (to input to an arm
template), which I can share if interested?
…On Thu, Aug 1, 2019, 10:49 PM Guillaume ***@***.***> wrote:
@fiksen99 <https://github.com/fiksen99> We went with the route proposed
by @PaulDotNet <https://github.com/PaulDotNet> (#541 (comment)
<#541 (comment)>)
and we simply call our generated Exe app with the relevant args to get the
Swagger Json file.
However, I'm interested in hearing your solution.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#541?email_source=notifications&email_token=AAT4R2EJG5URA7R5K4KK2VTQCPDFBA5CNFSM4EFCC4OKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD3MVROI#issuecomment-517560505>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAT4R2DUPNKYVGUPT7T2RSTQCPDFBANCNFSM4EFCC4OA>
.
|
Thanks a lot, but I think we are ok. It works, even though I would have preferred to use the CLI tool. Thanks anyway! |
Does this work for .NET core 3.1? the CLI does not seem to be compatible |
I'm finding the same.....errors every time with 3.1 |
Ran into the hostpolicy.dll issue this evening. Turns out I was only providing the assembly path and not the actual assembly name. To get the command to run, I just ran the following in my bin/Debug/netcoreapp3.1 path:
|
I'm having an issue running CLI. The app reads some variables from either environment or the appsettings.json file. But it seems when running commands it doesn't read the appsettings.json file hence throwing a null reference error. If I set those as env variables it works just fine. P.S. I've also tried setting ASPNETCORE_ENVIRONMENT=Development with having all settings stored in appsettings.Development.json file. but it also not working. I'm using .NETCoreApp Version=v3.1 Set of command I'm using.
and Error is:
I'm sure this is because my app is not able to find env variables. Thanks. |
Okay I fixed it by using the correct versions of the packages. (I wrote a detailed article here ). Should work with the Swashbuckle.AspNetCore.Cli package |
@Indigo744 or @fiksen99 can someone from you guys share the implemented solution - i have a task to generate swagger .json files from multiple projects all created on .net framework 4.8 and those jsons files to be merged in one swagger file used by .net core API which will behave as API Gateway |
@nikolay-dimitrov unfortunately I don't have access to the source code any more(changed company). From what I recall, we basically introduced a separate argument which would output the swagger, then called that from an ms build task. Unfortunately I don't have much more info. Sounds like you'll need to do some post processing to get the swagger files merged also |
@fiksen99 tnx for you answer - im not sure how exactly to get the instance of the swagger, in the example that is mention by @Indigo744 the following code is modified somehow
but since im on .net framework 4.8 there is no IWebHost host (Swagger it self is been plugged as middleware in .NET Core) while in the normal framework when Swagger package is been installed then its magically called runtime - without any DI or something else. Any help is really appreciated |
Hello, Just to be sure I'm understanding correctly, you are using ASPNET Core on .Net Framework, right? What we essentially do is: public static class Program
{
public static void Main(string[] args)
{
var webHost = CreateWebHostBuilder().Build();
// If args, then it's for generating swagger and exit
// otherwise run web server
if (args.Length > 0)
{
Console.WriteLine(GenerateSwagger(webHost, args[0]));
Environment.Exit(0);
}
webHost.Run();
}
private static IWebHostBuilder CreateWebHostBuilder()
{
IWebHostBuilder builder = WebHost
.CreateDefaultBuilder()
.UseConfiguration(...)
.UseUrls(...)
.UseStartup<Startup>()
....;
return builder;
}
private static string GenerateSwagger(IWebHost host, string docName)
{
ISwaggerProvider sw = (ISwaggerProvider)host.Services.GetService(typeof(ISwaggerProvider));
OpenApiDocument doc = sw.GetSwagger(docName);
return doc.SerializeAsJson(OpenApiSpecVersion.OpenApi3_0);
}
} Hope it helps. |
@Indigo744 no its normal .net framework 4.8 web api |
@nikolay-dimitrov you mean ASP.NET? I fear you're in the wrong repo then, as this is Swagger for ASP.NET Core... Maybe you should look into https://github.com/domaindrivendev/Swashbuckle.WebApi and open an issue there. Anyway, we don't use ASP.Net so I can't help you further. Good luck! |
@Indigo744 this looks very familiar and likely exactly what we did. However As @Indigo744 says - this issue (and this repo) is tracking Swashbuckle in ASP.NET Core (which you can use with .NET Framework). the ASP.NET Swashbuckle version is linked out, though I believe it is no longer maintained. |
I am using But when it's in the CI/CD, as far as I know, it needs to start the app, but the app will not start in CI/CD due to some dependencies problems. So the app will throw exceptions inside my question is, what's the recommended solution there? is there a way to skip exceptions? or even do not start the app? or should I configure what dependency to load in |
@awarrenlove Are you executing |
Feature Request
It would be great to generate the Swagger file in CI without running the app. I am willing to build such a feature (either in this repo or create another tool). Using reflection, a small program could read all docs and generate the swagger file.
What are your comments about it?
The text was updated successfully, but these errors were encountered: