Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added possibility to create ObjectMapper with custom Yaml/Json Factory class #4397

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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