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

Generated AsyncAPI schema can be invalid if there are models with the same name #4

Open
dm-rzn opened this issue Jun 19, 2024 · 0 comments

Comments

@dm-rzn
Copy link
Contributor

dm-rzn commented Jun 19, 2024

👋 I have noticed an edge case for generating the AsyncAPI schema. I will try to find some time to handle it soon.

Description

The generated AsyncAPI can potentially be invalid. If there are two or more models with the same name the components will refer to these models using their long name, whereas the channels will always refer to them by their short name.

To illustrate let's use the documented example:

# examples/from_readme.py

...

class PurrModel(BaseModel):
    detail: str
    loudness: int

class BellyRubModel(BaseModel):
    where_exactly: str
    scratches_num: int

...

purr_channel = sio_app.create_emitter(
    "purrs",
    model=PurrModel,
    summary="Channel for purrs",
    description="Receive any purrs here!",
)

@sio_app.on(
    "rubs",
    model=BellyRubModel,
    summary="Channel for belly rubs",
    description="Send your belly rubs through here!",
)
async def handle_rub(sid, data):
    await purr_channel.emit(
        PurrModel(loudness=2, detail="Purr for all listeners")
    )
    return "Ack to the one who rubbed"

In this situation, both channels and components will correctly refer to the models by their short names.

channels will reference:

  • BellyRubModel with "#/components/schemas/BellyRubModel"
  • PurrModel with "#/components/schemas/PurrModel"

components.schemas will contain:

  • BellyRubModel
  • PurrModel

Now consider the following file and model exists:

# examples/foobar.py

from pydantic import BaseModel

class PurrModel(BaseModel):
    foo: str
    bar: int

and the models is used in the example above:

# examples/from_readme.py
+ from examples.xyz import PurrModel as YetAnotherPurrModel

...

class PurrModel(BaseModel):
    detail: str
    loudness: int

class BellyRubModel(BaseModel):
    where_exactly: str
    scratches_num: int

...

purr_channel = sio_app.create_emitter(
    "purrs",
    model=PurrModel,
    summary="Channel for purrs",
    description="Receive any purrs here!",
)

+ yet_another_purr_channel = sio_app.create_emitter(
+    "purrs2",
+    model=YetAnotherPurrModel,
+    summary="Channel for purrs",
+    description="Receive more purrs here!",
+)

@sio_app.on(
    "rubs",
    model=BellyRubModel,
    summary="Channel for belly rubs",
    description="Send your belly rubs through here!",
)
async def handle_rub(sid, data):
    await purr_channel.emit(
        PurrModel(loudness=2, detail="Purr for all listeners")
    )
    return "Ack to the one who rubbed"

Channels will still reference the models by their short names, but the components will contain long names for the models with duplicate names.

channels will reference:

  • BellyRubModel with "#/components/schemas/BellyRubModel"
  • PurrModel with "#/components/schemas/PurrModel"

components.schemas will contain:

  • BellyRubModel
  • examples__from_readme__PurrModel
  • examples__foobar__PurrModel

Full example with the generated schema is available in this AsyncAPI Studio

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant