Skip to content

feat: generate spec-aware example files for Dart, JavaScript, and Python WebSocket clients#2139

Draft
Adi-204 wants to merge 49 commits into
asyncapi:masterfrom
Adi-204:fix/1627-dynamic-examples
Draft

feat: generate spec-aware example files for Dart, JavaScript, and Python WebSocket clients#2139
Adi-204 wants to merge 49 commits into
asyncapi:masterfrom
Adi-204:fix/1627-dynamic-examples

Conversation

@Adi-204

@Adi-204 Adi-204 commented Jun 29, 2026

Copy link
Copy Markdown
Member

Description

  • Dart: Replaces the hardcoded example.dart with a generated, spec-aware file emitted by template/example.dart.js. The example uses the real exported client class, registers per-op receive handlers, runs a bounded send loop with payloads from message.examples(), and wraps everything in proper error handling.
  • JavaScript: Same pattern for JS — replaces the hardcoded example with template/example.js.js, using async/await, try/catch, a bounded for loop, and main() entry point.
  • Python: Same pattern for Python — template/example.py.js emits a spec-aware example.py with PEP 8 indentation, per-op register_on_<op>_handler registration, time.sleep(30) keep-alive for receive-bearing specs (Python's connect() is non-blocking), and if __name__ == '__main__': main() entry point.
  • All three clients share the same architecture: 9 local JSX components per client (composer + 8 sub-components) under <client>/components/, spec-awareness driven by two inline booleans (hasSend, hasReceive), four distinct body shapes (send+receive, send-only, receive-only, none), snapshot tests for components with conditional logic, integration snapshots regenerated to capture the new example.* output per fixture, legacy example files left in place (cleanup is follow-up work), and one @asyncapi/generator minor changeset.

Related issue(s)

Fixes #1627

AI assistance

  • This PR was created with AI assistance — Generated-by: Cursor Agent (Claude Opus 4.8)
  • No AI assistance was used

Adi-204 added 30 commits June 24, 2026 23:28
@changeset-bot

changeset-bot Bot commented Jun 29, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 14bab3c

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@coderabbitai

coderabbitai Bot commented Jun 29, 2026

Copy link
Copy Markdown

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 7e96742f-7faa-4b37-9abd-77d17ad145f7

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@asyncapi-bot

Copy link
Copy Markdown
Contributor

What reviewer looks at during PR review

The following are ideal points maintainers look for during review. Reviewing these points yourself beforehand can help streamline the review process and reduce time to merge.

  1. PR Title: Use a concise title that follows our Conventional Commits guidelines and clearly summarizes the change using imperative mood (it means spoken or written as if giving a command or instruction, like "add new helper for listing operations")

    Note - In Generator, prepend feat: or fix: in PR title only when PATCH/MINOR release must be triggered.

  2. PR Description: Clearly explain the issue being solved, summarize the changes made, and mention the related issue.

    Note - In Generator, we use Maintainers Work board to track progress. Ensure the PR Description includes Resolves #<issue-number> or Fixes #<issue-number> this will automatically close the linked issue when the PR is merged and helps automate the maintainers workflow.

  3. Documentation: Update the relevant Generator documentation to accurately reflect the changes introduced in the PR, ensuring users and contributors have up-to-date guidance.

  4. Comments and JSDoc: Write clear and consistent JSDoc comments for functions, including parameter types, return values, and error conditions, so others can easily understand and use the code.

  5. DRY Code: Ensure the code follows the Don't Repeat Yourself principle. Look out for duplicate logic that can be reused.

  6. Test Coverage: Ensure the new code is well-tested with meaningful test cases that pass consistently and cover all relevant edge cases.

  7. Commit History: Contributors should avoid force-pushing as much as possible. It makes it harder to track incremental changes and review the latest updates.

  8. Template Design Principles Alignment: While reviewing template-related changes in the packages/ directory, ensure they align with the Assumptions and Principles. If any principle feels outdated or no longer applicable, start a discussion these principles are meant to evolve with the project.

  9. Reduce Scope When Needed: If an issue or PR feels too large or complex, consider splitting it and creating follow-up issues. Smaller, focused PRs are easier to review and merge.

  10. Bot Comments: As reviewers, check that contributors have appropriately addressed comments or suggestions made by automated bots. If there are bot comments the reviewer disagrees with, react to them or mark them as resolved, so the review history remains clear and accurate.

@Adi-204 Adi-204 force-pushed the fix/1627-dynamic-examples branch from 0661ca1 to 11e666a Compare June 29, 2026 17:03
Adi-204 added 8 commits June 29, 2026 23:11
… Dart

Drop the maxOpsToList cap from ExampleSendInvocations and derive instanceName from the spec client name (lowerFirst for JS, toSnakeCase for Python) so the generated examples mirror Dart's spec-aware output. Integration snapshots regenerated.
Coverage for the generated example pipeline now lives entirely in the protocol-shared integration suite. Per-component snapshot tests for Example, ExampleMain, ExampleSendInvocations, and (Python only) ExampleHandlers are redundant with the integration snapshots and were locking in noise from helper bugs (toSnakeCase acronym splitting, getClientName double-suffix). Removing them across Dart, JS, and Python keeps the test surface focused on observable client output.
These per-client changesets are superseded; the unified @asyncapi/generator changeset for the example-generation feature will be added separately.
Refreshes the hand-maintained reference file to import the hoppscotch snapshot result and use HoppscotchClient, matching the rest of the protocol fixtures.
Restore packages/templates/clients/websocket/dart/example.dart to its master state. This file is out of scope for the dynamic-examples PR.
Remove the conditional newLines={hasSend ? 1 : 2} trick (JS, Python) and the unconditional newLines={2} on registerErrorHandler (Dart). The visual blank line before the try block was not load-bearing; dropping it removes a layout responsibility that was split across two siblings and unifies the three clients on default trailing newlines. Integration snapshots regenerated.
Drop the hasSend ternary and use a fixed newLines={2} on registerErrorHandler/registerOutgoingProcessor across Dart, JS, and Python. Restores the blank line before try (and adds one between the two register calls when both are present).
ExampleKeepAlive was a one-liner used only by ExampleMain. Inlining it matches the Dart/JS pattern (no dedicated keep-alive component) and removes a now-unnecessary file. Output is byte-identical \u2014 integration snapshots unchanged.
@Adi-204 Adi-204 moved this to In Progress in Maintainers work Jul 1, 2026
Adi-204 added 2 commits July 1, 2026 23:53
…ckage

Move the per-client Example/Main/Imports/Handlers/OpenConnection/Close/
SendInvocations/OutgoingProcessor components from the dart, javascript
and python websocket templates into @asyncapi/generator-components under
components/example, gated by a `language` prop. Each template now imports
Example from the shared package. Regenerate api_components.md.
…ents

Main renders language-specific main-function bodies that are not reused
across templates, so per AGENTS.md #4.5 it belongs in each template's
local components/ directory rather than in @asyncapi/generator-components.

- Delete Example.js, Main.js and their tests/snapshots from the shared
  components package; each websocket template (dart, javascript, python)
  now has its own components/Main.js and composes Imports/Handlers/
  OpenConnection/Main/Close directly in its example.*.js entry.
- Drop the now-unused needsTime prop from the shared Imports component.
- Replace {@link X} JSDoc references in Close and OpenConnection with
  backtick code refs so the docs website's MDX parser does not treat
  them as JS expressions inside list items (fixes the unexpected-lazy
  micromark error on api_components.mdx).
- Regenerate api_components.md.
@asyncapi-bot

asyncapi-bot commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

🚀 Docs preview deployed
Below link points directly to the generator docs preview. May the force be with you!
https://6a4666ef514d8e0c7bb67588--asyncapi-website.netlify.app/docs/tools/generator

Adi-204 added 2 commits July 2, 2026 18:40
Replace `String#replace(/regex/g, ...)` with `String#replaceAll(literal, ...)`,
use `String.raw` for pure-backslash escapes, and use optional chaining for the
`message.name` guard.
…acements

Sonar's prefer-string-raw rule also fires on the `'\\\''` replacement
argument in the single-quote escape step of toJsLiteral / toPyLiteral /
toDartLiteral. Switch those to String.raw`\'` for consistency.
@sonarqubecloud

sonarqubecloud Bot commented Jul 2, 2026

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

Start generating examples

2 participants