From c9223f30768f076901320f1377d0ee75ee391f5e Mon Sep 17 00:00:00 2001 From: Calem Roelofs Date: Wed, 24 May 2023 10:21:21 +0200 Subject: [PATCH 1/3] Extend DeserializationUtils methods to enable providing a custom YAMLMapper --- .../v3/parser/util/DeserializationUtils.java | 45 ++++++++++++++----- 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/DeserializationUtils.java b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/DeserializationUtils.java index 005d74610b..a6b3f446de 100644 --- a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/DeserializationUtils.java +++ b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/DeserializationUtils.java @@ -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.YAMLMapper; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import io.swagger.v3.core.util.Yaml; import io.swagger.v3.core.util.Json; @@ -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() { @@ -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(ObjectMapper yaml30Mapper) { + DeserializationUtils.yaml30Mapper = yaml30Mapper; + } + + public static ObjectMapper getYaml30Mapper() { + return yaml30Mapper; + } + + private static ObjectMapper yaml31Mapper = Yaml31.mapper(); + + public static void setYaml31Mapper(ObjectMapper yaml31Mapper) { + DeserializationUtils.yaml31Mapper = yaml31Mapper; + } + + public static ObjectMapper getYaml31Mapper() { + return yaml31Mapper; + } + public static class CustomResolver extends Resolver { /* @@ -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); @@ -179,9 +200,9 @@ public static T deserialize(Object contents, String fileOrHost, Class exp } } else { if (openapi31) { - mapper = Yaml31.mapper(); + mapper = getYaml31Mapper(); } else { - mapper = Yaml.mapper(); + mapper = getYaml30Mapper(); } } result = mapper.readValue((String) contents, expectedType); @@ -197,7 +218,7 @@ public static T deserialize(Object contents, String fileOrHost, Class exp } public static boolean isJson(String contents) { - return contents.toString().trim().startsWith("{"); + return contents.trim().startsWith("{"); } public static LoaderOptions buildLoaderOptions() { @@ -239,7 +260,7 @@ public static JsonNode readYamlTree(String contents, ParseOptions parseOptions, boolean res = exceedsLimits(o, null, new Integer(0), new IdentityHashMap(), deserializationUtilsResult); if (res) { LOGGER.warn("Error converting snake-parsed yaml to JsonNode"); - return Yaml.mapper().readTree(contents); + return getYaml30Mapper().readTree(contents); } } try { From e0e9219b9d9e1a49e707d69ad9ed96e2ad89207e Mon Sep 17 00:00:00 2001 From: Calem Roelofs Date: Wed, 24 May 2023 10:37:03 +0200 Subject: [PATCH 2/3] Clean up imports --- .../java/io/swagger/v3/parser/util/DeserializationUtils.java | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/DeserializationUtils.java b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/DeserializationUtils.java index a6b3f446de..ff9b05d0c8 100644 --- a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/DeserializationUtils.java +++ b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/DeserializationUtils.java @@ -4,7 +4,6 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; -import com.fasterxml.jackson.dataformat.yaml.YAMLMapper; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import io.swagger.v3.core.util.Yaml; import io.swagger.v3.core.util.Json; From 45f25e3d413ace3d15932f91d5ae58e7d9d1b8ea Mon Sep 17 00:00:00 2001 From: Calem Roelofs Date: Fri, 26 May 2023 10:58:47 +0200 Subject: [PATCH 3/3] Switch from providing mapper to factory --- .../io/swagger/v3/parser/util/DeserializationUtils.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/DeserializationUtils.java b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/DeserializationUtils.java index ff9b05d0c8..06fbeaea8a 100644 --- a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/DeserializationUtils.java +++ b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/DeserializationUtils.java @@ -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; @@ -114,8 +115,8 @@ public static Options getOptions() { private static ObjectMapper yaml30Mapper = Yaml.mapper(); - public static void setYaml30Mapper(ObjectMapper yaml30Mapper) { - DeserializationUtils.yaml30Mapper = yaml30Mapper; + public static void setYaml30Mapper(YAMLFactory yamlFactory) { + DeserializationUtils.yaml30Mapper = io.swagger.v3.core.util.ObjectMapperFactory.createYaml(yamlFactory); } public static ObjectMapper getYaml30Mapper() { @@ -124,8 +125,8 @@ public static ObjectMapper getYaml30Mapper() { private static ObjectMapper yaml31Mapper = Yaml31.mapper(); - public static void setYaml31Mapper(ObjectMapper yaml31Mapper) { - DeserializationUtils.yaml31Mapper = yaml31Mapper; + public static void setYaml31Mapper(YAMLFactory yamlFactory) { + DeserializationUtils.yaml31Mapper = io.swagger.v3.core.util.ObjectMapperFactory.createYaml31(yamlFactory); } public static ObjectMapper getYaml31Mapper() {