Skip to content

Commit

Permalink
Merge pull request #69 from jmesnil/68_channel_serialization_order
Browse files Browse the repository at this point in the history
[#68] Orders the channel serialization so that `streams` is always last
  • Loading branch information
jmesnil authored Jun 8, 2022
2 parents 0d23d33 + 4a84d2a commit e388583
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
3 changes: 3 additions & 0 deletions core/src/main/java/org/wildfly/channel/Channel.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import org.wildfly.channel.spi.MavenVersionsResolver;
import org.wildfly.channel.version.VersionMatcher;

Expand Down Expand Up @@ -118,6 +119,7 @@ public Channel(String name,
* @param streams the streams defined by the channel - can be {@code null}
*/
@JsonCreator
@JsonPropertyOrder({ "schemaVersion", "name", "description", "vendor", "requires", "streams" })
public Channel(@JsonProperty(value = "schemaVersion", required = true) String schemaVersion,
@JsonProperty(value = "name") String name,
@JsonProperty(value = "description") String description,
Expand Down Expand Up @@ -157,6 +159,7 @@ public Vendor getVendor() {
}

@JsonInclude(NON_EMPTY)
@JsonProperty(value = "requires")
public List<ChannelRequirement> getChannelRequirements() {
return channelRequirements;
}
Expand Down
11 changes: 9 additions & 2 deletions core/src/main/java/org/wildfly/channel/ChannelMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.wildfly.channel;

import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES;
import static com.fasterxml.jackson.databind.SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS;
import static java.util.Collections.singletonList;
import static java.util.Objects.requireNonNull;

Expand All @@ -32,9 +33,13 @@
import java.util.Set;
import java.util.stream.Collectors;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator;
import com.fasterxml.jackson.dataformat.yaml.YAMLParser;
import com.networknt.schema.JsonSchema;
import com.networknt.schema.JsonSchemaFactory;
Expand All @@ -52,9 +57,11 @@ public class ChannelMapper {
public static final String CURRENT_SCHEMA_VERSION = SCHEMA_VERSION_1_0_0;

private static final String SCHEMA_1_0_0_FILE = "org/wildfly/channel/v1.0.0/schema.json";
private static final YAMLFactory YAML_FACTORY = new YAMLFactory();
private static final YAMLFactory YAML_FACTORY = new YAMLFactory()
.configure(YAMLGenerator.Feature.INDENT_ARRAYS_WITH_INDICATOR, true);
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(YAML_FACTORY)
.configure(FAIL_ON_UNKNOWN_PROPERTIES, false);
.configure(FAIL_ON_UNKNOWN_PROPERTIES, false)
.configure(ORDER_MAP_ENTRIES_BY_KEYS, true);
private static final JsonSchemaFactory SCHEMA_FACTORY = JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909)).objectMapper(OBJECT_MAPPER).build();
private static final Map<String, JsonSchema> SCHEMAS = new HashMap<>();

Expand Down
12 changes: 10 additions & 2 deletions core/src/test/java/org/wildfly/channel/ChannelMapperTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
import static org.wildfly.channel.ChannelMapper.CURRENT_SCHEMA_VERSION;

import java.net.URL;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.regex.Pattern;

import org.junit.jupiter.api.Test;

Expand All @@ -39,15 +41,21 @@ public void testWriteReadChannel() throws Exception {

@Test
public void testWriteMultipleChannels() throws Exception {
final Channel channel1 = new Channel("test_name_1", "test_desc", new Vendor("test_vendor_name", Vendor.Support.COMMUNITY), Collections.emptyList(), Collections.emptyList());
final ChannelRequirement req = new ChannelRequirement("org", "foo", "1.2.3");
final Stream stream1 = new Stream("org.bar", "example", "1.2.3");
final Stream stream2 = new Stream("org.bar", "other-example", Pattern.compile("\\.*"));
final Channel channel1 = new Channel("test_name_1", "test_desc", new Vendor("test_vendor_name", Vendor.Support.COMMUNITY), Arrays.asList(req), Arrays.asList(stream1, stream2));
final Channel channel2 = new Channel("test_name_2", "test_desc", new Vendor("test_vendor_name", Vendor.Support.COMMUNITY), Collections.emptyList(), Collections.emptyList());
final String yaml = ChannelMapper.toYaml(channel1, channel2);

System.out.println(yaml);
List<Channel> channels = ChannelMapper.fromString(yaml);
assertEquals(2, channels.size());
final Channel c1 = channels.get(0);
final Channel c2 = channels.get(1);
assertEquals(channel1.getName(), c1.getName());
assertEquals(1, c1.getChannelRequirements().size());
assertEquals("foo", c1.getChannelRequirements().get(0).getArtifactId());
final Channel c2 = channels.get(1);
assertEquals(channel2.getName(), c2.getName());
}

Expand Down

0 comments on commit e388583

Please sign in to comment.