Skip to content

Commit 99bec4c

Browse files
committed
Support for ValidatorContext#{clockProvider,addValueExtractor}
Signed-off-by: jansupol <jan.supol@oracle.com>
1 parent cb78c5d commit 99bec4c

File tree

4 files changed

+279
-4
lines changed

4 files changed

+279
-4
lines changed

ext/bean-validation/src/main/java/org/glassfish/jersey/server/validation/ValidationConfig.java

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2020 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2025 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -16,10 +16,15 @@
1616

1717
package org.glassfish.jersey.server.validation;
1818

19+
import jakarta.validation.ClockProvider;
1920
import jakarta.validation.ConstraintValidatorFactory;
2021
import jakarta.validation.MessageInterpolator;
2122
import jakarta.validation.ParameterNameProvider;
2223
import jakarta.validation.TraversableResolver;
24+
import jakarta.validation.valueextraction.ValueExtractor;
25+
26+
import java.util.ArrayList;
27+
import java.util.List;
2328

2429
/**
2530
* Configuration class for Bean Validation provider.
@@ -32,6 +37,8 @@ public final class ValidationConfig {
3237
private TraversableResolver traversableResolver;
3338
private ConstraintValidatorFactory constraintValidatorFactory;
3439
private ParameterNameProvider parameterNameProvider;
40+
private List<ValueExtractor<?>> valueExtractors;
41+
private ClockProvider clockProvider;
3542

3643
/**
3744
* Return {@code MessageInterpolator} implementation used for configuration.
@@ -69,6 +76,24 @@ public ParameterNameProvider getParameterNameProvider() {
6976
return parameterNameProvider;
7077
}
7178

79+
/**
80+
* Return {@code ClockProvider} implementation used for configuration.
81+
*
82+
* @return instance of {@code ClockProvider} or {@code null} if not defined.
83+
*/
84+
public ClockProvider getClockProvider() {
85+
return clockProvider;
86+
}
87+
88+
/**
89+
* Return {@code ValueExtractor} implementations used for configuration.
90+
*
91+
* @return instances of {@code ValueExtractor} or {@code null} if not defined.
92+
*/
93+
public List<ValueExtractor<?>> getValueExtractors() {
94+
return valueExtractors;
95+
}
96+
7297
/**
7398
* Defines the message interpolator.
7499
* If {@code null} is passed, the default message interpolator is used.
@@ -112,4 +137,30 @@ public ValidationConfig parameterNameProvider(final ParameterNameProvider parame
112137
this.parameterNameProvider = parameterNameProvider;
113138
return this;
114139
}
140+
141+
/**
142+
* Defines the clock provider.
143+
* If {@code null} is passed, the default clock provider is used.
144+
*
145+
* @param clockProvider clock provider implementation.
146+
*/
147+
public ValidationConfig setClockProvider(ClockProvider clockProvider) {
148+
this.clockProvider = clockProvider;
149+
return this;
150+
}
151+
152+
/**
153+
* Defines the value extractor.
154+
* If {@code null} is passed, the default value extractor is used.
155+
*
156+
* @param valueExtractors value extractor implementations.
157+
*/
158+
public ValidationConfig addValueExtractor(ValueExtractor<?> valueExtractors) {
159+
if (this.valueExtractors == null) {
160+
this.valueExtractors = new ArrayList<>();
161+
}
162+
this.valueExtractors.add(valueExtractors);
163+
return this;
164+
}
165+
115166
}

ext/bean-validation/src/main/java/org/glassfish/jersey/server/validation/internal/ValidationBinder.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2020 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, 2019 Payara Foundation and/or its affiliates. All rights reserved.
44
*
55
* This program and the accompanying materials are made available under the
@@ -25,6 +25,7 @@
2525
import java.util.logging.Level;
2626
import java.util.logging.Logger;
2727

28+
import jakarta.validation.valueextraction.ValueExtractor;
2829
import jakarta.ws.rs.container.ResourceContext;
2930
import jakarta.ws.rs.core.Context;
3031
import jakarta.ws.rs.core.MediaType;
@@ -213,6 +214,18 @@ public ConfiguredValidator get() {
213214
if (config.getParameterNameProvider() != null) {
214215
context.parameterNameProvider(config.getParameterNameProvider());
215216
}
217+
218+
// ValueExtractor
219+
if (config.getValueExtractors() != null) {
220+
for (ValueExtractor<?> valueExtractor : config.getValueExtractors()) {
221+
context.addValueExtractor(valueExtractor);
222+
}
223+
}
224+
225+
// ClockProvider
226+
if (config.getClockProvider() != null) {
227+
context.clockProvider(config.getClockProvider());
228+
}
216229
}
217230

218231
validatorCache.put(contextResolver,
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (c) 2025 Oracle and/or its affiliates. All rights reserved.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License v. 2.0, which is available at
6+
* http://www.eclipse.org/legal/epl-2.0.
7+
*
8+
* This Source Code may also be made available under the following Secondary
9+
* Licenses when the conditions for such availability set forth in the
10+
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11+
* version 2 with the GNU Classpath Exception, which is available at
12+
* https://www.gnu.org/software/classpath/license.html.
13+
*
14+
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15+
*/
16+
17+
package org.glassfish.jersey.server.validation;
18+
19+
import jakarta.validation.ValidatorContext;
20+
import org.junit.jupiter.api.Assertions;
21+
import org.junit.jupiter.api.Test;
22+
23+
import java.lang.reflect.Method;
24+
25+
public class ConfigurationCoverageTest {
26+
@Test
27+
public void testAllConfigurationPossibilitiesAreCovered() {
28+
String[] methodNames = {"messageInterpolator", "traversableResolver",
29+
"constraintValidatorFactory", "parameterNameProvider", "addValueExtractor", "clockProvider",
30+
// exclude method
31+
"getValidator"};
32+
Method[] methods = ValidatorContext.class.getDeclaredMethods();
33+
boolean passed = true;
34+
methodLoop:
35+
for (Method method : methods) {
36+
for (String methodName : methodNames) {
37+
if (methodName.equals(method.getName())) {
38+
continue methodLoop;
39+
}
40+
}
41+
passed = false;
42+
System.err.append(ValidationConfig.class.getName())
43+
.append(" contains no getter for method: ")
44+
.println(method.getName());
45+
}
46+
Assertions.assertTrue(passed);
47+
}
48+
}

0 commit comments

Comments
 (0)