Skip to content

Commit

Permalink
feat: add withObjectMapper method to allow custom serialization in …
Browse files Browse the repository at this point in the history
…Maven plugin (#388)
  • Loading branch information
takanuva15 authored Oct 25, 2023
1 parent 480935c commit 42cea4d
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,13 @@ public ObjectNode generateSchema(Type mainTargetType, Type... typeParameters) {
public SchemaBuilder buildMultipleSchemaDefinitions() {
return SchemaBuilder.forMultipleTypes(this.config, this.typeContext);
}

/**
* Returns the {@link SchemaGeneratorConfig} associated with this {@link SchemaGenerator}.
*
* @return a {@link SchemaGeneratorConfig} instance
*/
public SchemaGeneratorConfig getConfig() {
return this.config;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.fasterxml.classmate.AnnotationInclusion;
import com.fasterxml.jackson.core.json.JsonWriteFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.github.victools.jsonschema.generator.impl.SchemaGeneratorConfigImpl;
import java.lang.annotation.Annotation;
Expand All @@ -41,7 +42,9 @@ public class SchemaGeneratorConfigBuilder {
* @return default ObjectMapper instance
*/
private static ObjectMapper createDefaultObjectMapper() {
ObjectMapper mapper = new ObjectMapper();
ObjectMapper mapper = new ObjectMapper()
// since version 4.32.0; pretty print by default (can be overridden by supplying explicit mapper)
.enable(SerializationFeature.INDENT_OUTPUT);
mapper.getSerializationConfig()
// since version 4.21.0
.with(JsonWriteFeature.WRITE_NUMBERS_AS_STRINGS);
Expand All @@ -51,7 +54,7 @@ private static ObjectMapper createDefaultObjectMapper() {
return mapper;
}

private final ObjectMapper objectMapper;
private ObjectMapper objectMapper;
private final OptionPreset preset;
private final SchemaVersion schemaVersion;

Expand Down Expand Up @@ -306,4 +309,19 @@ public SchemaGeneratorConfigBuilder withAnnotationInclusionOverride(Class<? exte
this.annotationInclusionOverrides.put(annotationType, override);
return this;
}

/**
* Register a custom {@link ObjectMapper} to create object and array nodes for the JSON structure being
* generated. Additionally, it is used to serialize a given schema, e.g., within the standard Maven plugin as
* either YAML or JSON.
*
* @param objectMapper supplier for object and array nodes for the JSON structure being generated
* @return this builder instance (for chaining)
*
* @since 4.32.0
*/
public SchemaGeneratorConfigBuilder withObjectMapper(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.github.victools.jsonschema.generator;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -49,6 +50,15 @@ public void testGetSetting_WithoutOption() {
Assertions.assertFalse(this.builder.getSetting(Option.DEFINITIONS_FOR_ALL_OBJECTS));
}

@Test
public void testWithObjectMapper() {
ObjectMapper currMapper = this.builder.getObjectMapper();
ObjectMapper newMapper = new ObjectMapper();
this.builder.withObjectMapper(newMapper);
Assertions.assertSame(newMapper, this.builder.getObjectMapper());
Assertions.assertNotEquals(currMapper, this.builder.getObjectMapper());
}

@Test
public void testForFields() {
Assertions.assertNotNull(this.builder.forFields());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.github.victools.jsonschema.plugin.maven;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.victools.jsonschema.generator.Module;
import com.github.victools.jsonschema.generator.Option;
import com.github.victools.jsonschema.generator.OptionPreset;
Expand Down Expand Up @@ -626,9 +627,10 @@ private void addJacksonModule(SchemaGeneratorConfigBuilder configBuilder, Genera
* @throws MojoExecutionException In case of problems when writing the targeted file
*/
private void writeToFile(JsonNode jsonSchema, File file) throws MojoExecutionException {
ObjectMapper mapper = getGenerator().getConfig().getObjectMapper();
try (FileOutputStream outputStream = new FileOutputStream(file);
PrintWriter writer = new PrintWriter(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8))) {
writer.print(jsonSchema.toPrettyString());
writer.print(mapper.writeValueAsString(jsonSchema));
} catch (IOException e) {
throw new MojoExecutionException("Error: Can not write to file " + file, e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,12 @@ public void testGeneration(String testCaseName) throws Exception {
File referenceFile = new File(testCaseLocation, testCaseName + "-reference.json");
Assertions.assertTrue(referenceFile.exists());
Assertions.assertTrue(FileUtils.contentEqualsIgnoreEOL(resultFile, referenceFile, CHARSET_NAME),
"Generated schema for " + testCaseName + " is not equal to the expected reference.");
"Generated schema for " + testCaseName + " is not equal to the expected reference.\n"
+ "Generated:\n"
+ FileUtils.readFileToString(resultFile)
+ "\n----------\n"
+ "Expected:\n"
+ FileUtils.readFileToString(referenceFile));
}

/**
Expand Down

0 comments on commit 42cea4d

Please sign in to comment.