Skip to content

Commit 0e54312

Browse files
authored
Added possibility to create ObjectMapper with custom Yaml/Json Factory class (#4397)
* Added possibility to create ObjectMapper using ObjectMapperFactory with custom Yaml/Json Factory class. * Changed access modifiers to public in all methods of ObjectMapperFactory
1 parent 562e65d commit 0e54312

File tree

5 files changed

+811
-5
lines changed

5 files changed

+811
-5
lines changed

modules/swagger-core/src/main/java/io/swagger/util/ObjectMapperFactory.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,30 @@
2323

2424
public class ObjectMapperFactory {
2525

26-
protected static ObjectMapper createJson() {
26+
public static ObjectMapper createJson(JsonFactory jsonFactory) {
27+
return create(jsonFactory, true, true);
28+
}
29+
public static ObjectMapper createJson() {
2730
return createJson(true, true);
2831
}
2932

30-
protected static ObjectMapper createJson(boolean includePathDeserializer, boolean includeResponseDeserializer) {
33+
public static ObjectMapper createJson(boolean includePathDeserializer, boolean includeResponseDeserializer) {
3134
return create(null, includePathDeserializer, includeResponseDeserializer);
3235
}
3336

34-
protected static ObjectMapper createYaml() {
37+
public static ObjectMapper createYaml(YAMLFactory yamlFactory) {
38+
return create(yamlFactory, true, true);
39+
}
40+
41+
public static ObjectMapper createYaml() {
3542
return createYaml(true, true);
3643
}
3744

38-
protected static ObjectMapper createYaml(boolean includePathDeserializer, boolean includeResponseDeserializer) {
45+
public static ObjectMapper createYaml(boolean includePathDeserializer, boolean includeResponseDeserializer) {
3946
return create(new YAMLFactory(), includePathDeserializer, includeResponseDeserializer);
4047
}
4148

42-
private static ObjectMapper create(JsonFactory jsonFactory, boolean includePathDeserializer, boolean includeResponseDeserializer) {
49+
public static ObjectMapper create(JsonFactory jsonFactory, boolean includePathDeserializer, boolean includeResponseDeserializer) {
4350
ObjectMapper mapper = jsonFactory == null ? new ObjectMapper() : new ObjectMapper(jsonFactory);
4451

4552
mapper.registerModule(new SimpleModule() {

modules/swagger-core/src/test/java/io/swagger/util/JsonSerializationTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package io.swagger.util;
22

33

4+
import com.fasterxml.jackson.core.JsonFactory;
5+
import io.swagger.matchers.SerializationMatchers;
46
import io.swagger.models.*;
57
import org.testng.annotations.Test;
68

@@ -66,4 +68,18 @@ public void testSerializeSecurityRequirement_UsingSpecCompliantMethods() throws
6668
json = Json.mapper().writeValueAsString(swagger);
6769
assertEquals(json, "{\"swagger\":\"2.0\",\"security\":[{\"api_key\":[],\"basic_auth\":[]},{\"oauth2\":[\"hello\",\"world\"]}]}");
6870
}
71+
72+
@Test
73+
public void testSerializeWithCustomFactory() throws Exception {
74+
// given
75+
JsonFactory jsonFactory = new JsonFactory();
76+
final String json = ResourceUtils.loadClassResource(getClass(), "specFiles/petstore.json");
77+
final String expectedJson = ResourceUtils.loadClassResource(getClass(), "specFiles/jsonSerialization-expected-petstore.json");
78+
79+
// when
80+
Swagger deser = ObjectMapperFactory.createJson(jsonFactory).readValue(json, Swagger.class);
81+
82+
// then
83+
SerializationMatchers.assertEqualsToJson(deser, expectedJson);
84+
}
6985
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package io.swagger.util;
2+
3+
import com.fasterxml.jackson.core.JsonFactory;
4+
import com.fasterxml.jackson.dataformat.yaml.JacksonYAMLParseException;
5+
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
6+
import io.swagger.matchers.SerializationMatchers;
7+
import io.swagger.models.Swagger;
8+
import org.testng.annotations.Test;
9+
import org.yaml.snakeyaml.LoaderOptions;
10+
11+
public class YamlSerializationTest {
12+
13+
@Test
14+
public void testSerializeYAMLWithCustomFactory() throws Exception {
15+
// given
16+
LoaderOptions loaderOptions = new LoaderOptions();
17+
loaderOptions.setCodePointLimit(5 * 1024 * 1024);
18+
YAMLFactory yamlFactory = YAMLFactory.builder()
19+
.loaderOptions(loaderOptions)
20+
.build();
21+
final String yaml = ResourceUtils.loadClassResource(getClass(), "specFiles/petstore.yaml");
22+
23+
// when
24+
Swagger swagger = ObjectMapperFactory.createYaml(yamlFactory).readValue(yaml, Swagger.class);
25+
26+
// then
27+
SerializationMatchers.assertEqualsToYaml(swagger, yaml);
28+
}
29+
30+
@Test(expectedExceptions = JacksonYAMLParseException.class)
31+
public void testSerializeYAMLWithCustomFactoryAndCodePointLimitReached() throws Exception {
32+
// given
33+
LoaderOptions loaderOptions = new LoaderOptions();
34+
loaderOptions.setCodePointLimit(1);
35+
YAMLFactory yamlFactory = YAMLFactory.builder()
36+
.loaderOptions(loaderOptions)
37+
.build();
38+
final String yaml = ResourceUtils.loadClassResource(getClass(), "specFiles/petstore.yaml");
39+
40+
// when
41+
Swagger swagger = ObjectMapperFactory.createYaml(yamlFactory).readValue(yaml, Swagger.class);
42+
43+
// then - Throw JacksonYAMLParseException
44+
}
45+
}

0 commit comments

Comments
 (0)