Skip to content

Commit 578ee78

Browse files
authored
Replace Jackson DataformatYaml with SnakeYaml (#205)
1 parent d88b270 commit 578ee78

5 files changed

Lines changed: 62 additions & 38 deletions

File tree

pom.xml

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
<aerospike-client-jdk8.version>9.0.5</aerospike-client-jdk8.version>
3333
<aerospike-reactor.version>9.0.5</aerospike-reactor.version>
3434
<commons-lang3.version>3.18.0</commons-lang3.version>
35-
<jackson-dataformat-yaml.version>2.19.1</jackson-dataformat-yaml.version>
35+
<snakeyaml.version>2.6</snakeyaml.version>
36+
<jackson-databind.version>2.21.2</jackson-databind.version>
3637
<lombok.version>1.18.38</lombok.version>
3738
<reactor-test.version>3.7.7</reactor-test.version>
3839
<junit-jupiter.version>5.11.4</junit-jupiter.version>
@@ -101,12 +102,18 @@
101102
<version>${commons-lang3.version}</version>
102103
</dependency>
103104

104-
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.dataformat/jackson-dataformat-yaml -->
105-
<!-- Needed to parse the options file -->
106-
<dependency>
107-
<groupId>com.fasterxml.jackson.dataformat</groupId>
108-
<artifactId>jackson-dataformat-yaml</artifactId>
109-
<version>${jackson-dataformat-yaml.version}</version>
105+
<dependency>
106+
<groupId>org.yaml</groupId>
107+
<artifactId>snakeyaml</artifactId>
108+
<version>${snakeyaml.version}</version>
109+
</dependency>
110+
111+
<!-- Jackson databind for JSON comparison in tests -->
112+
<dependency>
113+
<groupId>com.fasterxml.jackson.core</groupId>
114+
<artifactId>jackson-databind</artifactId>
115+
<version>${jackson-databind.version}</version>
116+
<scope>test</scope>
110117
</dependency>
111118

112119
<dependency>

src/main/java/com/aerospike/mapper/tools/AbstractBuilder.java

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.BufferedReader;
44
import java.io.File;
5+
import java.io.FileInputStream;
56
import java.io.IOException;
67
import java.io.InputStream;
78
import java.io.InputStreamReader;
@@ -23,12 +24,14 @@
2324
import com.aerospike.client.policy.ScanPolicy;
2425
import com.aerospike.mapper.annotations.AerospikeRecord;
2526
import com.aerospike.mapper.tools.ClassCache.PolicyType;
27+
import com.aerospike.mapper.tools.configuration.BinConfig;
2628
import com.aerospike.mapper.tools.configuration.ClassConfig;
2729
import com.aerospike.mapper.tools.configuration.Configuration;
2830
import com.aerospike.mapper.tools.utils.TypeUtils;
29-
import com.fasterxml.jackson.core.JsonProcessingException;
30-
import com.fasterxml.jackson.databind.ObjectMapper;
31-
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
31+
import org.yaml.snakeyaml.LoaderOptions;
32+
import org.yaml.snakeyaml.TypeDescription;
33+
import org.yaml.snakeyaml.Yaml;
34+
import org.yaml.snakeyaml.constructor.Constructor;
3235

3336
public abstract class AbstractBuilder<T extends IBaseAeroMapper> {
3437
private final T mapper;
@@ -121,34 +124,48 @@ public AbstractBuilder<T> withConfigurationFile(File file) throws IOException {
121124
}
122125

123126
public AbstractBuilder<T> withConfigurationFile(File file, boolean allowsInvalid) throws IOException {
124-
ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
125-
Configuration configuration = objectMapper.readValue(file, Configuration.class);
126-
this.loadConfiguration(configuration, allowsInvalid);
127-
return this;
127+
try (InputStream fis = new FileInputStream(file)) {
128+
return this.withConfigurationFile(fis, allowsInvalid);
129+
}
128130
}
129131

130132
public AbstractBuilder<T> withConfigurationFile(InputStream ios) throws IOException {
131133
return this.withConfigurationFile(ios, false);
132134
}
133135

134136
public AbstractBuilder<T> withConfigurationFile(InputStream ios, boolean allowsInvalid) throws IOException {
135-
ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
136-
Configuration configuration = objectMapper.readValue(ios, Configuration.class);
137+
Yaml yaml = createYamlParser();
138+
Configuration configuration = yaml.load(ios);
137139
this.loadConfiguration(configuration, allowsInvalid);
138140
return this;
139141
}
140142

141-
public AbstractBuilder<T> withConfiguration(String configurationYaml) throws JsonProcessingException {
143+
public AbstractBuilder<T> withConfiguration(String configurationYaml) {
142144
return this.withConfiguration(configurationYaml, false);
143145
}
144146

145-
public AbstractBuilder<T> withConfiguration(String configurationYaml, boolean allowsInvalid) throws JsonProcessingException {
146-
ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
147-
Configuration configuration = objectMapper.readValue(configurationYaml, Configuration.class);
147+
public AbstractBuilder<T> withConfiguration(String configurationYaml, boolean allowsInvalid) {
148+
Yaml yaml = createYamlParser();
149+
Configuration configuration = yaml.load(configurationYaml);
148150
this.loadConfiguration(configuration, allowsInvalid);
149151
return this;
150152
}
151-
153+
154+
private static Yaml createYamlParser() {
155+
Constructor constructor = new Constructor(Configuration.class, new LoaderOptions());
156+
157+
TypeDescription configDesc = new TypeDescription(Configuration.class);
158+
configDesc.addPropertyParameters("classes", ClassConfig.class);
159+
constructor.addTypeDescription(configDesc);
160+
161+
TypeDescription classConfigDesc = new TypeDescription(ClassConfig.class);
162+
classConfigDesc.substituteProperty("class", String.class, "getClassName", "setClassName");
163+
classConfigDesc.addPropertyParameters("bins", BinConfig.class);
164+
constructor.addTypeDescription(classConfigDesc);
165+
166+
return new Yaml(constructor);
167+
}
168+
152169
public AbstractBuilder<T> withClassConfigurations(ClassConfig classConfig, ClassConfig ...classConfigs) {
153170
Configuration configuration = new Configuration();
154171
configuration.add(classConfig);
@@ -161,6 +178,9 @@ public AbstractBuilder<T> withClassConfigurations(ClassConfig classConfig, Class
161178
}
162179

163180
private void loadConfiguration(@NotNull Configuration configuration, boolean allowsInvalid) {
181+
if (configuration == null) {
182+
throw new AerospikeException("YAML configuration is empty or invalid");
183+
}
164184
for (ClassConfig config : configuration.getClasses()) {
165185
try {
166186
String name = config.getClassName();

src/main/java/com/aerospike/mapper/tools/configuration/ClassConfig.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@
99
import com.aerospike.mapper.annotations.AerospikeEmbed;
1010
import com.aerospike.mapper.annotations.AerospikeReference;
1111
import com.aerospike.mapper.tools.ConfigurationUtils;
12-
import com.fasterxml.jackson.annotation.JsonProperty;
1312

1413
public class ClassConfig {
15-
@JsonProperty(value = "class")
1614
private String className;
1715
private String namespace;
1816
private String set;
@@ -124,43 +122,43 @@ public void validate() {
124122
}
125123
}
126124

127-
private void setClassName(String className) {
125+
public void setClassName(String className) {
128126
this.className = className;
129127
}
130128

131-
private void setNamespace(String namespace) {
129+
public void setNamespace(String namespace) {
132130
this.namespace = namespace;
133131
}
134132

135-
private void setSet(String set) {
133+
public void setSet(String set) {
136134
this.set = set;
137135
}
138136

139-
private void setTtl(Integer ttl) {
137+
public void setTtl(Integer ttl) {
140138
this.ttl = ttl;
141139
}
142140

143-
private void setVersion(Integer version) {
141+
public void setVersion(Integer version) {
144142
this.version = version;
145143
}
146144

147-
private void setSendKey(Boolean sendKey) {
145+
public void setSendKey(Boolean sendKey) {
148146
this.sendKey = sendKey;
149147
}
150148

151-
private void setMapAll(Boolean mapAll) {
149+
public void setMapAll(Boolean mapAll) {
152150
this.mapAll = mapAll;
153151
}
154152

155-
private void setDurableDelete(Boolean durableDelete) {
153+
public void setDurableDelete(Boolean durableDelete) {
156154
this.durableDelete = durableDelete;
157155
}
158156

159-
private void setKey(KeyConfig key) {
157+
public void setKey(KeyConfig key) {
160158
this.key = key;
161159
}
162160

163-
private void setShortName(String shortName) {
161+
public void setShortName(String shortName) {
164162
this.shortName = shortName;
165163
}
166164

src/main/java/com/aerospike/mapper/tools/configuration/KeyConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public Boolean getStoreAsBin() {
2424
return storeAsBin;
2525
}
2626

27-
public void setStoreAsBin(boolean value) {
27+
public void setStoreAsBin(Boolean value) {
2828
this.storeAsBin = value;
2929
}
3030

src/test/java/com/aerospike/mapper/BooleanTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import com.aerospike.mapper.annotations.AerospikeKey;
77
import com.aerospike.mapper.annotations.AerospikeRecord;
88
import com.aerospike.mapper.tools.AeroMapper;
9-
import com.fasterxml.jackson.core.JsonProcessingException;
109
import org.junit.jupiter.api.AfterAll;
1110
import org.junit.jupiter.api.Test;
1211

@@ -28,7 +27,7 @@ public static class B {
2827
}
2928

3029
@Test
31-
public void testNumericEncoding() throws JsonProcessingException {
30+
public void testNumericEncoding() {
3231
UseBoolBin = false;
3332

3433
B b = new B();
@@ -57,7 +56,7 @@ public void testNumericEncoding() throws JsonProcessingException {
5756
}
5857

5958
@Test
60-
public void testObjectEncoding() throws JsonProcessingException {
59+
public void testObjectEncoding() {
6160
UseBoolBin = true;
6261

6362
B b = new B();
@@ -85,7 +84,7 @@ public void testObjectEncoding() throws JsonProcessingException {
8584
}
8685

8786
@Test
88-
public void testObjectByDefault() throws JsonProcessingException {
87+
public void testObjectByDefault() {
8988
B b = new B();
9089
b.boolValue = true;
9190
b.id = 1;

0 commit comments

Comments
 (0)