Skip to content

Microsoft.Extensions.ApiDescription.Server: Allow specifying --environment #54698

@josundt

Description

@josundt

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

I just added the Microsoft.Extensions.ApiDescription.Server package to our AspNetCore Web API project to automatically produce swagger JSON files as part of build.

The swagger generation in our solution depends on application settings (IConfiguration). Our swagger document defines and applies swagger security schemes, and the schemes include IdentityProvider URLs etc. - and these properties vary across different application environments and therefore need to be stored in configuration sources per environment.

When debugging the API locally from Visual Studio and accessing the swagger JSON file(s) through the swagger API endpoints, the Configuration provider includes appsettings.Development.json as a configuration source since the API launch settings sets the environment variable ASPNETCORE_ENVIRONMENT to "Development" before starting up.

I can alternatively control the environment name using command line when running the API project:
dotnet run --environment Development

After adding the Microsoft.Extensions.ApiDescription.Server package, the dotnet build command will cause this new package execute the API project's code to generate the swagger data; but there is unfortunately no way to pass the "environment name" to the API project when it's being executed. (The only way is to remember to always set the ASPNETCORE_ENVIRONMENT variable before dotnet build).

The missing feature: I can not control the environment name (and thereby the configuration sources) used when the swagger is generated.

Describe the solution you'd like

Proposal: Add support for a new csproj/msbuild property OpenApiGenerateEnvironment which will allow us to define the environment name to pass when executing the API project to generate the JSON during build.

The ability to control the environment name and thereby configuration sources when generating swagger during build is a feature that will add great value to the package. I hope it will get priority.

This is how I would prefer it to work (notice the new proposed OpenApiGenerateEnvironment property):

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <!-- ... -->
    <OpenApiDocumentsDirectory>$(BaseIntermediateOutputPath)</OpenApiDocumentsDirectory>
    <OpenApiGenerateDocuments>true</OpenApiGenerateDocuments>
    <OpenApiGenerateDocumentsOnBuild>true</OpenApiGenerateDocumentsOnBuild>
    <OpenApiGenerateEnvironment>Development</OpenApiGenerateEnvironment>
  </PropertyGroup>

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

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templatesfeature-openapihelp wantedUp for grabs. We would accept a PR to help resolve this issue

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions