Skip to content

Commit

Permalink
Support for Primitve union properties (#79)
Browse files Browse the repository at this point in the history
For `anyOf` properties where the type is primitive (int, float, bool,
str, etc), produce a `Union` of primitive types without creating a
special "singular name" type
  • Loading branch information
joshbode authored and koxudaxi committed Nov 22, 2019
1 parent a2dcffd commit 0199de5
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 6 deletions.
3 changes: 3 additions & 0 deletions datamodel_code_generator/parser/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ def parse_any_of(self, name: str, obj: JsonSchemaObject) -> List[DataType]:
version_compatible=True,
)
)
elif not any(v for k, v in vars(any_of_item).items() if k != 'type'):
# trivial types
any_of_data_types.append(self.get_data_type(any_of_item))
else:
singular_name = get_singular_name(name)
self.parse_object(singular_name, any_of_item)
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ install_requires =
openapi-spec-validator==0.2.8
jinja2==2.10.3
inflect==3.0.1
pydantic[email,ujson]==1.0
pydantic[email]==1.0
black==19.3b0
isort==4.3.21
PySnooper==0.2.8
Expand Down
7 changes: 6 additions & 1 deletion tests/data/modular.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,12 @@ components:
type: object
properties:
name:
type: string
anyOf:
- type: string
- type: number
- type: integer
- type: boolean
- type: object
Result:
type: object
properties:
Expand Down
4 changes: 2 additions & 2 deletions tests/parser/test_openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,7 @@ class Result(BaseModel):
from __future__ import annotations
from enum import Enum
from typing import Optional
from typing import Any, Dict, Optional, Union
from pydantic import BaseModel
Expand All @@ -970,7 +970,7 @@ class User(BaseModel):
class Event(BaseModel):
name: Optional[str] = None
name: Optional[Union[str, float, int, bool, Dict[str, Any]]] = None
''',
(
'collections.py',
Expand Down
4 changes: 2 additions & 2 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ class Result(BaseModel):
from __future__ import annotations
from enum import Enum
from typing import Optional
from typing import Any, Dict, Optional, Union
from pydantic import BaseModel
Expand All @@ -327,7 +327,7 @@ class User(BaseModel):
class Event(BaseModel):
name: Optional[str] = None
name: Optional[Union[str, float, int, bool, Dict[str, Any]]] = None
''',
(
'collections.py',
Expand Down

0 comments on commit 0199de5

Please sign in to comment.