Skip to content

Commit

Permalink
Add support for jakarta-validation (#42)
Browse files Browse the repository at this point in the history
* remove dependency on el-interpreter for tests

* add support for jakarta-validation

* add support for jakarta-validation

* make ValidatorFactory local again

* remove some unnecessary boxing of primitive integers
  • Loading branch information
bratkartoffel authored Dec 10, 2022
1 parent 4485fd1 commit f70a463
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 25 deletions.
12 changes: 6 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,19 @@
<version>2.0.1.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
<version>3.0.2</version>
<scope>provided</scope>
</dependency>
<!-- test dependencies -->
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.2.5.Final</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.el</artifactId>
<version>3.0.1-b09</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.openapitools.jackson.nullable;

import jakarta.validation.valueextraction.ExtractedValue;
import jakarta.validation.valueextraction.UnwrapByDefault;
import jakarta.validation.valueextraction.ValueExtractor;

/**
* Extractor for JsonNullable (modern jakarta-validation version)
*/
@UnwrapByDefault
public class JsonNullableJakartaValueExtractor implements ValueExtractor<JsonNullable<@ExtractedValue ?>> {
@Override
public void extractValues(JsonNullable<?> originalValue, ValueReceiver receiver) {
JsonNullableValueExtractorHelper.extractValues(originalValue, receiver::indexedValue, receiver::value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,14 @@
import javax.validation.valueextraction.ExtractedValue;
import javax.validation.valueextraction.UnwrapByDefault;
import javax.validation.valueextraction.ValueExtractor;
import java.util.Collection;

/**
* Extractor for JsonNullable
* Extractor for JsonNullable (classic javax-validation version)
*/
@UnwrapByDefault
public class JsonNullableValueExtractor implements ValueExtractor<JsonNullable<@ExtractedValue ?>> {
@Override
public void extractValues(JsonNullable<?> originalValue, ValueReceiver receiver) {
if (originalValue.isPresent()) {
Object unwrapped = originalValue.get();
if (unwrapped instanceof Collection<?>) {
Collection<?> unwrappedList = (Collection<?>) unwrapped;
Object[] objects = unwrappedList.toArray();
for (int i = 0; i < objects.length; i++) {
receiver.indexedValue("<list element>", i, objects[i]);
}
} else {
receiver.value(null, originalValue.get());
}
}
JsonNullableValueExtractorHelper.extractValues(originalValue, receiver::indexedValue, receiver::value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.openapitools.jackson.nullable;

import java.util.Collection;

abstract class JsonNullableValueExtractorHelper {
public static void extractValues(JsonNullable<?> originalValue, IndexedValueSetter indexedValueSetter, ValueSetter valueSetter) {
if (originalValue.isPresent()) {
Object unwrapped = originalValue.get();
if (unwrapped instanceof Collection<?>) {
Collection<?> unwrappedList = (Collection<?>) unwrapped;
Object[] objects = unwrappedList.toArray();
for (int i = 0; i < objects.length; i++) {
indexedValueSetter.apply("<list element>", i, objects[i]);
}
} else {
valueSetter.apply(null, originalValue.get());
}
}
}

@FunctionalInterface
interface IndexedValueSetter {
void apply(String var1, int var2, Object var3);
}

@FunctionalInterface
interface ValueSetter {
void apply(String var1, Object var2);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.openapitools.jackson.nullable.JsonNullableJakartaValueExtractor
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.openapitools.jackson.nullable;

import org.hibernate.validator.messageinterpolation.ParameterMessageInterpolator;
import org.junit.Before;
import org.junit.Test;

Expand All @@ -17,22 +18,25 @@


public class JsonNullableValueExtractorTest {

private Validator validator;

@Before
public void setUp() {
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
validator = factory.getValidator();
try (ValidatorFactory factory = Validation.byDefaultProvider()
.configure()
.messageInterpolator(new ParameterMessageInterpolator())
.buildValidatorFactory()) {
validator = factory.getValidator();
}
}

@Test
public void testUnwrap() {
final UnitIssue2 unitIssue2 = new UnitIssue2();
unitIssue2.setRestrictedString("a >15 character long string");
unitIssue2.setNullableRestrictedString("a >15 character long string");
unitIssue2.setRestrictedInt(Integer.valueOf(16));
unitIssue2.setNullableRestrictedInt(Integer.valueOf(16));
unitIssue2.setRestrictedInt(16);
unitIssue2.setNullableRestrictedInt(16);

final Set<ConstraintViolation<UnitIssue2>> validate = validator.validate(unitIssue2);

Expand Down

0 comments on commit f70a463

Please sign in to comment.