Copilot commented on code in PR #6662:
URL:
https://github.com/apache/incubator-kie-drools/pull/6662#discussion_r3091665893
##########
drools-model/drools-codegen-common/src/main/java/org/drools/codegen/common/context/AbstractDroolsModelBuildContext.java:
##########
@@ -81,11 +83,62 @@ protected static Properties load(File... resourcePaths) {
} catch (IOException ioe) {
LOGGER.debug("Unable to load '" +
APPLICATION_PROPERTIES_FILE_NAME + "'.");
}
+ File ymlFile = new File(resourcePath,
APPLICATION_PROPERTIES_YML_FILE_NAME);
+ loadYmlProperties(ymlFile, applicationProperties);
+ ymlFile = new File(resourcePath,
APPLICATION_PROPERTIES_YAML_FILE_NAME);
+ loadYmlProperties(ymlFile, applicationProperties);
}
-
return applicationProperties;
}
+ protected static void loadYmlProperties(File ymlFile, Properties
applicationProperties) {
+ Map<String, String> ymlMap = loadYmlStringMap(ymlFile);
+ if (ymlMap != null) {
+ applicationProperties.putAll(ymlMap);
+ }
+ }
+
+ protected static Map<String, String> loadYmlStringMap(File ymlFile) {
+ TreeMap<String, Object> ymlMap = loadYmlMap(ymlFile);
+ if (ymlMap != null) {
+ return convertYamlObjectToMap(ymlMap);
+ } else {
+ return null;
+ }
+ }
+
+ protected static TreeMap<String, Object> loadYmlMap(File ymlFile) {
+ if (ymlFile.exists() && ymlFile.isFile() && ymlFile.canRead()) {
+ Yaml yaml = new Yaml();
+ try {
+ return yaml.loadAs(new FileReader(ymlFile,
StandardCharsets.UTF_8), TreeMap.class);
+ } catch (IOException e) {
Review Comment:
`loadYmlMap` creates a `FileReader` without closing it. This can leak file
descriptors across repeated loads. Use try-with-resources around the
`FileReader` (and keep the YAML load inside it) so the reader is always closed,
including on success and on exceptions. Also consider catching SnakeYAML parse
exceptions (e.g., `YAMLException`) here so a malformed YAML doesn't
unexpectedly crash property loading (the `.properties` path logs+continues).
##########
drools-model/drools-codegen-common/src/test/java/org/drools/codegen/common/context/AbstractDroolsModelBuildContextTest.java:
##########
@@ -0,0 +1,113 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.
+ */
+package org.drools.codegen.common.context;
+
+import org.drools.util.FileUtils;
+import org.junit.jupiter.api.Test;
Review Comment:
Unused import `org.junit.jupiter.api.Test` (the class uses
`@ParameterizedTest` only). Removing it avoids noise and keeps the test file
tidy.
##########
drools-model/drools-codegen-common/src/main/java/org/drools/codegen/common/context/AbstractDroolsModelBuildContext.java:
##########
@@ -81,11 +83,62 @@ protected static Properties load(File... resourcePaths) {
} catch (IOException ioe) {
LOGGER.debug("Unable to load '" +
APPLICATION_PROPERTIES_FILE_NAME + "'.");
}
+ File ymlFile = new File(resourcePath,
APPLICATION_PROPERTIES_YML_FILE_NAME);
+ loadYmlProperties(ymlFile, applicationProperties);
+ ymlFile = new File(resourcePath,
APPLICATION_PROPERTIES_YAML_FILE_NAME);
+ loadYmlProperties(ymlFile, applicationProperties);
}
-
return applicationProperties;
}
+ protected static void loadYmlProperties(File ymlFile, Properties
applicationProperties) {
+ Map<String, String> ymlMap = loadYmlStringMap(ymlFile);
+ if (ymlMap != null) {
+ applicationProperties.putAll(ymlMap);
+ }
+ }
+
+ protected static Map<String, String> loadYmlStringMap(File ymlFile) {
+ TreeMap<String, Object> ymlMap = loadYmlMap(ymlFile);
+ if (ymlMap != null) {
+ return convertYamlObjectToMap(ymlMap);
+ } else {
+ return null;
+ }
+ }
+
+ protected static TreeMap<String, Object> loadYmlMap(File ymlFile) {
+ if (ymlFile.exists() && ymlFile.isFile() && ymlFile.canRead()) {
+ Yaml yaml = new Yaml();
+ try {
+ return yaml.loadAs(new FileReader(ymlFile,
StandardCharsets.UTF_8), TreeMap.class);
+ } catch (IOException e) {
+ LOGGER.debug("Unable to load '{}'.", ymlFile.getName(), e);
+ }
+ } else {
+ LOGGER.debug("Unable to load '{}'.", ymlFile.getName());
+ }
+ return null;
+ }
+
+ protected static Map<String, String>
convertYamlObjectToMap(TreeMap<String, Object> toConvert) {
+ Map<String, String> toReturn = new HashMap<>();
+ convertYamlObjectToMap(toConvert, new StringBuilder(), toReturn);
+ return toReturn;
+ }
+
+ protected static void convertYamlObjectToMap(Map<String, Object> toRead,
StringBuilder builder, Map<String, String> toPopulate) {
+ toRead.forEach((key, value) -> {
+ if (value instanceof Map) {
+ StringBuilder newBuilder = new StringBuilder(builder);
+ convertYamlObjectToMap((Map<String, Object>) value,
newBuilder.append(key).append("."), toPopulate);
+ } else {
+ String property = builder.toString() + key;
+ toPopulate.put(property, value.toString());
Review Comment:
`convertYamlObjectToMap` calls `value.toString()` unconditionally. If the
YAML contains an explicit null (e.g. `someKey:`) SnakeYAML will return `null`
and this will throw `NullPointerException` (and `Properties` also can't store
null values). Handle `null` explicitly (e.g., skip the entry or map it to an
empty string) so YAML with nulls doesn't crash property loading.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]