cli icon indicating copy to clipboard operation
cli copied to clipboard

[FEATURE] Generate Exported Constants for Properties with const Values

Open frankyhun opened this issue 1 year ago • 7 comments

Why do we need this improvement?

When defining schemas in AsyncAPI documents, the const keyword is used to enforce that a property must have a specific, fixed value. However, in the current TypeScript model generation, these fixed values are hardcoded in the interfaces, limiting reusability and clarity.

We propose that properties defined with the const keyword in the AsyncAPI schema should also generate exported constants in the TypeScript models. These constants can then be referenced in the generated interfaces, improving code maintainability and consistency.

Example Input Schema:

asyncapi: '3.0.0'
info:
  title: Example API
  version: '1.0.0'
channels:
  example/channel:
    publish:
      message:
        contentType: application/json
        payload:
          type: object
          properties:
            eventType:
              type: string
              const: "EXAMPLE_EVENT"

Expected Output in TypeScript:

// Exported constant
export const EVENT_TYPE = "EXAMPLE_EVENT";

// Interface using the constant
export interface ExampleMessage {
  eventType: typeof EVENT_TYPE; // Enforces the fixed value
}

How will this change help?

  • Improved Reusability: The constant can be reused across the codebase instead of hardcoding the value multiple times.
  • Enhanced Readability: Exported constants make the intent of fixed values more explicit.
  • Better Maintainability: If the constant value changes, it only needs to be updated in one place.
  • Alignment with TypeScript Best Practices: Encourages using constants for fixed values instead of inline literals.

Screenshots

No response

How could it be implemented/designed?

  • Modify the TypeScript template in the AsyncAPI Generator to:
  • Identify properties with the const keyword.
  • Generate an exported constant for each property with a const value.
  • Reference the exported constant in the corresponding interface.
  • This feature could optionally be configurable (e.g., via a generator flag) to provide flexibility for developers who may not require exported constants.

🚧 Breaking changes

No

👀 Have you checked for similar open issues?

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

🏢 Have you read the Contributing Guidelines?

Are you willing to work on this issue?

None

frankyhun avatar Nov 25 '24 08:11 frankyhun

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 Nov 25 '24 08:11 github-actions[bot]

i want to work on this issue ,kindly assign me @akshatnema

29deepanshutyagi avatar Dec 31 '24 13:12 29deepanshutyagi

Hello @AayushSaini101 , @derberg , @frankyhun , @akshatnema

I hope this message finds you well! This issue interests me , could you please assign it to me!

Thank you!

harshita9104 avatar Jan 11 '25 09:01 harshita9104

This issue has been automatically marked as stale because it has not had recent activity :sleeping:

It will be closed in 120 days if no further activity occurs. To unstale this issue, add a comment with a detailed explanation.

There can be many reasons why some specific issue has no activity. The most probable cause is lack of time, not lack of interest. AsyncAPI Initiative is a Linux Foundation project not owned by a single for-profit company. It is a community-driven initiative ruled under open governance model.

Let us figure out together how to push this issue forward. Connect with us through one of many communication channels we established here.

Thank you for your patience :heart:

github-actions[bot] avatar May 12 '25 00:05 github-actions[bot]

Hey @derberg I believe that this might be related to generator instead of cli or I believe to modelina as the models generation come from there only. WDYT

CC :- @Souvikns @Shurtu-gal

neoandmatrix avatar May 25 '25 13:05 neoandmatrix

this issue is related to modelina, we need to transfer it.

Souvikns avatar May 31 '25 09:05 Souvikns

This issue has been automatically marked as stale because it has not had recent activity :sleeping:

It will be closed in 120 days if no further activity occurs. To unstale this issue, add a comment with a detailed explanation.

There can be many reasons why some specific issue has no activity. The most probable cause is lack of time, not lack of interest. AsyncAPI Initiative is a Linux Foundation project not owned by a single for-profit company. It is a community-driven initiative ruled under open governance model.

Let us figure out together how to push this issue forward. Connect with us through one of many communication channels we established here.

Thank you for your patience :heart:

github-actions[bot] avatar Sep 29 '25 00:09 github-actions[bot]