From 42cea4d67e105e5d3b1c5410735d822586fa4ff7 Mon Sep 17 00:00:00 2001 From: takanuva15 Date: Wed, 25 Oct 2023 16:03:41 -0400 Subject: [PATCH] feat: add `withObjectMapper` method to allow custom serialization in Maven plugin (#388) --- .../jsonschema/generator/SchemaGenerator.java | 9 ++++++++ .../SchemaGeneratorConfigBuilder.java | 22 +++++++++++++++++-- .../SchemaGeneratorConfigBuilderTest.java | 10 +++++++++ .../plugin/maven/SchemaGeneratorMojo.java | 4 +++- .../plugin/maven/SchemaGeneratorMojoTest.java | 7 +++++- 5 files changed, 48 insertions(+), 4 deletions(-) diff --git a/jsonschema-generator/src/main/java/com/github/victools/jsonschema/generator/SchemaGenerator.java b/jsonschema-generator/src/main/java/com/github/victools/jsonschema/generator/SchemaGenerator.java index 34dff075..b02d4328 100644 --- a/jsonschema-generator/src/main/java/com/github/victools/jsonschema/generator/SchemaGenerator.java +++ b/jsonschema-generator/src/main/java/com/github/victools/jsonschema/generator/SchemaGenerator.java @@ -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; + } } diff --git a/jsonschema-generator/src/main/java/com/github/victools/jsonschema/generator/SchemaGeneratorConfigBuilder.java b/jsonschema-generator/src/main/java/com/github/victools/jsonschema/generator/SchemaGeneratorConfigBuilder.java index 90e56109..07a7d157 100644 --- a/jsonschema-generator/src/main/java/com/github/victools/jsonschema/generator/SchemaGeneratorConfigBuilder.java +++ b/jsonschema-generator/src/main/java/com/github/victools/jsonschema/generator/SchemaGeneratorConfigBuilder.java @@ -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; @@ -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); @@ -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; @@ -306,4 +309,19 @@ public SchemaGeneratorConfigBuilder withAnnotationInclusionOverride(Class