From 9de517281eb0766068672f62e79246ff06ca3e1f Mon Sep 17 00:00:00 2001 From: Dmytro Nosan Date: Thu, 16 Jan 2025 21:03:50 +0200 Subject: [PATCH 1/2] Fix potential NPE in GraylogExtendedLogFormatProperties Signed-off-by: Dmytro Nosan See gh-43863 --- .../GraylogExtendedLogFormatProperties.java | 7 ++++++- ...aylogExtendedLogFormatPropertiesTests.java | 19 ++++++++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/structured/GraylogExtendedLogFormatProperties.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/structured/GraylogExtendedLogFormatProperties.java index 396771e572..0e671c216f 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/structured/GraylogExtendedLogFormatProperties.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/structured/GraylogExtendedLogFormatProperties.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2024 the original author or authors. + * Copyright 2012-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -34,6 +34,11 @@ public record GraylogExtendedLogFormatProperties(String host, Service service) { static final GraylogExtendedLogFormatProperties NONE = new GraylogExtendedLogFormatProperties(null, Service.NONE); + public GraylogExtendedLogFormatProperties(String host, Service service) { + this.host = host; + this.service = (service != null) ? service : Service.NONE; + } + GraylogExtendedLogFormatProperties withDefaults(Environment environment) { String name = withFallbackProperty(environment, this.host, "spring.application.name"); Service service = this.service.withDefaults(environment); diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/structured/GraylogExtendedLogFormatPropertiesTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/structured/GraylogExtendedLogFormatPropertiesTests.java index 1d92c1c53b..71d2f02ec2 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/structured/GraylogExtendedLogFormatPropertiesTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/structured/GraylogExtendedLogFormatPropertiesTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2024 the original author or authors. + * Copyright 2012-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -41,6 +41,23 @@ class GraylogExtendedLogFormatPropertiesTests { assertThat(properties).isEqualTo(new GraylogExtendedLogFormatProperties("spring", new Service("1.2.3"))); } + @Test + void getBindsFromEnvironmentWhenHostIsPresentAndServiceIsMissing() { + MockEnvironment environment = new MockEnvironment(); + environment.setProperty("logging.structured.gelf.host", "spring"); + GraylogExtendedLogFormatProperties properties = GraylogExtendedLogFormatProperties.get(environment); + assertThat(properties).isEqualTo(new GraylogExtendedLogFormatProperties("spring", Service.NONE)); + } + + @Test + void getBindsFromEnvironmentWhenHostIsPresentAndServiceIsMissingUsesApplicationVersion() { + MockEnvironment environment = new MockEnvironment(); + environment.setProperty("logging.structured.gelf.host", "spring"); + environment.setProperty("spring.application.version", "1.2.3"); + GraylogExtendedLogFormatProperties properties = GraylogExtendedLogFormatProperties.get(environment); + assertThat(properties).isEqualTo(new GraylogExtendedLogFormatProperties("spring", new Service("1.2.3"))); + } + @Test void getWhenNoServiceNameUsesApplicationName() { MockEnvironment environment = new MockEnvironment(); From 7c52938168ace454413f20f2b1b6baa5ebbf342c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Nicoll?= Date: Sun, 19 Jan 2025 08:24:28 +0100 Subject: [PATCH 2/2] Polish "Fix potential NPE in GraylogExtendedLogFormatProperties" See gh-43863 --- .../GraylogExtendedLogFormatProperties.java | 2 +- .../GraylogExtendedLogFormatPropertiesTests.java | 11 ++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/structured/GraylogExtendedLogFormatProperties.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/structured/GraylogExtendedLogFormatProperties.java index 0e671c216f..4cb0491afe 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/structured/GraylogExtendedLogFormatProperties.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/structured/GraylogExtendedLogFormatProperties.java @@ -32,7 +32,7 @@ import org.springframework.util.StringUtils; */ public record GraylogExtendedLogFormatProperties(String host, Service service) { - static final GraylogExtendedLogFormatProperties NONE = new GraylogExtendedLogFormatProperties(null, Service.NONE); + static final GraylogExtendedLogFormatProperties NONE = new GraylogExtendedLogFormatProperties(null, null); public GraylogExtendedLogFormatProperties(String host, Service service) { this.host = host; diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/structured/GraylogExtendedLogFormatPropertiesTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/structured/GraylogExtendedLogFormatPropertiesTests.java index 71d2f02ec2..f030323647 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/structured/GraylogExtendedLogFormatPropertiesTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/structured/GraylogExtendedLogFormatPropertiesTests.java @@ -46,7 +46,7 @@ class GraylogExtendedLogFormatPropertiesTests { MockEnvironment environment = new MockEnvironment(); environment.setProperty("logging.structured.gelf.host", "spring"); GraylogExtendedLogFormatProperties properties = GraylogExtendedLogFormatProperties.get(environment); - assertThat(properties).isEqualTo(new GraylogExtendedLogFormatProperties("spring", Service.NONE)); + assertThat(properties).isEqualTo(new GraylogExtendedLogFormatProperties("spring", new Service(null))); } @Test @@ -58,6 +58,15 @@ class GraylogExtendedLogFormatPropertiesTests { assertThat(properties).isEqualTo(new GraylogExtendedLogFormatProperties("spring", new Service("1.2.3"))); } + @Test + void getBindsFromEnvironmentWhenVersionIsPresentAndHostIsMissingUsesApplicationName() { + MockEnvironment environment = new MockEnvironment(); + environment.setProperty("spring.application.name", "spring"); + environment.setProperty("logging.structured.gelf.service.version", "1.2.3"); + GraylogExtendedLogFormatProperties properties = GraylogExtendedLogFormatProperties.get(environment); + assertThat(properties).isEqualTo(new GraylogExtendedLogFormatProperties("spring", new Service("1.2.3"))); + } + @Test void getWhenNoServiceNameUsesApplicationName() { MockEnvironment environment = new MockEnvironment();