Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ indent_size = 4
indent_style = space
indent_size = 2

[*.yml]
[*.{yml,yaml}]
indent_style = space
indent_size = 2

Expand Down
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,11 @@
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.alexmond</groupId>
<artifactId>spring-boot-config-json-schema-starter</artifactId>
<version>3.5.16.1</version>
</dependency>

</dependencies>

Expand Down Expand Up @@ -323,6 +328,11 @@
<artifactId>lombok</artifactId>
<version>1.18.42</version>
</path>
<path>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>3.5.6</version>
</path>
</annotationProcessorPaths>
</configuration>
<version>3.14.1</version>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package org.patinanetwork.codebloom.utilities.generator.schema;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Set;
import lombok.extern.slf4j.Slf4j;
import org.alexmond.config.json.schema.service.JsonSchemaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

/** Regenerates config-schema on every dev boot */
@Profile("dev")
@Component
@Slf4j
public class ConfigSchemaGenerator implements CommandLineRunner {

private static final Path DEFAULT_OUTPUT_PATH = Path.of("src/main/resources/config-schema.json");

private final JsonSchemaService jsonSchemaService;

private final Path outputPath;

private final ObjectMapper objectMapper = new ObjectMapper();

@Autowired
public ConfigSchemaGenerator(final JsonSchemaService jsonSchemaService) {
this(jsonSchemaService, DEFAULT_OUTPUT_PATH);
}

ConfigSchemaGenerator(final JsonSchemaService jsonSchemaService, final Path outputPath) {
this.jsonSchemaService = jsonSchemaService;
this.outputPath = outputPath;
}

@Override
public void run(final String... args) {
try {
JsonNode schema = objectMapper.readTree(jsonSchemaService.generateFullSchemaJson());
relax(schema);
Files.writeString(
outputPath, objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(schema) + "\n");
log.info("Wrote config schema to {}", outputPath);
} catch (Exception e) {
log.error("Config schema generation failed", e);
}
}

private static final Set<String> STRING_COERCIBLE_TYPES = Set.of("boolean", "integer", "number", "array");

/**
* Loosens the library output to match what Spring actually accepts:
*
* <ul>
* <li>Drops {@code $anchor}: values are class names containing {@code $}/{@code :}, which violate the 2020-12
* anchor pattern. All refs use {@code #/$defs/...}, so anchors are unused. Only string values are removed; an
* object under a {@code $anchor} key is a property definition, not the keyword.
* <li>Widens boolean/integer/number/array to also allow string, so {@code ${ENV_VAR}} placeholders and
* comma-separated lists validate.
* </ul>
*/
private static void relax(final JsonNode node) {
if (node instanceof ObjectNode object) {
if (object.path("$anchor").isTextual()) {
object.remove("$anchor");
}
JsonNode type = object.path("type");
if (type.isTextual() && STRING_COERCIBLE_TYPES.contains(type.asText())) {
object.putArray("type").add(type.asText()).add("string");
}
}
node.forEach(ConfigSchemaGenerator::relax);
}
}
1 change: 1 addition & 0 deletions src/main/resources/application-ci.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# yaml-language-server: $schema=./config-schema.json
codebloom:
frontend:
index: file:js/index.html
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/application-dev.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# yaml-language-server: $schema=./config-schema.json
codebloom:
frontend:
index: file:js/index.html
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# yaml-language-server: $schema=./config-schema.json
logging:
pattern:
console: "%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] [%X{requestId}] %logger{36} - %msg%n"
Expand Down
Loading
Loading