Skip to content

Commit feced4a

Browse files
committed
fix: Address Sonar feedback on security additions
- OpenApiServer: use record deconstruction pattern for SecurityScheme.Unsupported - CredentialExtractor: rename unused catch binding to _ (java22 unnamed pattern) - SecurityBootValidationTest: hoist builder construction out of assertThatThrownBy lambdas so only build() can throw
1 parent 9cf7af8 commit feced4a

3 files changed

Lines changed: 16 additions & 24 deletions

File tree

src/main/java/com/retailsvc/http/OpenApiServer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,9 +307,9 @@ private static void validateSecurityWiring(Spec spec, Map<String, SchemeValidato
307307
throw new IllegalStateException(
308308
"security requirement references unknown scheme '" + name + "'");
309309
}
310-
if (scheme instanceof SecurityScheme.Unsupported u) {
310+
if (scheme instanceof SecurityScheme.Unsupported(String type)) {
311311
throw new IllegalStateException(
312-
"scheme '" + name + "' uses unsupported type '" + u.type() + "'");
312+
"scheme '" + name + "' uses unsupported type '" + type + "'");
313313
}
314314
if (!validators.containsKey(name)) {
315315
throw new IllegalStateException(

src/main/java/com/retailsvc/http/internal/CredentialExtractor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ private static ExtractionResult extractBasic(HttpExchange exchange) {
6060
byte[] decoded;
6161
try {
6262
decoded = Base64.getDecoder().decode(parts[1]);
63-
} catch (IllegalArgumentException e) {
63+
} catch (IllegalArgumentException _) {
6464
return ExtractionResult.malformed();
6565
}
6666
String creds = new String(decoded, StandardCharsets.UTF_8);

src/test/java/com/retailsvc/http/SecurityBootValidationTest.java

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,9 @@ void missingValidatorThrows() {
5151
List.of(),
5252
List.of(Map.of("bearerAuth", List.of())));
5353
Spec spec = Spec.from(r);
54+
OpenApiServer.Builder builder = handlerBuilder(spec);
5455

55-
assertThatThrownBy(
56-
() ->
57-
OpenApiServer.builder()
58-
.spec(spec)
59-
.handlers(Map.of("getX", req -> Response.ok(Map.of())))
60-
.port(0)
61-
.build())
56+
assertThatThrownBy(builder::build)
6257
.isInstanceOf(IllegalStateException.class)
6358
.hasMessageContaining("bearerAuth");
6459
}
@@ -71,14 +66,9 @@ void unsupportedSchemeThrowsWhenReferenced() {
7166
List.of(),
7267
List.of(Map.of("oauth", List.of())));
7368
Spec spec = Spec.from(r);
69+
OpenApiServer.Builder builder = handlerBuilder(spec);
7470

75-
assertThatThrownBy(
76-
() ->
77-
OpenApiServer.builder()
78-
.spec(spec)
79-
.handlers(Map.of("getX", req -> Response.ok(Map.of())))
80-
.port(0)
81-
.build())
71+
assertThatThrownBy(builder::build)
8272
.isInstanceOf(IllegalStateException.class)
8373
.hasMessageContaining("unsupported");
8474
}
@@ -91,18 +81,20 @@ void unknownSchemeReferenceThrows() {
9181
List.of(),
9282
List.of(Map.of("missingScheme", List.of())));
9383
Spec spec = Spec.from(r);
84+
OpenApiServer.Builder builder = handlerBuilder(spec);
9485

95-
assertThatThrownBy(
96-
() ->
97-
OpenApiServer.builder()
98-
.spec(spec)
99-
.handlers(Map.of("getX", req -> Response.ok(Map.of())))
100-
.port(0)
101-
.build())
86+
assertThatThrownBy(builder::build)
10287
.isInstanceOf(IllegalStateException.class)
10388
.hasMessageContaining("missingScheme");
10489
}
10590

91+
private static OpenApiServer.Builder handlerBuilder(Spec spec) {
92+
return OpenApiServer.builder()
93+
.spec(spec)
94+
.handlers(Map.of("getX", req -> Response.ok(Map.of())))
95+
.port(0);
96+
}
97+
10698
@Test
10799
void externalAuthSkipsAllChecks() throws Exception {
108100
Map<String, Object> r =

0 commit comments

Comments
 (0)