asyncapi / modelina

A library for generating typed models based on inputs such as AsyncAPI, OpenAPI, and JSON Schema documents with high customization
https://modelina.org
Apache License 2.0
306 stars 178 forks source link

[FEATURE] Change the name of the generated model class withe name of message name. #1996

Open AayushSaini101 opened 5 months ago

AayushSaini101 commented 5 months ago

Why do we need this improvement?

Asyncapi.yml:

asyncapi: '2.2.0'
info:
  title: Account Service
  version: 1.0.0
  description: This service is in charge of processing user signups
channels:
  user/signedup:
    subscribe:
      message:
        $ref: '#/components/messages/UserSignedUp'
components:
  messages:
    UserSignedUp:
      payload:
        type: object
        properties:
          displayName:
            type: string
            description: Name of the user
          email:
            type: string
            format: email
            description: Email of the user

Output:

asyncapi generate models java ./asyncapi.yml --packageName=src

Successfully generated the following models: 
## Model name: AnonymousSchema_1 // Name should be change to userSignedUp
package src;

import java.util.Map;
public class AnonymousSchema_1 {
  private String displayName;
  private String email;
  private Map<String, Object> additionalProperties;

  public String getDisplayName() { return this.displayName; }
  public void setDisplayName(String displayName) { this.displayName = displayName; }

  public String getEmail() { return this.email; }
  public void setEmail(String email) { this.email = email; }

  public Map<String, Object> getAdditionalProperties() { return this.additionalProperties; }
  public void setAdditionalProperties(Map<String, Object> additionalProperties) { this.additionalProperties = additionalProperties; }
}

How will this change help?

This change will help to adhere proper naming of the project, lets suppose if we want to call the model class we need to call the AnonymousSchema_1 which seems to be not valid.

Screenshots

No response

How could it be implemented/designed?

🚧 Breaking changes

No

πŸ‘€ Have you checked for similar open issues?

🏒 Have you read the Contributing Guidelines?

Are you willing to work on this issue?

Yes I am willing to submit a PR!

github-actions[bot] commented 5 months ago

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.

jonaslagoni commented 4 months ago

Makes sense, taget next @AayushSaini101 :v:

black-snow commented 3 months ago

I just stumbled over this. I ran asyncapi generate models and end up with:

Successfully generated the following models: AnonymousSchema1, AnonymousSchema25, AnonymousSchema28, AnonymousSchema31, AnonymousSchema38, AnonymousSchema41, AnonymousSchema44

I would've expected that the file name as well as the class name do use the actual name of the component / message / whatevs. Is there currently no way or do I have to set some extra annotation somewhere?

jonaslagoni commented 2 months ago

Happy to accept and help guide a PR πŸ™‚ Otherwise, yes, explicit annotations is the workaround

black-snow commented 2 months ago

What annotation would that be?

black-snow commented 2 months ago

This would be a breaking change, right?

Any idea where to start? I imagined this to be an easy change but grepping for anonymous yields only CommonModel and AsyncAPIInputProcessor apart from the tests / examples. It's not apparent to me where AnonymousSchema_N gets generated.

jonaslagoni commented 2 months ago

What annotation would that be?

You would have to explicitly define the message payload as the name of the message.

This would be a breaking change, right?

Yes, just target the next branch πŸ™‚ Unless you want to hide it behind an option.

Any idea where to start? I imagined this to be an easy change but grepping for anonymous yields only CommonModel and AsyncAPIInputProcessor apart from the tests / examples. It's not apparent to me where AnonymousSchema_N gets generated.

All this logic happens in the input processor for AsyncAPI: https://github.com/asyncapi/modelina/blob/9f9fc78e40bf1efcc64d73d03085de75d6012976/src/processors/AsyncAPIInputProcessor.ts#L128