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

[chore] Remove deprecated configuration options from java-generator #5001

Merged
merged 3 commits into from
Mar 30, 2023
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

#### _**Note**_: Breaking changes

* Fix #4875: Removed unused options from the java-generator

### 6.5.1 (2023-03-20)

#### Bugs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ public class SourceCodeGenerationBenchmark {
@Setup
public void setup() {
final Config config = new Config(
null,
null,
null,
null,
null,
null,
true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,30 +55,10 @@ public class GenerateJavaSources implements Runnable {
@Option(names = { "-enum-uppercase", "--enum-uppercase" }, description = "Uppercase the enum values", required = false)
Boolean uppercaseEnum = null;

@Deprecated
@Option(names = { "-prefix-strategy",
"--prefix-strategy" }, description = "The prefix strategy to be used", required = false, hidden = true)
String prefixStrategy = null;

@Deprecated
@Option(names = { "-suffix-strategy",
"--suffix-strategy" }, description = "The suffix strategy to be used", required = false, hidden = true)
String suffixStrategy = null;

@Deprecated
@Option(names = { "-always-preserve-unknown",
"--always-preserve-unknown" }, description = "Always preserve unknown fields in the generated classes", required = false, hidden = true)
Boolean alwaysPreserveUnkownFields = null;

@Option(names = { "-add-extra-annotations",
"--add-extra-annotations" }, description = "Add extra lombok and sundrio annotation to the generated classes", required = false)
Boolean addExtraAnnotations = null;

@Deprecated
@Option(names = { "-code-structure",
"--code-structure" }, description = "Generate classes using a specific layout", required = false, hidden = true)
String codeStructure = null;

@Option(names = { "-skip-generated-annotations",
"--skip-generated-annotations" }, description = "Skip emitting the @javax.annotation.processing.Generated annotation on the generated sources", required = false, hidden = true)
Boolean skipGeneratedAnnotations = null;
Expand All @@ -89,17 +69,10 @@ public class GenerateJavaSources implements Runnable {

@Override
public void run() {
final Config.Prefix pSt = (prefixStrategy != null) ? Config.Prefix.valueOf(prefixStrategy) : null;
final Config.Suffix sSt = (suffixStrategy != null) ? Config.Suffix.valueOf(suffixStrategy) : null;
final Config.CodeStructure structure = (codeStructure != null) ? Config.CodeStructure.valueOf(codeStructure) : null;
final Boolean noGeneratedAnnotations = (skipGeneratedAnnotations != null) ? skipGeneratedAnnotations : false;
final Config config = new Config(
uppercaseEnum,
pSt,
sSt,
alwaysPreserveUnkownFields,
addExtraAnnotations,
structure,
!noGeneratedAnnotations,
packageOverrides);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,24 +60,17 @@ public List<WritableCRCompilationUnit> generate(CustomResourceDefinition crd, St

AbstractJSONSchema2Pojo specGenerator = null;

String prefix = crName;
if (config.getPrefixStrategy() == Config.Prefix.NEVER) {
prefix = "";
}

JSONSchemaProps spec = crdv.getSchema().getOpenAPIV3Schema().getProperties().get("spec");
if (spec != null) {
String suffix = (config.getSuffixStrategy() != Config.Suffix.NEVER) ? "Spec" : "";
specGenerator = AbstractJSONSchema2Pojo.fromJsonSchema(
"spec", spec, pkg, prefix, suffix, config);
crName + "Spec", spec, pkg, config);
}

AbstractJSONSchema2Pojo statusGenerator = null;
JSONSchemaProps status = crdv.getSchema().getOpenAPIV3Schema().getProperties().get("status");
if (status != null) {
String suffix = (config.getSuffixStrategy() != Config.Suffix.NEVER) ? "Status" : "";
statusGenerator = AbstractJSONSchema2Pojo.fromJsonSchema(
"status", status, pkg, prefix, suffix, config);
crName + "Status", status, pkg, config);
}

AbstractJSONSchema2Pojo crGenerator = new JCRObject(
Expand All @@ -86,8 +79,8 @@ public List<WritableCRCompilationUnit> generate(CustomResourceDefinition crd, St
group,
version,
scope,
prefix + "Spec",
prefix + "Status",
crName + "Spec",
crName + "Status",
specGenerator != null,
statusGenerator != null,
crdv.getStorage(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,72 +24,27 @@
@Builder(toBuilder = true)
@NoArgsConstructor
public class Config {
public enum CodeStructure {
FLAT,
PACKAGE_NESTED,
}

public enum Prefix {
TOP_LEVEL,
ALWAYS,
NEVER,
}

public enum Suffix {
TOP_LEVEL,
ALWAYS,
NEVER,
}

private static final boolean DEFAULT_UPPERCASE_ENUM = true;
private static final Prefix DEFAULT_PREFIX_STRATEGY = Prefix.TOP_LEVEL;
private static final Suffix DEFAULT_SUFFIX_STRATEGY = Suffix.TOP_LEVEL;
private static final boolean DEFAULT_ALWAYS_PRESERVE_FIELDS = false;
private static final boolean DEFAULT_ADD_EXTRA_ANNOTATIONS = false;
private static final CodeStructure DEFAULT_CODE_STRUCTURE = CodeStructure.PACKAGE_NESTED;
private static final boolean DEFAULT_ADD_GENERATED_ANNOTATIONS = true;
private static final Map<String, String> DEFAULT_PACKAGE_OVERRIDES = new HashMap<>();

private Boolean uppercaseEnums = DEFAULT_UPPERCASE_ENUM;
@Deprecated
private Prefix prefixStrategy = DEFAULT_PREFIX_STRATEGY;
@Deprecated
private Suffix suffixStrategy = DEFAULT_SUFFIX_STRATEGY;
@Deprecated
private Boolean alwaysPreserveUnknownFields = DEFAULT_ALWAYS_PRESERVE_FIELDS;
private Boolean objectExtraAnnotations = DEFAULT_ADD_EXTRA_ANNOTATIONS;
@Deprecated
private CodeStructure structure = DEFAULT_CODE_STRUCTURE;
private Boolean generatedAnnotations = DEFAULT_ADD_GENERATED_ANNOTATIONS;
private Map<String, String> packageOverrides = DEFAULT_PACKAGE_OVERRIDES;

public Config(
Boolean uppercaseEnums,
Prefix prefixStrategy,
Suffix suffixStrategy,
Boolean alwaysPreserveUnknownFields,
Boolean objectExtraAnnotations,
CodeStructure structure,
Boolean generatedAnnotations,
Map<String, String> packageOverrides) {
if (uppercaseEnums != null) {
this.uppercaseEnums = uppercaseEnums;
}
if (prefixStrategy != null) {
this.prefixStrategy = prefixStrategy;
}
if (suffixStrategy != null) {
this.suffixStrategy = suffixStrategy;
}
if (alwaysPreserveUnknownFields != null) {
this.alwaysPreserveUnknownFields = alwaysPreserveUnknownFields;
}
if (objectExtraAnnotations != null) {
this.objectExtraAnnotations = objectExtraAnnotations;
}
if (structure != null) {
this.structure = structure;
}
if (generatedAnnotations != null) {
this.generatedAnnotations = generatedAnnotations;
}
Expand All @@ -102,30 +57,12 @@ public boolean isUppercaseEnums() {
return (uppercaseEnums == null) ? DEFAULT_UPPERCASE_ENUM : uppercaseEnums;
}

public Prefix getPrefixStrategy() {
return (prefixStrategy == null) ? DEFAULT_PREFIX_STRATEGY : prefixStrategy;
}

public Suffix getSuffixStrategy() {
return (suffixStrategy == null) ? DEFAULT_SUFFIX_STRATEGY : suffixStrategy;
}

public boolean isAlwaysPreserveUnknownFields() {
return (alwaysPreserveUnknownFields == null)
? DEFAULT_ALWAYS_PRESERVE_FIELDS
: alwaysPreserveUnknownFields;
}

public boolean isObjectExtraAnnotations() {
return (objectExtraAnnotations == null)
? DEFAULT_ADD_EXTRA_ANNOTATIONS
: objectExtraAnnotations;
}

public CodeStructure getCodeStructure() {
return (structure == null) ? DEFAULT_CODE_STRUCTURE : structure;
}

public boolean isGeneratedAnnotations() {
return (generatedAnnotations == null)
? DEFAULT_ADD_GENERATED_ANNOTATIONS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,16 +147,12 @@ public static AbstractJSONSchema2Pojo fromJsonSchema(
String key,
JSONSchemaProps prop,
String parentPkg,
String classPrefix,
String classSuffix,
Config config) {
Function<JavaNameAndType, AbstractJSONSchema2Pojo> fromJsonSchema = javaNameAndType -> fromJsonSchema(
key,
javaNameAndType,
prop,
parentPkg,
classPrefix,
classSuffix,
config);
String type = prop.getType();
if (Boolean.TRUE.equals(prop.getXKubernetesIntOrString())) {
Expand Down Expand Up @@ -220,8 +216,6 @@ private static AbstractJSONSchema2Pojo fromJsonSchema(
JavaNameAndType nt,
JSONSchemaProps prop,
String parentPkg,
String classPrefix,
String classSuffix,
Config config) {
final boolean isNullable = Boolean.TRUE.equals(prop.getNullable());
switch (nt.getType()) {
Expand All @@ -243,8 +237,6 @@ private static AbstractJSONSchema2Pojo fromJsonSchema(
key,
prop.getItems().getSchema(),
parentPkg,
classPrefix,
classSuffix,
config),
config,
prop.getDescription(),
Expand All @@ -256,8 +248,6 @@ private static AbstractJSONSchema2Pojo fromJsonSchema(
key,
prop.getAdditionalProperties().getSchema(),
parentPkg,
classPrefix,
classSuffix,
config),
config,
prop.getDescription(),
Expand All @@ -271,8 +261,6 @@ private static AbstractJSONSchema2Pojo fromJsonSchema(
prop.getProperties(),
prop.getRequired(),
preserveUnknownFields,
classPrefix,
classSuffix,
config,
prop.getDescription(),
isNullable,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,53 +55,33 @@ public JObject(
Map<String, JSONSchemaProps> fields,
List<String> required,
boolean preserveUnknownFields,
String classPrefix,
String classSuffix,
Config config,
String description,
final boolean isNullable, JsonNode defaultValue) {
final boolean isNullable,
JsonNode defaultValue) {
super(config, description, isNullable, defaultValue, null);
this.required = new HashSet<>(Optional.ofNullable(required).orElse(Collections.emptyList()));
this.fields = new HashMap<>();
this.preserveUnknownFields = preserveUnknownFields;

this.pkg = (pkg == null) ? "" : pkg.trim();
String pkgPrefix = (this.pkg.isEmpty()) ? this.pkg : this.pkg + ".";
String clazzPrefix = (classPrefix == null) ? "" : classPrefix.trim();
String clazzSuffix = (classSuffix == null
|| type.toLowerCase(Locale.ROOT)
.endsWith(classSuffix.toLowerCase(Locale.ROOT)))
? ""
: classSuffix.trim();
String upperCasedClassName = type.substring(0, 1).toUpperCase() + type.substring(1);
this.className = AbstractJSONSchema2Pojo.sanitizeString(
clazzPrefix + upperCasedClassName + clazzSuffix);
this.className = AbstractJSONSchema2Pojo.sanitizeString(upperCasedClassName);
this.type = pkgPrefix + this.className;

if (fields == null) {
// no fields
} else {
String nextPackagePath = null;
switch (config.getCodeStructure()) {
case FLAT:
nextPackagePath = this.pkg;
break;
case PACKAGE_NESTED:
nextPackagePath = pkgPrefix + AbstractJSONSchema2Pojo.packageName(this.className);
break;
}
String nextPackagePath = pkgPrefix + AbstractJSONSchema2Pojo.packageName(this.className);

for (Map.Entry<String, JSONSchemaProps> field : fields.entrySet()) {
String nextPrefix = (config.getPrefixStrategy() == Config.Prefix.ALWAYS) ? classPrefix : "";
String nextSuffix = (config.getSuffixStrategy() == Config.Suffix.ALWAYS) ? classSuffix : "";
this.fields.put(
field.getKey(),
AbstractJSONSchema2Pojo.fromJsonSchema(
field.getKey(),
field.getValue(),
nextPackagePath,
nextPrefix,
nextSuffix,
config));
}
}
Expand Down Expand Up @@ -270,7 +250,7 @@ public GeneratorResult generateJava() {
}
}

if (this.preserveUnknownFields || config.isAlwaysPreserveUnknownFields()) {
if (this.preserveUnknownFields) {
ClassOrInterfaceType mapType = new ClassOrInterfaceType()
.setName(Keywords.JAVA_UTIL_MAP)
.setTypeArguments(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ private static Stream<Arguments> getCRDGenerationInputData() {
return Stream.of(
Arguments.of("testCrontabCrd", "crontab-crd.yml", "CronTab", "CrontabJavaCr", new Config()),
Arguments.of("testCrontabExtraAnnotationsCrd", "crontab-crd.yml", "CronTab", "CrontabJavaExtraAnnotationsCr",
new Config(null, null, null, null, Boolean.TRUE, null, true, new HashMap<>())),
new Config(null, true, null, new HashMap<>())),
Arguments.of("testKeycloakCrd", "keycloak-crd.yml", "Keycloak", "KeycloakJavaCr", new Config()),
Arguments.of("testJokeCrd", "jokerequests-crd.yml", "JokeRequest", "JokeRequestJavaCr", new Config()),
Arguments.of("testAkkaMicroservicesCrd", "akka-microservices-crd.yml", "AkkaMicroservice", "AkkaMicroserviceJavaCr",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ class CompilationTest {
@BeforeEach
void setUp() {
config = Config.builder()
.structure(Config.CodeStructure.PACKAGE_NESTED)
.generatedAnnotations(false)
.build();
}
Expand Down Expand Up @@ -89,30 +88,11 @@ void yamlCompiles(String yamlFile, int expectedGeneratedSourceFiles) throws Exce
}

@Test
void crontabCRDCompilesWithFlatPackage() throws Exception {
// Arrange
File crd = getCRD("crontab-crd.yml");
config = config.toBuilder()
.structure(Config.CodeStructure.FLAT)
.build();

// Act
new FileJavaGenerator(config, crd).run(tempDir);
Compilation compilation = javac().compile(getSources(tempDir));

// Assert
assertTrue(compilation.errors().isEmpty());
assertEquals(3, compilation.sourceFiles().size());
assertEquals(Compilation.Status.SUCCESS, compilation.status());
}

@Test
void testCrontabCRDCompilesWithExtraAnnotationsAndUnknownFields() throws Exception {
void testCrontabCRDCompilesWithExtraAnnotations() throws Exception {
// Arrange
File crd = getCRD("crontab-crd.yml");
config = config.toBuilder()
.objectExtraAnnotations(true)
.alwaysPreserveUnknownFields(true)
.build();

// Act
Expand Down
Loading