Skip to content
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

Disable validation of JAXBContext by default #32105

Merged
merged 1 commit into from
Mar 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions extensions/jaxb/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>-Duser.language=en</argLine>
</configuration>
</plugin>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is necessary because some tests rely on English wording.

</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<BeanInfo> 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<BeanInfo> 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) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes fix the issue where the classes were not bound if the validation of the jaxb context was disabled.

validateJaxbContext(filteredClassesToBeBound, beanResolver, beans);
}
}
}
Expand All @@ -354,6 +338,27 @@ void registerProduces(BuildProducer<AdditionalBeanBuildItem> additionalBeans) {
additionalBeans.produce(new AdditionalBeanBuildItem(JaxbContextProducer.class));
}

private void validateJaxbContext(FilteredJaxbClassesToBeBoundBuildItem filteredClassesToBeBound, BeanResolver beanResolver,
Set<BeanInfo> 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<NativeImageResourceBuildItem> resource,
BuildProducer<ReflectiveClassBuildItem> reflectiveClass,
List<String> classesToBeBound) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
quarkus.jaxb.validate-jaxb-context=true
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down