Skip to content

Commit

Permalink
fix: correct single dataclass argument AsyncAPI payload generation (#…
Browse files Browse the repository at this point in the history
…1591)

* fix: correct single dataclass argument AsyncAPI payload generation

* lint: fix mypy

* chore: update dependencies

* chore: fix comment typo
  • Loading branch information
Lancetnik authored Jul 13, 2024
1 parent 6534e00 commit 22f15fd
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
7 changes: 6 additions & 1 deletion faststream/asyncapi/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,12 @@ def get_model_schema(
param_body = param_body[name]

if defs := body.get(DEF_KEY):
param_body[DEF_KEY] = defs
# single argument with useless reference
if param_body.get("$ref"):
ref_obj: Dict[str, Any] = next(iter(defs.values()))
return ref_obj
else:
param_body[DEF_KEY] = defs

original_title = param.title if PYDANTIC_V2 else param.field_info.title # type: ignore[attr-defined]

Expand Down
6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ devdocs = [
"mkdocstrings[python]==0.25.1",
"mkdocs-literate-nav==0.6.1",
"mkdocs-git-revision-date-localized-plugin==1.2.6",
"mike==2.1.1", # versioning
"mike==2.1.2", # versioning
"mkdocs-minify-plugin==0.8.0",
"mkdocs-macros-plugin==1.0.5", # includes with variables
"mkdocs-glightbox==0.4.0", # img zoom
Expand All @@ -97,7 +97,7 @@ devdocs = [

types = [
"faststream[optionals]",
"mypy==1.10.0",
"mypy==1.10.1",
# mypy extensions
"types-Deprecated",
"types-PyYAML",
Expand All @@ -113,7 +113,7 @@ lint = [
"faststream[types]",
"ruff==0.5.1",
"bandit==1.7.9",
"semgrep==1.78.0",
"semgrep==1.79.0",
"codespell==2.3.0",
]

Expand Down
28 changes: 28 additions & 0 deletions tests/asyncapi/base/arguments.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from dataclasses import dataclass
from enum import Enum
from typing import Optional, Type, Union

Expand Down Expand Up @@ -216,6 +217,33 @@ async def handle(msg: str, another: Optional[int] = None): ...
"type": "object",
}

def test_dataclass(self):
@dataclass
class User:
id: int
name: str = ""

broker = self.broker_class()

@broker.subscriber("test")
async def handle(user: User): ...

schema = get_app_schema(self.build_app(broker)).to_jsonable()

payload = schema["components"]["schemas"]

for key, v in payload.items():
assert key == "User"
assert v == {
"properties": {
"id": {"title": "Id", "type": "integer"},
"name": {"default": "", "title": "Name", "type": "string"},
},
"required": ["id"],
"title": key,
"type": "object",
}

def test_pydantic_model(self):
class User(pydantic.BaseModel):
name: str = ""
Expand Down

0 comments on commit 22f15fd

Please sign in to comment.