Skip to content

Commit 7b048a0

Browse files
adinauerclaude
andcommitted
fix(spring): Bind integer HTTP status codes from YAML
Support unquoted numeric entries in Spring Boot YAML lists while preserving status code validation across Boot 2, 3, and 4. Refs #4735 Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 5bbc5e6 commit 7b048a0

6 files changed

Lines changed: 105 additions & 1 deletion

File tree

‎sentry-spring-boot-4/src/test/kotlin/io/sentry/spring/boot4/SentryAutoConfigurationTest.kt‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ import org.springframework.context.annotation.Bean
8585
import org.springframework.context.annotation.Configuration
8686
import org.springframework.core.Ordered
8787
import org.springframework.core.annotation.Order
88+
import org.springframework.core.env.MapPropertySource
8889
import org.springframework.scheduling.annotation.EnableScheduling
8990
import org.springframework.scheduling.annotation.Scheduled
9091
import org.springframework.scheduling.quartz.SchedulerFactoryBean
@@ -341,6 +342,30 @@ class SentryAutoConfigurationTest {
341342
}
342343
}
343344

345+
@Test
346+
fun `YAML list binds integer ignored server HTTP status codes`() {
347+
dsnEnabledRunner
348+
.withInitializer { context ->
349+
// YAML exposes unquoted numbers as Integer values in indexed property sources.
350+
context.environment.propertySources.addFirst(
351+
MapPropertySource(
352+
"yaml",
353+
mapOf<String, Any>(
354+
"sentry.trace-ignore-status-codes[0]" to 404,
355+
"sentry.trace-ignore-status-codes[1]" to "500-599",
356+
),
357+
)
358+
)
359+
}
360+
.run {
361+
val ranges = it.getBean(SentryProperties::class.java).traceIgnoreStatusCodes
362+
assertThat(ranges).hasSize(2)
363+
assertThat(ranges!![0].isInRange(404)).isTrue()
364+
assertThat(ranges[1].isInRange(500)).isTrue()
365+
assertThat(ranges[1].isInRange(599)).isTrue()
366+
}
367+
}
368+
344369
@Test
345370
fun `empty ignored server HTTP status codes disable the configuration`() {
346371
contextRunner

‎sentry-spring-boot-jakarta/src/test/kotlin/io/sentry/spring/boot/jakarta/SentryAutoConfigurationTest.kt‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ import org.springframework.context.annotation.Bean
8888
import org.springframework.context.annotation.Configuration
8989
import org.springframework.core.Ordered
9090
import org.springframework.core.annotation.Order
91+
import org.springframework.core.env.MapPropertySource
9192
import org.springframework.scheduling.annotation.EnableScheduling
9293
import org.springframework.scheduling.annotation.Scheduled
9394
import org.springframework.scheduling.quartz.SchedulerFactoryBean
@@ -349,6 +350,30 @@ class SentryAutoConfigurationTest {
349350
}
350351
}
351352

353+
@Test
354+
fun `YAML list binds integer ignored server HTTP status codes`() {
355+
dsnEnabledRunner
356+
.withInitializer { context ->
357+
// YAML exposes unquoted numbers as Integer values in indexed property sources.
358+
context.environment.propertySources.addFirst(
359+
MapPropertySource(
360+
"yaml",
361+
mapOf<String, Any>(
362+
"sentry.trace-ignore-status-codes[0]" to 404,
363+
"sentry.trace-ignore-status-codes[1]" to "500-599",
364+
),
365+
)
366+
)
367+
}
368+
.run {
369+
val ranges = it.getBean(SentryProperties::class.java).traceIgnoreStatusCodes
370+
assertThat(ranges).hasSize(2)
371+
assertThat(ranges!![0].isInRange(404)).isTrue()
372+
assertThat(ranges[1].isInRange(500)).isTrue()
373+
assertThat(ranges[1].isInRange(599)).isTrue()
374+
}
375+
}
376+
352377
@Test
353378
fun `empty ignored server HTTP status codes disable the configuration`() {
354379
contextRunner

‎sentry-spring-boot/src/test/kotlin/io/sentry/spring/boot/SentryAutoConfigurationTest.kt‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ import org.springframework.context.annotation.Bean
8888
import org.springframework.context.annotation.Configuration
8989
import org.springframework.core.Ordered
9090
import org.springframework.core.annotation.Order
91+
import org.springframework.core.env.MapPropertySource
9192
import org.springframework.scheduling.annotation.EnableScheduling
9293
import org.springframework.scheduling.annotation.Scheduled
9394
import org.springframework.scheduling.quartz.SchedulerFactoryBean
@@ -347,6 +348,30 @@ class SentryAutoConfigurationTest {
347348
}
348349
}
349350

351+
@Test
352+
fun `YAML list binds integer ignored server HTTP status codes`() {
353+
dsnEnabledRunner
354+
.withInitializer { context ->
355+
// YAML exposes unquoted numbers as Integer values in indexed property sources.
356+
context.environment.propertySources.addFirst(
357+
MapPropertySource(
358+
"yaml",
359+
mapOf<String, Any>(
360+
"sentry.trace-ignore-status-codes[0]" to 404,
361+
"sentry.trace-ignore-status-codes[1]" to "500-599",
362+
),
363+
)
364+
)
365+
}
366+
.run {
367+
val ranges = it.getBean(SentryProperties::class.java).traceIgnoreStatusCodes
368+
assertThat(ranges).hasSize(2)
369+
assertThat(ranges!![0].isInRange(404)).isTrue()
370+
assertThat(ranges[1].isInRange(500)).isTrue()
371+
assertThat(ranges[1].isInRange(599)).isTrue()
372+
}
373+
}
374+
350375
@Test
351376
fun `empty ignored server HTTP status codes disable the configuration`() {
352377
contextRunner

‎sentry/api/sentry.api‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,7 @@ public final class io/sentry/HttpStatusCodeRange {
712712
public fun <init> (I)V
713713
public fun <init> (II)V
714714
public fun isInRange (I)Z
715+
public static fun valueOf (Ljava/lang/Integer;)Lio/sentry/HttpStatusCodeRange;
715716
public static fun valueOf (Ljava/lang/String;)Lio/sentry/HttpStatusCodeRange;
716717
}
717718

‎sentry/src/main/java/io/sentry/HttpStatusCodeRange.java‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,19 @@ public HttpStatusCodeRange(final int statusCode) {
5757
return new HttpStatusCodeRange(min, max);
5858
}
5959

60+
/**
61+
* Converts an integer HTTP status code to a range containing only that status. Configuration
62+
* binding uses this overload for unquoted numeric YAML list entries.
63+
*
64+
* @param value the status code
65+
* @return the status code range
66+
* @throws IllegalArgumentException if the value is outside 100-599
67+
*/
68+
@ApiStatus.Internal
69+
public static @NotNull HttpStatusCodeRange valueOf(final @NotNull Integer value) {
70+
return new HttpStatusCodeRange(validateStatusCode(value, value.toString()));
71+
}
72+
6073
private static int parseStatusCode(final @NotNull String value) {
6174
for (int i = 0; i < value.length(); i++) {
6275
if (!Character.isDigit(value.charAt(i))) {
@@ -70,8 +83,12 @@ private static int parseStatusCode(final @NotNull String value) {
7083
} catch (NumberFormatException e) {
7184
throw invalidRange(value);
7285
}
86+
return validateStatusCode(statusCode, value);
87+
}
88+
89+
private static int validateStatusCode(final int statusCode, final @NotNull String originalValue) {
7390
if (statusCode < MIN_HTTP_STATUS_CODE || statusCode > MAX_HTTP_STATUS_CODE) {
74-
throw invalidRange(value);
91+
throw invalidRange(originalValue);
7592
}
7693
return statusCode;
7794
}

‎sentry/src/test/java/io/sentry/HttpStatusCodeRangeTest.kt‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ class HttpStatusCodeRangeTest {
6363
assertFalse(range.isInRange(405))
6464
}
6565

66+
@Test
67+
fun `parses an integer status code`() {
68+
val range = HttpStatusCodeRange.valueOf(404)
69+
70+
assertTrue(range.isInRange(404))
71+
assertFalse(range.isInRange(405))
72+
}
73+
6674
@Test
6775
fun `parses an inclusive range with whitespace`() {
6876
val range = HttpStatusCodeRange.valueOf(" 500 - 599 ")
@@ -95,5 +103,8 @@ class HttpStatusCodeRangeTest {
95103
listOf("99", "600", "99-200", "500-600").forEach {
96104
assertFailsWith<IllegalArgumentException> { HttpStatusCodeRange.valueOf(it) }
97105
}
106+
listOf(99, 600).forEach {
107+
assertFailsWith<IllegalArgumentException> { HttpStatusCodeRange.valueOf(it) }
108+
}
98109
}
99110
}

0 commit comments

Comments
 (0)