Skip to content

Commit

Permalink
Added possibility to provide JSON/YAML Factory to initialise ObjectMa…
Browse files Browse the repository at this point in the history
…pper (#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
  • Loading branch information
MateuszPol authored Apr 28, 2023
1 parent 2b7a4f8 commit 769941a
Show file tree
Hide file tree
Showing 5 changed files with 489 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,23 @@

public class ObjectMapperFactory {

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

public static ObjectMapper createJson() {
return create(null, false);
}

protected static ObjectMapper createYaml(boolean openapi31) {
public static ObjectMapper createYaml(YAMLFactory yamlFactory) {
return create(yamlFactory, false);
}

public static ObjectMapper createYaml() {
return createYaml(false);
}

public static ObjectMapper createYaml(boolean openapi31) {
YAMLFactory factory = new YAMLFactory();
factory.disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER);
factory.enable(YAMLGenerator.Feature.MINIMIZE_QUOTES);
Expand All @@ -87,20 +99,23 @@ protected static ObjectMapper createYaml(boolean openapi31) {
return create(factory, openapi31);
}

protected static ObjectMapper createYaml() {
return createYaml(false);
public static ObjectMapper createJson31(JsonFactory jsonFactory) {
return create(jsonFactory, true);
}

protected static ObjectMapper createJson31() {
public static ObjectMapper createJson31() {
return create(null, true);
}

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

protected static ObjectMapper createYaml31() {
public static ObjectMapper createYaml31() {
return createYaml(true);
}

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

if (!openapi31) {
Expand Down Expand Up @@ -215,7 +230,7 @@ public JsonSerializer<?> modifySerializer(
return mapper;
}

protected static ObjectMapper createJsonConverter() {
public static ObjectMapper createJsonConverter() {

ObjectMapper mapper = new ObjectMapper();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package io.swagger.v3.core.serialization;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.dataformat.yaml.JacksonYAMLParseException;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import io.swagger.v3.core.matchers.SerializationMatchers;
import io.swagger.v3.core.util.Json;
import io.swagger.v3.core.util.ObjectMapperFactory;
import io.swagger.v3.core.util.ResourceUtils;
import io.swagger.v3.core.util.Yaml;
import io.swagger.v3.oas.models.OpenAPI;
Expand All @@ -12,6 +16,7 @@
import io.swagger.v3.oas.models.responses.ApiResponses;
import io.swagger.v3.oas.models.servers.Server;
import org.testng.annotations.Test;
import org.yaml.snakeyaml.LoaderOptions;

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

}

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

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

// then
SerializationMatchers.assertEqualsToJson(deser, expectedJson);
}

@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/null-example.yaml");

// when
OpenAPI deser = ObjectMapperFactory.createYaml(yamlFactory).readValue(yaml, OpenAPI.class);

// then
SerializationMatchers.assertEqualsToYaml(deser, 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/null-example.yaml");

// when
OpenAPI deser = ObjectMapperFactory.createYaml(yamlFactory).readValue(yaml, OpenAPI.class);

// then - Throw JacksonYAMLParseException
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package io.swagger.v3.core.serialization;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.util.DefaultIndenter;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.dataformat.yaml.JacksonYAMLParseException;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import io.swagger.v3.core.matchers.SerializationMatchers;
import io.swagger.v3.core.util.Json;
import io.swagger.v3.core.util.Json31;
import io.swagger.v3.core.util.ResourceUtils;
import io.swagger.v3.core.util.Yaml;
import io.swagger.v3.core.util.Yaml31;
import io.swagger.v3.core.util.*;
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
Expand All @@ -30,6 +30,7 @@
import io.swagger.v3.oas.models.responses.ApiResponses;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.testng.annotations.Test;
import org.yaml.snakeyaml.LoaderOptions;

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

}

@Test
public void testJSONSerializePetstoreWithCustomFactory() throws Exception {

//given
final String jsonString = ResourceUtils.loadClassResource(getClass(), "specFiles/3.1.0/petstore-3.1.json");
JsonFactory jsonFactory = new JsonFactory();

//when
final OpenAPI swagger = ObjectMapperFactory.createJson31(jsonFactory).readValue(jsonString, OpenAPI.class);

// then
assertNotNull(swagger);
SerializationMatchers.assertEqualsToJson31(swagger, jsonString);
}

@Test
public void testInfoSerialization() {
OpenAPI openAPI = new OpenAPI()
Expand Down Expand Up @@ -1497,6 +1513,22 @@ public void testBooleanAdditionalPropertiesSerialization() throws Exception{
assertTrue(Boolean.TRUE.equals(openAPI.getComponents().getSchemas().get("test").getAdditionalProperties()));
}

@Test(expectedExceptions = JacksonYAMLParseException.class)
public void testSerializeYAML31WithCustomFactoryAndCodePointLimitReached() 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-3.0.yaml");

// when
OpenAPI deser = ObjectMapperFactory.createYaml31(yamlFactory).readValue(yaml, OpenAPI.class);

// then - Throw JacksonYAMLParseException
}

private static String withJacksonSystemLineSeparator(String s) {
return s.replace("\n", DefaultIndenter.SYS_LF);
}
Expand Down
Loading

0 comments on commit 769941a

Please sign in to comment.