Skip to content

Commit 794e02b

Browse files
authored
Fix URL port handling in get_url_path_parts (#2933)
* Fix URL port handling in get_url_path_parts * Add test for URL with custom port
1 parent 61c08d6 commit 794e02b

2 files changed

Lines changed: 48 additions & 1 deletion

File tree

src/datamodel_code_generator/parser/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1070,7 +1070,7 @@ def _get_text_from_url(self, url: str) -> str:
10701070
def get_url_path_parts(cls, url: ParseResult) -> list[str]:
10711071
"""Split URL into scheme/host and path components."""
10721072
return [
1073-
f"{url.scheme}://{url.hostname}",
1073+
f"{url.scheme}://{url.netloc}",
10741074
*url.path.split("/")[1:],
10751075
]
10761076

tests/main/openapi/test_main_openapi.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1813,6 +1813,53 @@ def get_mock_response(path: str) -> Mock:
18131813
])
18141814

18151815

1816+
def test_main_http_openapi_with_custom_port(mocker: MockerFixture, output_file: Path) -> None:
1817+
"""Test OpenAPI code generation from HTTP URL with custom port preserves port in refs."""
1818+
schema_content = """\
1819+
openapi: "3.0.0"
1820+
info:
1821+
title: Minimal API
1822+
version: "1.0.0"
1823+
paths: {}
1824+
components:
1825+
schemas:
1826+
Item:
1827+
type: object
1828+
properties:
1829+
id:
1830+
type: string
1831+
owner:
1832+
$ref: "#/components/schemas/User"
1833+
User:
1834+
type: object
1835+
properties:
1836+
name:
1837+
type: string
1838+
"""
1839+
mock_response = mocker.Mock()
1840+
mock_response.text = schema_content
1841+
1842+
httpx_get_mock = mocker.patch("httpx.get", return_value=mock_response)
1843+
1844+
from datamodel_code_generator.__main__ import main
1845+
1846+
return_code = main(["--url", "http://127.0.0.1:8123/openapi.json", "--output", str(output_file)])
1847+
assert return_code == Exit.OK
1848+
1849+
result = output_file.read_text(encoding="utf-8")
1850+
assert "class Item" in result
1851+
assert "class User" in result
1852+
1853+
httpx_get_mock.assert_called_once_with(
1854+
"http://127.0.0.1:8123/openapi.json",
1855+
headers=None,
1856+
verify=True,
1857+
follow_redirects=True,
1858+
params=None,
1859+
timeout=30.0,
1860+
)
1861+
1862+
18161863
@pytest.mark.cli_doc(
18171864
options=["--disable-appending-item-suffix"],
18181865
option_description="""Disable appending 'Item' suffix to array item types.

0 commit comments

Comments
 (0)