Skip to content

Commit 7341a5d

Browse files
feat: enable test package access from Maven plugin + documentation (#389)
* chore: add documentation for Maven plugin YAML support * chore(test): include ObjectMapper change in module used by Maven plugin * feat: support custom module loaded from test classpath * chore(docs): describe loading Maven plugin module from test classes
1 parent 42cea4d commit 7341a5d

File tree

9 files changed

+89
-8
lines changed

9 files changed

+89
-8
lines changed

CHANGELOG.md

+11
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88
### `jsonschema-generator`
9+
#### Added
10+
- offer `SchemaGeneratorConfigBuilder.withObjectMapper()`; mainly for use in custom modules in combination with the Maven plugin, where the constructor parameter cannot be used instead
11+
912
#### Changed
1013
- consider JavaBeans API specification in getter naming convention for field names with the second character being uppercase (e.g., a field `xIndex` has the getter `getxIndex()` according to the specification)
1114
- allow for field names starting with `is` to have getter of the same name (e.g., a field `isBool` may have the getter `isBool()`)
15+
- the default `ObjectMapper` instance now includes the enabled `SerializationFeature.INDENT_OUTPUT`
1216

1317
### `jsonschema-module-jackson`
1418
#### Added
@@ -23,6 +27,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2327
#### Fixed
2428
- avoid rounding error when taking over the value from `@Schema(multipleOf)`
2529

30+
### `jsonschema-naven-plugin`
31+
### Added
32+
- support custom configuration `Module` being loaded from test classpath elements
33+
34+
### Changed
35+
- a generated schema is now serialized through the configuration's `ObjectMapper` instance (e.g., granting control over pretty printing or even generating YAML instead of JSON files)
36+
2637
## [4.31.1] - 2023-04-28
2738
### `jsonschema-generator`
2839
#### Fixed

jsonschema-generator-parent/pom.xml

+7
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,13 @@
106106
<role>Provided PR #314 (adding explicit Java module descriptors)</role>
107107
</roles>
108108
</contributor>
109+
<contributor>
110+
<name>takanuva15</name>
111+
<url>https://github.com/takanuva15</url>
112+
<roles>
113+
<role>Provided PR #388 (allowing configuration of Maven plugin serialization behavior)</role>
114+
</roles>
115+
</contributor>
109116
</contributors>
110117

111118
<properties>

jsonschema-maven-plugin/src/main/java/com/github/victools/jsonschema/plugin/maven/ClasspathType.java

+13-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,12 @@ public enum ClasspathType {
4343
/**
4444
* Classes from the project, compile time and runtime dependencies.
4545
*/
46-
WITH_ALL_DEPENDENCIES;
46+
WITH_ALL_DEPENDENCIES,
47+
/**
48+
* Classes from the project (including tests), compile time, runtime and test dependencies.
49+
* Mainly intended for internal use when looking up custom modules.
50+
*/
51+
WITH_ALL_DEPENDENCIES_AND_TESTS;
4752

4853
public Collection<String> getClasspathElements(MavenProject project) {
4954
Collection<String> classpathElements;
@@ -64,6 +69,13 @@ public Collection<String> getClasspathElements(MavenProject project) {
6469
classpathElements.addAll(project.getRuntimeClasspathElements());
6570
classpathElements.addAll(project.getCompileClasspathElements());
6671
break;
72+
case WITH_ALL_DEPENDENCIES_AND_TESTS:
73+
// to remove duplicates
74+
classpathElements = new HashSet<>();
75+
classpathElements.addAll(project.getRuntimeClasspathElements());
76+
classpathElements.addAll(project.getCompileClasspathElements());
77+
classpathElements.addAll(project.getTestClasspathElements());
78+
break;
6779
default:
6880
throw new IllegalArgumentException("ClasspathType " + this + " not supported");
6981
}

jsonschema-maven-plugin/src/main/java/com/github/victools/jsonschema/plugin/maven/SchemaGeneratorMojo.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ private URLClassLoader getClassLoader() {
494494
if (this.classLoader == null) {
495495
// fix the classpath such that the classloader can get classes from any possible dependency
496496
// this does not affect filtering, as the classgraph library uses its own classloader and allows for caching
497-
List<URL> urls = ClasspathType.WITH_ALL_DEPENDENCIES.getUrls(this.project);
497+
List<URL> urls = ClasspathType.WITH_ALL_DEPENDENCIES_AND_TESTS.getUrls(this.project);
498498
this.classLoader = new URLClassLoader(urls.toArray(new URL[0]),
499499
Thread.currentThread().getContextClassLoader());
500500
}

jsonschema-maven-plugin/src/test/java/com/github/victools/jsonschema/plugin/maven/TestClass.java

+4
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,8 @@ public class TestClass {
2525
public TestClass(int anInt) {
2626
this.anInt = anInt;
2727
}
28+
29+
public int getAnInt() {
30+
return this.anInt;
31+
}
2832
}

jsonschema-maven-plugin/src/test/java/com/github/victools/jsonschema/plugin/maven/TestModule.java

+3
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@
1616

1717
package com.github.victools.jsonschema.plugin.maven;
1818

19+
import com.fasterxml.jackson.databind.ObjectMapper;
20+
import com.fasterxml.jackson.databind.SerializationFeature;
1921
import com.github.victools.jsonschema.generator.Module;
2022
import com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder;
2123

2224
public class TestModule implements Module {
2325

2426
@Override
2527
public void applyToConfigBuilder(SchemaGeneratorConfigBuilder builder) {
28+
builder.withObjectMapper(new ObjectMapper().disable(SerializationFeature.INDENT_OUTPUT));
2629
}
2730
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
{
2-
"type" : "object",
3-
"description" : "Jackson annotation class",
4-
"additionalProperties" : false
5-
}
1+
{"type":"object","properties":{"anInt":{"type":"integer"},"getAnInt()":{"type":"integer"}},"description":"Jackson annotation class","additionalProperties":false}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
{
22
"$schema" : "http://json-schema.org/draft-07/schema#",
33
"type" : "object",
4+
"properties" : {
5+
"anInt" : {
6+
"type" : "integer"
7+
}
8+
},
49
"description" : "Jackson annotation class"
510
}

slate-docs/source/includes/_maven-plugin.md

+44-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ The considered `<classpath>` may be further specified as one of four values:
3434
- `WITH_COMPILE_DEPENDENCIES` : `PROJECT_ONLY` and compile dependencies
3535
- `WITH_RUNTIME_DEPENDENCIES` : `PROJECT_ONLY` and runtime dependencies (default, if unspecified)
3636
- `WITH_ALL_DEPENDENCIES` : all of the above
37-
37+
- `WITH_ALL_DEPENDENCIES_AND_TESTS` : all of the above, with the addition of the current project's test files
38+
Note that this requires a different `<phase>` (e.g., `test-compile`) being specified on the `<execution>`.
3839
----
3940

4041
By default, the plugin aborts if the glob pattern does not match any class. If this is not desired, the `<failIfNoClassesMatch>` property can be set to `false`.
@@ -122,3 +123,45 @@ Through the `<modules>` tag you can include the standard modules – potentially
122123
You can also group any kind of configurations into a Module of your own and include it via its full class name.
123124
Make sure your custom module is on the classpath (considering the project itself as well as all compile and runtime dependencies) and has a default constructor.
124125
It is not possible to configure options for custom modules.
126+
127+
### Altering the format of generated schema files
128+
129+
```java
130+
public class MavenPluginYamlModule implements Module {
131+
@Override
132+
public void applyToConfigBuilder(SchemaGeneratorConfigBuilder builder) {
133+
// Maven plugin should produce YAML files instead of JSON
134+
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
135+
// set additional serialization options
136+
mapper.getSerializationConfig()
137+
.with(JsonWriteFeature.WRITE_NUMBERS_AS_STRINGS);
138+
mapper.setNodeFactory(JsonNodeFactory.withExactBigDecimals(true));
139+
builder.withObjectMapper(mapper);
140+
}
141+
}
142+
```
143+
144+
One possibility within such a custom Module (as mentioned above) is to configure the format of the generated schema files.
145+
The file contents are being produced by the schema generator's associated `ObjectMapper`.
146+
That default `ObjectMapper` can be replaced, e.g., to opt-out of the default pretty-printing or changing the file format to YAML.
147+
The given example requires the inclusion of the extra `com.fasterxml.jackson.dataformat:jackson-dataformat-yaml` dependency.
148+
149+
### Loading custom Module from test classes
150+
151+
```xml
152+
<executions>
153+
<execution>
154+
<phase>test-compile</phase>
155+
<goals>
156+
<goal>generate</goal>
157+
</goals>
158+
</execution>
159+
</executions>
160+
```
161+
162+
When you're using a custom Module (as mentioned above) for additional configuration options, but don't want to include it among your application code,
163+
you can either package it as separate artifact and include that as dependency of the plugin (not going into further detail here)
164+
or the custom Module class can be included in your test packages.
165+
When you do the latter, the Maven plugin will by default not be able to load that class, since it won't be compiled yet in the Maven phase during which the schema generation is being executed.
166+
The Maven `compile` phase is when the schema generation gets triggered by default.
167+
If you want the test classes (including the custom Module) to be available, a later phase (most likely: `test-compile`) needs to be specified.

0 commit comments

Comments
 (0)