4848if TYPE_CHECKING :
4949 from pytest_mock import MockerFixture
5050
51+ SKIP_PYDANTIC_V1 = pytest .mark .skipif (
52+ pydantic .VERSION < "2.0.0" ,
53+ reason = "This test requires Pydantic v2" ,
54+ )
55+
5156
5257@pytest .mark .benchmark
5358def test_main (output_file : Path ) -> None :
@@ -2580,26 +2585,24 @@ def test_main_openapi_discriminator(input_: str, output: str, output_file: Path)
25802585
25812586@freeze_time ("2023-07-27" )
25822587@pytest .mark .parametrize (
2583- ("kind" , "option" , "expected" ),
2588+ ("kind" , "option" , "output_model" , " expected" ),
25842589 [
2585- (
2586- "anyOf" ,
2587- "--collapse-root-models" ,
2588- "in_array_collapse_root_models.py" ,
2589- ),
2590- (
2591- "oneOf" ,
2592- "--collapse-root-models" ,
2593- "in_array_collapse_root_models.py" ,
2594- ),
2595- ("anyOf" , None , "in_array.py" ),
2596- ("oneOf" , None , "in_array.py" ),
2590+ ("anyOf" , "--collapse-root-models" , None , "in_array_collapse_root_models.py" ),
2591+ ("oneOf" , "--collapse-root-models" , None , "in_array_collapse_root_models.py" ),
2592+ ("anyOf" , None , None , "in_array.py" ),
2593+ ("oneOf" , None , None , "in_array.py" ),
2594+ ("anyOf" , "--collapse-root-models" , "pydantic_v2.BaseModel" , "in_array_collapse_root_models_pydantic_v2.py" ),
2595+ ("oneOf" , "--collapse-root-models" , "pydantic_v2.BaseModel" , "in_array_collapse_root_models_pydantic_v2.py" ),
25972596 ],
25982597)
2599- def test_main_openapi_discriminator_in_array (kind : str , option : str | None , expected : str , output_file : Path ) -> None :
2598+ def test_main_openapi_discriminator_in_array (
2599+ kind : str , option : str | None , output_model : str | None , expected : str , output_file : Path
2600+ ) -> None :
26002601 """Test OpenAPI generation with discriminator in array."""
26012602 input_file = f"discriminator_in_array_{ kind .lower ()} .yaml"
26022603 extra_args = [option ] if option else []
2604+ if output_model :
2605+ extra_args .extend (["--output-model-type" , output_model ])
26032606 run_main_and_assert (
26042607 input_path = OPEN_API_DATA_PATH / input_file ,
26052608 output_path = output_file ,
@@ -2611,6 +2614,20 @@ def test_main_openapi_discriminator_in_array(kind: str, option: str | None, expe
26112614 )
26122615
26132616
2617+ @freeze_time ("2023-07-27" )
2618+ @SKIP_PYDANTIC_V1
2619+ def test_main_openapi_discriminator_in_array_underscore (output_file : Path ) -> None :
2620+ """Test discriminator with underscore property name generates valid Pydantic v2 code."""
2621+ run_main_and_assert (
2622+ input_path = OPEN_API_DATA_PATH / "discriminator_in_array_underscore.yaml" ,
2623+ output_path = output_file ,
2624+ input_file_type = "openapi" ,
2625+ assert_func = assert_file_content ,
2626+ expected_file = "discriminator/in_array_underscore_pydantic_v2.py" ,
2627+ extra_args = ["--output-model-type" , "pydantic_v2.BaseModel" , "--collapse-root-models" ],
2628+ )
2629+
2630+
26142631@pytest .mark .parametrize (
26152632 ("output_model" , "expected_output" ),
26162633 [
0 commit comments