|
11 | 11 | import pytest |
12 | 12 | import yaml |
13 | 13 |
|
14 | | -from datamodel_code_generator import AllOfMergeMode |
| 14 | +from datamodel_code_generator import AllOfMergeMode, Error |
15 | 15 | from datamodel_code_generator.imports import Import |
16 | 16 | from datamodel_code_generator.model import DataModelFieldBase |
17 | 17 | from datamodel_code_generator.model.dataclass import DataClass |
@@ -249,6 +249,64 @@ class SubElement(BaseModel): |
249 | 249 | mock_get.assert_not_called() |
250 | 250 |
|
251 | 251 |
|
| 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 | + |
252 | 310 | @pytest.mark.parametrize( |
253 | 311 | ("source_obj", "generated_classes"), |
254 | 312 | [ |
|
0 commit comments