Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -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<String> options);
}
16 changes: 15 additions & 1 deletion jsonschema-maven-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,21 @@ To enable a custom module in the generation the following construct can be used:
</configuration>
```
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
<configuration>
<classNames>com.myOrg.myApp.MyClass</classNames>
<modules>
<module>
<className>com.myOrg.myApp.CustomModule</className>
<options>
<option>MyOptionStringOne</option>
<option>MyOptionStringTwo</option>
</options>
</module>
</modules>
</configuration>
```

### Complete Example
```xml
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -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<? extends Module> moduleClass = (Class<? extends Module>) this.loadClass(moduleClassName);
Module moduleInstance = moduleClass.getConstructor().newInstance();
if (moduleInstance instanceof CustomModule) {
CustomModule customModule = (CustomModule) moduleInstance;
List<String> options = Util.nullSafe(module.options);
this.getLog().debug("- Adding custom Module options " + options);
customModule.setOptions(options);
}
configBuilder.with(moduleInstance);
} catch (ClassCastException | InstantiationException
| IllegalAccessException | NoSuchMethodException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> options) {
if (options.size() > 1) {
throw new IllegalArgumentException("More than one option specified");
}
this.skipFieldsStartingWith = options.get(0);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<project>
<build>
<plugins>
<plugin>
<groupId>com.github.victools</groupId>
<artifactId>jsonschema-maven-plugin</artifactId>
<configuration>
<classNames>com.github.victools.jsonschema.plugin.maven.TestClass</classNames>
<schemaFilePath>target/generated-test-sources/CustomModuleWithOptions</schemaFilePath>
<modules>
<module>
<className>com.github.victools.jsonschema.plugin.maven.TestCustomModule</className>
<options>
<option>a</option>
</options>
</module>
</modules>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"$schema" : "http://json-schema.org/draft-07/schema#",
"type" : "object"
}