From 3d6fcbde2aa4c0ddf9cc4670b0210e2afc9d7dfb Mon Sep 17 00:00:00 2001 From: Nikita Pastukhov Date: Thu, 11 Jul 2024 14:52:03 +0300 Subject: [PATCH 1/4] fix: correct single dataclass argument AsyncAPI payload generation --- faststream/asyncapi/message.py | 5 ++++- tests/asyncapi/base/arguments.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/faststream/asyncapi/message.py b/faststream/asyncapi/message.py index a12ffb5e03..b8021bd6a5 100644 --- a/faststream/asyncapi/message.py +++ b/faststream/asyncapi/message.py @@ -112,7 +112,10 @@ def get_model_schema( param_body = param_body[name] if defs := body.get(DEF_KEY): - param_body[DEF_KEY] = defs + if param_body.get("$ref"): + return next(iter(defs.values())) + 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/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 = "" From da3d3f0880c0c813c4b1ac536010c4c138068fce Mon Sep 17 00:00:00 2001 From: Nikita Pastukhov Date: Thu, 11 Jul 2024 14:54:13 +0300 Subject: [PATCH 2/4] lint: fix mypy --- faststream/asyncapi/message.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/faststream/asyncapi/message.py b/faststream/asyncapi/message.py index b8021bd6a5..be6b71bee0 100644 --- a/faststream/asyncapi/message.py +++ b/faststream/asyncapi/message.py @@ -112,8 +112,10 @@ def get_model_schema( param_body = param_body[name] if defs := body.get(DEF_KEY): + # single argument with useless refference if param_body.get("$ref"): - return next(iter(defs.values())) + ref_obj: Dict[str, Any] = next(iter(defs.values())) + return ref_obj else: param_body[DEF_KEY] = defs From 1db69df9e53174134b486ad991f2c9dda4f13a31 Mon Sep 17 00:00:00 2001 From: Nikita Pastukhov Date: Thu, 11 Jul 2024 14:54:59 +0300 Subject: [PATCH 3/4] chore: update dependencies --- pyproject.toml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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", ] From 9c9da3767bebe3d9ba4286b59596632de473c2a2 Mon Sep 17 00:00:00 2001 From: Nikita Pastukhov Date: Thu, 11 Jul 2024 14:58:56 +0300 Subject: [PATCH 4/4] chore: fix comment typo --- faststream/asyncapi/message.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/faststream/asyncapi/message.py b/faststream/asyncapi/message.py index be6b71bee0..e97c50f08f 100644 --- a/faststream/asyncapi/message.py +++ b/faststream/asyncapi/message.py @@ -112,7 +112,7 @@ def get_model_schema( param_body = param_body[name] if defs := body.get(DEF_KEY): - # single argument with useless refference + # single argument with useless reference if param_body.get("$ref"): ref_obj: Dict[str, Any] = next(iter(defs.values())) return ref_obj