Skip to content

Commit

Permalink
feat: adding support to change the collection type for arrays in Java…
Browse files Browse the repository at this point in the history
… models (#2029)

Co-authored-by: Borrull Alonso <[email protected]>
  • Loading branch information
borrull and Borrull Alonso authored Jun 11, 2024
1 parent ad97ec8 commit 4ac03ba
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
4 changes: 3 additions & 1 deletion modelina-cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ USAGE
[--tsModelType class|interface] [--tsEnumType enum|union] [--tsModuleSystem ESM|CJS] [--tsIncludeComments]
[--tsExportType default|named] [--tsJsonBinPack] [--tsMarshalling] [--tsExampleInstance] [--tsRawPropertyNames]
[--csharpAutoImplement] [--csharpNewtonsoft] [--csharpArrayType Array|List] [--csharpHashcode] [--csharpEqual]
[--csharpSystemJson] [--javaIncludeComments] [--javaJackson] [--javaConstraints]
[--csharpSystemJson] [--javaIncludeComments] [--javaJackson] [--javaConstraints] [--javaArrayType Array|List]
ARGUMENTS
LANGUAGE (typescript|csharp|golang|java|javascript|dart|python|rust|kotlin|php|cplusplus|scala) The language you want
Expand All @@ -424,6 +424,8 @@ FLAGS
--csharpHashcode C# specific, generate the models with the GetHashCode method overwritten
--csharpNewtonsoft C# specific, generate the models with newtonsoft serialization support
--csharpSystemJson C# specific, generate the models with System.Text.Json serialization support
--javaArrayType [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
14 changes: 12 additions & 2 deletions modelina-cli/src/helpers/java.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ export const JavaOclifFlags = {
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'
})
}

/**
Expand All @@ -27,7 +34,7 @@ export const JavaOclifFlags = {
* @returns
*/
export function buildJavaGenerator(flags: any): BuilderReturnType {
const { packageName, javaIncludeComments, javaJackson, javaConstraints } = flags;
const { packageName, javaIncludeComments, javaJackson, javaConstraints, javaArrayType } = flags;
const presets = []

if (packageName === undefined) {
Expand All @@ -40,7 +47,10 @@ export function buildJavaGenerator(flags: any): BuilderReturnType {
if (javaIncludeComments) {presets.push(JAVA_DESCRIPTION_PRESET);}
if (javaJackson) {presets.push(JAVA_JACKSON_PRESET);}
if (javaConstraints) {presets.push(JAVA_CONSTRAINTS_PRESET);}
const fileGenerator = new JavaFileGenerator({ presets });
const fileGenerator = new JavaFileGenerator({
presets,
collectionType: javaArrayType as 'Array' | 'List'
});
const fileOptions = {
packageName
};
Expand Down
10 changes: 10 additions & 0 deletions modelina-cli/test/integration/generate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,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', ASYNCAPI_V2_DOCUMENT, `-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

0 comments on commit 4ac03ba

Please sign in to comment.