Skip to content

Commit b582125

Browse files
authored
Merge pull request #224 from LongBeachHXC/add-enum-field-as-literal
Adding support for datamodel-code-generator's enum-field-as-literal argument
2 parents 127dddd + 68dfe2e commit b582125

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

fastapi_code_generator/__main__.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from typing import Dict, List, Optional
44

55
import typer
6-
from datamodel_code_generator import PythonVersion, chdir
6+
from datamodel_code_generator import LiteralType, PythonVersion, chdir
77
from datamodel_code_generator.format import CodeFormatter
88
from datamodel_code_generator.imports import Import, Imports
99
from datamodel_code_generator.reference import Reference
@@ -25,13 +25,25 @@ def main(
2525
output_dir: Path = typer.Option(..., "--output", "-o"),
2626
model_file: str = typer.Option(None, "--model-file", "-m"),
2727
template_dir: Optional[Path] = typer.Option(None, "--template-dir", "-t"),
28+
enum_field_as_literal: Optional[LiteralType] = typer.Option(
29+
None, "--enum-field-as-literal"
30+
),
2831
) -> None:
2932
input_name: str = input_file.name
3033
input_text: str = input_file.read()
3134
if model_file:
3235
model_path = Path(model_file).with_suffix('.py')
3336
else:
3437
model_path = MODEL_PATH
38+
if enum_field_as_literal:
39+
return generate_code(
40+
input_name,
41+
input_text,
42+
output_dir,
43+
template_dir,
44+
model_path,
45+
enum_field_as_literal,
46+
)
3547
return generate_code(input_name, input_text, output_dir, template_dir, model_path)
3648

3749

@@ -51,15 +63,18 @@ def generate_code(
5163
output_dir: Path,
5264
template_dir: Optional[Path],
5365
model_path: Optional[Path] = None,
66+
enum_field_as_literal: Optional[str] = None,
5467
) -> None:
5568
if not model_path:
5669
model_path = MODEL_PATH
5770
if not output_dir.exists():
5871
output_dir.mkdir(parents=True)
5972
if not template_dir:
6073
template_dir = BUILTIN_TEMPLATE_DIR
61-
62-
parser = OpenAPIParser(input_text)
74+
if enum_field_as_literal:
75+
parser = OpenAPIParser(input_text, enum_field_as_literal=enum_field_as_literal)
76+
else:
77+
parser = OpenAPIParser(input_text)
6378
with chdir(output_dir):
6479
models = parser.parse()
6580
if not models:

0 commit comments

Comments
 (0)