fix(openapi-generator): fixes GlobalSettings (#20744)

* fix(openapi-generator): fixes GlobalSettings class to avoid ClassCastException when GlobalSettings#log is invoked

* fix(openapi-generator): sets GlobalSettings log level to debug
This commit is contained in:
cagliostro92 2025-03-16 15:03:03 +01:00 committed by GitHub
parent 191eba4afa
commit 995c6c5a88
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 44 additions and 5 deletions

View File

@ -38,12 +38,13 @@ public class GlobalSettings {
private static final Logger LOGGER = LoggerFactory.getLogger(GlobalSettings.class);
private static ThreadLocal<Properties> properties = new InheritableThreadLocal<Properties>() {
private static ThreadLocal<Properties> properties = new InheritableThreadLocal<>() {
@Override
protected Properties initialValue() {
// avoid using System.getProperties().clone() which is broken in Gradle - see https://github.com/gradle/gradle/issues/17344
Properties copy = new Properties();
copy.putAll(System.getProperties());
System.getProperties()
.forEach((k,v) -> copy.put(String.valueOf(k), String.valueOf(v)));
return copy;
}
};
@ -69,8 +70,10 @@ public class GlobalSettings {
}
public static void log() {
StringWriter stringWriter = new StringWriter();
properties.get().list(new PrintWriter(stringWriter));
LOGGER.debug("GlobalSettings: {}", stringWriter);
if(LOGGER.isDebugEnabled()) {
StringWriter stringWriter = new StringWriter();
properties.get().list(new PrintWriter(stringWriter));
LOGGER.debug("GlobalSettings: {}", stringWriter);
}
}
}

View File

@ -0,0 +1,36 @@
package org.openapitools.codegen.config;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import java.util.Properties;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatNoException;
import org.slf4j.LoggerFactory;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
/**
* Test class for {@link GlobalSettings}
* @author Edoardo Patti
*/
public class GlobalSettingsTest {
private static final Object OBJECT = new Object();
@BeforeClass
public void setUp() {
((Logger) LoggerFactory.getLogger(GlobalSettings.class)).setLevel(Level.DEBUG);
Properties props = new Properties(2);
props.put("test1", OBJECT);
props.put(OBJECT, "test2");
System.getProperties().putAll(props);
}
@Test
public void testNonStringSystemProperties() {
assertThat(GlobalSettings.getProperty(OBJECT.toString())).isNotNull();
assertThat(GlobalSettings.getProperty("test1")).isNotNull();
assertThatNoException().isThrownBy(GlobalSettings::log);
}
}