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 #2029

Merged
merged 1 commit into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
options: ['Array', 'List'],
options: ['array', 'list'],

Lets keep it to all lower case or all upper case options for now ✌️

required: false,
default: 'Array'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
default: 'Array'
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'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
collectionType: javaArrayType as 'Array' | 'List'
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'])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.command([...generalOptions, 'java', ASYNCAPI_V2_DOCUMENT, `-o=${ path.resolve(outputDir, './java')}`, '--packageName', 'test.pkg', '--javaArrayType=List'])
.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
Loading