Skip to content

Commit 769941a

Browse files
authored
Added possibility to provide JSON/YAML Factory to initialise ObjectMapper (#4396)
* Added possibility to provide custom JsonFactory/YamlFactory to yaml mapper initializer. * Cleared unused import in ObjectMapperFactory * - Removed mapper methods from Json/Json31/Yaml/Yaml31 classes - Added public methods to ObjectMapperFactory to give possibility to create mapper with custom factory * Redundant space remove in ObjectMapperFactory * Changed access modifiers to public in all methods of ObjectMapperFactory
1 parent 2b7a4f8 commit 769941a

File tree

5 files changed

+489
-13
lines changed

5 files changed

+489
-13
lines changed

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

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,23 @@
7373

7474
public class ObjectMapperFactory {
7575

76-
protected static ObjectMapper createJson() {
76+
public static ObjectMapper createJson(JsonFactory jsonFactory) {
77+
return create(jsonFactory, false);
78+
}
79+
80+
public static ObjectMapper createJson() {
7781
return create(null, false);
7882
}
7983

80-
protected static ObjectMapper createYaml(boolean openapi31) {
84+
public static ObjectMapper createYaml(YAMLFactory yamlFactory) {
85+
return create(yamlFactory, false);
86+
}
87+
88+
public static ObjectMapper createYaml() {
89+
return createYaml(false);
90+
}
91+
92+
public static ObjectMapper createYaml(boolean openapi31) {
8193
YAMLFactory factory = new YAMLFactory();
8294
factory.disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER);
8395
factory.enable(YAMLGenerator.Feature.MINIMIZE_QUOTES);
@@ -87,20 +99,23 @@ protected static ObjectMapper createYaml(boolean openapi31) {
8799
return create(factory, openapi31);
88100
}
89101

90-
protected static ObjectMapper createYaml() {
91-
return createYaml(false);
102+
public static ObjectMapper createJson31(JsonFactory jsonFactory) {
103+
return create(jsonFactory, true);
92104
}
93105

94-
protected static ObjectMapper createJson31() {
106+
public static ObjectMapper createJson31() {
95107
return create(null, true);
96108
}
97109

110+
public static ObjectMapper createYaml31(YAMLFactory yamlFactory) {
111+
return create(yamlFactory, true);
112+
}
98113

99-
protected static ObjectMapper createYaml31() {
114+
public static ObjectMapper createYaml31() {
100115
return createYaml(true);
101116
}
102117

103-
private static ObjectMapper create(JsonFactory jsonFactory, boolean openapi31) {
118+
public static ObjectMapper create(JsonFactory jsonFactory, boolean openapi31) {
104119
ObjectMapper mapper = jsonFactory == null ? new ObjectMapper() : new ObjectMapper(jsonFactory);
105120

106121
if (!openapi31) {
@@ -215,7 +230,7 @@ public JsonSerializer<?> modifySerializer(
215230
return mapper;
216231
}
217232

218-
protected static ObjectMapper createJsonConverter() {
233+
public static ObjectMapper createJsonConverter() {
219234

220235
ObjectMapper mapper = new ObjectMapper();
221236

modules/swagger-core/src/test/java/io/swagger/v3/core/serialization/JsonSerializationTest.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package io.swagger.v3.core.serialization;
22

3+
import com.fasterxml.jackson.core.JsonFactory;
4+
import com.fasterxml.jackson.dataformat.yaml.JacksonYAMLParseException;
5+
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
36
import io.swagger.v3.core.matchers.SerializationMatchers;
47
import io.swagger.v3.core.util.Json;
8+
import io.swagger.v3.core.util.ObjectMapperFactory;
59
import io.swagger.v3.core.util.ResourceUtils;
610
import io.swagger.v3.core.util.Yaml;
711
import io.swagger.v3.oas.models.OpenAPI;
@@ -12,6 +16,7 @@
1216
import io.swagger.v3.oas.models.responses.ApiResponses;
1317
import io.swagger.v3.oas.models.servers.Server;
1418
import org.testng.annotations.Test;
19+
import org.yaml.snakeyaml.LoaderOptions;
1520

1621
import static org.testng.Assert.assertEquals;
1722
import static org.testng.Assert.assertFalse;
@@ -86,4 +91,51 @@ public void testSerializeNullInSchemaExample() throws Exception {
8691
SerializationMatchers.assertEqualsToYaml(deser, yaml);
8792

8893
}
94+
95+
@Test
96+
public void testSerializeJSONWithCustomFactory() throws Exception {
97+
// given
98+
JsonFactory jsonFactory = new JsonFactory();
99+
final String json = ResourceUtils.loadClassResource(getClass(), "specFiles/petstore-3.0.json");
100+
final String expectedJson = ResourceUtils.loadClassResource(getClass(), "specFiles/jsonSerialization-expected-petstore-3.0.json");
101+
102+
// when
103+
OpenAPI deser = ObjectMapperFactory.createJson(jsonFactory).readValue(json, OpenAPI.class);
104+
105+
// then
106+
SerializationMatchers.assertEqualsToJson(deser, expectedJson);
107+
}
108+
109+
@Test
110+
public void testSerializeYAMLWithCustomFactory() throws Exception {
111+
// given
112+
LoaderOptions loaderOptions = new LoaderOptions();
113+
loaderOptions.setCodePointLimit(5 * 1024 * 1024);
114+
YAMLFactory yamlFactory = YAMLFactory.builder()
115+
.loaderOptions(loaderOptions)
116+
.build();
117+
final String yaml = ResourceUtils.loadClassResource(getClass(), "specFiles/null-example.yaml");
118+
119+
// when
120+
OpenAPI deser = ObjectMapperFactory.createYaml(yamlFactory).readValue(yaml, OpenAPI.class);
121+
122+
// then
123+
SerializationMatchers.assertEqualsToYaml(deser, yaml);
124+
}
125+
126+
@Test(expectedExceptions = JacksonYAMLParseException.class)
127+
public void testSerializeYAMLWithCustomFactoryAndCodePointLimitReached() throws Exception {
128+
// given
129+
LoaderOptions loaderOptions = new LoaderOptions();
130+
loaderOptions.setCodePointLimit(1);
131+
YAMLFactory yamlFactory = YAMLFactory.builder()
132+
.loaderOptions(loaderOptions)
133+
.build();
134+
final String yaml = ResourceUtils.loadClassResource(getClass(), "specFiles/null-example.yaml");
135+
136+
// when
137+
OpenAPI deser = ObjectMapperFactory.createYaml(yamlFactory).readValue(yaml, OpenAPI.class);
138+
139+
// then - Throw JacksonYAMLParseException
140+
}
89141
}

modules/swagger-core/src/test/java/io/swagger/v3/core/serialization/OpenAPI3_1SerializationTest.java

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package io.swagger.v3.core.serialization;
22

3+
import com.fasterxml.jackson.core.JsonFactory;
34
import com.fasterxml.jackson.core.util.DefaultIndenter;
5+
import com.fasterxml.jackson.databind.json.JsonMapper;
6+
import com.fasterxml.jackson.dataformat.yaml.JacksonYAMLParseException;
7+
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
48
import io.swagger.v3.core.matchers.SerializationMatchers;
5-
import io.swagger.v3.core.util.Json;
6-
import io.swagger.v3.core.util.Json31;
7-
import io.swagger.v3.core.util.ResourceUtils;
8-
import io.swagger.v3.core.util.Yaml;
9-
import io.swagger.v3.core.util.Yaml31;
9+
import io.swagger.v3.core.util.*;
1010
import io.swagger.v3.oas.models.Components;
1111
import io.swagger.v3.oas.models.OpenAPI;
1212
import io.swagger.v3.oas.models.Operation;
@@ -30,6 +30,7 @@
3030
import io.swagger.v3.oas.models.responses.ApiResponses;
3131
import io.swagger.v3.oas.models.security.SecurityScheme;
3232
import org.testng.annotations.Test;
33+
import org.yaml.snakeyaml.LoaderOptions;
3334

3435
import static org.testng.Assert.assertEquals;
3536
import static org.testng.Assert.assertNotNull;
@@ -351,6 +352,21 @@ public void testSerializePetstore() throws Exception {
351352

352353
}
353354

355+
@Test
356+
public void testJSONSerializePetstoreWithCustomFactory() throws Exception {
357+
358+
//given
359+
final String jsonString = ResourceUtils.loadClassResource(getClass(), "specFiles/3.1.0/petstore-3.1.json");
360+
JsonFactory jsonFactory = new JsonFactory();
361+
362+
//when
363+
final OpenAPI swagger = ObjectMapperFactory.createJson31(jsonFactory).readValue(jsonString, OpenAPI.class);
364+
365+
// then
366+
assertNotNull(swagger);
367+
SerializationMatchers.assertEqualsToJson31(swagger, jsonString);
368+
}
369+
354370
@Test
355371
public void testInfoSerialization() {
356372
OpenAPI openAPI = new OpenAPI()
@@ -1497,6 +1513,22 @@ public void testBooleanAdditionalPropertiesSerialization() throws Exception{
14971513
assertTrue(Boolean.TRUE.equals(openAPI.getComponents().getSchemas().get("test").getAdditionalProperties()));
14981514
}
14991515

1516+
@Test(expectedExceptions = JacksonYAMLParseException.class)
1517+
public void testSerializeYAML31WithCustomFactoryAndCodePointLimitReached() throws Exception {
1518+
// given
1519+
LoaderOptions loaderOptions = new LoaderOptions();
1520+
loaderOptions.setCodePointLimit(1);
1521+
YAMLFactory yamlFactory = YAMLFactory.builder()
1522+
.loaderOptions(loaderOptions)
1523+
.build();
1524+
final String yaml = ResourceUtils.loadClassResource(getClass(), "specFiles/petstore-3.0.yaml");
1525+
1526+
// when
1527+
OpenAPI deser = ObjectMapperFactory.createYaml31(yamlFactory).readValue(yaml, OpenAPI.class);
1528+
1529+
// then - Throw JacksonYAMLParseException
1530+
}
1531+
15001532
private static String withJacksonSystemLineSeparator(String s) {
15011533
return s.replace("\n", DefaultIndenter.SYS_LF);
15021534
}

0 commit comments

Comments
 (0)