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

OpenApiGenerateDocumentsOnBuild not working with .net 6 minimal api ? #2350

Closed
GMouron opened this issue Feb 21, 2022 · 7 comments
Closed

OpenApiGenerateDocumentsOnBuild not working with .net 6 minimal api ? #2350

GMouron opened this issue Feb 21, 2022 · 7 comments

Comments

@GMouron
Copy link

GMouron commented Feb 21, 2022

Hello,

I am trying to generate the swagger docs on build with <OpenApiGenerateDocumentsOnBuild>true</OpenApiGenerateDocumentsOnBuild> but I experience the following error :
No method 'BuildWebHost', 'CreateHostBuilder' or 'CreateWebHostBuilder' found in type 'Program' with expected signatures.
Version used : 6.2.3
You can find a minimum repro here :
LuccaSA/minmal-api-samples#2
This repo is a fork of https://github.com/Minimal-APIs/samples made by a microsoftie where they describe how to add open api support (https://minimal-apis.github.io/quickstart/openapi.html)
In the first PR (LuccaSA/minmal-api-samples#1), I added the nuget and the necessary code following the tutorial, and it does indeed work at runtime.

When I inspect the dll, those methods do not exist, so this is "right".
(Note : I experienced the same problem on an internal project also setup as minmal api)

If it's not fixable, would there be a workaround possible ? The doc should probably be updated to mention this case (I could take care of that) ?

Thank you.

@crhairr
Copy link

crhairr commented Feb 22, 2022

I'm seeing the same problem with .NET 6.0 minimal APIs. My guess is that this is caused by this line and declared as a dependency here. It appears that Microsoft.Extension.ApiDescription.Server version 3.0.0 doesn't support minimal apis. I manually installed 6.0.2 and ran the included dotnet-getdocument tool on my web api assembly, and it correctly generated the expected json output file.

Update: adding a direct package reference to Microsoft.Extensions.ApiDescription.Server version 6.0.2 to my web api project allows the build to succeed. It just takes 2 builds for MSBuild to fully replace the reference.

@GMouron
Copy link
Author

GMouron commented Feb 23, 2022

Yes, it solved my problem !
I know have a different problem, there is no endpoint listed in the file but I don't think it's linked

@agilenut
Copy link

agilenut commented Apr 7, 2022

Same problem.

Also, I'd like to have a better way to override the host. I know that we can use SwaggerHostFactory or SwaggerWebHostFactory but, now with minimal apis, there's no Startup.cs, and I'd rather not add it. So, I can't use the web host builder to add everything. Ideally, I could use the existing Program.cs but override a couple of things like the app configuration block and logging setup.

@SirSeven
Copy link

SirSeven commented May 6, 2022

We experienced the same problem when switching our "controller"-based APIs to the minimal-API approach. While NSwag and SwashBuckle can generate the swagger.json just fine. dotnet-getdocument always produces an empty document. Did any of you already solve this problem?

@nielsheeren
Copy link

nielsheeren commented Aug 9, 2022

We experienced the same problem when switching our "controller"-based APIs to the minimal-API approach. While NSwag and SwashBuckle can generate the swagger.json just fine. dotnet-getdocument always produces an empty document. Did any of you already solve this problem?

I had had the same problem and had to fix it by manually setting the ApplicationName in WebApplicationOptions like this:

public class SwaggerHostFactory
{
    public static IHost CreateHost()
    {
        var options = new WebApplicationOptions() { ApplicationName = typeof(SwaggerHostFactory).Assembly.FullName };
        var builder = WebApplication.CreateBuilder(options);
        ...
    }
}

Copy link
Contributor

This issue is stale because it has been open for 60 days with no activity. It will be automatically closed in 14 days if no further updates are made.

@github-actions github-actions bot added the stale Stale issues or pull requests label Apr 29, 2024
@martincostello
Copy link
Collaborator

Duplicate of #2290.

@martincostello martincostello closed this as not planned Won't fix, can't repro, duplicate, stale Apr 29, 2024
@martincostello martincostello removed the stale Stale issues or pull requests label Apr 29, 2024
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

6 participants