Skip to content

Commit

Permalink
Added possibility to create ObjectMapper with custom Yaml/Json Factor…
Browse files Browse the repository at this point in the history
…y 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
  • Loading branch information
MateuszPol authored Apr 28, 2023
1 parent 562e65d commit 0e54312
Show file tree
Hide file tree
Showing 5 changed files with 811 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,30 @@

public class ObjectMapperFactory {

protected static ObjectMapper createJson() {
public static ObjectMapper createJson(JsonFactory jsonFactory) {
return create(jsonFactory, true, true);
}
public static ObjectMapper createJson() {
return createJson(true, true);
}

protected static ObjectMapper createJson(boolean includePathDeserializer, boolean includeResponseDeserializer) {
public static ObjectMapper createJson(boolean includePathDeserializer, boolean includeResponseDeserializer) {
return create(null, includePathDeserializer, includeResponseDeserializer);
}

protected static ObjectMapper createYaml() {
public static ObjectMapper createYaml(YAMLFactory yamlFactory) {
return create(yamlFactory, true, true);
}

public static ObjectMapper createYaml() {
return createYaml(true, true);
}

protected static ObjectMapper createYaml(boolean includePathDeserializer, boolean includeResponseDeserializer) {
public static ObjectMapper createYaml(boolean includePathDeserializer, boolean includeResponseDeserializer) {
return create(new YAMLFactory(), includePathDeserializer, includeResponseDeserializer);
}

private static ObjectMapper create(JsonFactory jsonFactory, boolean includePathDeserializer, boolean includeResponseDeserializer) {
public static ObjectMapper create(JsonFactory jsonFactory, boolean includePathDeserializer, boolean includeResponseDeserializer) {
ObjectMapper mapper = jsonFactory == null ? new ObjectMapper() : new ObjectMapper(jsonFactory);

mapper.registerModule(new SimpleModule() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.swagger.util;


import com.fasterxml.jackson.core.JsonFactory;
import io.swagger.matchers.SerializationMatchers;
import io.swagger.models.*;
import org.testng.annotations.Test;

Expand Down Expand Up @@ -66,4 +68,18 @@ public void testSerializeSecurityRequirement_UsingSpecCompliantMethods() throws
json = Json.mapper().writeValueAsString(swagger);
assertEquals(json, "{\"swagger\":\"2.0\",\"security\":[{\"api_key\":[],\"basic_auth\":[]},{\"oauth2\":[\"hello\",\"world\"]}]}");
}

@Test
public void testSerializeWithCustomFactory() throws Exception {
// given
JsonFactory jsonFactory = new JsonFactory();
final String json = ResourceUtils.loadClassResource(getClass(), "specFiles/petstore.json");
final String expectedJson = ResourceUtils.loadClassResource(getClass(), "specFiles/jsonSerialization-expected-petstore.json");

// when
Swagger deser = ObjectMapperFactory.createJson(jsonFactory).readValue(json, Swagger.class);

// then
SerializationMatchers.assertEqualsToJson(deser, expectedJson);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package io.swagger.util;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.dataformat.yaml.JacksonYAMLParseException;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import io.swagger.matchers.SerializationMatchers;
import io.swagger.models.Swagger;
import org.testng.annotations.Test;
import org.yaml.snakeyaml.LoaderOptions;

public class YamlSerializationTest {

@Test
public void testSerializeYAMLWithCustomFactory() throws Exception {
// given
LoaderOptions loaderOptions = new LoaderOptions();
loaderOptions.setCodePointLimit(5 * 1024 * 1024);
YAMLFactory yamlFactory = YAMLFactory.builder()
.loaderOptions(loaderOptions)
.build();
final String yaml = ResourceUtils.loadClassResource(getClass(), "specFiles/petstore.yaml");

// when
Swagger swagger = ObjectMapperFactory.createYaml(yamlFactory).readValue(yaml, Swagger.class);

// then
SerializationMatchers.assertEqualsToYaml(swagger, yaml);
}

@Test(expectedExceptions = JacksonYAMLParseException.class)
public void testSerializeYAMLWithCustomFactoryAndCodePointLimitReached() throws Exception {
// given
LoaderOptions loaderOptions = new LoaderOptions();
loaderOptions.setCodePointLimit(1);
YAMLFactory yamlFactory = YAMLFactory.builder()
.loaderOptions(loaderOptions)
.build();
final String yaml = ResourceUtils.loadClassResource(getClass(), "specFiles/petstore.yaml");

// when
Swagger swagger = ObjectMapperFactory.createYaml(yamlFactory).readValue(yaml, Swagger.class);

// then - Throw JacksonYAMLParseException
}
}
Loading

0 comments on commit 0e54312

Please sign in to comment.