Skip to content

Commit

Permalink
[java-generator] Add the possibility to always emit additionalPropert…
Browse files Browse the repository at this point in the history
…ies on generated POJOs
  • Loading branch information
andreaTP committed Sep 7, 2023
1 parent a7e7014 commit b29740b
Show file tree
Hide file tree
Showing 14 changed files with 474 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* Fix #5423: OkHttpClientImpl supports setting request method for empty payload requests

#### Improvements
* Fix #5432: [java-generator] Add the possibility to always emit `additionalProperties` on generated POJOs
* Fix #5368: added support for additional ListOptions fields
* Fix #5377: added a createOr and unlock function to provide a straight-forward replacement for createOrReplace.
* Fix #5388: [crd-generator] Generate deterministic CRDs
Expand Down
7 changes: 7 additions & 0 deletions doc/java-generation-from-CRD.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ Usage: java-gen [-hV] [-add-extra-annotations] [-enum-uppercase]
-add-extra-annotations, --add-extra-annotations
Add extra lombok and sundrio annotation to the
generated classes
-always-preserve-unknown, --always-preserve-unknown
Always preserve unknown fields in the generated
classes
-deserialization-datetime-format,
--deserialization-datetime-format=<deserializationDateTimeFormat>
DateTime format used for Deserialization of fields of
Expand Down Expand Up @@ -117,6 +120,10 @@ Usage: java-gen [-hV] [-add-extra-annotations] [-enum-uppercase]
And the corresponding configurations of the Maven plugin are (output of `mvn help:describe -DgroupId=io.fabric8 -DartifactId=java-generator-maven-plugin -Dversion=<version> -Ddetail`):

```
alwaysPreserveUnknown
User property: fabric8.java-generator.always-preserve-unknown
Always inject additional properties in the generated classes
datetimeDeserializationFormat
User property: fabric8.java-generator.datetime-deserialization-format
DateTime format used for Deserialization of fields of type `date-time`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ public class GenerateJavaSources implements Runnable {
"--skip-generated-annotations" }, description = "Skip emitting the @javax.annotation.processing.Generated annotation on the generated sources", required = false, hidden = true)
Boolean skipGeneratedAnnotations = null;

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

@Option(names = { "-package-overrides",
"--package-overrides" }, description = "Apply the overrides to the package names", required = false)
Map<String, String> packageOverrides = null;
Expand All @@ -86,6 +90,7 @@ public void run() {
uppercaseEnum,
addExtraAnnotations,
!noGeneratedAnnotations,
alwaysPreserveUnkown,
packageOverrides,
filesSuffixes,
serializationDateTimeFormat,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class Config {
public static final boolean DEFAULT_UPPERCASE_ENUM = true;
public static final boolean DEFAULT_ADD_EXTRA_ANNOTATIONS = false;
public static final boolean DEFAULT_ADD_GENERATED_ANNOTATIONS = true;
public static final boolean DEFAULT_ALWAYS_PRESERVE_UNKNOWN = false;
public static final Map<String, String> DEFAULT_PACKAGE_OVERRIDES = new HashMap<>();
public static final List<String> DEFAULT_FILES_SUFFIXES = Arrays.asList(".yaml", ".yml", ".json");
// RFC 3339 - from: https://swagger.io/docs/specification/data-models/data-types/
Expand All @@ -38,6 +39,7 @@ public class Config {
private Boolean uppercaseEnums = DEFAULT_UPPERCASE_ENUM;
private Boolean objectExtraAnnotations = DEFAULT_ADD_EXTRA_ANNOTATIONS;
private Boolean generatedAnnotations = DEFAULT_ADD_GENERATED_ANNOTATIONS;
private Boolean alwaysPreserveUnknown = DEFAULT_ALWAYS_PRESERVE_UNKNOWN;
private Map<String, String> packageOverrides = DEFAULT_PACKAGE_OVERRIDES;
private List<String> filesSuffixes = DEFAULT_FILES_SUFFIXES;
private String serDatetimeFormat = DEFAULT_SER_DATETIME_FORMAT;
Expand Down Expand Up @@ -116,6 +118,41 @@ public Config(
}
}

public Config(
Boolean uppercaseEnums,
Boolean objectExtraAnnotations,
Boolean generatedAnnotations,
Boolean alwaysPreserveUnknown,
Map<String, String> packageOverrides,
List<String> filesSuffixes,
String serDatetimeFormat,
String deserDatetimeFormat) {
if (uppercaseEnums != null) {
this.uppercaseEnums = uppercaseEnums;
}
if (objectExtraAnnotations != null) {
this.objectExtraAnnotations = objectExtraAnnotations;
}
if (generatedAnnotations != null) {
this.generatedAnnotations = generatedAnnotations;
}
if (alwaysPreserveUnknown != null) {
this.alwaysPreserveUnknown = alwaysPreserveUnknown;
}
if (packageOverrides != null) {
this.packageOverrides = packageOverrides;
}
if (filesSuffixes != null) {
this.filesSuffixes = filesSuffixes;
}
if (serDatetimeFormat != null) {
this.serDatetimeFormat = serDatetimeFormat;
}
if (deserDatetimeFormat != null) {
this.deserDatetimeFormat = deserDatetimeFormat;
}
}

public boolean isUppercaseEnums() {
return (uppercaseEnums == null) ? DEFAULT_UPPERCASE_ENUM : uppercaseEnums;
}
Expand All @@ -132,6 +169,12 @@ public boolean isGeneratedAnnotations() {
: generatedAnnotations;
}

public boolean isAlwaysPreserveUnknown() {
return (alwaysPreserveUnknown == null)
? DEFAULT_ALWAYS_PRESERVE_UNKNOWN
: alwaysPreserveUnknown;
}

public Map<String, String> getPackageOverrides() {
return (packageOverrides == null || packageOverrides.isEmpty())
? DEFAULT_PACKAGE_OVERRIDES
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ public GeneratorResult generateJava() {
}
}

if (this.preserveUnknownFields) {
if (this.preserveUnknownFields || config.isAlwaysPreserveUnknown()) {
ClassOrInterfaceType mapType = new ClassOrInterfaceType()
.setName(Keywords.JAVA_UTIL_MAP)
.setTypeArguments(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ class ConfigTest {

@Test
void defaultValuesWithAllArgsConstructor() {
final Config result = new Config(null, null, null, null, null, null, null);
final Config result = new Config(null, null, null, null, null, null, null, null);
assertThat(result)
.returns(Config.DEFAULT_UPPERCASE_ENUM, Config::isUppercaseEnums)
.returns(Config.DEFAULT_ADD_EXTRA_ANNOTATIONS, Config::isObjectExtraAnnotations)
.returns(Config.DEFAULT_ADD_GENERATED_ANNOTATIONS, Config::isGeneratedAnnotations)
.returns(Config.DEFAULT_ALWAYS_PRESERVE_UNKNOWN, Config::isAlwaysPreserveUnknown)
.returns(Config.DEFAULT_PACKAGE_OVERRIDES, Config::getPackageOverrides)
.returns(Config.DEFAULT_FILES_SUFFIXES, Config::getFilesSuffixes)
.returns(Config.DEFAULT_SER_DATETIME_FORMAT, Config::getSerDatetimeFormat)
Expand All @@ -41,6 +42,7 @@ void defaultValuesWithNoArgsConstructor() {
.returns(Config.DEFAULT_UPPERCASE_ENUM, Config::isUppercaseEnums)
.returns(Config.DEFAULT_ADD_EXTRA_ANNOTATIONS, Config::isObjectExtraAnnotations)
.returns(Config.DEFAULT_ADD_GENERATED_ANNOTATIONS, Config::isGeneratedAnnotations)
.returns(Config.DEFAULT_ALWAYS_PRESERVE_UNKNOWN, Config::isAlwaysPreserveUnknown)
.returns(Config.DEFAULT_PACKAGE_OVERRIDES, Config::getPackageOverrides)
.returns(Config.DEFAULT_FILES_SUFFIXES, Config::getFilesSuffixes)
.returns(Config.DEFAULT_SER_DATETIME_FORMAT, Config::getSerDatetimeFormat)
Expand All @@ -54,6 +56,7 @@ void defaultValuesWithBuilder() {
.returns(Config.DEFAULT_UPPERCASE_ENUM, Config::isUppercaseEnums)
.returns(Config.DEFAULT_ADD_EXTRA_ANNOTATIONS, Config::isObjectExtraAnnotations)
.returns(Config.DEFAULT_ADD_GENERATED_ANNOTATIONS, Config::isGeneratedAnnotations)
.returns(Config.DEFAULT_ALWAYS_PRESERVE_UNKNOWN, Config::isAlwaysPreserveUnknown)
.returns(Config.DEFAULT_PACKAGE_OVERRIDES, Config::getPackageOverrides)
.returns(Config.DEFAULT_FILES_SUFFIXES, Config::getFilesSuffixes)
.returns(Config.DEFAULT_SER_DATETIME_FORMAT, Config::getSerDatetimeFormat)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,33 @@ void testObjectWithPreservedFields() {
assertTrue(clzT.get().getFieldByName("additionalProperties").isPresent());
}

@Test
void testConfigToGeneratePreservedUnknownFields() {
// Arrange
Config config = new Config(null, null, null, true, new HashMap<>(), new ArrayList<>(), null, null);
JObject obj = new JObject(
null,
"t",
null,
null,
false,
config,
null,
Boolean.FALSE,
null);

// Act
GeneratorResult res = obj.generateJava();

// Assert
assertEquals(1, res.getTopLevelClasses().size());
assertEquals("T", res.getTopLevelClasses().get(0).getName());

Optional<ClassOrInterfaceDeclaration> clzT = res.getTopLevelClasses().get(0).getClassByName("T");
assertTrue(clzT.isPresent());
assertTrue(clzT.get().getFieldByName("additionalProperties").isPresent());
}

@Test
void testObjectWithSpecialFieldNames() {
// Arrange
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public void setEnumUppercase(final Boolean isEnumUppercase) {
javaGeneratorConfig = new Config(isEnumUppercase,
javaGeneratorConfig.isObjectExtraAnnotations(),
javaGeneratorConfig.isGeneratedAnnotations(),
javaGeneratorConfig.isAlwaysPreserveUnknown(),
javaGeneratorConfig.getPackageOverrides(),
javaGeneratorConfig.getFilesSuffixes(),
javaGeneratorConfig.getSerDatetimeFormat(),
Expand All @@ -118,6 +119,7 @@ public void setExtraAnnotations(final Boolean isExtraAnnotations) {
javaGeneratorConfig = new Config(javaGeneratorConfig.isUppercaseEnums(),
isExtraAnnotations,
javaGeneratorConfig.isGeneratedAnnotations(),
javaGeneratorConfig.isAlwaysPreserveUnknown(),
javaGeneratorConfig.getPackageOverrides(),
javaGeneratorConfig.getFilesSuffixes(),
javaGeneratorConfig.getSerDatetimeFormat(),
Expand All @@ -136,12 +138,32 @@ public void setGeneratedAnnotations(final Boolean isGeneratedAnnotations) {
javaGeneratorConfig = new Config(javaGeneratorConfig.isUppercaseEnums(),
javaGeneratorConfig.isObjectExtraAnnotations(),
isGeneratedAnnotations,
javaGeneratorConfig.isAlwaysPreserveUnknown(),
javaGeneratorConfig.getPackageOverrides(),
javaGeneratorConfig.getFilesSuffixes(),
javaGeneratorConfig.getSerDatetimeFormat(),
javaGeneratorConfig.getDeserDatetimeFormat());
}

/**
* Emit the @javax.annotation.processing.Generated annotation on the generated sources
*
*/
public Boolean getAlwaysPreserveUnknown() {
return javaGeneratorConfig.isAlwaysPreserveUnknown();
}

public void setAlwaysPreserveUnknown(final Boolean isAlwaysPreserveUnknown) {
javaGeneratorConfig = new Config(javaGeneratorConfig.isUppercaseEnums(),
javaGeneratorConfig.isObjectExtraAnnotations(),
javaGeneratorConfig.isGeneratedAnnotations(),
isAlwaysPreserveUnknown,
javaGeneratorConfig.getPackageOverrides(),
javaGeneratorConfig.getFilesSuffixes(),
javaGeneratorConfig.getSerDatetimeFormat(),
javaGeneratorConfig.getDeserDatetimeFormat());
}

/**
* Package names to be substituted
*
Expand All @@ -154,6 +176,7 @@ public void setPackageOverrides(final Map<String, String> packageOverrides) {
javaGeneratorConfig = new Config(javaGeneratorConfig.isUppercaseEnums(),
javaGeneratorConfig.isObjectExtraAnnotations(),
javaGeneratorConfig.isGeneratedAnnotations(),
javaGeneratorConfig.isAlwaysPreserveUnknown(),
packageOverrides,
javaGeneratorConfig.getFilesSuffixes(),
javaGeneratorConfig.getSerDatetimeFormat(),
Expand All @@ -172,6 +195,7 @@ public void setPackageOverrides(final List<String> filesSuffixes) {
javaGeneratorConfig = new Config(javaGeneratorConfig.isUppercaseEnums(),
javaGeneratorConfig.isObjectExtraAnnotations(),
javaGeneratorConfig.isGeneratedAnnotations(),
javaGeneratorConfig.isAlwaysPreserveUnknown(),
javaGeneratorConfig.getPackageOverrides(),
filesSuffixes,
javaGeneratorConfig.getSerDatetimeFormat(),
Expand All @@ -190,6 +214,7 @@ public void setSerializationDatetimeFormat(final String serDatetimeFmt) {
javaGeneratorConfig = new Config(javaGeneratorConfig.isUppercaseEnums(),
javaGeneratorConfig.isObjectExtraAnnotations(),
javaGeneratorConfig.isGeneratedAnnotations(),
javaGeneratorConfig.isAlwaysPreserveUnknown(),
javaGeneratorConfig.getPackageOverrides(),
javaGeneratorConfig.getFilesSuffixes(),
serDatetimeFmt,
Expand All @@ -208,6 +233,7 @@ public void setDeserializationDatetimeFormat(final String deserDatetimeFmt) {
javaGeneratorConfig = new Config(javaGeneratorConfig.isUppercaseEnums(),
javaGeneratorConfig.isObjectExtraAnnotations(),
javaGeneratorConfig.isGeneratedAnnotations(),
javaGeneratorConfig.isAlwaysPreserveUnknown(),
javaGeneratorConfig.getPackageOverrides(),
javaGeneratorConfig.getFilesSuffixes(),
javaGeneratorConfig.getSerDatetimeFormat(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#
# Copyright (C) 2015 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

invoker.goals=test
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (C) 2015 Red Hat, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project>

<modelVersion>4.0.0</modelVersion>

<artifactId>preserve-unknown-objects</artifactId>
<groupId>io.fabric8.it</groupId>
<version>0.0-SNAPSHOT</version>
<packaging>jar</packaging>

<properties>
<maven.compiler.source>@maven.compiler.source@</maven.compiler.source>
<maven.compiler.target>@maven.compiler.target@</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>java-generator-integration-tests</artifactId>
<version>@project.version@</version>
</dependency>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>generator-annotations</artifactId>
<version>@project.version@</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>@junit.version@</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>@junit.version@</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>java-generator-maven-plugin</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<source>src/test/resources/dapr-components-crd.yaml</source>
<generatedAnnotations>false</generatedAnnotations>
<alwaysPreserveUnknown>true</alwaysPreserveUnknown>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M7</version>
<configuration>
<useFile>false</useFile>
<trimStackTrace>false</trimStackTrace>
</configuration>
</plugin>
</plugins>
</build>

</project>
Loading

0 comments on commit b29740b

Please sign in to comment.