diff --git a/CHANGELOG.md b/CHANGELOG.md index 4acd9a70..f59fffa6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 #### Fixed - avoid exception in subtype resolution, when targeting void method +### `jsonschema-maven-plugin` +### Added +- support `` flag to exclude abstract types (not interfaces) +- support `` flag to exclude interface types + ## [4.36.0] - 2024-07-20 ### `jsonschema-generator` #### Added diff --git a/jsonschema-maven-plugin/README.md b/jsonschema-maven-plugin/README.md index 5cb20d66..8f7f9927 100644 --- a/jsonschema-maven-plugin/README.md +++ b/jsonschema-maven-plugin/README.md @@ -61,6 +61,16 @@ The content of each of these elements can be either: ``` +Additionally, you can omit the generation for abstract classes and/or interfaces by setting the respective `` or `` +flags to `true` (by default, they are `false`). +```xml + + com/myOrg/myApp/package/** + true + true + +``` + #### Based on Annotations (``) Alternatively classes can be selected based on annotations using the `` element. Then diff --git a/jsonschema-maven-plugin/src/main/java/com/github/victools/jsonschema/plugin/maven/SchemaGeneratorMojo.java b/jsonschema-maven-plugin/src/main/java/com/github/victools/jsonschema/plugin/maven/SchemaGeneratorMojo.java index 64a05f7e..272bfa65 100644 --- a/jsonschema-maven-plugin/src/main/java/com/github/victools/jsonschema/plugin/maven/SchemaGeneratorMojo.java +++ b/jsonschema-maven-plugin/src/main/java/com/github/victools/jsonschema/plugin/maven/SchemaGeneratorMojo.java @@ -45,6 +45,7 @@ import java.io.PrintWriter; import java.lang.reflect.Array; import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Modifier; import java.net.URL; import java.net.URLClassLoader; import java.nio.charset.StandardCharsets; @@ -101,6 +102,22 @@ public class SchemaGeneratorMojo extends AbstractMojo { @Parameter(property = "annotations") private List annotations = new ArrayList<>(); + /** + * Flag indicating whether abstract classes should be ignored, even if they are matching the classname and/or package pattern. + * + * @since 4.37.0 + */ + @Parameter(property = "skipAbstractTypes", defaultValue = "false") + private boolean skipAbstractTypes; + + /** + * Flag indicating whether interfaces should be ignored, even if they are matching the classname and/or package pattern. + * + * @since 4.37.0 + */ + @Parameter(property = "skipInterfaces", defaultValue = "false") + private boolean skipInterfaces; + /** * The classpath to look for classes to generate schema files. */ @@ -212,9 +229,7 @@ private void generateSchema(String classOrPackageName, boolean targetPackage) th if (potentialTarget.isAlreadyGenerated()) { this.getLog().info("- Skipping already generated " + potentialTarget.getFullClassName()); } else { - // Load the class for which the schema will be generated - Class schemaClass = this.loadClass(potentialTarget.getFullClassName()); - this.generateSchema(schemaClass); + this.generateSchema(potentialTarget); potentialTarget.setAlreadyGenerated(); } } @@ -223,6 +238,25 @@ private void generateSchema(String classOrPackageName, boolean targetPackage) th } } + /** + * Generate the JSON schema for the indicated type matching a class name or package pattern. Considering further config flags potentially skipping + * the schema file generation. + * + * @param potentialTarget class to produce JSON schema file for + * @throws MojoExecutionException In case of problems + */ + private void generateSchema(PotentialSchemaClass potentialTarget) throws MojoExecutionException { + // Load the class for which the schema will be generated + Class schemaClass = this.loadClass(potentialTarget.getFullClassName()); + if (this.skipInterfaces && schemaClass.isInterface()) { + this.getLog().info("- Skipping interface " + potentialTarget.getFullClassName()); + } else if (this.skipAbstractTypes && this.isAbstractClass(schemaClass)) { + this.getLog().info("- Skipping abstract type " + potentialTarget.getFullClassName()); + } else { + this.generateSchema(schemaClass); + } + } + /** * Generate the JSON schema for the given className. * @@ -565,4 +599,14 @@ private void writeToFile(JsonNode jsonSchema, File file) throws MojoExecutionExc throw new MojoExecutionException("Error: Can not write to file " + file, e); } } + + /** + * Check whether a given class is deemed abstract but not an interface. + * + * @param targetClass type to check + * @return whether the indicated type represents an abstract non-interface class + */ + private boolean isAbstractClass(Class targetClass) { + return Modifier.isAbstract(targetClass.getModifiers()) && !targetClass.isInterface(); + } } diff --git a/jsonschema-maven-plugin/src/test/java/com/github/victools/jsonschema/plugin/maven/SchemaGeneratorMojoTest.java b/jsonschema-maven-plugin/src/test/java/com/github/victools/jsonschema/plugin/maven/SchemaGeneratorMojoTest.java index 93900410..0f5ab366 100644 --- a/jsonschema-maven-plugin/src/test/java/com/github/victools/jsonschema/plugin/maven/SchemaGeneratorMojoTest.java +++ b/jsonschema-maven-plugin/src/test/java/com/github/victools/jsonschema/plugin/maven/SchemaGeneratorMojoTest.java @@ -158,13 +158,14 @@ public void testTwoClasses() throws Exception { /** * Unit test to test the generation of schemas for multiple classes */ - @Test - public void testPackageName() throws Exception { + @ParameterizedTest + @ValueSource(strings = { "WithoutAbstracts", "WithoutInterfaces" }) + public void testPackageName(String scenario) throws Exception { File testCaseLocation = new File("src/test/resources/reference-test-cases"); - File generationLocation = new File("target/generated-test-sources/PackageName"); + File generationLocation = new File("target/generated-test-sources/PackageName" + scenario); // Execute the pom - executePom(new File("src/test/resources/reference-test-cases/PackageName-pom.xml")); + executePom(new File("src/test/resources/reference-test-cases/PackageName" + scenario + "-pom.xml")); // Validate that the schema files are created. File resultFileA = new File(generationLocation,"TestClassA-schema.json"); @@ -179,6 +180,11 @@ public void testPackageName() throws Exception { Assertions.assertTrue(resultFileC.exists()); resultFileC.deleteOnExit(); + File resultFileAbstract = new File(generationLocation,"AbstractTestClass-schema.json"); + Assertions.assertNotEquals("WithoutAbstracts".equals(scenario), resultFileAbstract.exists()); + File resultFileInterface = new File(generationLocation,"TestInterface-schema.json"); + Assertions.assertNotEquals("WithoutInterfaces".equals(scenario), resultFileInterface.exists()); + // Validate that they are the same as the reference File referenceFileA = new File(testCaseLocation, "TestClassA-reference.json"); Assertions.assertTrue(referenceFileA.exists()); diff --git a/jsonschema-maven-plugin/src/test/java/com/github/victools/jsonschema/plugin/maven/testpackage/AbstractTestClass.java b/jsonschema-maven-plugin/src/test/java/com/github/victools/jsonschema/plugin/maven/testpackage/AbstractTestClass.java new file mode 100644 index 00000000..7f7b490d --- /dev/null +++ b/jsonschema-maven-plugin/src/test/java/com/github/victools/jsonschema/plugin/maven/testpackage/AbstractTestClass.java @@ -0,0 +1,22 @@ +/* + * Copyright 2024 VicTools. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.github.victools.jsonschema.plugin.maven.testpackage; + +public abstract class AbstractTestClass { + + public abstract int getInteger(); +} diff --git a/jsonschema-maven-plugin/src/test/java/com/github/victools/jsonschema/plugin/maven/testpackage/TestInterface.java b/jsonschema-maven-plugin/src/test/java/com/github/victools/jsonschema/plugin/maven/testpackage/TestInterface.java new file mode 100644 index 00000000..0ae4b706 --- /dev/null +++ b/jsonschema-maven-plugin/src/test/java/com/github/victools/jsonschema/plugin/maven/testpackage/TestInterface.java @@ -0,0 +1,22 @@ +/* + * Copyright 2024 VicTools. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.github.victools.jsonschema.plugin.maven.testpackage; + +public interface TestInterface { + + String getValue(); +} diff --git a/jsonschema-maven-plugin/src/test/resources/reference-test-cases/PackageNameWithoutAbstracts-pom.xml b/jsonschema-maven-plugin/src/test/resources/reference-test-cases/PackageNameWithoutAbstracts-pom.xml new file mode 100644 index 00000000..b1164995 --- /dev/null +++ b/jsonschema-maven-plugin/src/test/resources/reference-test-cases/PackageNameWithoutAbstracts-pom.xml @@ -0,0 +1,16 @@ + + + + + com.github.victools + jsonschema-maven-plugin + + com.github.victools.jsonschema.plugin.maven.testpackage + target/generated-test-sources/PackageNameWithoutAbstracts + com.github.victools.jsonschema.plugin.maven.testpackage.TestClassB + true + + + + + \ No newline at end of file diff --git a/jsonschema-maven-plugin/src/test/resources/reference-test-cases/PackageName-pom.xml b/jsonschema-maven-plugin/src/test/resources/reference-test-cases/PackageNameWithoutInterfaces-pom.xml similarity index 85% rename from jsonschema-maven-plugin/src/test/resources/reference-test-cases/PackageName-pom.xml rename to jsonschema-maven-plugin/src/test/resources/reference-test-cases/PackageNameWithoutInterfaces-pom.xml index 1417f84d..11f7b80a 100644 --- a/jsonschema-maven-plugin/src/test/resources/reference-test-cases/PackageName-pom.xml +++ b/jsonschema-maven-plugin/src/test/resources/reference-test-cases/PackageNameWithoutInterfaces-pom.xml @@ -6,8 +6,9 @@ jsonschema-maven-plugin com.github.victools.jsonschema.plugin.maven.testpackage - target/generated-test-sources/PackageName + target/generated-test-sources/PackageNameWithoutInterfaces com.github.victools.jsonschema.plugin.maven.testpackage.TestClassB + true diff --git a/slate-docs/source/includes/_maven-plugin.md b/slate-docs/source/includes/_maven-plugin.md index 3378f3d0..0a7acb51 100644 --- a/slate-docs/source/includes/_maven-plugin.md +++ b/slate-docs/source/includes/_maven-plugin.md @@ -21,6 +21,8 @@ There are a number of basic configuration options as well as the possibility to com.myOrg.MySchemaAnnotation + true + true PROJECT_ONLY false @@ -83,6 +85,15 @@ For example, the given configuration will create a `MyClass.schema` file. To store the generated schema files in the same directory structure as the originating classes, the following can be used `{1}/{0}-schema.json`. The default `` is `{0}-schema.json`. +Additionally, you can omit the generation for abstract classes and/or interfaces by setting the respective `` or `` flags to `true` (by default, they are `false`). +```xml + + com/myOrg/myApp/package/** + true + true + +``` + ### Selecting Options ```xml