|
1 | 1 | # @asyncapi/generator |
2 | 2 |
|
| 3 | +## 3.1.2 |
| 4 | + |
| 5 | +### Patch Changes |
| 6 | + |
| 7 | +- 4a09f57: Bump @asyncapi/parser to 3.6.0 to support AsyncAPI 3.1.0 |
| 8 | + |
| 9 | +## 3.1.1 |
| 10 | + |
| 11 | +### Patch Changes |
| 12 | + |
| 13 | +- 2bfad27: Fix generator handling of template parameters with `false` default values, ensuring defaults are correctly injected and conditional generation works as expected. |
| 14 | + |
| 15 | +## 3.1.0 |
| 16 | + |
| 17 | +### Minor Changes |
| 18 | + |
| 19 | +- 11a1b8d: - **Updated Component**: `OnMessage` (Python) - Added discriminator-based routing logic that automatically dispatches messages to operation-specific handlers before falling back to generic handlers |
| 20 | + |
| 21 | + - **New Helpers**: |
| 22 | + - `getMessageDiscriminatorData` - Extracts discriminator key and value from individual messages |
| 23 | + - `getMessageDiscriminatorsFromOperations` - Collects all discriminator metadata from receive operations |
| 24 | + - Enhanced Python webSocket client generation with **automatic operation-based message routing**: |
| 25 | + |
| 26 | + ## How python routing works |
| 27 | + |
| 28 | + - Generated WebSocket clients now automatically route incoming messages to operation-specific handlers based on message discriminators. Users can register handlers for specific message types without manually parsing or filtering messages. |
| 29 | + - When a message arrives, the client checks it against registered discriminators (e.g., `type: "hello"`, `type: "events_api"`) |
| 30 | + - If a match is found, the message is routed to the specific operation handler (e.g., `onHelloMessage`, `onEvent`) |
| 31 | + - If no match is found, the message falls back to generic message handlers |
| 32 | + - This enables clean separation of message handling logic based on message types |
| 33 | + |
| 34 | + > `discriminator` is a `string` field that you can add to any AsyncAPI Schema. This also means that it is limited to AsyncAPI Schema only, and it won't work with other schema formats, like for example, Avro. |
| 35 | +
|
| 36 | + The implementation automatically derives discriminator information from your AsyncAPI document: |
| 37 | + |
| 38 | + - Discriminator `key` is extracted from the `discriminator` field in your AsyncAPI spec |
| 39 | + - Discriminator `value` is extracted from the `const` property defined in message schemas |
| 40 | + |
| 41 | + Example AsyncAPI Schema with `discriminator` and `const`: |
| 42 | + |
| 43 | + ```yaml |
| 44 | + schemas: |
| 45 | + hello: |
| 46 | + type: object |
| 47 | + discriminator: type # you specify name of property |
| 48 | + properties: |
| 49 | + type: |
| 50 | + type: string |
| 51 | + const: hello # you specify the value of the discriminator property that is used for routing |
| 52 | + description: A hello string confirming WebSocket connection |
| 53 | + ``` |
| 54 | +
|
| 55 | + ## Fallback |
| 56 | +
|
| 57 | + When defaults aren't available in the AsyncAPI document, users must provide **both** `discriminator_key` and `discriminator_value` when registering handlers. Providing only one parameter is not supported - you must provide either both or neither. |
| 58 | + |
| 59 | + > **Why this limitation exists**: When a receive operation has multiple messages sharing the same discriminator key (e.g., all use `"type"` field), we need the specific value (e.g., `"hello"`, `"disconnect"`) to distinguish between them. Without both pieces of information, the routing becomes ambiguous. |
| 60 | + |
| 61 | + Example: |
| 62 | + |
| 63 | + ```python |
| 64 | + # Default case - discriminator info auto-derived from AsyncAPI doc |
| 65 | + client.register_on_hello_message_handler(my_handler) |
| 66 | +
|
| 67 | + # Custom case - must provide both key AND value |
| 68 | + client.register_on_hello_message_handler( |
| 69 | + my_handler, |
| 70 | + discriminator_key="message_type", |
| 71 | + discriminator_value="custom_hello" |
| 72 | + ) |
| 73 | + ``` |
| 74 | + |
| 75 | +### Patch Changes |
| 76 | + |
| 77 | +- Updated dependencies [11a1b8d] |
| 78 | + - @asyncapi/generator-components@0.5.0 |
| 79 | + - @asyncapi/generator-helpers@1.1.0 |
| 80 | + |
3 | 81 | ## 3.0.1 |
4 | 82 |
|
5 | 83 | ### Patch Changes |
|
0 commit comments