Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add withObjectMapper method to customize serialization of schemas to file #388

Merged
merged 3 commits into from
Oct 25, 2023
Merged
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
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));
CarstenWickner marked this conversation as resolved.
Show resolved Hide resolved
} 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
Loading