|
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
| 5 | +from pathlib import Path |
| 6 | + |
5 | 7 | import pytest |
6 | 8 | from inline_snapshot import snapshot |
7 | 9 |
|
|
14 | 16 | detect_openapi_version, |
15 | 17 | ) |
16 | 18 |
|
| 19 | +# Path to test data |
| 20 | +JSON_SCHEMA_DATA_PATH = Path(__file__).parent.parent / "data" / "jsonschema" |
| 21 | + |
17 | 22 |
|
18 | 23 | def test_detect_jsonschema_version_draft4() -> None: |
19 | 24 | """Test detection of Draft 4 from $schema field.""" |
@@ -421,3 +426,72 @@ def test_openapi_parser_schema_features_detection() -> None: |
421 | 426 | features = parser.schema_features |
422 | 427 | assert features.nullable_keyword == snapshot(False) |
423 | 428 | assert features.null_in_type_array == snapshot(True) |
| 429 | + |
| 430 | + |
| 431 | +def test_jsonschema_parser_config_version_override() -> None: |
| 432 | + """Test that JsonSchemaParser uses config version over auto-detection.""" |
| 433 | + from datamodel_code_generator.parser.jsonschema import JsonSchemaParser |
| 434 | + |
| 435 | + parser = JsonSchemaParser("", jsonschema_version=JsonSchemaVersion.Draft4) |
| 436 | + parser.raw_obj = {"$schema": "http://json-schema.org/draft-07/schema#"} |
| 437 | + features = parser.schema_features |
| 438 | + assert features.id_field == snapshot("id") |
| 439 | + assert features.boolean_schemas == snapshot(False) |
| 440 | + |
| 441 | + |
| 442 | +def test_openapi_parser_config_version_override() -> None: |
| 443 | + """Test that OpenAPIParser uses config version over auto-detection.""" |
| 444 | + from datamodel_code_generator.parser.openapi import OpenAPIParser |
| 445 | + |
| 446 | + parser = OpenAPIParser("", openapi_version=OpenAPIVersion.V30) |
| 447 | + parser.raw_obj = {"openapi": "3.1.0"} |
| 448 | + features = parser.schema_features |
| 449 | + assert features.nullable_keyword == snapshot(True) |
| 450 | + assert features.null_in_type_array == snapshot(False) |
| 451 | + |
| 452 | + |
| 453 | +@pytest.mark.cli_doc( |
| 454 | + options=["--schema-version"], |
| 455 | + option_description="""Schema version to use for parsing. |
| 456 | +
|
| 457 | +The `--schema-version` option specifies the schema version to use instead of auto-detection. |
| 458 | +Valid values depend on input type: JsonSchema (draft-04, draft-06, draft-07, 2019-09, 2020-12) |
| 459 | +or OpenAPI (3.0, 3.1). Default is 'auto' (detected from $schema or openapi field).""", |
| 460 | + input_schema="jsonschema/simple_string.json", |
| 461 | + cli_args=["--schema-version", "draft-07"], |
| 462 | + golden_output="jsonschema/simple_string.py", |
| 463 | +) |
| 464 | +def test_cli_schema_version_jsonschema() -> None: |
| 465 | + """Test --schema-version option with JSON Schema input.""" |
| 466 | + from datamodel_code_generator import generate |
| 467 | + |
| 468 | + result = generate( |
| 469 | + JSON_SCHEMA_DATA_PATH / "simple_string.json", |
| 470 | + input_file_type=datamodel_code_generator.InputFileType.JsonSchema, |
| 471 | + schema_version="draft-07", |
| 472 | + ) |
| 473 | + assert result is not None |
| 474 | + assert "class Model" in result or "Model" in result |
| 475 | + |
| 476 | + |
| 477 | +@pytest.mark.cli_doc( |
| 478 | + options=["--schema-version-mode"], |
| 479 | + option_description="""Schema version validation mode. |
| 480 | +
|
| 481 | +The `--schema-version-mode` option controls how schema version validation is performed. |
| 482 | +'lenient' (default): accept all features regardless of version. |
| 483 | +'strict': warn on features outside the declared/detected version.""", |
| 484 | + input_schema="jsonschema/simple_string.json", |
| 485 | + cli_args=["--schema-version-mode", "lenient"], |
| 486 | + golden_output="jsonschema/simple_string.py", |
| 487 | +) |
| 488 | +def test_cli_schema_version_mode() -> None: |
| 489 | + """Test --schema-version-mode option.""" |
| 490 | + from datamodel_code_generator import generate |
| 491 | + |
| 492 | + result = generate( |
| 493 | + JSON_SCHEMA_DATA_PATH / "simple_string.json", |
| 494 | + input_file_type=datamodel_code_generator.InputFileType.JsonSchema, |
| 495 | + schema_version_mode=VersionMode.Lenient, |
| 496 | + ) |
| 497 | + assert result is not None |
0 commit comments