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

parser-v3: Extend DeserializationUtils to enable providing a custom YAML Mapper #1934

Merged
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 @@ -4,6 +4,7 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import io.swagger.v3.core.util.Yaml;
import io.swagger.v3.core.util.Json;
Expand Down Expand Up @@ -36,14 +37,13 @@
public class DeserializationUtils {

public static class Options {
private Integer maxYamlDepth = System.getProperty("maxYamlDepth") == null ? 2000 : Integer.valueOf(System.getProperty("maxYamlDepth"));
private Long maxYamlReferences = System.getProperty("maxYamlReferences") == null ? 10000000L : Long.valueOf(System.getProperty("maxYamlReferences"));
private boolean validateYamlInput = System.getProperty("validateYamlInput") == null ? true : Boolean.valueOf(System.getProperty("validateYamlInput"));
private boolean yamlCycleCheck = System.getProperty("yamlCycleCheck") == null ? true : Boolean.valueOf(System.getProperty("yamlCycleCheck"));
private Integer maxYamlCodePoints = System.getProperty("maxYamlCodePoints") == null ? 3 * 1024 * 1024 : Integer.valueOf(System.getProperty("maxYamlCodePoints"));

private Integer maxYamlAliasesForCollections = System.getProperty("maxYamlAliasesForCollections") == null ? Integer.MAX_VALUE : Integer.valueOf(System.getProperty("maxYamlAliasesForCollections"));
private boolean yamlAllowRecursiveKeys = System.getProperty("yamlAllowRecursiveKeys") == null ? true : Boolean.valueOf(System.getProperty("yamlAllowRecursiveKeys"));
private Integer maxYamlDepth = System.getProperty("maxYamlDepth") == null ? 2000 : Integer.parseInt(System.getProperty("maxYamlDepth"));
private Long maxYamlReferences = System.getProperty("maxYamlReferences") == null ? 10000000L : Long.parseLong(System.getProperty("maxYamlReferences"));
private boolean validateYamlInput = System.getProperty("validateYamlInput") == null || Boolean.parseBoolean(System.getProperty("validateYamlInput"));
private boolean yamlCycleCheck = System.getProperty("yamlCycleCheck") == null || Boolean.parseBoolean(System.getProperty("yamlCycleCheck"));
private Integer maxYamlCodePoints = System.getProperty("maxYamlCodePoints") == null ? 3 * 1024 * 1024 : Integer.parseInt(System.getProperty("maxYamlCodePoints"));
private Integer maxYamlAliasesForCollections = System.getProperty("maxYamlAliasesForCollections") == null ? Integer.MAX_VALUE : Integer.parseInt(System.getProperty("maxYamlAliasesForCollections"));
private boolean yamlAllowRecursiveKeys = System.getProperty("yamlAllowRecursiveKeys") == null || Boolean.parseBoolean(System.getProperty("yamlAllowRecursiveKeys"));


public Integer getMaxYamlDepth() {
Expand Down Expand Up @@ -113,6 +113,26 @@ public static Options getOptions() {

private static final ObjectMapper JSON_MAPPER_FOR_YAML = new ObjectMapper();

private static ObjectMapper yaml30Mapper = Yaml.mapper();

public static void setYaml30Mapper(YAMLFactory yamlFactory) {
DeserializationUtils.yaml30Mapper = io.swagger.v3.core.util.ObjectMapperFactory.createYaml(yamlFactory);
}

public static ObjectMapper getYaml30Mapper() {
return yaml30Mapper;
}

private static ObjectMapper yaml31Mapper = Yaml31.mapper();

public static void setYaml31Mapper(YAMLFactory yamlFactory) {
DeserializationUtils.yaml31Mapper = io.swagger.v3.core.util.ObjectMapperFactory.createYaml31(yamlFactory);
}

public static ObjectMapper getYaml31Mapper() {
return yaml31Mapper;
}

public static class CustomResolver extends Resolver {

/*
Expand All @@ -128,6 +148,7 @@ protected void addImplicitResolvers() {
// addImplicitResolver(Tag.TIMESTAMP, TIMESTAMP, "0123456789");
}
}

static {
JSON_MAPPER_FOR_YAML.registerModule(new JavaTimeModule());
JSON_MAPPER_FOR_YAML.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
Expand Down Expand Up @@ -179,9 +200,9 @@ public static <T> T deserialize(Object contents, String fileOrHost, Class<T> exp
}
} else {
if (openapi31) {
mapper = Yaml31.mapper();
mapper = getYaml31Mapper();
} else {
mapper = Yaml.mapper();
mapper = getYaml30Mapper();
}
}
result = mapper.readValue((String) contents, expectedType);
Expand All @@ -197,7 +218,7 @@ public static <T> T deserialize(Object contents, String fileOrHost, Class<T> exp
}

public static boolean isJson(String contents) {
return contents.toString().trim().startsWith("{");
return contents.trim().startsWith("{");
}

public static LoaderOptions buildLoaderOptions() {
Expand Down Expand Up @@ -239,7 +260,7 @@ public static JsonNode readYamlTree(String contents, ParseOptions parseOptions,
boolean res = exceedsLimits(o, null, new Integer(0), new IdentityHashMap<Object, Long>(), deserializationUtilsResult);
if (res) {
LOGGER.warn("Error converting snake-parsed yaml to JsonNode");
return Yaml.mapper().readTree(contents);
return getYaml30Mapper().readTree(contents);
}
}
try {
Expand Down