44import com .fasterxml .jackson .databind .JsonNode ;
55import com .fasterxml .jackson .databind .ObjectMapper ;
66import com .fasterxml .jackson .databind .SerializationFeature ;
7+ import com .fasterxml .jackson .dataformat .yaml .YAMLMapper ;
78import com .fasterxml .jackson .datatype .jsr310 .JavaTimeModule ;
89import io .swagger .v3 .core .util .Yaml ;
910import io .swagger .v3 .core .util .Json ;
3637public class DeserializationUtils {
3738
3839 public static class Options {
39- private Integer maxYamlDepth = System .getProperty ("maxYamlDepth" ) == null ? 2000 : Integer .valueOf (System .getProperty ("maxYamlDepth" ));
40- private Long maxYamlReferences = System .getProperty ("maxYamlReferences" ) == null ? 10000000L : Long .valueOf (System .getProperty ("maxYamlReferences" ));
41- private boolean validateYamlInput = System .getProperty ("validateYamlInput" ) == null ? true : Boolean .valueOf (System .getProperty ("validateYamlInput" ));
42- private boolean yamlCycleCheck = System .getProperty ("yamlCycleCheck" ) == null ? true : Boolean .valueOf (System .getProperty ("yamlCycleCheck" ));
43- private Integer maxYamlCodePoints = System .getProperty ("maxYamlCodePoints" ) == null ? 3 * 1024 * 1024 : Integer .valueOf (System .getProperty ("maxYamlCodePoints" ));
44-
45- private Integer maxYamlAliasesForCollections = System .getProperty ("maxYamlAliasesForCollections" ) == null ? Integer .MAX_VALUE : Integer .valueOf (System .getProperty ("maxYamlAliasesForCollections" ));
46- private boolean yamlAllowRecursiveKeys = System .getProperty ("yamlAllowRecursiveKeys" ) == null ? true : Boolean .valueOf (System .getProperty ("yamlAllowRecursiveKeys" ));
40+ private Integer maxYamlDepth = System .getProperty ("maxYamlDepth" ) == null ? 2000 : Integer .parseInt (System .getProperty ("maxYamlDepth" ));
41+ private Long maxYamlReferences = System .getProperty ("maxYamlReferences" ) == null ? 10000000L : Long .parseLong (System .getProperty ("maxYamlReferences" ));
42+ private boolean validateYamlInput = System .getProperty ("validateYamlInput" ) == null || Boolean .parseBoolean (System .getProperty ("validateYamlInput" ));
43+ private boolean yamlCycleCheck = System .getProperty ("yamlCycleCheck" ) == null || Boolean .parseBoolean (System .getProperty ("yamlCycleCheck" ));
44+ private Integer maxYamlCodePoints = System .getProperty ("maxYamlCodePoints" ) == null ? 3 * 1024 * 1024 : Integer .parseInt (System .getProperty ("maxYamlCodePoints" ));
45+ private Integer maxYamlAliasesForCollections = System .getProperty ("maxYamlAliasesForCollections" ) == null ? Integer .MAX_VALUE : Integer .parseInt (System .getProperty ("maxYamlAliasesForCollections" ));
46+ private boolean yamlAllowRecursiveKeys = System .getProperty ("yamlAllowRecursiveKeys" ) == null || Boolean .parseBoolean (System .getProperty ("yamlAllowRecursiveKeys" ));
4747
4848
4949 public Integer getMaxYamlDepth () {
@@ -113,6 +113,26 @@ public static Options getOptions() {
113113
114114 private static final ObjectMapper JSON_MAPPER_FOR_YAML = new ObjectMapper ();
115115
116+ private static ObjectMapper yaml30Mapper = Yaml .mapper ();
117+
118+ public static void setYaml30Mapper (ObjectMapper yaml30Mapper ) {
119+ DeserializationUtils .yaml30Mapper = yaml30Mapper ;
120+ }
121+
122+ public static ObjectMapper getYaml30Mapper () {
123+ return yaml30Mapper ;
124+ }
125+
126+ private static ObjectMapper yaml31Mapper = Yaml31 .mapper ();
127+
128+ public static void setYaml31Mapper (ObjectMapper yaml31Mapper ) {
129+ DeserializationUtils .yaml31Mapper = yaml31Mapper ;
130+ }
131+
132+ public static ObjectMapper getYaml31Mapper () {
133+ return yaml31Mapper ;
134+ }
135+
116136 public static class CustomResolver extends Resolver {
117137
118138 /*
@@ -128,6 +148,7 @@ protected void addImplicitResolvers() {
128148 // addImplicitResolver(Tag.TIMESTAMP, TIMESTAMP, "0123456789");
129149 }
130150 }
151+
131152 static {
132153 JSON_MAPPER_FOR_YAML .registerModule (new JavaTimeModule ());
133154 JSON_MAPPER_FOR_YAML .configure (SerializationFeature .FAIL_ON_EMPTY_BEANS , false );
@@ -179,9 +200,9 @@ public static <T> T deserialize(Object contents, String fileOrHost, Class<T> exp
179200 }
180201 } else {
181202 if (openapi31 ) {
182- mapper = Yaml31 . mapper ();
203+ mapper = getYaml31Mapper ();
183204 } else {
184- mapper = Yaml . mapper ();
205+ mapper = getYaml30Mapper ();
185206 }
186207 }
187208 result = mapper .readValue ((String ) contents , expectedType );
@@ -197,7 +218,7 @@ public static <T> T deserialize(Object contents, String fileOrHost, Class<T> exp
197218 }
198219
199220 public static boolean isJson (String contents ) {
200- return contents .toString (). trim ().startsWith ("{" );
221+ return contents .trim ().startsWith ("{" );
201222 }
202223
203224 public static LoaderOptions buildLoaderOptions () {
@@ -239,7 +260,7 @@ public static JsonNode readYamlTree(String contents, ParseOptions parseOptions,
239260 boolean res = exceedsLimits (o , null , new Integer (0 ), new IdentityHashMap <Object , Long >(), deserializationUtilsResult );
240261 if (res ) {
241262 LOGGER .warn ("Error converting snake-parsed yaml to JsonNode" );
242- return Yaml . mapper ().readTree (contents );
263+ return getYaml30Mapper ().readTree (contents );
243264 }
244265 }
245266 try {
0 commit comments