Repository: smallrye/smallrye-graphql
Issue: Maven plugin does not work if implementation-cdi module is in the classpath

Because in that case, it picks up the `io.smallrye.graphql.cdi.CdiLookupService` and tries to use that for injection validation (the `Bootstrap` class validates that all operation classes are beans), but it does not work because the CDI container is not properly initialized. We need to skip injection validation in the Maven plugin.

Workaround: set `test.skip.injection.validation=true` as a system property when executing the plugin

Changed files:
tools/maven-plugin/src/main/java/io/smallrye/graphql/mavenplugin/GenerateSchemaMojo.java
tools/gradle-plugin/plugin/src/main/java/io/smallrye/graphql/gradle/tasks/GenerateSchemaTask.java
server/implementation/src/test/java/io/smallrye/graphql/schema/SchemaTest.java
server/implementation/src/main/java/io/smallrye/graphql/bootstrap/Bootstrap.java
server/implementation-cdi/src/test/java/io/smallrye/graphql/execution/SchemaTest.java

diff --git a/server/implementation-cdi/src/test/java/io/smallrye/graphql/execution/SchemaTest.java b/server/implementation-cdi/src/test/java/io/smallrye/graphql/execution/SchemaTest.java
index 24a4b48c..8801722d 100644
--- a/server/implementation-cdi/src/test/java/io/smallrye/graphql/execution/SchemaTest.java
+++ b/server/implementation-cdi/src/test/java/io/smallrye/graphql/execution/SchemaTest.java
@@ -9,7 +9,6 @@ import java.util.Scanner;
 
 import org.jboss.jandex.IndexView;
 import org.jboss.logging.Logger;
-import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 
@@ -30,22 +29,14 @@ public class SchemaTest {
 
     @BeforeEach
     public void init() {
-        // in a unit test we don't have injection available, this is a hack needed to tell the Bootstrap class
-        // that it should not verify injection availability
-        System.setProperty("test.skip.injection.validation", "true");
         IndexView index = Indexer.getTCKIndex();
         this.schema = SchemaBuilder.build(index);
         assertNotNull(schema);
     }
 
-    @AfterEach
-    public void skipInjectionValidationCleanup() {
-        System.clearProperty("test.skip.injection.validation");
-    }
-
     @Test
     public void testSchemaModelCreation() {
-        GraphQLSchema graphQLSchema = Bootstrap.bootstrap(schema);
+        GraphQLSchema graphQLSchema = Bootstrap.bootstrap(schema, false, true);
         assertNotNull(graphQLSchema);
         String schemaString = new SchemaPrinter().print(graphQLSchema);
         assertNotNull(schemaString);
diff --git a/server/implementation/src/main/java/io/smallrye/graphql/bootstrap/Bootstrap.java b/server/implementation/src/main/java/io/smallrye/graphql/bootstrap/Bootstrap.java
index b73fe0b9..565e8e6f 100644
--- a/server/implementation/src/main/java/io/smallrye/graphql/bootstrap/Bootstrap.java
+++ b/server/implementation/src/main/java/io/smallrye/graphql/bootstrap/Bootstrap.java
@@ -99,12 +99,16 @@ public class Bootstrap {
     private final ClassloadingService classloadingService = ClassloadingService.get();
 
     public static GraphQLSchema bootstrap(Schema schema) {
-        return bootstrap(schema, false);
+        return bootstrap(schema, false, false);
     }
 
     public static GraphQLSchema bootstrap(Schema schema, boolean allowMultipleDeployments) {
+        return bootstrap(schema, allowMultipleDeployments, false);
+    }
+
+    public static GraphQLSchema bootstrap(Schema schema, boolean allowMultipleDeployments, boolean skipInjectionValidation) {
         if (schema != null && (schema.hasOperations())) {
-            Bootstrap bootstrap = new Bootstrap(schema, allowMultipleDeployments);
+            Bootstrap bootstrap = new Bootstrap(schema, allowMultipleDeployments, skipInjectionValidation);
             bootstrap.generateGraphQLSchema();
             return bootstrap.graphQLSchema;
         } else {
@@ -113,10 +117,12 @@ public class Bootstrap {
         }
     }
 
-    private Bootstrap(Schema schema, boolean allowMultipleDeployments) {
+    private Bootstrap(Schema schema, boolean allowMultipleDeployments, boolean skipInjectionValidation) {
         this.schema = schema;
         SmallRyeContext.setSchema(schema, allowMultipleDeployments);
-        if (!Boolean.getBoolean("test.skip.injection.validation")) {
+        // setting `skipInjectionValidation` through a system property is not recommended,
+        // but kept for backward compatibility for now
+        if (!Boolean.getBoolean("test.skip.injection.validation") && !skipInjectionValidation) {
             verifyInjectionIsAvailable();
         }
     }
diff --git a/server/implementation/src/test/java/io/smallrye/graphql/schema/SchemaTest.java b/server/implementation/src/test/java/io/smallrye/graphql/schema/SchemaTest.java
index 0514d320..40308cfd 100644
--- a/server/implementation/src/test/java/io/smallrye/graphql/schema/SchemaTest.java
+++ b/server/implementation/src/test/java/io/smallrye/graphql/schema/SchemaTest.java
@@ -13,8 +13,6 @@ import java.util.stream.Stream;
 import org.jboss.jandex.IndexView;
 import org.jboss.jandex.Indexer;
 import org.jboss.logging.Logger;
-import org.junit.jupiter.api.AfterEach;
-import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 
 import graphql.schema.GraphQLArgument;
@@ -30,25 +28,13 @@ import io.smallrye.graphql.schema.model.Schema;
 class SchemaTest {
     private static final Logger LOG = Logger.getLogger(SchemaTest.class.getName());
 
-    @BeforeEach
-    public void skipInjectionValidation() {
-        // in a unit test we don't have injection available, this is a hack needed to tell the Bootstrap class
-        // that it should not verify injection availability
-        System.setProperty("test.skip.injection.validation", "true");
-    }
-
-    @AfterEach
-    public void skipInjectionValidationCleanup() {
-        System.clearProperty("test.skip.injection.validation");
-    }
-
     @Test
     void testSchemaWithDirectives() {
         Schema schema = SchemaBuilder
                 .build(scan(Directive.class, IntArrayTestDirective.class, FieldDirective.class,
                         TestTypeWithDirectives.class, DirectivesTestApi.class));
         assertNotNull(schema);
-        GraphQLSchema graphQLSchema = Bootstrap.bootstrap(schema);
+        GraphQLSchema graphQLSchema = Bootstrap.bootstrap(schema, false, true);
         assertNotNull(graphQLSchema);
 
         GraphQLDirective typeDirective = graphQLSchema.getDirective("intArrayTestDirective");
diff --git a/tools/gradle-plugin/plugin/src/main/java/io/smallrye/graphql/gradle/tasks/GenerateSchemaTask.java b/tools/gradle-plugin/plugin/src/main/java/io/smallrye/graphql/gradle/tasks/GenerateSchemaTask.java
index 3aac2b7a..26db9898 100644
--- a/tools/gradle-plugin/plugin/src/main/java/io/smallrye/graphql/gradle/tasks/GenerateSchemaTask.java
+++ b/tools/gradle-plugin/plugin/src/main/java/io/smallrye/graphql/gradle/tasks/GenerateSchemaTask.java
@@ -179,7 +179,6 @@ public class GenerateSchemaTask extends DefaultTask {
     @TaskAction
     public void generateSchema() throws Exception {
         this.config = new GradleConfig(includeScalars, includeDirectives, includeSchemaDefinition, includeIntrospectionTypes);
-        System.setProperty("test.skip.injection.validation", "true");
         ClassLoader classLoader = getClassLoader();
         Thread.currentThread().setContextClassLoader(classLoader);
         IndexView index = createIndex();
@@ -242,7 +241,7 @@ public class GenerateSchemaTask extends DefaultTask {
 
     private String generateSchema(IndexView index) {
         Schema internalSchema = SchemaBuilder.build(index);
-        GraphQLSchema graphQLSchema = Bootstrap.bootstrap(internalSchema);
+        GraphQLSchema graphQLSchema = Bootstrap.bootstrap(internalSchema, false, true);
         if(graphQLSchema!=null){
             return new SchemaPrinter().print(graphQLSchema);
         }
diff --git a/tools/maven-plugin/src/main/java/io/smallrye/graphql/mavenplugin/GenerateSchemaMojo.java b/tools/maven-plugin/src/main/java/io/smallrye/graphql/mavenplugin/GenerateSchemaMojo.java
index 1337a2bf..f838a851 100644
--- a/tools/maven-plugin/src/main/java/io/smallrye/graphql/mavenplugin/GenerateSchemaMojo.java
+++ b/tools/maven-plugin/src/main/java/io/smallrye/graphql/mavenplugin/GenerateSchemaMojo.java
@@ -172,7 +172,7 @@ public class GenerateSchemaMojo extends AbstractMojo {
 
     private String generateSchema(IndexView index) {
         Schema internalSchema = SchemaBuilder.build(index, mavenConfig.typeAutoNameStrategy);
-        GraphQLSchema graphQLSchema = Bootstrap.bootstrap(internalSchema);
+        GraphQLSchema graphQLSchema = Bootstrap.bootstrap(internalSchema, false, true);
         if (graphQLSchema != null) {
             return new SchemaPrinter().print(graphQLSchema);
         }