Skip to content

Commit 33b9a62

Browse files
committed
custom providers included in the fromString() results
Signed-off-by: Maxim Nesen <[email protected]>
1 parent 795e4ac commit 33b9a62

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

core-common/src/main/java/org/glassfish/jersey/internal/inject/ParamConverters.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2024 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2025 Oracle and/or its affiliates. All rights reserved.
33
* Copyright (c) 2018 Payara Foundation and/or its affiliates.
44
*
55
* This program and the accompanying materials are made available under the
@@ -326,7 +326,8 @@ public T fromString(String value) {
326326
final List<ClassTypePair> ctps = ReflectionHelper.getTypeArgumentAndClass(genericType);
327327
final ClassTypePair ctp = (ctps.size() == 1) ? ctps.get(0) : null;
328328
final boolean empty = value.isEmpty();
329-
for (ParamConverterProvider provider : Providers.getProviders(manager, ParamConverterProvider.class)) {
329+
for (final ParamConverterProvider provider
330+
: Providers.getAllProviders(manager, ParamConverterProvider.class)) {
330331
final ParamConverter<?> converter = provider.getConverter(ctp.rawClass(), ctp.type(), annotations);
331332
if (converter != null) {
332333
if (empty) {

core-server/src/test/java/org/glassfish/jersey/server/internal/inject/ParamConverterInternalTest.java

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2023 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2025 Oracle and/or its affiliates. All rights reserved.
33
* Copyright (c) 2018 Payara Foundation and/or its affiliates.
44
*
55
* This program and the accompanying materials are made available under the
@@ -27,8 +27,11 @@
2727
import java.util.Date;
2828
import java.util.List;
2929
import java.util.concurrent.ExecutionException;
30+
import java.util.concurrent.atomic.AtomicInteger;
3031
import java.util.stream.Collectors;
3132

33+
import javax.annotation.Priority;
34+
import javax.inject.Singleton;
3235
import javax.ws.rs.DefaultValue;
3336
import javax.ws.rs.GET;
3437
import javax.ws.rs.HeaderParam;
@@ -104,6 +107,15 @@ public void testBadEnumResource() throws ExecutionException, InterruptedExceptio
104107
assertEquals(404, responseContext.getStatus());
105108
}
106109

110+
@Test
111+
public void testCustomEnumResource() throws ExecutionException, InterruptedException {
112+
initiateWebApplication(BadEnumResource.class, EnumParamConverterProvider.class);
113+
final ContainerResponse responseContext = getResponseContext(UriBuilder.fromPath("/")
114+
.queryParam("d", "A").build().toString());
115+
assertEquals(1, counter.get());
116+
assertEquals(200, responseContext.getStatus());
117+
}
118+
107119
public static class URIStringReaderProvider implements ParamConverterProvider {
108120

109121
@Override
@@ -188,6 +200,40 @@ public String toString(final List<String> value) throws IllegalArgumentException
188200
}
189201
}
190202

203+
static final AtomicInteger counter = new AtomicInteger(0);
204+
@Singleton
205+
@Priority(1)
206+
public static class EnumParamConverterProvider implements ParamConverterProvider {
207+
208+
@Override
209+
public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType, Annotation[] annotations) {
210+
if (Enum.class.isAssignableFrom(rawType)) {
211+
return new ParamConverter<T>() {
212+
@Override
213+
public T fromString(final String value) {
214+
counter.addAndGet(1);
215+
if (value == null) {
216+
return null;
217+
}
218+
Class<? extends Enum> enumClass = null;
219+
try {
220+
enumClass = (Class<Enum>) Class.forName(genericType.getTypeName());
221+
} catch (ClassNotFoundException e) {
222+
throw new RuntimeException(e);
223+
}
224+
return (T) Enum.valueOf(enumClass, value.toUpperCase());
225+
}
226+
227+
@Override
228+
public String toString(final T value) {
229+
return String.valueOf(value);
230+
}
231+
};
232+
}
233+
return null;
234+
}
235+
}
236+
191237
@Path("/")
192238
public static class ListOfStringResource {
193239

0 commit comments

Comments
 (0)