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

feat: adding support to change the collection type for arrays in Java models #1437

Closed
12 changes: 7 additions & 5 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -433,11 +433,11 @@ Generates typed models
USAGE
$ asyncapi generate models LANGUAGE FILE [-h] [--no-interactive] [-o <value>] [--tsModelType class|interface]
[--tsEnumType enum|union] [--tsModuleSystem ESM|CJS] [--tsIncludeComments] [--tsExportType default|named]
[--tsJsonBinPack] [--tsMarshalling] [--tsExampleInstance] [--tsRawPropertyNames] [--packageName <value>]
[--javaIncludeComments] [--javaJackson] [--javaConstraints] [--namespace <value>] [--csharpAutoImplement]
[--csharpNewtonsoft] [--csharpArrayType Array|List] [--csharpHashcode] [--csharpEqual] [--csharpSystemJson]
[--log-diagnostics] [--diagnostics-format json|stylish|junit|html|text|teamcity|pretty] [--fail-severity
error|warn|info|hint]
[--tsJsonBinPack] [--tsMarshalling] [--tsExampleInstance] [--tsRawPropertyNames] [--packageName <value>]
[--javaIncludeComments] [--javaJackson] [--javaConstraints] [--javaArrayType Array|List] [--namespace <value>]
[--csharpAutoImplement] [--csharpNewtonsoft] [--csharpArrayType Array|List] [--csharpHashcode] [--csharpEqual]
[--csharpSystemJson] [--log-diagnostics] [--diagnostics-format json|stylish|junit|html|text|teamcity|pretty]
[--fail-severity error|warn|info|hint]

ARGUMENTS
LANGUAGE (typescript|csharp|golang|java|javascript|dart|python|rust|kotlin|php|cplusplus) The language you want the
Expand All @@ -460,6 +460,8 @@ FLAGS
--fail-severity=<option> [default: error] diagnostics of this level or above will trigger a failure exit
code
<options: error|warn|info|hint>
--javaArrayType=<option> [default: Array] Java specific, define which type of array needs to be generated.
<options: Array|List>
--javaConstraints Java specific, generate the models with constraints
--javaIncludeComments Java specific, if enabled add comments while generating models.
--javaJackson Java specific, generate the models with Jackson serialization support
Expand Down
16 changes: 14 additions & 2 deletions src/commands/generate/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,13 @@ export default class Models extends Command {
required: false,
default: false
}),
javaArrayType: Flags.string({
type: 'option',
description: 'Java specific, define which type of array needs to be generated.',
options: ['Array', 'List'],
required: false,
default: 'Array'
}),

/**
* C++ and C# and PHP specific namespace to use for the generated models
Expand Down Expand Up @@ -178,7 +185,7 @@ export default class Models extends Command {
async run() {
const { args, flags } = await this.parse(Models);

const { tsModelType, tsEnumType, tsIncludeComments, tsModuleSystem, tsExportType, tsJsonBinPack, tsMarshalling, tsExampleInstance, tsRawPropertyNames, namespace, csharpAutoImplement, csharpArrayType, csharpNewtonsoft, csharpHashcode, csharpEqual, csharpSystemJson, packageName, javaIncludeComments, javaJackson, javaConstraints } = flags;
const { tsModelType, tsEnumType, tsIncludeComments, tsModuleSystem, tsExportType, tsJsonBinPack, tsMarshalling, tsExampleInstance, tsRawPropertyNames, namespace, csharpAutoImplement, csharpArrayType, csharpNewtonsoft, csharpHashcode, csharpEqual, csharpSystemJson, packageName, javaIncludeComments, javaJackson, javaConstraints, javaArrayType } = flags;
let { language, file } = args;
let output = flags.output || 'stdout';
const interactive = !flags['no-interactive'];
Expand Down Expand Up @@ -327,7 +334,12 @@ export default class Models extends Command {
if (javaIncludeComments) {presets.push(JAVA_DESCRIPTION_PRESET);}
if (javaJackson) {presets.push(JAVA_JACKSON_PRESET);}
if (javaConstraints) {presets.push(JAVA_CONSTRAINTS_PRESET);}
fileGenerator = new JavaFileGenerator({ presets });

fileGenerator = new JavaFileGenerator({
presets,
collectionType: javaArrayType as 'Array' | 'List'
});

fileOptions = {
packageName
};
Expand Down
10 changes: 10 additions & 0 deletions test/integration/generate/models.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,16 @@ describe('models', () => {
expect(ctx.stderr).to.contain('Error: In order to generate models to Java, we need to know which package they are under. Add `--packageName=PACKAGENAME` to set the desired package name.\n');
done();
});
test
.stderr()
.stdout()
.command([...generalOptions, 'java', './test/fixtures/specification.yml', `-o=${ path.resolve(outputDir, './java')}`, '--packageName', 'test.pkg', '--javaArrayType=List'])
.it('works when array type is provided', (ctx, done) => {
expect(ctx.stdout).to.contain(
'Successfully generated the following models: '
);
done();
});
});

describe('for Go', () => {
Expand Down
Loading