Swagger shows "No operations defined in spec!"
kobelobster opened this issue · comments
Describe the issue
Hello,
we are writing an Azure function with HTTPTrigger that already exists. We now want to add OpenAPI specifications for it.
What I did is the following
- Added the package
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.OpenApi" Version="1.5.1" />
(below there is full .csproj reference)
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0"/>
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.DurableTask" Version="2.9.6"/>
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.OpenApi" Version="1.5.1"/>
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.ServiceBus" Version="4.3.0"/>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.1"/>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1"/>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.4"/>
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.2.0"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.19">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\PM.OS.Audience.DataQueryService.Service.Application\PM.OS.Audience.DataQueryService.Service.Application.csproj"/>
<ProjectReference Include="..\PM.OS.Audience.DataQueryService.Service.Infra.AudienceApi\PM.OS.Audience.DataQueryService.Service.Infra.AudienceApi.csproj"/>
<ProjectReference Include="..\PM.OS.Audience.DataQueryService.Service.Infra.DataSourceApi\PM.OS.Audience.DataQueryService.Service.Infra.DataSourceApi.csproj"/>
<ProjectReference Include="..\PM.OS.Audience.DataQueryService.Service.Infra.EntityFrameworkCore.Sql\PM.OS.Audience.DataQueryService.Service.Infra.EntityFrameworkCore.Sql.csproj"/>
</ItemGroup>
<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="local.settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
</ItemGroup>
<Target Name="CopyCustomContent" AfterTargets="AfterBuild">
<ItemGroup>
<_CustomFiles Include="..\..\automation\commit-msg"/>
<_CustomFiles Include="..\..\automation\pre-push"/>
</ItemGroup>
<Copy SourceFiles="@(_CustomFiles)" DestinationFolder="./../../.git/hooks"/>
</Target>
</Project>
- In
Startup.cs
configure the OpenAPI options
builder.Services.AddSingleton<IOpenApiConfigurationOptions>(
_ =>
{
OpenApiConfigurationOptions options = new OpenApiConfigurationOptions
{
Info = new OpenApiInfo
{
Version = "v1",
Title = "PM.OS.Audience.DataQueryService.Service",
},
Servers = DefaultOpenApiConfigurationOptions.GetHostNames(),
OpenApiVersion = OpenApiVersionType.V3,
IncludeRequestingHostName = true,
ForceHttps = false,
ForceHttp = false,
};
return options;
});
- In the HTTPFunction add the OppenAPI tags
[FunctionName(nameof(HttpTriggerFunction))]
[OpenApiOperation(operationId: nameof(HttpTriggerFunction))]
[OpenApiRequestBody("application/json", typeof(CancelProcessModel), Description = "Cancel Id")]
[OpenApiResponseWithBody(HttpStatusCode.OK, "application/json", typeof(object))]
[OpenApiResponseWithBody(HttpStatusCode.InternalServerError, bodyType: typeof(Exception),
contentType: "application/json")]
When I start my function app, I get the Swagger UI but it always says that there are no specs. This is the result json of the swagger
{
"openapi": "3.0.1",
"info": {
"title": "PM.OS.Audience.DataQueryService.Service",
"version": "v1"
},
"servers": [
{
"url": "http://localhost:7071/api"
}
],
"paths": { },
"components": { }
}
It's the exact same code we have in another project where it works. I also visited 2 sites to explain how to implement it and did it the same way. Why could it be that there are still no operations defined?
To Reproduce
I don't know if it's easy to reproduce, because as I wrote above in another project it is working as expected, so it doesn't seem to be a general bug, but I can't find the reason for it.
Expected behavior
Swagger contains the operations
Screenshots
Not applicable
Environment (please complete the following information, if applicable):
- OS: Windows
- Browser not important
- Version not important
Additional context
I will gladly add additional context if needed.
Hi everyone,
I gladly found the issue. One of our developers added a reference to a test
project (whysoever). I figured this out by creating a new solution and copying one file after the other to test when it stops working. After I wanted to copy the .csproj file I saw that there were two weird entries
- Content remove for a user-defined directory
- Refernce to a test project
After removing those two, Swagger was generated without any issue.
Since this is not a bug, but just a misconfiguration on our side, I will close the issue.