Skip to content

Commit

Permalink
[#337] Make the parser forward compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
spyrkob committed Jan 8, 2025
1 parent 62a35f6 commit bec93e5
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 9 deletions.
6 changes: 3 additions & 3 deletions core/src/main/java/org/wildfly/channel/Blocklist.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
import static java.util.Collections.singletonList;
import static java.util.Objects.requireNonNull;

public class Blocklist {
public class Blocklist extends VersionedMapper {

public static final String SCHEMA_VERSION_1_0_0 = "1.0.0";
private static final String SCHEMA_1_0_0_FILE = "org/wildfly/blocklist/v1.0.0/schema.json";
Expand Down Expand Up @@ -130,9 +130,9 @@ private static JsonSchema getSchema(JsonNode node) {
JsonNode schemaVersion = node.path("schemaVersion");
String version = schemaVersion.asText();
if (version == null || version.isEmpty()) {
throw new InvalidChannelMetadataException("Invalid Manifest", List.of("The manifest does not specify a schemaVersion."));
throw new InvalidChannelMetadataException("Invalid Blocklist", List.of("The blocklist definition does not specify a schemaVersion."));
}
JsonSchema schema = SCHEMAS.get(version);
JsonSchema schema = getSchema(version, SCHEMAS);
if (schema == null) {
throw new InvalidChannelMetadataException("Invalid Manifest", List.of("Unknown schema version " + schemaVersion));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import com.networknt.schema.JsonSchemaFactory;
import com.networknt.schema.SpecVersion;
import com.networknt.schema.ValidationMessage;
import org.jboss.logging.Logger;
import org.wildfly.channel.version.VersionMatcher;

import java.io.FileNotFoundException;
import java.io.IOException;
Expand All @@ -35,7 +37,9 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES;
Expand All @@ -48,7 +52,7 @@
*
* YAML input is validated against a schema.
*/
public class ChannelManifestMapper {
public class ChannelManifestMapper extends VersionedMapper {

public static final String SCHEMA_VERSION_1_0_0 = "1.0.0";
public static final String SCHEMA_VERSION_1_1_0 = "1.1.0";
Expand All @@ -75,7 +79,7 @@ private static JsonSchema getSchema(JsonNode node) {
if (version == null || version.isEmpty()) {
throw new InvalidChannelMetadataException("Invalid Manifest", List.of("The manifest does not specify a schemaVersion."));
}
JsonSchema schema = SCHEMAS.get(version);
JsonSchema schema = getSchema(version, SCHEMAS);
if (schema == null) {
throw new InvalidChannelMetadataException("Invalid Manifest", List.of("Unknown schema version " + schemaVersion));
}
Expand Down
9 changes: 5 additions & 4 deletions core/src/main/java/org/wildfly/channel/ChannelMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
*
* YAML input is validated against a schema.
*/
public class ChannelMapper {
public class ChannelMapper extends VersionedMapper {

public static final String SCHEMA_VERSION_1_0_0 = "1.0.0";
public static final String SCHEMA_VERSION_2_0_0 = "2.0.0";
Expand Down Expand Up @@ -80,11 +80,12 @@ private static JsonSchema getSchema(JsonNode node) {
JsonNode schemaVersion = node.path("schemaVersion");
String version = schemaVersion.asText();
if (version == null || version.isEmpty()) {
throw new InvalidChannelMetadataException("Invalid Manifest", List.of("The manifest does not specify a schemaVersion."));
throw new InvalidChannelMetadataException("Invalid Channel", List.of("The channel definition does not specify a schemaVersion."));
}
JsonSchema schema = SCHEMAS.get(version);

final JsonSchema schema = getSchema(version, SCHEMAS);
if (schema == null) {
throw new InvalidChannelMetadataException("Invalid Manifest", List.of("Unknown schema version " + schemaVersion));
throw new InvalidChannelMetadataException("Invalid Channel", List.of("Unknown schema version " + schemaVersion));
}
return schema;
}
Expand Down
30 changes: 30 additions & 0 deletions core/src/main/java/org/wildfly/channel/VersionedMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.wildfly.channel;

import java.util.Map;
import java.util.Optional;
import java.util.regex.Pattern;

import com.networknt.schema.JsonSchema;
import org.jboss.logging.Logger;
import org.wildfly.channel.version.VersionMatcher;

abstract class VersionedMapper {

private static final Logger LOG = Logger.getLogger(VersionedMapper.class.getName());

protected static JsonSchema getSchema(String version, Map<String, JsonSchema> schemas) {
if (schemas.containsKey(version)) {
return schemas.get(version);
}

Pattern versionPattern = Pattern.compile(version.substring(0, version.lastIndexOf('.') + 1).replace(".", "\\.") + ".*");
final Optional<String> latestVersion = schemas.keySet().stream().filter(v -> versionPattern.matcher(v).matches()).max(VersionMatcher.COMPARATOR);

if (latestVersion.isPresent()) {
LOG.warnf("The schema version [%s] is not supported. The latest supported version is [%s], some features might be ignored.", version, latestVersion.get());
return schemas.get(latestVersion.get());
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class ChannelManifestMapperTestCase {

Expand Down Expand Up @@ -67,6 +68,21 @@ public void testReadChannelWithUnknownProperties() {
assertNotNull(channel);
}

@Test
public void testReadChannelWithUnknownMicroVersionFallsBackToLatestParser() {
String yaml = "schemaVersion: 1.0.99999";

ChannelManifest manifest = ChannelManifestMapper.fromString(yaml);
assertNotNull(manifest);
}

@Test
public void testReadChannelWithUnknownMinorVersionReturnsError() {
String yaml = "schemaVersion: 1.999999999.0";

assertThrows(InvalidChannelMetadataException.class, ()->ChannelManifestMapper.fromString(yaml));
}

@Test
public void testWriteRequires() throws Exception {
final ChannelManifest manifest = new ManifestBuilder()
Expand Down

0 comments on commit bec93e5

Please sign in to comment.