Skip to content

Commit

Permalink
Add supporting of dict[Any, Any] in models
Browse files Browse the repository at this point in the history
  • Loading branch information
artsmolin committed Dec 6, 2023
1 parent e4bbc33 commit 9782c2a
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pythogen"
version = "0.2.32"
version = "0.2.33"
description = "Generator of python HTTP-clients from OpenApi specification."
homepage = "https://github.com/artsmolin/pythogen"
repository = "https://github.com/artsmolin/pythogen"
Expand Down
15 changes: 15 additions & 0 deletions pythogen/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,21 @@ def inline_allof_models(self) -> list[SchemaObject]:
def named_allof_models(self) -> list[SchemaObject]:
return [item for item in self.all_of if not item.is_inline]

@property
def is_empty_object(self) -> bool:
return all(
(
self.type is Type.object,
not self.enum,
not self.items,
not self.properties,
not self.required,
not self.all_of,
not self.any_of,
not self.discriminator,
)
)


@dataclass
class ParameterObject(SafetyKeyMixin):
Expand Down
5 changes: 4 additions & 1 deletion pythogen/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,10 @@ def j2_typerepr(schema: models.SchemaObject, document: models.Document) -> str:
representation = PRIMITIVE_TYPE_MAPPING[schema.type]

elif schema.type == models.Type.object and schema.id != "<inline+SchemaObject>":
representation = classname(schema.id)
if schema.is_empty_object:
representation = "dict[Any, Any]"
else:
representation = classname(schema.id)

elif schema.any_of:
representation = classname(schema.id)
Expand Down
3 changes: 2 additions & 1 deletion pythogen/templates/http_client/main.j2
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ class {{ schema.name }}(BaseModel):
{%- for model in models %}

{%- if model.type.value == "null" %}
{%- elif model.is_empty_object %}
{%- elif model.any_of %}
class {{ classname(model.id) }}(RootModel):
"""
Expand Down Expand Up @@ -421,7 +422,7 @@ class {{ name }}:


{% for model in models %}
{%- if model.type.value not in ("any_of", "null") %}
{%- if model.type.value not in ("any_of", "null") and not model.is_empty_object %}
{{ classname(model.id) }}.model_rebuild()
{%- else %}
{%- endif -%}
Expand Down
1 change: 1 addition & 0 deletions tests/clients/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@ class GetObjectNoRefSchemaResponse200(BaseModel):
integer_data: int | None = None
array_data: list[str] | None = None
boolean_data: bool | None = None
array_of_dicts_data: list[dict[Any, Any]] | None = None


class IntEnumOrNullObj(RootModel):
Expand Down
5 changes: 5 additions & 0 deletions tests/docs/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ paths:
boolean_data:
title: Boolean Data
type: boolean
array_of_dicts_data:
type: array
items:
type: object
additionalProperties: true
additionalProperties: false
/objects/{object_id}:
get:
Expand Down

0 comments on commit 9782c2a

Please sign in to comment.