Skip to content

Commit 9036249

Browse files
committed
Merge pull request #43863 from nosan
* pr/43863: Polish "Fix potential NPE in GraylogExtendedLogFormatProperties" Fix potential NPE in GraylogExtendedLogFormatProperties Closes gh-43863
2 parents ae2ca01 + 7c52938 commit 9036249

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/structured/GraylogExtendedLogFormatProperties.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,7 +32,12 @@
3232
*/
3333
public record GraylogExtendedLogFormatProperties(String host, Service service) {
3434

35-
static final GraylogExtendedLogFormatProperties NONE = new GraylogExtendedLogFormatProperties(null, Service.NONE);
35+
static final GraylogExtendedLogFormatProperties NONE = new GraylogExtendedLogFormatProperties(null, null);
36+
37+
public GraylogExtendedLogFormatProperties(String host, Service service) {
38+
this.host = host;
39+
this.service = (service != null) ? service : Service.NONE;
40+
}
3641

3742
GraylogExtendedLogFormatProperties withDefaults(Environment environment) {
3843
String name = withFallbackProperty(environment, this.host, "spring.application.name");

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/structured/GraylogExtendedLogFormatPropertiesTests.java

+27-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -41,6 +41,32 @@ void getBindsFromEnvironment() {
4141
assertThat(properties).isEqualTo(new GraylogExtendedLogFormatProperties("spring", new Service("1.2.3")));
4242
}
4343

44+
@Test
45+
void getBindsFromEnvironmentWhenHostIsPresentAndServiceIsMissing() {
46+
MockEnvironment environment = new MockEnvironment();
47+
environment.setProperty("logging.structured.gelf.host", "spring");
48+
GraylogExtendedLogFormatProperties properties = GraylogExtendedLogFormatProperties.get(environment);
49+
assertThat(properties).isEqualTo(new GraylogExtendedLogFormatProperties("spring", new Service(null)));
50+
}
51+
52+
@Test
53+
void getBindsFromEnvironmentWhenHostIsPresentAndServiceIsMissingUsesApplicationVersion() {
54+
MockEnvironment environment = new MockEnvironment();
55+
environment.setProperty("logging.structured.gelf.host", "spring");
56+
environment.setProperty("spring.application.version", "1.2.3");
57+
GraylogExtendedLogFormatProperties properties = GraylogExtendedLogFormatProperties.get(environment);
58+
assertThat(properties).isEqualTo(new GraylogExtendedLogFormatProperties("spring", new Service("1.2.3")));
59+
}
60+
61+
@Test
62+
void getBindsFromEnvironmentWhenVersionIsPresentAndHostIsMissingUsesApplicationName() {
63+
MockEnvironment environment = new MockEnvironment();
64+
environment.setProperty("spring.application.name", "spring");
65+
environment.setProperty("logging.structured.gelf.service.version", "1.2.3");
66+
GraylogExtendedLogFormatProperties properties = GraylogExtendedLogFormatProperties.get(environment);
67+
assertThat(properties).isEqualTo(new GraylogExtendedLogFormatProperties("spring", new Service("1.2.3")));
68+
}
69+
4470
@Test
4571
void getWhenNoServiceNameUsesApplicationName() {
4672
MockEnvironment environment = new MockEnvironment();

0 commit comments

Comments
 (0)