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

do not assume discriminator is not a property #1811

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 4 additions & 1 deletion faststream/asyncapi/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,10 @@ def _move_pydantic_refs(
for i in range(len(data[k])):
data[k][i] = _move_pydantic_refs(item[i], key)

if isinstance(desciminator := data.get("discriminator"), dict):
if (
isinstance(desciminator := data.get("discriminator"), dict)
and "propertyName" in desciminator
):
data["discriminator"] = desciminator["propertyName"]

return data
27 changes: 27 additions & 0 deletions tests/asyncapi/base/arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,33 @@ async def handle(user: User): ...
"type": "object",
}

def test_pydantic_model_with_keyword_property(self):
class TestModel(pydantic.BaseModel):
discriminator: int = 0

broker = self.broker_class()

@broker.subscriber("test")
async def handle(model: TestModel): ...

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

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

for key, v in payload.items():
assert key == "TestModel"
assert v == {
"properties": {
"discriminator": {
"default": 0,
"title": "Discriminator",
"type": "integer",
},
},
"title": key,
"type": "object",
}

def test_ignores_depends(self):
broker = self.broker_class()

Expand Down
26 changes: 26 additions & 0 deletions tests/asyncapi/base/publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,29 @@ async def handler(msg: str):
schema = get_app_schema(self.build_app(broker))

assert schema.channels == {}, schema.channels

def test_pydantic_model_with_keyword_property_publisher(self):
class TestModel(pydantic.BaseModel):
discriminator: int = 0

broker = self.broker_class()

@broker.publisher("test")
async def handle(msg) -> TestModel: ...

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

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

for key, v in payload.items():
assert v == {
"properties": {
"discriminator": {
"default": 0,
"title": "Discriminator",
"type": "integer",
},
},
"title": key,
"type": "object",
}
Loading