Skip to content
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

Why SwaggerGen won't have any API info if dont execute app.Run #2478

Closed
ldsenow opened this issue Aug 5, 2022 · 2 comments
Closed

Why SwaggerGen won't have any API info if dont execute app.Run #2478

ldsenow opened this issue Aug 5, 2022 · 2 comments

Comments

@ldsenow
Copy link

ldsenow commented Aug 5, 2022

I am trying to use the SwaggerGenerator to export the swagger.json file. I follow how the cli does.

public static void ExportSwaggerJsonFile(this WebApplication app, string outputFilePath)
 {
            var swaggerProvider = app.Services.GetRequiredService<ISwaggerProvider>();
            var swagger = swaggerProvider.GetSwagger(app.Environment.ApplicationName);

            var s = swagger.Paths;

            var outputPath = Path.Combine(Directory.GetCurrentDirectory(), outputFilePath);

            FileInfo fi = new(outputPath);
            fi.Directory?.Create();

            using var streamWriter = File.CreateText(outputPath);
            IOpenApiWriter writer = new OpenApiJsonWriter(streamWriter);

            swagger.SerializeAsV3(writer);

            Console.WriteLine($"Swagger JSON/YAML successfully written to {outputPath}");
}

However, if I dont run the app.Run or app.Start or execute the function before the app.Run or app.Start, the output file wont contain the paths and schemas etc.

Is there a particular reason that we have to start server? We cant generate the json file based on the metadata? Or am i missing something? Any help is much appreciated.

BTW, i am on .net 6 and using swashbuckle 6.4.

@martincostello
Copy link
Collaborator

Duplicate of #2774

@martincostello martincostello marked this as a duplicate of #2774 Apr 14, 2024
@martincostello martincostello closed this as not planned Won't fix, can't repro, duplicate, stale Apr 14, 2024
@remcolam
Copy link
Contributor

@ldsenow Check also #2668, that might provide a solution for you. It should be in the next release

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants