Skip to content

Commit

Permalink
do not assume discriminator is not a property (#1811)
Browse files Browse the repository at this point in the history
* do not assume discriminator is not a property

* chore: fix precommit

* add test_pydantic_model_with_keyword_property

* rename argument to hide copy-paste origin

* chore: fix precommit

---------

Co-authored-by: Nikita Pastukhov <[email protected]>
  • Loading branch information
lecko-cngroup and Lancetnik authored Sep 25, 2024
1 parent af92337 commit f3cdffd
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
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",
}

0 comments on commit f3cdffd

Please sign in to comment.