Skip to content

Fix enum values in YARP environment configuration - #19403

Open
air-hand (air-hand) wants to merge 4 commits into
microsoft:mainfrom
air-hand:fix/yarp-forwarder-config-version-policy
Open

Fix enum values in YARP environment configuration#19403
air-hand (air-hand) wants to merge 4 commits into
microsoft:mainfrom
air-hand:fix/yarp-forwarder-config-version-policy

Conversation

@air-hand

@air-hand air-hand (air-hand) commented Aug 15, 2026

Copy link
Copy Markdown
Contributor

Description

WithForwarderRequestConfig can set HttpVersionPolicy, but the container environment-variable generator drops enum values.
This prevents YARP from receiving settings such as RequestVersionExact, which are needed when an HTTPS-terminating proxy forwards gRPC traffic to an h2c backend.

This change serializes enum values while flattening YARP configuration to environment variables.
In addition to HttpVersionPolicy, it preserves explicitly configured route match modes and session-affinity cookie settings that were previously omitted.
Uri values are already recursively expanded; this change also emits their HostNameType enum leaf.

The README includes an h2c/gRPC example using RequestVersionExact.
The destination must use http://, and its backend must accept HTTP/2 without TLS.

User-facing usage

var cluster = yarp.AddCluster(
        "h2c-backend",
        new Uri("http://localhost:5000"))
    .WithForwarderRequestConfig(new ForwarderRequestConfig
    {
        Version = HttpVersion.Version20,
        VersionPolicy = HttpVersionPolicy.RequestVersionExact,
    });

Validation:

dotnet test --project tests/Aspire.Hosting.Yarp.Tests/Aspire.Hosting.Yarp.Tests.csproj --no-launch-profile -- --filter-not-trait "quarantined=true" --filter-not-trait "outerloop=true"

Fixes # (issue)

Checklist

  • Is this feature complete?
    • Yes. Ready to ship.
    • No. Follow-up changes expected.
  • Are you including unit tests for the changes and scenario tests if relevant?
    • Yes
    • No
  • Did you add public API?
    • Yes
      • If yes, did you have an API Review for it?
        • Yes
        • No
      • Did you add <remarks /> and <code /> elements on your triple slash comments?
        • Yes
        • No
    • No
  • Does the change make any security assumptions or guarantees?
    • Yes
      • If yes, have you done a threat model and had a security review?
        • Yes
        • No
    • No

Copilot AI balanced review requested due to automatic review settings August 15, 2026 01:01
@github-actions

Copy link
Copy Markdown
Contributor

🚀 Dogfood this PR with:

⚠️ WARNING: Do not do this without first carefully reviewing the code of this PR to satisfy yourself it is safe.

curl -fsSL https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.sh | bash -s -- 19403

Or

  • Run remotely in PowerShell:
iex "& { $(irm https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.ps1) } 19403"

@github-actions github-actions Bot added the area-integrations Issues pertaining to Aspire Integrations packages label Aug 15, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR improves YARP environment-variable configuration generation by correctly flattening enum values and updates tests/docs to reflect the new behavior.

Changes:

  • Add enum handling to YarpEnvConfigGenerator.FlattenToEnvVars so enum-typed properties serialize as strings.
  • Update the verified snapshot to include the new generated env var for HttpVersionPolicy.
  • Add README documentation showing how to terminate TLS and proxy gRPC via h2c using ForwarderRequestConfig.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
tests/Aspire.Hosting.Yarp.Tests/Snapshots/YarpConfigGeneratorTests.GenerateEnvVariablesConfiguration.verified.env Updates snapshot expectations to include the newly generated enum env var.
src/Aspire.Hosting.Yarp/YarpEnvConfigGenerator.cs Adds enum flattening support so enums are emitted as scalar env var values.
src/Aspire.Hosting.Yarp/README.md Documents the new/expected configuration pattern for TLS termination + h2c forwarding for gRPC.

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/Aspire.Hosting.Yarp/YarpEnvConfigGenerator.cs Outdated
Comment thread src/Aspire.Hosting.Yarp/README.md Outdated
@github-actions

Copy link
Copy Markdown
Contributor

Retrying the failed CI jobs for this pull request from the CI run attempt. The rerun is being tracked in the rerun attempt.

Copilot AI review requested due to automatic review settings August 15, 2026 01:35

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

Copilot AI review requested due to automatic review settings August 15, 2026 01:36

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

Suppressed comments (1)

src/Aspire.Hosting.Yarp/YarpEnvConfigGenerator.cs:89

  • Enum.ToString() does not return null, so the null-coalescing fallback (?? "") is redundant here. Consider simplifying to environmentVariables.Add(prefix, obj.ToString()); (or string.Empty if you want a consistent pattern elsewhere) to reduce noise and avoid implying null is expected.
        else if (obj is Enum)
        {
            environmentVariables.Add(prefix, obj.ToString() ?? "");
        }

Copilot AI review requested due to automatic review settings August 15, 2026 01:58

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

Suppressed comments (2)

src/Aspire.Hosting.Yarp/README.md:181

  • This example may not work out-of-the-box on .NET without enabling HTTP/2 over cleartext (h2c) support (e.g., the System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport switch). Consider adding a short note indicating that the gateway process must enable h2c support (and/or link to the relevant .NET documentation) so users don’t end up with HTTP/1.1 downgrade or gRPC failures.
### Terminate TLS and proxy h2c (for gRPC)

src/Aspire.Hosting.Yarp/YarpEnvConfigGenerator.cs:89

  • Consider using pattern matching (obj is Enum e) and formatting explicitly (e.g., general format) to make the intent clear and avoid relying on object.ToString() for serialization semantics. This also makes it easier to adjust behavior later for flags enums (comma-separated values) if configuration binding ever needs stricter formatting.
        else if (obj is Enum)
        {
            environmentVariables.Add(prefix, obj.ToString());
        }

Copilot AI review requested due to automatic review settings August 15, 2026 02:14
@air-hand
air-hand (air-hand) marked this pull request as ready for review August 15, 2026 02:14

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-integrations Issues pertaining to Aspire Integrations packages

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants