diff --git a/src/Generator/Python.php b/src/Generator/Python.php index 918a5218..a627e850 100644 --- a/src/Generator/Python.php +++ b/src/Generator/Python.php @@ -20,12 +20,14 @@ namespace PSX\Schema\Generator; +use PSX\Schema\Format; use PSX\Schema\Generator\Normalizer\NormalizerInterface; use PSX\Schema\Generator\Type\GeneratorInterface; use PSX\Schema\Type\AnyType; use PSX\Schema\Type\ArrayType; use PSX\Schema\Type\MapType; use PSX\Schema\Type\ReferenceType; +use PSX\Schema\Type\StringType; use PSX\Schema\Type\StructType; use PSX\Schema\Type\TypeAbstract; use PSX\Schema\Type\UnionType; @@ -151,6 +153,14 @@ private function getImports(TypeAbstract $origin): array $imports[] = 'from typing import Union'; } + if (TypeUtil::contains($origin, StringType::class, Format::DATE)) { + $imports[] = 'import datetime'; + } elseif (TypeUtil::contains($origin, StringType::class, Format::TIME)) { + $imports[] = 'import datetime'; + } elseif (TypeUtil::contains($origin, StringType::class, Format::DATETIME)) { + $imports[] = 'import datetime'; + } + $refs = []; TypeUtil::walk($origin, function(TypeInterface $type) use (&$refs){ if ($type instanceof ReferenceType) { diff --git a/tests/Generator/resource/python/python.py b/tests/Generator/resource/python/python.py index a80af8b3..4730db5b 100644 --- a/tests/Generator/resource/python/python.py +++ b/tests/Generator/resource/python/python.py @@ -48,6 +48,7 @@ class Meta(Dict[str, str]): from typing import List from typing import Dict from typing import Union +import datetime from .meta import Meta from .author import Author from .location import Location