-
-
Notifications
You must be signed in to change notification settings - Fork 435
Fix URL port handling in get_url_path_parts #2933
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion | 🟠 Major
Test doesn't verify the fix for external $refs with custom ports.
The test uses an internal
$ref(#/components/schemas/Userat line 1832), which doesn't trigger additional HTTP requests. According to issue #2931, the bug occurs when "subsequent requests for $ref resources attempt to connect to port 80 instead of the original custom port."To properly verify the fix, the test should include an external
$ref(e.g.,$ref: "http://127.0.0.1:8123/definitions.yaml"or a relative$ref: "./definitions.yaml"), mock multiplehttpx.getcalls, and assert that all requests preserve the custom port.🔎 Proposed enhancement to test external refs
Consider following the pattern from
test_main_http_openapi(lines 1773-1813):def test_main_http_openapi_with_custom_port(mocker: MockerFixture, output_file: Path) -> None: """Test OpenAPI code generation from HTTP URL with custom port preserves port in refs.""" - schema_content = """\ + main_schema = """\ openapi: "3.0.0" info: title: Minimal API version: "1.0.0" paths: {} components: schemas: Item: type: object properties: id: type: string owner: - $ref: "#/components/schemas/User" - User: - type: object - properties: - name: - type: string + $ref: "http://127.0.0.1:8123/definitions.yaml#/components/schemas/User" """ - mock_response = mocker.Mock() - mock_response.text = schema_content + + definitions_schema = """\ +components: + schemas: + User: + type: object + properties: + name: + type: string +""" - httpx_get_mock = mocker.patch("httpx.get", return_value=mock_response) + def get_mock_response(url: str) -> mocker.Mock: + mock = mocker.Mock() + if "definitions.yaml" in url: + mock.text = definitions_schema + else: + mock.text = main_schema + return mock + + httpx_get_mock = mocker.patch("httpx.get", side_effect=get_mock_response) from datamodel_code_generator.__main__ import main return_code = main(["--url", "http://127.0.0.1:8123/openapi.json", "--output", str(output_file)]) assert return_code == Exit.OK result = output_file.read_text(encoding="utf-8") assert "class Item" in result assert "class User" in result - httpx_get_mock.assert_called_once_with( - "http://127.0.0.1:8123/openapi.json", - headers=None, - verify=True, - follow_redirects=True, - params=None, - timeout=30.0, - ) + httpx_get_mock.assert_has_calls([ + call( + "http://127.0.0.1:8123/openapi.json", + headers=None, + verify=True, + follow_redirects=True, + params=None, + timeout=30.0, + ), + call( + "http://127.0.0.1:8123/definitions.yaml", + headers=None, + verify=True, + follow_redirects=True, + params=None, + timeout=30.0, + ), + ])This would verify that both the initial request and the subsequent request for the external $ref preserve the custom port 8123.
🤖 Prompt for AI Agents