generator icon indicating copy to clipboard operation
generator copied to clipboard

[BUG] Generated clients have syntax errors when document title is not a valid language identifier

Open devxdh opened this issue 1 month ago • 3 comments

Describe the bug.

When generating client SDKs from an AsyncAPI document, info.title is used to name the client class (e.g. class ${clientName}). If the title contains characters that are invalid in programming language identifiers, such as hyphens, dots, symbols, or leading digits; the generator produces code with syntax errors:

  1. Titles with hyphens/dots/symbols (e.g. title: my-service-api):

    • JavaScript: class My-service-api { -> SyntaxError: Unexpected token '-'
    • Python: class My-service-api: -> SyntaxError: invalid syntax
    • Java: public class My-service-api -> compile error: invalid identifier
  2. Titles starting with digits (e.g. title: "123 Service"):

    • JavaScript: class 123Service { -> SyntaxError: Invalid or unexpected token
    • Python: class 123Service: -> SyntaxError: invalid decimal literal
    • Java: public class 123Service -> compile error: identifier cannot start with digit

In packages/helpers/src/utils.js, getClientName currently only removes whitespace (title.replace(/\s+/g, '')). It doesn't sanitize non-alphanumeric characters or handle leading digits & symbols (except underscore as it is a valid identifier in most of the languages).

Expected behavior

The title should produce a valid, compilable class identifier across target programming languages instead of throwing syntax errors.

One possible approach could be sanitizing getClientName in @asyncapi/generator-helpers to follow standard identifier rules:

  • Only allow alphanumeric characters and underscores in the identifier (converting words to PascalCase, e.g. my-service-api -> MyServiceApi).
  • Ensure it does not start with a digit and a special symbol (except underscore), prefixing if needed (e.g. 123Service -> _123Service or Client123Service and #Service -> Service).
  • Strip or transform any other symbols (except underscore), so only valid identifier characters remain.

I do need maintainers' thoughts and guidance on how you'd prefer this to be handled before starting on a fix.

Screenshots

N/A (CLI / syntax compilation output):

# Hyphenated title:
$ node -c /tmp/test-js/client.js
/tmp/test-js/client.js:13
class My-service-api {
        ^
SyntaxError: Unexpected token '-'
# Title starting with digits:
$ node -c /tmp/test-digit/client.js
/tmp/test-digit/client.js:13
class 123Service {
      ^^^
SyntaxError: Invalid or unexpected token

How to Reproduce

  1. Create an AsyncAPI document asyncapi.yaml with a title like my-service-api or 123 Service:
asyncapi: 3.0.0
info:
  title: my-service-api
  version: 1.0.0
servers:
  dev:
    host: localhost:8080
    protocol: ws
channels:
  testChannel:
    address: test
operations:
  testOp:
    action: send
    channel:
      $ref: '#/channels/testChannel'
  1. Generate client code using any client template:
# JavaScript client
node apps/generator/test/cli.js asyncapi.yaml packages/templates/clients/websocket/javascript -o /tmp/test-js -p server=dev --force-write

# Python client
node apps/generator/test/cli.js asyncapi.yaml packages/templates/clients/websocket/python -o /tmp/test-py -p server=dev --force-write
  1. Check syntax of the generated file:
node -c /tmp/test-js/client.js
# or
python -m py_compile /tmp/test-py/client.py

🖥️ Device Information [optional]

  • Operating System (OS): Linux / Fedora Workstation 44
  • Browser: Chrome
  • Browser Version: 154.0

👀 Have you checked for similar open issues?

  • [x] I checked and didn't find similar issue

🏢 Have you read the Contributing Guidelines?

Are you willing to work on this issue ?

Yes I am willing to submit a PR!

devxdh avatar Oct 08 '26 07:10 devxdh

Welcome to AsyncAPI. Thanks a lot for reporting your first issue. Please check out our contributors guide and the instructions about a basic recommended setup useful for opening a pull request.
Keep in mind there are also other channels you can use to interact with AsyncAPI community. For more details check out this issue.

github-actions[bot] avatar Oct 08 '26 07:10 github-actions[bot]

yeah we are starting with JS solution for above with PR https://github.com/asyncapi/generator/pull/2142 we can wait once it is merge

Adi-204 avatar Oct 29 '26 18:10 Adi-204

sure

devxdh avatar Oct 29 '26 18:10 devxdh