Skip to content

Add polyglot endpoint callback exports#15856

Open
sebastienros wants to merge 4 commits intomainfrom
sebros/issue-15854-ts-endpoint-overloads
Open

Add polyglot endpoint callback exports#15856
sebastienros wants to merge 4 commits intomainfrom
sebros/issue-15854-ts-endpoint-overloads

Conversation

@sebastienros
Copy link
Copy Markdown
Contributor

Description

Add polyglot-safe endpoint mutation callbacks for AppHosts so existing endpoints can be updated by name without changing the creation semantics of withEndpoint(...) or withHttpEndpoint(...).

This change introduces an ATS-safe EndpointUpdateContext wrapper and exports separate callback-based mutation APIs:

  • withEndpointCallback(...)
  • withHttpEndpointCallback(...)

These are generated into the TypeScript, Java, and Python AppHost surfaces and covered by the shared polyglot apphost samples. This enables scenarios like mutating an auto-created http endpoint, including the Vite endpoint case from the issue.

Fixes #15854

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
  • Does the change require an update in our Aspire docs?

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings April 3, 2026 19:09
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 3, 2026

🚀 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 -- 15856

Or

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

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds polyglot-safe endpoint mutation callback exports so TypeScript/Java/Python AppHosts can update already-created endpoints (e.g., auto-created http endpoints) by name without changing existing endpoint creation semantics.

Changes:

  • Introduces EndpointUpdateContext as an ATS-safe wrapper for mutating endpoint settings in polyglot callbacks.
  • Adds new exported mutation capabilities: withEndpointCallback(...) and withHttpEndpointCallback(...) (plus generated SDK surface updates).
  • Updates polyglot AppHost samples and codegen snapshots to validate the new callback APIs.

Reviewed changes

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

Show a summary per file
File Description
tests/PolyglotAppHosts/Aspire.Hosting/TypeScript/apphost.ts Adds usage coverage for endpoint callback mutation APIs in TS AppHost sample.
tests/PolyglotAppHosts/Aspire.Hosting/Python/apphost.py Adds usage coverage for endpoint callback mutation APIs in Python AppHost sample.
tests/PolyglotAppHosts/Aspire.Hosting/Java/AppHost.java Adds usage coverage for endpoint callback mutation APIs in Java AppHost sample.
tests/Aspire.Hosting.CodeGeneration.TypeScript.Tests/Snapshots/TwoPassScanningGeneratedAspire.verified.ts Updates TS generated surface to include EndpointUpdateContext and callback methods/options.
tests/Aspire.Hosting.CodeGeneration.TypeScript.Tests/Snapshots/HostingContainerResourceCapabilities.verified.txt Adds the new capability IDs to the TS codegen capability snapshot.
tests/Aspire.Hosting.CodeGeneration.Python.Tests/Snapshots/TwoPassScanningGeneratedAspire.verified.py Updates Python generated surface to include EndpointUpdateContext and callback methods/kwargs.
tests/Aspire.Hosting.CodeGeneration.Java.Tests/Snapshots/TwoPassScanningGeneratedAspire.verified.java Updates Java generated surface to include EndpointUpdateContext and callback methods/options.
src/Aspire.Hosting/ResourceBuilderExtensions.cs Adds internal exported endpoint mutation methods and updates polyglot guidance/ignore reason text.
src/Aspire.Hosting/ApplicationModel/EndpointUpdateContext.cs Introduces the new exported context wrapper type for endpoint mutation.

sebastienros and others added 3 commits April 3, 2026 12:20
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 3, 2026

🎬 CLI E2E Test Recordings — 55 recordings uploaded (commit ab19db9)

View recordings
Test Recording
AddPackageInteractiveWhileAppHostRunningDetached ▶️ View Recording
AddPackageWhileAppHostRunningDetached ▶️ View Recording
AgentCommands_AllHelpOutputs_AreCorrect ▶️ View Recording
AgentInitCommand_DefaultSelection_InstallsSkillOnly ▶️ View Recording
AgentInitCommand_MigratesDeprecatedConfig ▶️ View Recording
AllPublishMethodsBuildDockerImages ▶️ View Recording
AspireAddPackageVersionToDirectoryPackagesProps ▶️ View Recording
AspireUpdateRemovesAppHostPackageVersionFromDirectoryPackagesProps ▶️ View Recording
Banner_DisplayedOnFirstRun ▶️ View Recording
Banner_DisplayedWithExplicitFlag ▶️ View Recording
Banner_NotDisplayedWithNoLogoFlag ▶️ View Recording
CertificatesClean_RemovesCertificates ▶️ View Recording
CertificatesTrust_WithNoCert_CreatesAndTrustsCertificate ▶️ View Recording
CertificatesTrust_WithUntrustedCert_TrustsCertificate ▶️ View Recording
ConfigSetGet_CreatesNestedJsonFormat ▶️ View Recording
CreateAndRunAspireStarterProject ▶️ View Recording
CreateAndRunAspireStarterProjectWithBundle ▶️ View Recording
CreateAndRunEmptyAppHostProject ▶️ View Recording
CreateAndRunJavaEmptyAppHostProject ▶️ View Recording
CreateAndRunJsReactProject ▶️ View Recording
CreateAndRunPythonReactProject ▶️ View Recording
CreateAndRunTypeScriptEmptyAppHostProject ▶️ View Recording
CreateAndRunTypeScriptStarterProject ▶️ View Recording
CreateJavaAppHostWithViteApp ▶️ View Recording
CreateStartAndStopAspireProject ▶️ View Recording
CreateTypeScriptAppHostWithViteApp ▶️ View Recording
DescribeCommandResolvesReplicaNames ▶️ View Recording
DescribeCommandShowsRunningResources ▶️ View Recording
DetachFormatJsonProducesValidJson ▶️ View Recording
DoctorCommand_DetectsDeprecatedAgentConfig ▶️ View Recording
DoctorCommand_WithSslCertDir_ShowsTrusted ▶️ View Recording
DoctorCommand_WithoutSslCertDir_ShowsPartiallyTrusted ▶️ View Recording
GlobalMigration_HandlesCommentsAndTrailingCommas ▶️ View Recording
GlobalMigration_HandlesMalformedLegacyJson ▶️ View Recording
GlobalMigration_PreservesAllValueTypes ▶️ View Recording
GlobalMigration_SkipsWhenNewConfigExists ▶️ View Recording
GlobalSettings_MigratedFromLegacyFormat ▶️ View Recording
InvalidAppHostPathWithComments_IsHealedOnRun ▶️ View Recording
LegacySettingsMigration_AdjustsRelativeAppHostPath ▶️ View Recording
LogsCommandShowsResourceLogs ▶️ View Recording
PsCommandListsRunningAppHost ▶️ View Recording
PsFormatJsonOutputsOnlyJsonToStdout ▶️ View Recording
PublishWithDockerComposeServiceCallbackSucceeds ▶️ View Recording
RestoreGeneratesSdkFiles ▶️ View Recording
RestoreSupportsConfigOnlyHelperPackageAndCrossPackageTypes ▶️ View Recording
RunFromParentDirectory_UsesExistingConfigNearAppHost ▶️ View Recording
RunWithMissingAwaitShowsHelpfulError ▶️ View Recording
SecretCrudOnDotNetAppHost ▶️ View Recording
SecretCrudOnTypeScriptAppHost ▶️ View Recording
StagingChannel_ConfigureAndVerifySettings_ThenSwitchChannels ▶️ View Recording
StopAllAppHostsFromAppHostDirectory ▶️ View Recording
StopAllAppHostsFromUnrelatedDirectory ▶️ View Recording
StopNonInteractiveMultipleAppHostsShowsError ▶️ View Recording
StopNonInteractiveSingleAppHost ▶️ View Recording
StopWithNoRunningAppHostExitsSuccessfully ▶️ View Recording

📹 Recordings uploaded automatically from CI run #23963660138

}

[AspireExport(Description = "Updates an HTTP endpoint via callback")]
internal static IResourceBuilder<T> WithHttpEndpointCallback<T>(this IResourceBuilder<T> builder, Action<EndpointUpdateContext> callback, [EndpointName] string? name = null, bool createIfNotExists = true) where T : IResourceWithEndpoints
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.

There's also WithHttpsEndpointCallback?

Copy link
Copy Markdown
Member

@JamesNK JamesNK left a comment

Choose a reason for hiding this comment

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

3 comments: 1 likely bug (missing WithHttpsEndpointCallback symmetry), 1 unnecessary complexity (reflection in test when InternalsVisibleTo is available), 1 minor (unrelated blank line removal).

}

[AspireExport(Description = "Updates an HTTP endpoint via callback")]
internal static IResourceBuilder<T> WithHttpEndpointCallback<T>(this IResourceBuilder<T> builder, Action<EndpointUpdateContext> callback, [EndpointName] string? name = null, bool createIfNotExists = true) where T : IResourceWithEndpoints
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

There's WithEndpointCallback and WithHttpEndpointCallback here but no WithHttpsEndpointCallback. The public API has the symmetric trio (WithEndpoint / WithHttpEndpoint / WithHttpsEndpoint), so the callback surface should match.

The polyglot samples work around this by calling withEndpointCallback("callback-https", ...), but when createIfNotExists is true that creates a bare TCP endpoint (via WithEndpoint(endpointName, ...)) rather than one with scheme: "https" — so the created endpoint would have the wrong URI scheme and transport.

using var builder = TestDistributedApplicationBuilder.Create();

var container = builder.AddContainer("app", "image");
var endpointUpdateContextType = typeof(ContainerResource).Assembly.GetType("Aspire.Hosting.ApplicationModel.EndpointUpdateContext", throwOnError: true)!;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This test uses Expression.Lambda, Assembly.GetType, and MethodInfo.Invoke to call WithHttpEndpointCallback, but all of that is unnecessary — Aspire.Hosting.csproj already grants InternalsVisibleTo to Aspire.Hosting.Tests, so both WithHttpEndpointCallback and EndpointUpdateContext are directly accessible.

This could be simplified to:

[Fact]
public void WithHttpEndpointCallbackCreatesHttpEndpointWhenMissing()
{
    using var builder = TestDistributedApplicationBuilder.Create();

    var container = builder.AddContainer("app", "image")
        .WithHttpEndpointCallback(_ => { }, name: "callback-http");

    var endpoint = container.Resource.Annotations.OfType<EndpointAnnotation>()
        .Single(endpoint => string.Equals(endpoint.Name, "callback-http", EndpointAnnotationName));

    Assert.Equal("http", endpoint.UriScheme);
    Assert.Equal("http", endpoint.Transport);
    Assert.Equal(ProtocolType.Tcp, endpoint.Protocol);
}

return builder;
}

/// <summary>
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nit: The blank line between WithExternalHttpEndpoints and the GetEndpoint XML doc comment was removed — appears to be an unrelated formatting change.

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

Successfully merging this pull request may close these issues.

TypeScript apphost: withEndpoint/withHttpEndpoint lacks callback overload to modify existing endpoints

5 participants