Skip to content

Commit

Permalink
Fix no generated enum in array (#971)
Browse files Browse the repository at this point in the history
  • Loading branch information
koxudaxi authored Dec 28, 2022
1 parent 55b73f9 commit c4e8f2c
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions datamodel_code_generator/parser/jsonschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
):
Expand Down
19 changes: 19 additions & 0 deletions tests/data/expected/main/main_openapi_max_items_enum/output.py
Original file line number Diff line number Diff line change
@@ -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)
17 changes: 17 additions & 0 deletions tests/data/openapi/max_items_enum.yaml
Original file line number Diff line number Diff line change
@@ -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
25 changes: 25 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit c4e8f2c

Please sign in to comment.