From 8a199fc5d822f937b271108f002ac283c57a36d8 Mon Sep 17 00:00:00 2001 From: Jose Date: Fri, 24 Mar 2023 10:30:30 +0100 Subject: [PATCH] Disable validation of JAXBContext by default Since the validation of the JAXBContext is causing lots of troubles to users, we're going to disable it by default. Relates to https://github.com/quarkusio/quarkus/pull/31043#issuecomment-1482430997 --- extensions/jaxb/deployment/pom.xml | 7 +++ .../jaxb/deployment/JaxbProcessor.java | 53 ++++++++++--------- ...flictingModelClassesMarshalerOnlyTest.java | 1 + .../ConflictingModelClassesTest.java | 1 + .../application-enable-validation.properties | 1 + .../io/quarkus/jaxb/runtime/JaxbConfig.java | 2 +- 6 files changed, 40 insertions(+), 25 deletions(-) create mode 100644 extensions/jaxb/deployment/src/test/resources/application-enable-validation.properties diff --git a/extensions/jaxb/deployment/pom.xml b/extensions/jaxb/deployment/pom.xml index fc665ddc3fe65..a496418bac757 100644 --- a/extensions/jaxb/deployment/pom.xml +++ b/extensions/jaxb/deployment/pom.xml @@ -52,6 +52,13 @@ + + org.apache.maven.plugins + maven-surefire-plugin + + -Duser.language=en + + diff --git a/extensions/jaxb/deployment/src/main/java/io/quarkus/jaxb/deployment/JaxbProcessor.java b/extensions/jaxb/deployment/src/main/java/io/quarkus/jaxb/deployment/JaxbProcessor.java index 29255342e9a08..22ce22a0d4643 100644 --- a/extensions/jaxb/deployment/src/main/java/io/quarkus/jaxb/deployment/JaxbProcessor.java +++ b/extensions/jaxb/deployment/src/main/java/io/quarkus/jaxb/deployment/JaxbProcessor.java @@ -316,35 +316,19 @@ FilteredJaxbClassesToBeBoundBuildItem filterBoundClasses( @BuildStep @Record(ExecutionTime.STATIC_INIT) - void validateDefaultJaxbContext( + void bindClassesToJaxbContext( JaxbConfig config, FilteredJaxbClassesToBeBoundBuildItem filteredClassesToBeBound, SynthesisFinishedBuildItem beanContainerState, JaxbContextConfigRecorder jaxbContextConfig /* Force the build time container to invoke this method */) { - if (config.validateJaxbContext) { - final BeanResolver beanResolver = beanContainerState.getBeanResolver(); - final Set beans = beanResolver - .resolveBeans(Type.create(DotName.createSimple(JAXBContext.class), org.jboss.jandex.Type.Kind.CLASS)); - if (!beans.isEmpty()) { - jaxbContextConfig.addClassesToBeBound(filteredClassesToBeBound.getClasses()); - final BeanInfo bean = beanResolver.resolveAmbiguity(beans); - if (bean.isDefaultBean()) { - /* - * Validate the default JAXB context at build time and fail early. - * Do this only if the user application actually requires the default JAXBContext bean - */ - try { - JAXBContext.newInstance(filteredClassesToBeBound.getClasses().toArray(new Class[0])); - } catch (JAXBException e) { - /* - * Producing a ValidationErrorBuildItem would perhaps be more natural here, - * but doing so causes a cycle between this and reactive JAXB extension - * Throwing from here works well too - */ - throw new DeploymentException("Failed to create or validate the default JAXBContext", e); - } - } + final BeanResolver beanResolver = beanContainerState.getBeanResolver(); + final Set beans = beanResolver + .resolveBeans(Type.create(DotName.createSimple(JAXBContext.class), org.jboss.jandex.Type.Kind.CLASS)); + if (!beans.isEmpty()) { + jaxbContextConfig.addClassesToBeBound(filteredClassesToBeBound.getClasses()); + if (config.validateJaxbContext) { + validateJaxbContext(filteredClassesToBeBound, beanResolver, beans); } } } @@ -354,6 +338,27 @@ void registerProduces(BuildProducer additionalBeans) { additionalBeans.produce(new AdditionalBeanBuildItem(JaxbContextProducer.class)); } + private void validateJaxbContext(FilteredJaxbClassesToBeBoundBuildItem filteredClassesToBeBound, BeanResolver beanResolver, + Set beans) { + final BeanInfo bean = beanResolver.resolveAmbiguity(beans); + if (bean.isDefaultBean()) { + /* + * Validate the default JAXB context at build time and fail early. + * Do this only if the user application actually requires the default JAXBContext bean + */ + try { + JAXBContext.newInstance(filteredClassesToBeBound.getClasses().toArray(new Class[0])); + } catch (JAXBException e) { + /* + * Producing a ValidationErrorBuildItem would perhaps be more natural here, + * but doing so causes a cycle between this and reactive JAXB extension + * Throwing from here works well too + */ + throw new DeploymentException("Failed to create or validate the default JAXBContext", e); + } + } + } + private void handleJaxbFile(Path p, BuildProducer resource, BuildProducer reflectiveClass, List classesToBeBound) { diff --git a/extensions/jaxb/deployment/src/test/java/io/quarkus/jaxb/deployment/ConflictingModelClassesMarshalerOnlyTest.java b/extensions/jaxb/deployment/src/test/java/io/quarkus/jaxb/deployment/ConflictingModelClassesMarshalerOnlyTest.java index 14badfff6285f..8a04c217d2a37 100644 --- a/extensions/jaxb/deployment/src/test/java/io/quarkus/jaxb/deployment/ConflictingModelClassesMarshalerOnlyTest.java +++ b/extensions/jaxb/deployment/src/test/java/io/quarkus/jaxb/deployment/ConflictingModelClassesMarshalerOnlyTest.java @@ -26,6 +26,7 @@ public class ConflictingModelClassesMarshalerOnlyTest { @RegisterExtension static final QuarkusUnitTest config = new QuarkusUnitTest() + .withConfigurationResource("application-enable-validation.properties") .withApplicationRoot((jar) -> jar .addClasses( io.quarkus.jaxb.deployment.one.Model.class, diff --git a/extensions/jaxb/deployment/src/test/java/io/quarkus/jaxb/deployment/ConflictingModelClassesTest.java b/extensions/jaxb/deployment/src/test/java/io/quarkus/jaxb/deployment/ConflictingModelClassesTest.java index bcded32283401..1c7274481b75a 100644 --- a/extensions/jaxb/deployment/src/test/java/io/quarkus/jaxb/deployment/ConflictingModelClassesTest.java +++ b/extensions/jaxb/deployment/src/test/java/io/quarkus/jaxb/deployment/ConflictingModelClassesTest.java @@ -25,6 +25,7 @@ public class ConflictingModelClassesTest { @RegisterExtension static final QuarkusUnitTest config = new QuarkusUnitTest() + .withConfigurationResource("application-enable-validation.properties") .withApplicationRoot((jar) -> jar .addClasses( io.quarkus.jaxb.deployment.one.Model.class, diff --git a/extensions/jaxb/deployment/src/test/resources/application-enable-validation.properties b/extensions/jaxb/deployment/src/test/resources/application-enable-validation.properties new file mode 100644 index 0000000000000..ab8094de90586 --- /dev/null +++ b/extensions/jaxb/deployment/src/test/resources/application-enable-validation.properties @@ -0,0 +1 @@ +quarkus.jaxb.validate-jaxb-context=true \ No newline at end of file diff --git a/extensions/jaxb/runtime/src/main/java/io/quarkus/jaxb/runtime/JaxbConfig.java b/extensions/jaxb/runtime/src/main/java/io/quarkus/jaxb/runtime/JaxbConfig.java index bd2a0114e49e6..10aee70162135 100644 --- a/extensions/jaxb/runtime/src/main/java/io/quarkus/jaxb/runtime/JaxbConfig.java +++ b/extensions/jaxb/runtime/src/main/java/io/quarkus/jaxb/runtime/JaxbConfig.java @@ -13,7 +13,7 @@ public class JaxbConfig { /** * If enabled, it will validate the default JAXB context at build time. */ - @ConfigItem(defaultValue = "true") + @ConfigItem(defaultValue = "false") public boolean validateJaxbContext; /**