asp.net-coreopenapiopenapi-generator

How to control the generated filename when generating OpenAPI specification at build time from the code in ASP.NET Core


I followed the steps that are stated in this blogpost https://www.meziantou.net/generate-openapi-specification-at-build-time-from-the-code-in-asp-net-core.htm

dotnet new webapi --framework net8.0
dotnet add package Microsoft.Extensions.ApiDescription.Server
<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>

    <OpenApiDocumentsDirectory>$(MSBuildProjectDirectory)</OpenApiDocumentsDirectory>
    <OpenApiGenerateDocuments>true</OpenApiGenerateDocuments>
    <OpenApiGenerateDocumentsOnBuild>true</OpenApiGenerateDocumentsOnBuild>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.3" />
    <PackageReference Include="Microsoft.Extensions.ApiDescription.Server" Version="8.0.3">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
  </ItemGroup>

</Project>

When you have multiple versions, this will generate files like SampleWebApi.json and SampleWebApi_v2.json.

Is it possible to change this behavior so it will always include the version, or change the template?

So you get SampleWebApi_v1.json and SampleWebApi_v2.json

Or change template so it will be SampleWebApi.v1.json and SampleWebApi.v2.json

I searched for documentation but didn't find anything. Neither changing the value in <OpenApiDocumentsDirectory>$(MSBuildProjectDirectory)</OpenApiDocumentsDirectory> helped.


Solution

  • It only appears to miss the version off when the version matches 'v1', with a lower case 'v', if you use an upper case 'V' it gets appended, so if you are able to update the casing you can use that as a workaround.

    I've raised a feature request to update the behaviour in the future.