Skip to content

fix(csharp): preserve operation server paths - #25548

Merged
wing328 merged 1 commit into
OpenAPITools:masterfrom
AndreyVMarkelov:andreymarkelov/fix-csharp-operation-server-path
Nov 9, 2026
Merged

wing328 merged 1 commit into
OpenAPITools:masterfrom
AndreyVMarkelov:andreymarkelov/fix-csharp-operation-server-path

Conversation

@AndreyVMarkelov

@AndreyVMarkelov AndreyVMarkelov commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Description

Fixes #25546

For C# generichost operations with their own server, append the operation path to the server base path and preserve an explicit port. Path parameter substitution remains in place.

Tests

  • Added a focused generator regression test with a server base path, port, and path parameter.
  • Verified a generated client sends https://example.com:8443/base/files/hello%20world through a local handler; before the fix it threw UriFormatException.
  • Regenerated the C# generichost samples and built the .NET 10 Petstore sample.
  • Verified the full public Dropbox C# client builds and uses the expected upload/download paths with this change and [BUG][C#] Generic host loses object oneOf values during serialization #25547's fix applied locally.

Summary by cubic

Fixes C# generichost operations with per-operation servers so the operation path is appended to the server base path and explicit ports are preserved, which previously caused UriFormatException at runtime.

  • Uses Host and Port separately instead of Authority, which lost explicit ports and produced invalid URIs.
  • Appends the operation path to the server base path, keeping the server's base path intact.
  • Adds a regression test covering a server with a base path, port, and path parameter, and regenerates all C# generichost samples.

Written for commit 4d5408b. Summary will update on new commits.

View guided diff

@cubic-dev-ai cubic-dev-ai Bot 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.

No issues found across 21 files

View guided diff | Re-trigger cubic

@devhl-labs

Copy link
Copy Markdown
Contributor

@wing328 Please merge this. Copilot follows. I don't think any of it is blocking:

Review of #25548

Assessment

This is a correct improvement, and I would approve it.

The change follows the established .NET URI component model and the expected OpenAPI behavior for absolute operation server URLs:

  • UriBuilder.Host receives only the hostname or IP address.
  • UriBuilder.Port receives the port separately.
  • UriBuilder.Scheme receives the scheme.
  • The operation path is appended to the server's base path.
  • A root base path and a trailing slash are handled without producing duplicate separators.
  • Path parameter substitution occurs after the complete path has been constructed.

All 72 CI checks pass, the mergeable state is clean, and there are no unresolved review threads.

Consistency with .NET and OpenAPI

Assigning Uri.Authority to UriBuilder.Host is incorrect when the authority includes a port:

uriBuilder.Host = url.Authority;

The standard .NET representation keeps those components separate:

uriBuilder.Host = url.Host;
uriBuilder.Port = url.Port;
uriBuilder.Scheme = url.Scheme;

This is consistent with the Microsoft UriBuilder API, where Host is the DNS hostname or IP address and Port is an independent property.

The path behavior is also correct for an OpenAPI operation server:

server: https://example.com:8443/base
path:   /files/{id}
result: https://example.com:8443/base/files/{id}

Explicit path joining is necessary here. Resolving /files/{id} as an ordinary RFC 3986 URI reference would replace /base because the operation path begins with /, while OpenAPI expects the path-template entry to be appended to the server base URL.

Relevant history

The previous changes in this area are not repeatedly undoing one another. They have consistently moved toward preserving every configured URI component:

Change Purpose
#12795 Preserve HttpClient.BaseAddress.Port; its description explicitly notes that ports were previously ignored even with a custom client.
#14685 Use the configured HttpClient.BaseAddress.Scheme instead of a generated constant.
#20896 Preserve the configured HttpClient.BaseAddress.AbsolutePath instead of relying on generated CONTEXT_PATH.
#21081 Prevent a root base address from producing //operation.
#23355 Trim a trailing base-path slash to prevent /base//operation.
#25548 Apply the established host, port, and path behavior to operation-specific servers.

The design question was discussed directly in #19451. When a user asked why BaseAddress was decomposed and reconstructed, the response noted the remembered problem of ports not being set. That concern is confirmed by #12795.

The same discussion also produced the root-path and trailing-slash follow-ups that became #21081 and #23355.

Therefore, #25548 does not reverse an earlier decision. It completes the same behavior in the operation-server branch, which had been left with the older Authority assignment and without operation-path concatenation.

Why changes have been iterative

The recurring fixes are caused by two separate URI-building branches:

{{^servers}}
    // Build from HttpClient.BaseAddress
{{/servers}}

{{#servers}}
    // Build from the operation server
{{/servers}}

Both branches independently reconstruct the URI. Fixes made to the HttpClient.BaseAddress branch were not automatically applied to the operation-server branch.

The test matrix was also incomplete:

  1. A base subpath was preserved.
  2. A root base URL then exposed a duplicate-slash problem.
  3. A trailing base-path slash exposed another duplicate-slash problem.
  4. The operation-server branch was later found not to append the operation path at all.

This is not a cycle of doing and undoing work, but it is a reactive pattern caused by duplicated URI composition and narrow regression tests.

Non-blocking recommendations

The new operation-server test currently covers an explicit port and a /base path. Given the previous regressions, it would be useful to cover:

https://example.com
https://example.com/
https://example.com/base
https://example.com/base/
https://example.com:8443/base

The first four forms verify the root and trailing-slash behavior already established by #21081 and #23355.

A later refactor should centralize URI and path composition so both branches use the same implementation.

Remaining OpenAPI gap

OpenAPI permits relative server URLs such as:

servers:
  - url: /base

The generated code still accesses Host, Port, and Scheme on that relative URI, which requires an absolute URI. Relative operation servers therefore remain unsupported.

This is a pre-existing limitation and is not introduced or worsened by #25548. It should be handled separately if complete OpenAPI Server Object support is required.

Recommendation

Approve and merge #25548.

The implementation is consistent with prior decisions, follows the .NET UriBuilder contract, and correctly composes absolute OpenAPI operation server URLs. The repeated history reflects missing shared URI composition and test coverage rather than contradictory behavior.

@wing328
wing328 merged commit 0b3ef88 into OpenAPITools:master Nov 9, 2026
76 checks passed
@wing328 wing328 added this to the 7.28.0 milestone Nov 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG][C#] Generic host omits operation path when operation has server

3 participants