Skip to content

Commit 586f570

Browse files
committed
feat: add null checks to FriendlyIds utility methods
1 parent c5f79bf commit 586f570

4 files changed

Lines changed: 25 additions & 22 deletions

File tree

friendly-id-openfeign/src/test/java/com/devskiller/friendly_id/openfeign/FriendlyIdEncoderTest.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ void shouldEncodeUuidAsFriendlyId() {
2121
String expectedFriendlyId = toFriendlyId(uuid);
2222

2323
String[] capturedValue = new String[1];
24-
Encoder delegateEncoder = (object, bodyType, template) -> {
25-
capturedValue[0] = (String) object;
26-
};
24+
Encoder delegateEncoder = (object, bodyType, template)
25+
-> capturedValue[0] = (String) object;
2726
FriendlyIdEncoder encoder = new FriendlyIdEncoder(delegateEncoder);
2827
RequestTemplate template = new RequestTemplate();
2928

@@ -42,9 +41,8 @@ void shouldEncodeFriendlyIdValueObjectAsString() {
4241
String expectedString = friendlyId.toString();
4342

4443
String[] capturedValue = new String[1];
45-
Encoder delegateEncoder = (object, bodyType, template) -> {
46-
capturedValue[0] = (String) object;
47-
};
44+
Encoder delegateEncoder = (object, bodyType, template)
45+
-> capturedValue[0] = (String) object;
4846
FriendlyIdEncoder encoder = new FriendlyIdEncoder(delegateEncoder);
4947
RequestTemplate template = new RequestTemplate();
5048

@@ -61,9 +59,8 @@ void shouldDelegateOtherTypes() {
6159
String regularString = "test";
6260

6361
String[] capturedValue = new String[1];
64-
Encoder delegateEncoder = (object, bodyType, template) -> {
65-
capturedValue[0] = (String) object;
66-
};
62+
Encoder delegateEncoder = (object, bodyType, template)
63+
-> capturedValue[0] = (String) object;
6764
FriendlyIdEncoder encoder = new FriendlyIdEncoder(delegateEncoder);
6865
RequestTemplate template = new RequestTemplate();
6966

friendly-id-samples/friendly-id-contracts/src/main/java/com/devskiller/friendly_id/sample/contracts/SecurityConfig.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.springframework.context.annotation.Configuration;
55
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
66
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
7+
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
78
import org.springframework.security.web.SecurityFilterChain;
89

910
@Configuration
@@ -18,7 +19,7 @@ SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
1819
.requestMatchers(org.springframework.http.HttpMethod.POST, "/items").authenticated()
1920
.anyRequest().permitAll()
2021
)
21-
.csrf(csrf -> csrf.disable())
22+
.csrf(AbstractHttpConfigurer::disable)
2223
.httpBasic(basic -> {})
2324
.build();
2425
}

friendly-id-samples/friendly-id-spring-boot-jpa-demo/src/main/java/com/devskiller/friendly_id/sample/jpa/FriendlyIdJpaDemoApplication.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,23 +73,23 @@ public CommandLineRunner initData(ProductRepository repository) {
7373
repository.save(keyboard);
7474
System.out.println("Created product: Mechanical Keyboard with ID: " + keyboard.getId());
7575

76-
System.out.println("""
77-
76+
System.out.printf("""
77+
7878
========================================
7979
Demo ready!
8080
========================================
8181
REST API: http://localhost:8080/api/products
8282
H2 Console: http://localhost:8080/h2-console
83-
JDBC URL: jdbc:h2:mem:friendlyid_demo
84-
Username: sa
85-
Password: (empty)
83+
JDBC URL: jdbc:h2:mem:friendlyid_demo
84+
Username: sa
85+
Password: (empty)
8686
========================================
87-
87+
8888
Try these commands:
89-
curl http://localhost:8080/api/products
90-
curl http://localhost:8080/api/products/%s
91-
92-
""".formatted(laptop.getId()));
89+
curl http://localhost:8080/api/products
90+
curl http://localhost:8080/api/products/%s
91+
92+
%n""", laptop.getId());
9393
};
9494
}
9595
}

friendly-id/src/main/java/com/devskiller/friendly_id/FriendlyIds.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.devskiller.friendly_id;
22

3+
import java.util.Objects;
34
import java.util.UUID;
45

56
/**
@@ -42,10 +43,12 @@ public static String createFriendlyId() {
4243
/**
4344
* Encodes a UUID to FriendlyId string.
4445
*
45-
* @param uuid UUID to be encoded
46+
* @param uuid UUID to be encoded, must not be null
4647
* @return FriendlyId encoded UUID
48+
* @throws NullPointerException if uuid is null
4749
*/
4850
public static String toFriendlyId(UUID uuid) {
51+
Objects.requireNonNull(uuid, "UUID cannot be null");
4952
return Url62.encode(uuid);
5053
}
5154

@@ -58,11 +61,13 @@ public static String toFriendlyId(UUID uuid) {
5861
* <li>FriendlyId format (up to 22 chars): decoded from Base62</li>
5962
* </ul>
6063
*
61-
* @param value UUID or FriendlyId string
64+
* @param value UUID or FriendlyId string, must not be null
6265
* @return parsed UUID
66+
* @throws NullPointerException if value is null
6367
* @throws IllegalArgumentException if value is not a valid UUID or FriendlyId
6468
*/
6569
public static UUID toUuid(String value) {
70+
Objects.requireNonNull(value, "Value cannot be null");
6671
if (isStandardUuidFormat(value)) {
6772
return UUID.fromString(value);
6873
}

0 commit comments

Comments
 (0)