Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correct single dataclass argument AsyncAPI payload generation #1591

Merged
merged 4 commits into from
Jul 13, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 refference
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
Loading