Skip to content

Conversation

@Mahdigln
Copy link

@Mahdigln Mahdigln commented Oct 3, 2025

Fix port omission from BaseUrl in V2 server deserialization
Problem
When setting a BaseUrl with a non-standard port and the OpenAPI V2 document doesn't contain any server definitions, the port number was being omitted from the default server URL.
Example:
• Input: BaseUrl = "http://demo.testfire.net:8080"
• Current behavior: Server URL = "http://demo.testfire.net" ❌
• Expected behavior: Server URL = "http://demo.testfire.net:8080" ✅
Root Cause
The MakeServers method in OpenApiDocumentDeserializer.cs was using UriComponents.NormalizedHost which only extracts the hostname without the port number.
Solution
Changed UriComponents.NormalizedHost to UriComponents.Host | UriComponents.Port to preserve port information when extracting host details from the BaseUrl. This approach is consistent with the existing pattern used in OpenApiDocument.WriteHostInfoV2.
Additionally, fixed the early return logic to allow server creation from BaseUrl even when no host, basePath, or schemes are defined in the document.
Changes

  1. src/Microsoft.OpenApi.Readers/V2/OpenApiDocumentDeserializer.cs:
    • Line 154: Changed from UriComponents.NormalizedHost to UriComponents.Host | UriComponents.Port
    • Line 144: Modified early return condition to check for defaultUrl != null
  2. test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiServerTests.cs:
    • Added 3 new test cases to verify port preservation in various scenarios
    Testing
    • All existing tests pass ✅
    • Added comprehensive test coverage for port preservation scenarios
    • 16/16 OpenApiServerTests passing
    • 78/78 V2Tests passing
    Fixes When BaseUrl has a port, the default server url doesn't contain the port #1294

Updated the `MakeServers` method to include a check for `defaultUrl` when creating servers. The method now returns early if all relevant parameters are null, and the host assignment from `defaultUrl` now preserves the port.

Added new unit tests in `OpenApiServerTests.cs` to ensure that base URLs with ports are correctly handled. Tests cover scenarios for base URLs with a port, with a port and path, and with a non-standard port.
@Mahdigln Mahdigln requested a review from a team as a code owner October 3, 2025 07:33
Copy link
Member

@baywet baywet left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution!

Here are a few comments to help drive this forward.

Refactor `MakeServers` method for better null checks on
`host`, `basePath`, `schemes`, and `defaultUrl`. Enhance
`BuildUrl` to remove default ports (80 for HTTP, 443 for
HTTPS) from generated URLs. Add unit tests in
`OpenApiServerTests.cs` to verify correct URL generation
when standard ports are used.
@Mahdigln Mahdigln requested a review from baywet October 3, 2025 13:56
Copy link
Member

@baywet baywet left a comment

Choose a reason for hiding this comment

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

Thank you for making the changes!
An additional minor comment and I think we'll be ready to merge.
I'd like the v2 PR to be updated and ready to go before we merge this one.

Copy link
Member

@baywet baywet left a comment

Choose a reason for hiding this comment

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

Thank you for making the changes!

Can you stand up a PR for v2/main branch please before we can merge everything?

@Mahdigln
Copy link
Author

Mahdigln commented Oct 3, 2025

Thank you for making the changes! An additional minor comment and I think we'll be ready to merge. I'd like the v2 PR to be updated and ready to go before we merge this one.

Thank you! I've applied the changes

@Mahdigln
Copy link
Author

Mahdigln commented Oct 3, 2025

Thank you for making the changes!

Can you stand up a PR for v2/main branch please before we can merge everything?

Sure! I'll create a separate PR for the v2/main branch.

@Mahdigln
Copy link
Author

Mahdigln commented Oct 3, 2025

Thank you for making the changes!
Can you stand up a PR for v2/main branch please before we can merge everything?

Sure! I'll create a separate PR for the v2/main branch.

I'm unable to create a pull request directly from the main branch - it seems I don't have the necessary permissions or there's a branch protection rule.

@Mahdigln
Copy link
Author

Mahdigln commented Oct 6, 2025

@baywet

Hi
I noticed that the CLA check hasn’t been marked as signed on this PR.
However, I already signed the CLA before (on my previous pull request that was merged).
Could you please check why it’s not recognized this time, or let me know how I can re-confirm it?
Thanks a lot!

@baywet
Copy link
Member

baywet commented Oct 6, 2025

@microsoft-github-policy-service rerun

@baywet
Copy link
Member

baywet commented Oct 6, 2025

@mahdigIn the CLA issue is fixed, sometimes it fails for random reasons.

Just to clarify my ask:

  1. create a branch from main
  2. apply the code changes to that branch
  3. create a pull request from that branch to main

You'll need to be working on a branch from your fork for this, like you've done for the v1 patch.

Let me know if you have any additional comments or questions.

@Mahdigln
Copy link
Author

Mahdigln commented Oct 6, 2025

@mahdigIn the CLA issue is fixed, sometimes it fails for random reasons.

Just to clarify my ask:

1. create a branch from main

2. apply the code changes to that branch

3. create a pull request from that branch to main

You'll need to be working on a branch from your fork for this, like you've done for the v1 patch.

Let me know if you have any additional comments or questions.

Thank you very much for your help and clarification. 🙏
I’ve created a branch from main, applied the changes, and opened the pull request as you suggested.
I really appreciate your time and support

@baywet baywet merged commit a15c50e into microsoft:support/v1 Oct 6, 2025
8 checks passed
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.

2 participants