From 963ed9fd04e02f4c7dbf1d8df3d3a38ab296b8c7 Mon Sep 17 00:00:00 2001 From: christian Date: Thu, 23 Oct 2025 01:09:18 +0200 Subject: [PATCH] Allow Custom Modules to be configures with options --- .../jsonschema/generator/CustomModule.java | 23 +++++++++++ jsonschema-maven-plugin/README.md | 16 +++++++- .../plugin/maven/SchemaGeneratorMojo.java | 14 +++++-- .../plugin/maven/SchemaGeneratorMojoTest.java | 3 +- .../plugin/maven/TestCustomModule.java | 39 +++++++++++++++++++ .../CustomModuleWithOptions-pom.xml | 22 +++++++++++ .../CustomModuleWithOptions-reference.json | 4 ++ 7 files changed, 116 insertions(+), 5 deletions(-) create mode 100644 jsonschema-generator/src/main/java/com/github/victools/jsonschema/generator/CustomModule.java create mode 100644 jsonschema-maven-plugin/src/test/java/com/github/victools/jsonschema/plugin/maven/TestCustomModule.java create mode 100644 jsonschema-maven-plugin/src/test/resources/reference-test-cases/CustomModuleWithOptions-pom.xml create mode 100644 jsonschema-maven-plugin/src/test/resources/reference-test-cases/CustomModuleWithOptions-reference.json diff --git a/jsonschema-generator/src/main/java/com/github/victools/jsonschema/generator/CustomModule.java b/jsonschema-generator/src/main/java/com/github/victools/jsonschema/generator/CustomModule.java new file mode 100644 index 00000000..40ace886 --- /dev/null +++ b/jsonschema-generator/src/main/java/com/github/victools/jsonschema/generator/CustomModule.java @@ -0,0 +1,23 @@ +/* + * Copyright 2025 Christian Scheer. + * + * 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.generator; + +import java.util.List; + +public interface CustomModule extends Module { + void setOptions(List options); +} diff --git a/jsonschema-maven-plugin/README.md b/jsonschema-maven-plugin/README.md index 8f7f9927..81fa6317 100644 --- a/jsonschema-maven-plugin/README.md +++ b/jsonschema-maven-plugin/README.md @@ -223,7 +223,21 @@ To enable a custom module in the generation the following construct can be used: ``` Make sure your custom module is on the classpath and has a default constructor. -It is not possible to configure options for custom modules. +If your module implements `com.github.victools.jsonschema.generator.CustomModule`, you can configure options: +```xml + + com.myOrg.myApp.MyClass + + + com.myOrg.myApp.CustomModule + + + + + + + +``` ### Complete Example ```xml 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 272bfa65..bbe62a2b 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 @@ -18,6 +18,7 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; +import com.github.victools.jsonschema.generator.CustomModule; import com.github.victools.jsonschema.generator.Module; import com.github.victools.jsonschema.generator.OptionPreset; import com.github.victools.jsonschema.generator.SchemaGenerator; @@ -466,7 +467,7 @@ private void setOptions(SchemaGeneratorConfigBuilder configBuilder) { private void setModules(SchemaGeneratorConfigBuilder configBuilder) throws MojoExecutionException { for (GeneratorModule module : Util.nullSafe(this.modules)) { if (!Util.isNullOrEmpty(module.className)) { - this.addCustomModule(module.className, configBuilder); + this.addCustomModule(module, configBuilder); } else if (!Util.isNullOrEmpty(module.name)) { this.addStandardModule(module, configBuilder); } @@ -476,15 +477,22 @@ private void setModules(SchemaGeneratorConfigBuilder configBuilder) throws MojoE /** * Instantiate and apply the custom module with the given class name to the config builder. * - * @param moduleClassName Class name of the custom module to add. + * @param module Record in the modules section from the pom containing at least a name. * @param configBuilder The builder on which the module is added. * @throws MojoExecutionException When failing to instantiate the indicated module class. */ - private void addCustomModule(String moduleClassName, SchemaGeneratorConfigBuilder configBuilder) throws MojoExecutionException { + private void addCustomModule(GeneratorModule module, SchemaGeneratorConfigBuilder configBuilder) throws MojoExecutionException { + String moduleClassName = module.className; this.getLog().debug("- Adding custom Module " + moduleClassName); try { Class moduleClass = (Class) this.loadClass(moduleClassName); Module moduleInstance = moduleClass.getConstructor().newInstance(); + if (moduleInstance instanceof CustomModule) { + CustomModule customModule = (CustomModule) moduleInstance; + List options = Util.nullSafe(module.options); + this.getLog().debug("- Adding custom Module options " + options); + customModule.setOptions(options); + } configBuilder.with(moduleInstance); } catch (ClassCastException | InstantiationException | IllegalAccessException | NoSuchMethodException 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 010ea79c..4110ebf5 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 @@ -57,7 +57,8 @@ public void setUp() throws Exception { "JakartaValidationModule", "Swagger15Module", "Swagger2Module", - "Complete" + "Complete", + "CustomModuleWithOptions" }) public void testGeneration(String testCaseName) throws Exception { File testCaseLocation = new File("src/test/resources/reference-test-cases"); diff --git a/jsonschema-maven-plugin/src/test/java/com/github/victools/jsonschema/plugin/maven/TestCustomModule.java b/jsonschema-maven-plugin/src/test/java/com/github/victools/jsonschema/plugin/maven/TestCustomModule.java new file mode 100644 index 00000000..51b2c4ac --- /dev/null +++ b/jsonschema-maven-plugin/src/test/java/com/github/victools/jsonschema/plugin/maven/TestCustomModule.java @@ -0,0 +1,39 @@ +/* + * Copyright 2025 Christian Scheer. + * + * 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; + +import com.github.victools.jsonschema.generator.CustomModule; +import com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder; + +import java.util.List; + +public class TestCustomModule implements CustomModule { + private String skipFieldsStartingWith; + + @Override + public void applyToConfigBuilder(SchemaGeneratorConfigBuilder builder) { + builder.forFields().withIgnoreCheck(field -> field.getName().startsWith(skipFieldsStartingWith)); + } + + @Override + public void setOptions(List options) { + if (options.size() > 1) { + throw new IllegalArgumentException("More than one option specified"); + } + this.skipFieldsStartingWith = options.get(0); + } +} diff --git a/jsonschema-maven-plugin/src/test/resources/reference-test-cases/CustomModuleWithOptions-pom.xml b/jsonschema-maven-plugin/src/test/resources/reference-test-cases/CustomModuleWithOptions-pom.xml new file mode 100644 index 00000000..71e078b8 --- /dev/null +++ b/jsonschema-maven-plugin/src/test/resources/reference-test-cases/CustomModuleWithOptions-pom.xml @@ -0,0 +1,22 @@ + + + + + com.github.victools + jsonschema-maven-plugin + + com.github.victools.jsonschema.plugin.maven.TestClass + target/generated-test-sources/CustomModuleWithOptions + + + com.github.victools.jsonschema.plugin.maven.TestCustomModule + + + + + + + + + + \ No newline at end of file diff --git a/jsonschema-maven-plugin/src/test/resources/reference-test-cases/CustomModuleWithOptions-reference.json b/jsonschema-maven-plugin/src/test/resources/reference-test-cases/CustomModuleWithOptions-reference.json new file mode 100644 index 00000000..442c2faf --- /dev/null +++ b/jsonschema-maven-plugin/src/test/resources/reference-test-cases/CustomModuleWithOptions-reference.json @@ -0,0 +1,4 @@ +{ + "$schema" : "http://json-schema.org/draft-07/schema#", + "type" : "object" +} \ No newline at end of file