Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate overwritten GetHashCode method for C# models #467

Closed
jonaslagoni opened this issue Mar 6, 2023 · 9 comments · Fixed by #583
Closed

Generate overwritten GetHashCode method for C# models #467

jonaslagoni opened this issue Mar 6, 2023 · 9 comments · Fixed by #583
Labels
enhancement New feature or request good first issue Good for newcomers released

Comments

@jonaslagoni
Copy link
Member

jonaslagoni commented Mar 6, 2023

Reason/Context

In Modelina for C#, you can make it generate the models with the GetHashCode method overwritten, this should be possible through the CLI.

To do this I suggest we add a flag called csharpHashcode as a boolean. If true the generator should include the following options:

presets: [
    {
      preset: CSHARP_COMMON_PRESET,
      options: {
        hashCode: true
      }
    }
  ]

Which should be added here:

fileGenerator = new CSharpFileGenerator();
as the code example shows: https://github.com/asyncapi/modelina/blob/master/examples/csharp-generate-equals-and-hashcode/index.ts

These will be the generated results: https://github.com/asyncapi/modelina/blob/master/examples/csharp-generate-json-serializer/__snapshots__/index.spec.ts.snap

Remember to also add a test here:

describe('for C#', () => {

Related documentation: https://github.com/asyncapi/modelina/blob/master/docs/languages/Csharp.md#generate-models-with-equals-and-gethashcode-methods

Feel free to reach out if you have any questions or get stuck!

Related issue #466

@jonaslagoni jonaslagoni added the enhancement New feature or request label Mar 6, 2023
@Souvikns Souvikns added the good first issue Good for newcomers label Mar 6, 2023
@octonawish-akcodes
Copy link

Can I take this

@jonaslagoni
Copy link
Member Author

@octonawish-akcodes there are no assignments or anything in that way, so it's all about just doing it 😊 Go for it 👍

@kaushik-rishi
Copy link
Contributor

kaushik-rishi commented May 23, 2023

Working on it @jonaslagoni along with #466

@kaushik-rishi
Copy link
Contributor

Hey @jonaslagoni.
Why does it seem like modelina's generating the same code when providing presets of hashcode, equals either of these or both of these.

  • when i have my preset set to equals: true, my hashcode method is being overriden
  • when i have my preset set to hashcode: true, my equals method is being overriden

or is this a default behaviour ? (kinda new to c#)

reference gist of the outputs generated

@kaushik-rishi
Copy link
Contributor

@jonaslagoni

We can include both the equals and hashcode flags in a single preset.
What are we supposed to do if the user provides both the autoImplement flag and the hashcode flag ? - Are we allowed to have both the presets ?

May be i'm lacking some knowledge of C# here meaning to what these flags actually are doing to the generated models.

@jonaslagoni
Copy link
Member Author

jonaslagoni commented May 24, 2023

By default (for this preset), there is no difference between not setting the options and setting them to true, both are added by default. So from the CLI's perspective, you can default both to false.

For autoImplement, its actually a bit obscure and the example does not really show it correct... Because the preset, is just the default preset, with custom options (i.e. it's actually rendered twice, we just don't know it).

What are we supposed to do if the user provides both the autoImplement flag and the hashcode flag ? - Are we allowed to have both the presets ?

Yep, you can have as many as you wish, you can read more about them here: https://github.com/asyncapi/modelina/blob/master/docs/presets.md

So as long as the CSHARP_DEFAULT_PRESET is first, you can stack them similar to something like this (just pseudo code):

let options = {presets: [], collectionType: csharpArrayType as 'Array' | 'List'};
if(csharpAutoImplement) {
  options.presets.push({
            preset: CSHARP_DEFAULT_PRESET,
            options: {
              autoImplementedProperties: true
            }
          })
}
if(equals || hashcode) {
  options.presets.push(
    {
      preset: CSHARP_COMMON_PRESET,
      options: {
        equal: equals,
        hashCode: hashcode
      }
    })
}
fileGenerator = new CSharpFileGenerator(options);

@kaushik-rishi
Copy link
Contributor

@jonaslagoni thanks. Cleared my mind.

Do you think one PR would be sufficient to fix both the issues in that case?

@jonaslagoni
Copy link
Member Author

Definitely 👍

@asyncapi-bot
Copy link
Contributor

🎉 This issue has been resolved in version 0.42.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers released
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants