Skip to content

Commit

Permalink
fix: apply review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
takanuva15 committed Oct 25, 2023
1 parent f00da9b commit 2992c56
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public SchemaBuilder buildMultipleSchemaDefinitions() {
*
* @return {@link ObjectMapper} instance
*/
public ObjectMapper getObjectMapper() {
return this.config.getObjectMapper();
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()
// 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);
Expand Down
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 @@ -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);
}

/**
Expand Down Expand Up @@ -628,7 +628,7 @@ private void addJacksonModule(SchemaGeneratorConfigBuilder configBuilder, Genera
*/
private void writeToFile(ObjectMapper mapper, JsonNode jsonSchema, File file) throws MojoExecutionException {
try (FileOutputStream outputStream = new FileOutputStream(file);
PrintWriter writer = new PrintWriter(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8))) {
PrintWriter writer = new PrintWriter(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8))) {
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,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));
}

/**
Expand Down

0 comments on commit 2992c56

Please sign in to comment.