From 30ae21357b337ddbf3f07193914b32a356200633 Mon Sep 17 00:00:00 2001 From: Josh Bode Date: Tue, 15 Oct 2019 13:49:24 +0200 Subject: [PATCH] add support for object arrays (#70) --- datamodel_code_generator/model/pydantic/types.py | 7 +++++++ datamodel_code_generator/parser/jsonschema.py | 1 + datamodel_code_generator/types.py | 1 + tests/data/modular.yaml | 6 ++++++ tests/parser/test_openapi.py | 6 +++++- tests/test_main.py | 6 +++++- 6 files changed, 25 insertions(+), 2 deletions(-) diff --git a/datamodel_code_generator/model/pydantic/types.py b/datamodel_code_generator/model/pydantic/types.py index 8396b0de7..43d0e952f 100644 --- a/datamodel_code_generator/model/pydantic/types.py +++ b/datamodel_code_generator/model/pydantic/types.py @@ -52,6 +52,13 @@ type='IPv6Address', imports_=[Import(from_='pydantic', import_='IPv6Address')] ), Types.boolean: DataType(type='bool'), + Types.object: DataType( + type='Dict[str, Any]', + imports_=[ + Import(from_='typing', import_='Any'), + Import(from_='typing', import_='Dict'), + ], + ), } diff --git a/datamodel_code_generator/parser/jsonschema.py b/datamodel_code_generator/parser/jsonschema.py index 704ff9df7..54dc18489 100644 --- a/datamodel_code_generator/parser/jsonschema.py +++ b/datamodel_code_generator/parser/jsonschema.py @@ -31,6 +31,7 @@ 'ipv6': Types.ipv6, }, 'boolean': {'default': Types.boolean}, + 'object': {'default': Types.object}, } diff --git a/datamodel_code_generator/types.py b/datamodel_code_generator/types.py index 52f97e18a..1ea63930b 100644 --- a/datamodel_code_generator/types.py +++ b/datamodel_code_generator/types.py @@ -69,3 +69,4 @@ class Types(Enum): ipv4 = auto() ipv6 = auto() boolean = auto() + object = auto() diff --git a/tests/data/modular.yaml b/tests/data/modular.yaml index dd8baaf1f..06e952c5c 100644 --- a/tests/data/modular.yaml +++ b/tests/data/modular.yaml @@ -188,3 +188,9 @@ components: properties: attributes: type: object + foo.bar.Thang: + properties: + attributes: + type: array + items: + type: object diff --git a/tests/parser/test_openapi.py b/tests/parser/test_openapi.py index 0874c655d..4a4bc5d39 100644 --- a/tests/parser/test_openapi.py +++ b/tests/parser/test_openapi.py @@ -1001,13 +1001,17 @@ class apis(BaseModel): ): '''\ from __future__ import annotations -from typing import Any, Dict, Optional +from typing import Any, Dict, List, Optional from pydantic import BaseModel class Thing(BaseModel): attributes: Optional[Dict[str, Any]] = None + + +class Thang(BaseModel): + attributes: Optional[List[Dict[str, Any]]] = None ''', }, ) diff --git a/tests/test_main.py b/tests/test_main.py index e660a743e..372081f11 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -385,13 +385,17 @@ class apis(BaseModel): from __future__ import annotations -from typing import Any, Dict, Optional +from typing import Any, Dict, List, Optional from pydantic import BaseModel class Thing(BaseModel): attributes: Optional[Dict[str, Any]] = None + + +class Thang(BaseModel): + attributes: Optional[List[Dict[str, Any]]] = None ''', } ],