Skip to content

Commit

Permalink
Added default values mapping logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bramaten committed Dec 3, 2024
1 parent 0dd1660 commit 7812190
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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()
Expand All @@ -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()));

Expand All @@ -60,7 +71,7 @@ private static List<ResolvedType> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -38,9 +39,10 @@ public String validateYaml(JsonSchema schema, String filePath) throws IOExceptio
String jsonString = objectMapper.writeValueAsString(yamlData);
Set<ValidationMessage> 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.";
}
Expand Down Expand Up @@ -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");
Expand All @@ -83,7 +85,8 @@ public void generateSchemaAndValidTest() throws IOException {
objectMapper.writerWithDefaultPrettyPrinter().writeValue(file, node);
JsonNode rootNode = mapper.readTree(file);
Set<ValidationMessage> validationMessages = schema.validate(rootNode);
System.out.println(validationMessages);
System.out.println("Generated schema.");
validationMessages.forEach(msg -> System.out.println("- " + msg.getMessage()));
}

@ConfigMapping
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"type" : "string"
},
"type" : {
"type" : "string"
"type" : "string",
"default" : "jar"
}
},
"additionalProperties" : false
Expand All @@ -44,7 +45,8 @@
"type" : "string"
},
"type" : {
"type" : "string"
"type" : "string",
"default" : "jar"
},
"version" : {
"type" : "string"
Expand All @@ -71,7 +73,8 @@
"type" : "object",
"properties" : {
"enabled" : {
"type" : "boolean"
"type" : "boolean",
"default" : "false"
},
"file" : {
"type" : "string"
Expand All @@ -83,9 +86,11 @@
}
},
"include-constraints" : {
"default" : "*",
"type" : "array",
"items" : {
"type" : "string"
"type" : "string",
"default" : "*"
}
}
},
Expand All @@ -110,7 +115,8 @@
}
},
"execute-applied-concepts" : {
"type" : "boolean"
"type" : "boolean",
"default" : "false"
},
"groups" : {
"type" : "array",
Expand All @@ -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
Expand All @@ -158,7 +168,8 @@
"type" : "string"
},
"required-concepts-are-optional-by-default" : {
"type" : "boolean"
"type" : "boolean",
"default" : "true"
}
},
"additionalProperties" : false
Expand All @@ -167,7 +178,8 @@
"$ref" : "#/$defs/java.util.map"
},
"warn-on-execution-time-seconds" : {
"type" : "integer"
"type" : "integer",
"default" : "5"
}
},
"additionalProperties" : false
Expand All @@ -188,7 +200,8 @@
"type" : "object",
"properties" : {
"continue-on-error" : {
"type" : "boolean"
"type" : "boolean",
"default" : "false"
},
"include" : {
"type" : "object",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -275,7 +283,8 @@
"type" : "object",
"properties" : {
"encryption" : {
"type" : "boolean"
"type" : "boolean",
"default" : "false"
},
"password" : {
"type" : "string"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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


0 comments on commit 7812190

Please sign in to comment.