From 701d35c0d59e3189cd039078a4fddd959917b231 Mon Sep 17 00:00:00 2001 From: takanuva15 Date: Tue, 24 Oct 2023 23:07:00 -0400 Subject: [PATCH] fix: apply review comments --- .../victools/jsonschema/generator/SchemaGenerator.java | 9 ++++----- .../generator/SchemaGeneratorConfigBuilder.java | 5 ++++- .../generator/SchemaGeneratorConfigBuilderTest.java | 10 ++++++++++ .../jsonschema/plugin/maven/SchemaGeneratorMojo.java | 3 ++- .../plugin/maven/SchemaGeneratorMojoTest.java | 6 +++++- 5 files changed, 25 insertions(+), 8 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 2a7aa0ea..45de738f 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 @@ -78,12 +78,11 @@ public SchemaBuilder buildMultipleSchemaDefinitions() { } /** - * Returns the {@link ObjectMapper} instance that is associated with the {@link SchemaGeneratorConfig} that was - * used to build this {@link SchemaGenerator}. + * Returns the {@link SchemaGeneratorConfig} associated with this {@link SchemaGenerator}. * - * @return {@link ObjectMapper} instance + * @return a {@link SchemaGeneratorConfig} instance */ - public ObjectMapper getObjectMapper() { - return this.config.getObjectMapper(); + 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 a6dc4057..c7c15372 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() + // enables pretty-printing by default (users can override this by supplying their own mapper) + .enable(SerializationFeature.INDENT_OUTPUT); mapper.getSerializationConfig() // since version 4.21.0 .with(JsonWriteFeature.WRITE_NUMBERS_AS_STRINGS); diff --git a/jsonschema-generator/src/test/java/com/github/victools/jsonschema/generator/SchemaGeneratorConfigBuilderTest.java b/jsonschema-generator/src/test/java/com/github/victools/jsonschema/generator/SchemaGeneratorConfigBuilderTest.java index 46d87448..73f45f2a 100644 --- a/jsonschema-generator/src/test/java/com/github/victools/jsonschema/generator/SchemaGeneratorConfigBuilderTest.java +++ b/jsonschema-generator/src/test/java/com/github/victools/jsonschema/generator/SchemaGeneratorConfigBuilderTest.java @@ -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; @@ -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()); diff --git a/jsonschema-maven-plugin/src/main/java/com/github/victools/jsonschema/plugin/maven/SchemaGeneratorMojo.java b/jsonschema-maven-plugin/src/main/java/com/github/victools/jsonschema/plugin/maven/SchemaGeneratorMojo.java index e3fe6ca6..d335cb4e 100644 --- a/jsonschema-maven-plugin/src/main/java/com/github/victools/jsonschema/plugin/maven/SchemaGeneratorMojo.java +++ b/jsonschema-maven-plugin/src/main/java/com/github/victools/jsonschema/plugin/maven/SchemaGeneratorMojo.java @@ -249,7 +249,7 @@ private void generateSchema(Class schemaClass) throws MojoExecutionException JsonNode jsonSchema = getGenerator().generateSchema(schemaClass); File file = getSchemaFile(schemaClass); this.getLog().info("- Writing schema to file: " + file); - this.writeToFile(getGenerator().getObjectMapper(), jsonSchema, file); + this.writeToFile(getGenerator().getConfig().getObjectMapper(), jsonSchema, file); } /** @@ -622,6 +622,7 @@ private void addJacksonModule(SchemaGeneratorConfigBuilder configBuilder, Genera /** * Write generated schema to a file. * + * @param mapper ObjectMapper to use when writing the given schema to the given file * @param jsonSchema Generated schema to be written * @param file The file to write to * @throws MojoExecutionException In case of problems when writing the targeted file diff --git a/jsonschema-maven-plugin/src/test/java/com/github/victools/jsonschema/plugin/maven/SchemaGeneratorMojoTest.java b/jsonschema-maven-plugin/src/test/java/com/github/victools/jsonschema/plugin/maven/SchemaGeneratorMojoTest.java index 1b3dffcb..d5709708 100644 --- a/jsonschema-maven-plugin/src/test/java/com/github/victools/jsonschema/plugin/maven/SchemaGeneratorMojoTest.java +++ b/jsonschema-maven-plugin/src/test/java/com/github/victools/jsonschema/plugin/maven/SchemaGeneratorMojoTest.java @@ -75,7 +75,11 @@ 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: " + FileUtils.readFileToString(resultFile) + "\n" + + "Expected: " + FileUtils.readFileToString(referenceFile)); + System.out.println(FileUtils.readFileToString(resultFile)); + System.out.println(FileUtils.readFileToString(referenceFile)); } /**