-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Compatibility issue with spring boot 3 / jakarta api #39
Comments
Reproducer: package eu.fraho.example;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.validation.ConstraintViolation;
import jakarta.validation.Validation;
import jakarta.validation.Validator;
import jakarta.validation.ValidatorFactory;
import jakarta.validation.constraints.Size;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.openapitools.jackson.nullable.JsonNullable;
import org.openapitools.jackson.nullable.JsonNullableModule;
import java.util.Set;
public class TestJsonNullable {
static class Pet {
@Size(max = 10)
public JsonNullable<String> name = JsonNullable.undefined();
public Pet name(JsonNullable<String> name) {
this.name = name;
return this;
}
}
@Test
void test() throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
mapper.registerModule(new JsonNullableModule());
Assertions.assertEquals("{}", mapper.writeValueAsString(new Pet().name(JsonNullable.<String>undefined())));
Assertions.assertEquals("{\"name\":null}", mapper.writeValueAsString(new Pet().name(JsonNullable.<String>of(null))));
Assertions.assertEquals("{\"name\":\"Rex\"}", mapper.writeValueAsString(new Pet().name(JsonNullable.of("Rex"))));
Assertions.assertEquals(JsonNullable.of("Rex"), mapper.readValue("{\"name\":\"Rex\"}", Pet.class).name);
Assertions.assertEquals(JsonNullable.<String>of(null), mapper.readValue("{\"name\":null}", Pet.class).name);
Assertions.assertEquals(JsonNullable.<String>undefined(), mapper.readValue("{}", Pet.class).name);
// instantiate javax.validation.Validator
try (ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory()) {
Validator validator = validatorFactory.getValidator();
Pet myPet = new Pet().name(JsonNullable.of("My Pet's really long name"));
Set<ConstraintViolation<Pet>> validationResult = validator.validate(myPet); // <-----
Assertions.assertEquals(1, validationResult.size());
}
}
}
When using spring boot 2.7.6 (and javax imports):
|
as a quick fix you could:
package com.some.package;
import jakarta.validation.valueextraction.ExtractedValue;
import jakarta.validation.valueextraction.UnwrapByDefault;
import jakarta.validation.valueextraction.ValueExtractor;
import org.openapitools.jackson.nullable.JsonNullable;
@UnwrapByDefault
public class JsonNullableValueExtractor implements ValueExtractor<JsonNullable<@ExtractedValue ?>> {
@Override
public void extractValues(JsonNullable<?> originalValue, ValueReceiver receiver) {
if (originalValue.isPresent()) {
receiver.value(null, originalValue.get());
}
}
}
|
Thanks for the workaround, works like a charm. |
Closed
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
thanks for this great plugin! AS spring boot 3.0 was released yesterday, I tried to upgrade my project to that new version. This upgrade also includes the change from javax -> jakarta api, which I think breaks this library.
Version 0.2.4 does not work with the jakarta-api:
Changing that configuration like proposed by #18 (comment) results in another exception:
Can you please make it work with the jakarta-validation annotations as it did with the java-validation annotations?
Thanks,
Simon
The text was updated successfully, but these errors were encountered: