ballerina-platform/ballerina-library

Function name issue in AsyncAPI spec for Web-sockets generation tool

Opened this issue · 0 comments

Description:
The remote function name of a websocket:Service class only gets mapped to a channel when the function name has the format of on<ParameterType>, as opposed to allowing any on<EventName> for the function name. If the accepted parameter of type User, then the remote function name should be onUser.

Steps to reproduce:

  • Create a websocket:Service class, for eg,
// This service is included here for clarity as a websocket:Service class should be returned after an `http` request upgrade.
service / on new websocket:Listener(8080) {
    # Allows clients to get real-time data on users.
    # + return - User status
    resource function get .() returns websocket:Service|websocket:UpgradeError {
        return new WsService();
    }
    
}

service class WsService {
    *websocket:Service;
    private map<types:User> users = {};

    // pay attention to the function name
    remote function onHello(types:ClientData clientData) returns types:User[] {
        return self.users.toArray();
    }
}
  • Run the generation command on the terminal
bal asyncapi1 -i ./main.bal -o spec
  • This would give the below specification
asyncapi: 2.5.0
info:
  title: /
  version: 0.1.0
servers:
  development:
    url: "{server}:{port}/"
    protocol: ws
    protocolVersion: "13"
    variables:
      server:
        default: ws://localhost
      port:
        default: "8080"
channels:
  /:
    description: Allows clients to get real-time data on users.
components: {}
x-dispatcherKey: event

Note that the above generated specification does not contain any information regarding the schemas, messages nor the publish and subscribe fields in the channel.

However, if you do the same with a websocket:Service class given as follows,

service class WsService {
    *websocket:Service;
    private map<types:User> users = {};

    // notice how the function name is in `on<ParameterType>` format
    remote function onClientData(types:ClientData clientData) returns types:User[] {
        return self.users.toArray();
    }
}

The generated spec would be,

asyncapi: 2.5.0
info:
  title: /
  version: 0.1.0
servers:
  development:
    url: "{server}:{port}/"
    protocol: ws
    protocolVersion: "13"
    variables:
      server:
        default: ws://localhost
      port:
        default: "8080"
channels:
  /:
    description: Allows clients to get real-time data on users.
    subscribe:
      message:
        payload:
          type: array
          items:
            $ref: '#/components/schemas/User'
    publish:
      message:
        $ref: '#/components/messages/ClientData'
components:
  schemas:
    ClientData:
      type: object
      required:
      - event
      - id
      properties:
        event:
          type: string
          const: ClientData
          description: type of event
        id:
          type: string
          description: id of the event
      description: Representation of a basic event.
    User:
      type: object
      required:
      - name
      - gender
      properties:
        name:
          type: string
          description: name of the user
        gender:
          type: string
          description: gender of the user
      description: Representation of a user.
  messages:
    ClientData:
      payload:
        $ref: '#/components/schemas/ClientData'
      x-response:
        payload:
          type: array
          items:
            $ref: '#/components/schemas/User'
      x-response-type: simple-rpc
x-dispatcherKey: event

Note that this contains all the necessary schemas, messages, and the publish and subscribe fields in the channel.

Affected Versions:
AsyncAPI for Web-sockets (Unreleased)
Ballerina Swan Lake Update 09

OS, DB, other environment details and versions:
OS: Ubuntu 22.04.4 LTS

Related Issues:
/ballerina-platform/ballerina-library/issues/4290