Skip to content

Commit 42cea4d

Browse files
authored
feat: add withObjectMapper method to allow custom serialization in Maven plugin (#388)
1 parent 480935c commit 42cea4d

File tree

5 files changed

+48
-4
lines changed

5 files changed

+48
-4
lines changed

jsonschema-generator/src/main/java/com/github/victools/jsonschema/generator/SchemaGenerator.java

+9
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,13 @@ public ObjectNode generateSchema(Type mainTargetType, Type... typeParameters) {
7575
public SchemaBuilder buildMultipleSchemaDefinitions() {
7676
return SchemaBuilder.forMultipleTypes(this.config, this.typeContext);
7777
}
78+
79+
/**
80+
* Returns the {@link SchemaGeneratorConfig} associated with this {@link SchemaGenerator}.
81+
*
82+
* @return a {@link SchemaGeneratorConfig} instance
83+
*/
84+
public SchemaGeneratorConfig getConfig() {
85+
return this.config;
86+
}
7887
}

jsonschema-generator/src/main/java/com/github/victools/jsonschema/generator/SchemaGeneratorConfigBuilder.java

+20-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.fasterxml.classmate.AnnotationInclusion;
2020
import com.fasterxml.jackson.core.json.JsonWriteFeature;
2121
import com.fasterxml.jackson.databind.ObjectMapper;
22+
import com.fasterxml.jackson.databind.SerializationFeature;
2223
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
2324
import com.github.victools.jsonschema.generator.impl.SchemaGeneratorConfigImpl;
2425
import java.lang.annotation.Annotation;
@@ -41,7 +42,9 @@ public class SchemaGeneratorConfigBuilder {
4142
* @return default ObjectMapper instance
4243
*/
4344
private static ObjectMapper createDefaultObjectMapper() {
44-
ObjectMapper mapper = new ObjectMapper();
45+
ObjectMapper mapper = new ObjectMapper()
46+
// since version 4.32.0; pretty print by default (can be overridden by supplying explicit mapper)
47+
.enable(SerializationFeature.INDENT_OUTPUT);
4548
mapper.getSerializationConfig()
4649
// since version 4.21.0
4750
.with(JsonWriteFeature.WRITE_NUMBERS_AS_STRINGS);
@@ -51,7 +54,7 @@ private static ObjectMapper createDefaultObjectMapper() {
5154
return mapper;
5255
}
5356

54-
private final ObjectMapper objectMapper;
57+
private ObjectMapper objectMapper;
5558
private final OptionPreset preset;
5659
private final SchemaVersion schemaVersion;
5760

@@ -306,4 +309,19 @@ public SchemaGeneratorConfigBuilder withAnnotationInclusionOverride(Class<? exte
306309
this.annotationInclusionOverrides.put(annotationType, override);
307310
return this;
308311
}
312+
313+
/**
314+
* Register a custom {@link ObjectMapper} to create object and array nodes for the JSON structure being
315+
* generated. Additionally, it is used to serialize a given schema, e.g., within the standard Maven plugin as
316+
* either YAML or JSON.
317+
*
318+
* @param objectMapper supplier for object and array nodes for the JSON structure being generated
319+
* @return this builder instance (for chaining)
320+
*
321+
* @since 4.32.0
322+
*/
323+
public SchemaGeneratorConfigBuilder withObjectMapper(ObjectMapper objectMapper) {
324+
this.objectMapper = objectMapper;
325+
return this;
326+
}
309327
}

jsonschema-generator/src/test/java/com/github/victools/jsonschema/generator/SchemaGeneratorConfigBuilderTest.java

+10
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.github.victools.jsonschema.generator;
1818

19+
import com.fasterxml.jackson.databind.ObjectMapper;
1920
import org.junit.jupiter.api.Assertions;
2021
import org.junit.jupiter.api.BeforeEach;
2122
import org.junit.jupiter.api.Test;
@@ -49,6 +50,15 @@ public void testGetSetting_WithoutOption() {
4950
Assertions.assertFalse(this.builder.getSetting(Option.DEFINITIONS_FOR_ALL_OBJECTS));
5051
}
5152

53+
@Test
54+
public void testWithObjectMapper() {
55+
ObjectMapper currMapper = this.builder.getObjectMapper();
56+
ObjectMapper newMapper = new ObjectMapper();
57+
this.builder.withObjectMapper(newMapper);
58+
Assertions.assertSame(newMapper, this.builder.getObjectMapper());
59+
Assertions.assertNotEquals(currMapper, this.builder.getObjectMapper());
60+
}
61+
5262
@Test
5363
public void testForFields() {
5464
Assertions.assertNotNull(this.builder.forFields());

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.github.victools.jsonschema.plugin.maven;
1818

1919
import com.fasterxml.jackson.databind.JsonNode;
20+
import com.fasterxml.jackson.databind.ObjectMapper;
2021
import com.github.victools.jsonschema.generator.Module;
2122
import com.github.victools.jsonschema.generator.Option;
2223
import com.github.victools.jsonschema.generator.OptionPreset;
@@ -626,9 +627,10 @@ private void addJacksonModule(SchemaGeneratorConfigBuilder configBuilder, Genera
626627
* @throws MojoExecutionException In case of problems when writing the targeted file
627628
*/
628629
private void writeToFile(JsonNode jsonSchema, File file) throws MojoExecutionException {
630+
ObjectMapper mapper = getGenerator().getConfig().getObjectMapper();
629631
try (FileOutputStream outputStream = new FileOutputStream(file);
630632
PrintWriter writer = new PrintWriter(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8))) {
631-
writer.print(jsonSchema.toPrettyString());
633+
writer.print(mapper.writeValueAsString(jsonSchema));
632634
} catch (IOException e) {
633635
throw new MojoExecutionException("Error: Can not write to file " + file, e);
634636
}

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,12 @@ public void testGeneration(String testCaseName) throws Exception {
7575
File referenceFile = new File(testCaseLocation, testCaseName + "-reference.json");
7676
Assertions.assertTrue(referenceFile.exists());
7777
Assertions.assertTrue(FileUtils.contentEqualsIgnoreEOL(resultFile, referenceFile, CHARSET_NAME),
78-
"Generated schema for " + testCaseName + " is not equal to the expected reference.");
78+
"Generated schema for " + testCaseName + " is not equal to the expected reference.\n"
79+
+ "Generated:\n"
80+
+ FileUtils.readFileToString(resultFile)
81+
+ "\n----------\n"
82+
+ "Expected:\n"
83+
+ FileUtils.readFileToString(referenceFile));
7984
}
8085

8186
/**

0 commit comments

Comments
 (0)