From 22f15fd3b74e49db0645deaa5486befa13b93a1f Mon Sep 17 00:00:00 2001 From: Pastukhov Nikita Date: Sat, 13 Jul 2024 17:43:04 +0300 Subject: [PATCH] fix: correct single dataclass argument AsyncAPI payload generation (#1591) * fix: correct single dataclass argument AsyncAPI payload generation * lint: fix mypy * chore: update dependencies * chore: fix comment typo --- faststream/asyncapi/message.py | 7 ++++++- pyproject.toml | 6 +++--- tests/asyncapi/base/arguments.py | 28 ++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/faststream/asyncapi/message.py b/faststream/asyncapi/message.py index a12ffb5e03..e97c50f08f 100644 --- a/faststream/asyncapi/message.py +++ b/faststream/asyncapi/message.py @@ -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] diff --git a/pyproject.toml b/pyproject.toml index fec54fac58..df0a8ce089 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 @@ -97,7 +97,7 @@ devdocs = [ types = [ "faststream[optionals]", - "mypy==1.10.0", + "mypy==1.10.1", # mypy extensions "types-Deprecated", "types-PyYAML", @@ -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", ] diff --git a/tests/asyncapi/base/arguments.py b/tests/asyncapi/base/arguments.py index 3d984af6ef..9b7bf75e39 100644 --- a/tests/asyncapi/base/arguments.py +++ b/tests/asyncapi/base/arguments.py @@ -1,3 +1,4 @@ +from dataclasses import dataclass from enum import Enum from typing import Optional, Type, Union @@ -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 = ""