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: added presets for JavaFileGenerator #1039

Merged
merged 4 commits into from
Jan 15, 2024
Merged
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
43 changes: 40 additions & 3 deletions src/commands/generate/models.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CSharpFileGenerator, JavaFileGenerator, JavaScriptFileGenerator, TypeScriptFileGenerator, GoFileGenerator, Logger, DartFileGenerator, PythonFileGenerator, RustFileGenerator, TS_COMMON_PRESET, TS_JSONBINPACK_PRESET, CSHARP_DEFAULT_PRESET, CSHARP_NEWTONSOFT_SERIALIZER_PRESET, CSHARP_COMMON_PRESET, CSHARP_JSON_SERIALIZER_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_NEWTONSOFT_SERIALIZER_PRESET, CSHARP_COMMON_PRESET, CSHARP_JSON_SERIALIZER_PRESET, KotlinFileGenerator, TS_DESCRIPTION_PRESET, PhpFileGenerator, CplusplusFileGenerator, JAVA_CONSTRAINTS_PRESET, JAVA_JACKSON_PRESET, JAVA_COMMON_PRESET, JAVA_DESCRIPTION_PRESET } from '@asyncapi/modelina';
import { Flags } from '@oclif/core';
import { ConvertDocumentParserAPIVersion } from '@smoya/multi-parser';
import Command from '../../base';
Expand Down Expand Up @@ -100,6 +100,24 @@ export default class Models extends Command {
description: 'Go, Java and Kotlin specific, define the package to use for the generated models. This is required when language is `go`, `java` or `kotlin`.',
required: false
}),
/**
* Java specific options
*/
javaIncludeComments: Flags.boolean({
description: 'Java specific, if enabled add comments while generating models.',
required: false,
default: false
}),
javaJackson: Flags.boolean({
description: 'Java specific, generate the models with Jackson serialization support',
required: false,
default: false
}),
javaConstraints: Flags.boolean({
description: 'Java specific, generate the models with constraints',
required: false,
default: false
}),

/**
* C++ and C# and PHP specific namespace to use for the generated models
Expand Down Expand Up @@ -150,7 +168,7 @@ export default class Models extends Command {
/* eslint-disable sonarjs/cognitive-complexity */
async run() {
const { args, flags } = await this.parse(Models);
const { tsModelType, tsEnumType, tsIncludeComments, tsModuleSystem, tsExportType, tsJsonBinPack, tsMarshalling, tsExampleInstance, namespace, csharpAutoImplement, csharpArrayType, csharpNewtonsoft, csharpHashcode, csharpEqual, csharpSystemJson, packageName, output } = flags;
const { tsModelType, tsEnumType, tsIncludeComments, tsModuleSystem, tsExportType, tsJsonBinPack, tsMarshalling, tsExampleInstance, namespace, csharpAutoImplement, csharpArrayType, csharpNewtonsoft, csharpHashcode, csharpEqual, csharpSystemJson, packageName, javaIncludeComments, javaJackson, javaConstraints, output } = flags;
const { language, file } = args;
const inputFile = (await load(file)) || (await load());
if (inputFile.isAsyncAPI3()) {
Expand Down Expand Up @@ -279,7 +297,26 @@ export default class Models extends Command {
if (packageName === undefined) {
throw new 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.');
}
fileGenerator = new JavaFileGenerator();
presets.push({
preset: JAVA_COMMON_PRESET,
options
});
if (javaIncludeComments) {presets.push(JAVA_DESCRIPTION_PRESET);}
if (javaJackson) {
presets.push({
preset: JAVA_COMMON_PRESET,
options
},
JAVA_JACKSON_PRESET);
}
if (javaConstraints) {
presets.push({
preset: JAVA_COMMON_PRESET,
options
},
JAVA_CONSTRAINTS_PRESET);
}
fileGenerator = new JavaFileGenerator({ presets });
fileOptions = {
packageName
};
Expand Down