diff --git a/datamodel_code_generator/parser/jsonschema.py b/datamodel_code_generator/parser/jsonschema.py index d54e8c392..ae011d6dd 100644 --- a/datamodel_code_generator/parser/jsonschema.py +++ b/datamodel_code_generator/parser/jsonschema.py @@ -673,6 +673,7 @@ def parse_item( singular_name = False if ( parent + and not item.enum and item.has_constraint and (parent.has_constraint or self.field_constraints) ): diff --git a/tests/data/expected/main/main_openapi_max_items_enum/output.py b/tests/data/expected/main/main_openapi_max_items_enum/output.py new file mode 100644 index 000000000..1dcd60303 --- /dev/null +++ b/tests/data/expected/main/main_openapi_max_items_enum/output.py @@ -0,0 +1,19 @@ +# generated by datamodel-codegen: +# filename: max_items_enum.yaml +# timestamp: 2019-07-26T00:00:00+00:00 + +from __future__ import annotations + +from enum import Enum +from typing import List, Optional + +from pydantic import BaseModel, Field + + +class BarEnum(Enum): + hello = 'hello' + goodbye = 'goodbye' + + +class Foo(BaseModel): + bar: Optional[List[BarEnum]] = Field(None, max_items=3) diff --git a/tests/data/openapi/max_items_enum.yaml b/tests/data/openapi/max_items_enum.yaml new file mode 100644 index 000000000..7c1520469 --- /dev/null +++ b/tests/data/openapi/max_items_enum.yaml @@ -0,0 +1,17 @@ +openapi: "3.1.0" +components: + schemas: + Foo: + type: object + properties: + bar: + type: array + items: + enum: + - hello + - goodbye + maxLength: 5 + minLength: 1 + type: string + pattern: "^.*$" + maxItems: 3 \ No newline at end of file diff --git a/tests/test_main.py b/tests/test_main.py index 3c815bab9..88806487a 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -4157,3 +4157,28 @@ def test_main_collapse_root_models_field_constraints(): ) with pytest.raises(SystemExit): main() + + +@freeze_time('2019-07-26') +def test_main_openapi_max_items_enum(): + with TemporaryDirectory() as output_dir: + output_file: Path = Path(output_dir) / 'output.py' + return_code: Exit = main( + [ + '--input', + str(OPEN_API_DATA_PATH / 'max_items_enum.yaml'), + '--output', + str(output_file), + '--input-file-type', + 'openapi', + ] + ) + assert return_code == Exit.OK + assert ( + output_file.read_text() + == ( + EXPECTED_MAIN_PATH / 'main_openapi_max_items_enum' / 'output.py' + ).read_text() + ) + with pytest.raises(SystemExit): + main()