diff --git a/src/commands/generate/models.ts b/src/commands/generate/models.ts index b248f2f906e..8d4b393c59a 100644 --- a/src/commands/generate/models.ts +++ b/src/commands/generate/models.ts @@ -1,4 +1,4 @@ -import { CSharpFileGenerator, JavaFileGenerator, JavaScriptFileGenerator, TypeScriptFileGenerator, GoFileGenerator, Logger, DartFileGenerator, PythonFileGenerator, RustFileGenerator, TS_COMMON_PRESET, TS_JSONBINPACK_PRESET, CSHARP_DEFAULT_PRESET, KotlinFileGenerator, TS_DESCRIPTION_PRESET, PhpFileGenerator, CplusplusFileGenerator } from '@asyncapi/modelina'; +import { CSharpFileGenerator, JavaFileGenerator, JavaScriptFileGenerator, TypeScriptFileGenerator, GoFileGenerator, Logger, DartFileGenerator, PythonFileGenerator, RustFileGenerator, TS_COMMON_PRESET, TS_JSONBINPACK_PRESET, CSHARP_DEFAULT_PRESET, CSHARP_COMMON_PRESET, KotlinFileGenerator, TS_DESCRIPTION_PRESET, PhpFileGenerator, CplusplusFileGenerator } from '@asyncapi/modelina'; import { Flags } from '@oclif/core'; import Command from '../../base'; import { load } from '../../models/SpecificationFile'; @@ -91,7 +91,7 @@ export default class Models extends Command { }), /** - * C++ and C# specific namespace to use for the generated models + * C++ and C# and PHP specific namespace to use for the generated models */ namespace: Flags.string({ description: 'C#, C++ and PHP specific, define the namespace to use for the generated models. This is required when language is `csharp`,`c++` or `php`.', @@ -113,13 +113,23 @@ export default class Models extends Command { required: false, default: 'Array' }), + csharpHashcode: Flags.boolean({ + description: 'C# specific, generate the models with the GetHashCode method overwritten', + required: false, + default: false + }), + csharpEqual: Flags.boolean({ + description: 'C# specific, generate the models with the Equal method overwritten', + required: false, + default: false + }), ...validationFlags({ logDiagnostics: false }), }; /* eslint-disable sonarjs/cognitive-complexity */ async run() { const { args, flags } = await this.parse(Models); - const { tsModelType, tsEnumType, tsIncludeComments, tsModuleSystem, tsExportType, tsJsonBinPack, namespace, csharpAutoImplement, csharpArrayType, packageName, output } = flags; + const { tsModelType, tsEnumType, tsIncludeComments, tsModuleSystem, tsExportType, tsJsonBinPack, namespace, csharpAutoImplement, csharpArrayType, csharpHashcode, csharpEqual, packageName, output } = flags; const { language, file } = args; const inputFile = (await load(file)) || (await load()); const { document, status } = await parse(this, inputFile, flags); @@ -178,15 +188,26 @@ export default class Models extends Command { throw new Error('In order to generate models to C#, we need to know which namespace they are under. Add `--namespace=NAMESPACE` to set the desired namespace.'); } - fileGenerator = new CSharpFileGenerator({ - presets: csharpAutoImplement ? [ - { - preset: CSHARP_DEFAULT_PRESET, - options: { - autoImplementedProperties: true - } + if (csharpAutoImplement) { + presets.push({ + preset: CSHARP_DEFAULT_PRESET, + options: { + autoImplementedProperties: true + } + }); + } + if (csharpHashcode || csharpEqual) { + presets.push({ + preset: CSHARP_COMMON_PRESET, + options: { + hashCode: csharpHashcode, + equals: csharpEqual } - ] : [], + }); + } + + fileGenerator = new CSharpFileGenerator({ + presets, collectionType: csharpArrayType as 'Array' | 'List' }); diff --git a/test/commands/generate/models.test.ts b/test/commands/generate/models.test.ts index a7c48e1471c..1ae93e6278a 100644 --- a/test/commands/generate/models.test.ts +++ b/test/commands/generate/models.test.ts @@ -15,7 +15,7 @@ describe('models', () => { expect(ctx.stdout).toMatchSnapshot(); done(); }); - + test .stderr() .stdout() @@ -34,8 +34,8 @@ describe('models', () => { expect(ctx.stdout).toMatchSnapshot(); done(); }); - - describe('for TypeScript', () => { + + describe('for TypeScript', () => { test .stderr() .stdout() @@ -69,7 +69,7 @@ describe('models', () => { }); }); - describe('for JavaScript', () => { + describe('for JavaScript', () => { test .stderr() .stdout() @@ -83,7 +83,7 @@ describe('models', () => { }); }); - describe('for Python', () => { + describe('for Python', () => { test .stderr() .stdout() @@ -97,7 +97,7 @@ describe('models', () => { }); }); - describe('for Rust', () => { + describe('for Rust', () => { test .stderr() .stdout() @@ -146,7 +146,31 @@ describe('models', () => { test .stderr() .stdout() - .command([...generalOptions, 'csharp', './test/specification.yml', `-o=${ path.resolve(outputDir, './csharp')}`, '--namespace=\'asyncapi.models\'', '--csharpArrayType=List']) + .command([...generalOptions, 'csharp', './test/specification.yml', `-o=${ path.resolve(outputDir, './csharp')}`, '--namespace=\'asyncapi.models\'', '--csharpHashcode']) + .it('works when hash code flag is passed', (ctx, done) => { + expect(ctx.stderr).toEqual(''); + expect(ctx.stdout).toContain( + 'Successfully generated the following models: ' + ); + done(); + }); + + test + .stderr() + .stdout() + .command([...generalOptions, 'csharp', './test/specification.yml', `-o=${ path.resolve(outputDir, './csharp')}`, '--namespace=\'asyncapi.models\'', '--csharpEqual']) + .it('works when equal flag is passed', (ctx, done) => { + expect(ctx.stderr).toEqual(''); + expect(ctx.stdout).toContain( + 'Successfully generated the following models: ' + ); + done(); + }); + + test + .stderr() + .stdout() + .command([...generalOptions, 'csharp', './test/specification.yml', `-o=${ path.resolve(outputDir, './csharp')}`, '--namespace=\'asyncapi.models\'', '--csharpArrayType=List']) .it('works when array type is provided', (ctx, done) => { expect(ctx.stderr).toEqual(''); expect(ctx.stdout).toContain( @@ -201,8 +225,8 @@ describe('models', () => { done(); }); }); - - describe('for Go', () => { + + describe('for Go', () => { test .stderr() .stdout() @@ -225,7 +249,7 @@ describe('models', () => { }); }); - describe('for Kotlin', () => { + describe('for Kotlin', () => { test .stderr() .stdout() @@ -248,7 +272,7 @@ describe('models', () => { }); }); - describe('for Dart', () => { + describe('for Dart', () => { test .stderr() .stdout() @@ -270,7 +294,7 @@ describe('models', () => { done(); }); }); - + describe('for PHP', () => { test .stderr()