diff --git a/core/runtime/src/main/java/com/buschmais/jqassistant/core/runtime/api/configuration/JsonSchemaGenerator.java b/core/runtime/src/main/java/com/buschmais/jqassistant/core/runtime/api/configuration/JsonSchemaGenerator.java index 641a0988a..fc66b6bc6 100644 --- a/core/runtime/src/main/java/com/buschmais/jqassistant/core/runtime/api/configuration/JsonSchemaGenerator.java +++ b/core/runtime/src/main/java/com/buschmais/jqassistant/core/runtime/api/configuration/JsonSchemaGenerator.java @@ -4,11 +4,14 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; import com.github.victools.jsonschema.generator.*; +import io.smallrye.config.WithDefault; import java.io.File; import java.io.IOException; import java.net.URI; -import java.util.*; +import java.util.List; +import java.util.Map; +import java.util.Optional; public class JsonSchemaGenerator { @@ -23,7 +26,8 @@ public ObjectNode generateSchema(Class clazz) { return getResolvedTypes(target, resolvedType); }); configBuilder.forFields() - .withIgnoreCheck(field -> field.getName().startsWith("PREFIX") || field.getName().startsWith("SKIP")); + .withIgnoreCheck(field -> field.getName().startsWith("PREFIX") || field.getName().startsWith("SKIP") || field.getName().startsWith("DEFAULT") ); + configBuilder.forMethods().withPropertyNameOverrideResolver((member) -> mapToKebabCase(member.getName())); configBuilder.forFields().withPropertyNameOverrideResolver((member) -> mapToKebabCase(member.getName())); configBuilder.forTypesInGeneral() @@ -41,6 +45,13 @@ public ObjectNode generateSchema(Class clazz) { } return null; }); + configBuilder.forMethods().withDefaultResolver(field -> { + WithDefault annotation = field.getAnnotationConsideringFieldAndGetter(WithDefault.class); + if (annotation != null) { + return annotation.value(); + } + return null; + }); configBuilder.forTypesInGeneral() .withDefinitionNamingStrategy((definitionKey, context) -> mapToKebabCase(definitionKey.getType().getTypeName())); @@ -60,7 +71,7 @@ private static List getResolvedTypes(MethodScope target, ResolvedT return getResolvedTypes(target, resolvedType.getTypeParameters().get(0)); } } - return List.of(target.getContext().resolve(resolvedType)); + return List.of(target.getContext().resolve(resolvedType)); } public void saveSchemaToFile(ObjectNode schema, String targetFolder, String fileName) { diff --git a/core/runtime/src/test/java/com/buschmais/jqassistant/core/runtime/api/configuration/JsonSchemaGeneratingTest.java b/core/runtime/src/test/java/com/buschmais/jqassistant/core/runtime/api/configuration/JsonSchemaGeneratingTest.java index 109b4a74a..25707791f 100644 --- a/core/runtime/src/test/java/com/buschmais/jqassistant/core/runtime/api/configuration/JsonSchemaGeneratingTest.java +++ b/core/runtime/src/test/java/com/buschmais/jqassistant/core/runtime/api/configuration/JsonSchemaGeneratingTest.java @@ -27,7 +27,8 @@ public class JsonSchemaGeneratingTest { public String validateYaml(JsonSchema schema, String filePath) throws IOException { JsonNode node = generator.generateSchema(JQAssistant.class); - File file = new File("target/test-classes/jsonSchema.schema.json"); + File file = new File("src/test/resources/jqassistant-configuration.schema.json"); + System.out.println("Schema saved: " + file.getAbsolutePath()); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.writerWithDefaultPrettyPrinter().writeValue(file, node); @@ -38,9 +39,10 @@ public String validateYaml(JsonSchema schema, String filePath) throws IOExceptio String jsonString = objectMapper.writeValueAsString(yamlData); Set validationMessages = schema.validate(objectMapper.readTree(jsonString)); if (validationMessages.isEmpty()) { + System.out.println("Schema was successfully validated."); return "YAML is valid."; } else { - + System.out.println("Schema validation failed on errors:"); validationMessages.forEach(msg -> System.out.println("- " + msg.getMessage())); return "YAML is invalid."; } @@ -71,7 +73,7 @@ public void testInvalidYaml() throws IOException { } @Test - public void generateSchemaAndValidTest() throws IOException { + public void generateSchema() throws IOException { JsonNode node = generator.generateSchema(JQAssistant.class); assertThat(node).isNotNull(); File file = new File("src/test/resources/jqassistant-configuration.schema.json"); @@ -83,7 +85,8 @@ public void generateSchemaAndValidTest() throws IOException { objectMapper.writerWithDefaultPrettyPrinter().writeValue(file, node); JsonNode rootNode = mapper.readTree(file); Set validationMessages = schema.validate(rootNode); - System.out.println(validationMessages); + System.out.println("Generated schema."); + validationMessages.forEach(msg -> System.out.println("- " + msg.getMessage())); } @ConfigMapping diff --git a/core/runtime/src/test/java/com/buschmais/jqassistant/core/runtime/api/configuration/JsonSchemaGeneratorTest.java b/core/runtime/src/test/java/com/buschmais/jqassistant/core/runtime/api/configuration/JsonSchemaGeneratorTest.java deleted file mode 100644 index 0e0202489..000000000 --- a/core/runtime/src/test/java/com/buschmais/jqassistant/core/runtime/api/configuration/JsonSchemaGeneratorTest.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.buschmais.jqassistant.core.runtime.api.configuration; - -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; -import com.networknt.schema.JsonSchema; -import com.networknt.schema.JsonSchemaFactory; -import com.networknt.schema.SpecVersion; -import com.networknt.schema.ValidationMessage; -import io.smallrye.config.ConfigMapping; -import org.junit.Test; - -import java.io.File; -import java.io.IOException; -import java.util.Set; - -import static org.assertj.core.api.Assertions.assertThat; - -public class JsonSchemaGeneratorTest { - - private final JsonSchemaGenerator generator = new JsonSchemaGenerator(); - - @Test - public void testJsonSchemaGenerating() throws IOException { - JsonNode node = generator.generateSchema(JQAssistant.class); - assertThat(node).isNotNull(); - - File file = new File("target/test-classes/jsonSchema.json"); - ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); - JsonSchemaFactory bluePrintFactory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909); - JsonSchemaFactory schemaFactory = JsonSchemaFactory.builder(bluePrintFactory).build(); - JsonSchema schema = schemaFactory.getSchema(node); - JsonNode rootNode = mapper.readTree(file); - Set validationMessages = schema.validate(rootNode); - System.out.println(validationMessages); - - } - - @ConfigMapping - private interface JQAssistant { - - Configuration jqassistant(); - - } -} diff --git a/core/runtime/src/test/resources/jqassistant-configuration.schema.json b/core/runtime/src/test/resources/jqassistant-configuration.schema.json index 753b01c55..65f1761f8 100644 --- a/core/runtime/src/test/resources/jqassistant-configuration.schema.json +++ b/core/runtime/src/test/resources/jqassistant-configuration.schema.json @@ -34,7 +34,8 @@ "type" : "string" }, "type" : { - "type" : "string" + "type" : "string", + "default" : "jar" } }, "additionalProperties" : false @@ -44,7 +45,8 @@ "type" : "string" }, "type" : { - "type" : "string" + "type" : "string", + "default" : "jar" }, "version" : { "type" : "string" @@ -71,7 +73,8 @@ "type" : "object", "properties" : { "enabled" : { - "type" : "boolean" + "type" : "boolean", + "default" : "false" }, "file" : { "type" : "string" @@ -83,9 +86,11 @@ } }, "include-constraints" : { + "default" : "*", "type" : "array", "items" : { - "type" : "string" + "type" : "string", + "default" : "*" } } }, @@ -110,7 +115,8 @@ } }, "execute-applied-concepts" : { - "type" : "boolean" + "type" : "boolean", + "default" : "false" }, "groups" : { "type" : "array", @@ -122,22 +128,26 @@ "type" : "object", "properties" : { "continue-on-failure" : { - "type" : "boolean" + "type" : "boolean", + "default" : "false" }, "create-archive" : { - "type" : "boolean" + "type" : "boolean", + "default" : "false" }, "directory" : { "type" : "string" }, "fail-on-severity" : { - "type" : "string" + "type" : "string", + "default" : "MAJOR" }, "properties" : { "$ref" : "#/$defs/java.util.map" }, "warn-on-severity" : { - "type" : "string" + "type" : "string", + "default" : "MINOR" } }, "additionalProperties" : false @@ -158,7 +168,8 @@ "type" : "string" }, "required-concepts-are-optional-by-default" : { - "type" : "boolean" + "type" : "boolean", + "default" : "true" } }, "additionalProperties" : false @@ -167,7 +178,8 @@ "$ref" : "#/$defs/java.util.map" }, "warn-on-execution-time-seconds" : { - "type" : "integer" + "type" : "integer", + "default" : "5" } }, "additionalProperties" : false @@ -188,7 +200,8 @@ "type" : "object", "properties" : { "continue-on-error" : { - "type" : "boolean" + "type" : "boolean", + "default" : "false" }, "include" : { "type" : "object", @@ -221,16 +234,19 @@ "type" : "object", "properties" : { "daemon" : { - "type" : "boolean" + "type" : "boolean", + "default" : "false" }, "open-browser" : { - "type" : "boolean" + "type" : "boolean", + "default" : "false" } }, "additionalProperties" : false }, "skip" : { - "type" : "boolean" + "type" : "boolean", + "default" : "false" }, "store" : { "type" : "object", @@ -239,28 +255,20 @@ "type" : "object", "properties" : { "bolt-port" : { - "type" : "integer" + "type" : "integer", + "default" : "7687" }, "connector-enabled" : { - "type" : "boolean" - }, - "default-bolt-port" : { - "type" : "string", - "const" : "7687" - }, - "default-http-port" : { - "type" : "string", - "const" : "7474" - }, - "default-listen-address" : { - "type" : "string", - "const" : "localhost" + "type" : "boolean", + "default" : "false" }, "http-port" : { - "type" : "integer" + "type" : "integer", + "default" : "7474" }, "listen-address" : { - "type" : "string" + "type" : "string", + "default" : "localhost" }, "neo4j-plugins" : { "type" : "array", @@ -275,7 +283,8 @@ "type" : "object", "properties" : { "encryption" : { - "type" : "boolean" + "type" : "boolean", + "default" : "false" }, "password" : { "type" : "string" diff --git a/core/runtime/src/test/resources/testdata/generate-schema/validJQAYaml.yaml b/core/runtime/src/test/resources/testdata/generate-schema/validJQAYaml.yaml index 1f5f62f7c..1b9e367e9 100644 --- a/core/runtime/src/test/resources/testdata/generate-schema/validJQAYaml.yaml +++ b/core/runtime/src/test/resources/testdata/generate-schema/validJQAYaml.yaml @@ -11,11 +11,19 @@ jqassistant: properties: custom.scan.value: testValue jqassistant.plugin.jacoco.filename: jacoco.xml + store: + embedded: + http-port: 7474 analyze: groups: - default + rule: + default-concept-severity: CRITICAL report: + fail-on-severity: MINOR + warn-on-severity: MINOR properties: customReport.fileName: ${project.build.directory}/customReport.txt fail-on-severity: MINOR +