Skip to content

Commit 3683fb2

Browse files
committed
Cover local HTTP ref errors
1 parent df90729 commit 3683fb2

1 file changed

Lines changed: 59 additions & 1 deletion

File tree

tests/parser/test_jsonschema.py

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import pytest
1212
import yaml
1313

14-
from datamodel_code_generator import AllOfMergeMode
14+
from datamodel_code_generator import AllOfMergeMode, Error
1515
from datamodel_code_generator.imports import Import
1616
from datamodel_code_generator.model import DataModelFieldBase
1717
from datamodel_code_generator.model.dataclass import DataClass
@@ -249,6 +249,64 @@ class SubElement(BaseModel):
249249
mock_get.assert_not_called()
250250

251251

252+
def test_json_schema_ref_url_from_local_http_path_with_extension(tmp_path: Path, mocker: MockerFixture) -> None:
253+
"""Test HTTP JSON schema references with an extension resolved from a local schema store."""
254+
schema_store = tmp_path / "schemas"
255+
local_schema = schema_store / "example.com" / "application" / "package" / "element" / "sub-element.json"
256+
local_schema.parent.mkdir(parents=True)
257+
local_schema.write_text(
258+
json.dumps(
259+
{
260+
"title": "SubElement",
261+
"type": "object",
262+
"properties": {
263+
"name": {
264+
"type": "string",
265+
},
266+
},
267+
},
268+
),
269+
encoding="utf-8",
270+
)
271+
272+
parser = JsonSchemaParser("", allow_remote_refs=False, http_local_ref_path=schema_store)
273+
mock_get = mocker.patch("httpx.get")
274+
275+
assert parser._get_ref_body_from_url("http://example.com/application/package/element/sub-element.json") == {
276+
"title": "SubElement",
277+
"type": "object",
278+
"properties": {
279+
"name": {
280+
"type": "string",
281+
},
282+
},
283+
}
284+
mock_get.assert_not_called()
285+
286+
287+
@pytest.mark.parametrize(
288+
"ref",
289+
[
290+
"http:///application/package/element/sub-element",
291+
"http://example.com/application/package/../sub-element",
292+
],
293+
)
294+
def test_json_schema_ref_url_from_local_http_path_invalid_path(tmp_path: Path, ref: str) -> None:
295+
"""Test invalid local HTTP JSON schema reference paths are rejected."""
296+
parser = JsonSchemaParser("", allow_remote_refs=False, http_local_ref_path=tmp_path)
297+
298+
with pytest.raises(Error, match="Unsupported local HTTP \\$ref URL path"):
299+
parser._get_ref_body_from_url(ref)
300+
301+
302+
def test_json_schema_ref_url_from_local_http_path_missing_file(tmp_path: Path) -> None:
303+
"""Test missing local HTTP JSON schema references show the attempted local paths."""
304+
parser = JsonSchemaParser("", allow_remote_refs=False, http_local_ref_path=tmp_path)
305+
306+
with pytest.raises(Error, match=r"\$ref local file not found for http://example.com/schema"):
307+
parser._get_ref_body_from_url("http://example.com/schema")
308+
309+
252310
@pytest.mark.parametrize(
253311
("source_obj", "generated_classes"),
254312
[

0 commit comments

Comments
 (0)