[BUG] Generated clients have syntax errors when document title is not a valid language identifier
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:
-
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
- JavaScript:
-
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
- JavaScript:
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->_123ServiceorClient123Serviceand#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
- Create an AsyncAPI document
asyncapi.yamlwith a title likemy-service-apior123 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'
- 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
- 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?
- [x] I have read the Contributing Guidelines
Are you willing to work on this issue ?
Yes I am willing to submit a PR!
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.
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
sure