From 516610bfd1803e88a5630d5f4680fb2e7c08ed40 Mon Sep 17 00:00:00 2001 From: frantuma Date: Mon, 23 Sep 2024 13:10:06 +0200 Subject: [PATCH] Make Jackson ObjectMapper initialization thread safe --- .../java/io/swagger/v3/core/util/Json31.java | 19 +++++++++---------- .../java/io/swagger/v3/core/util/Yaml.java | 10 +++++----- .../java/io/swagger/v3/core/util/Yaml31.java | 11 ++++++----- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/modules/swagger-core/src/main/java/io/swagger/v3/core/util/Json31.java b/modules/swagger-core/src/main/java/io/swagger/v3/core/util/Json31.java index 16a883c090..0ac52783a8 100644 --- a/modules/swagger-core/src/main/java/io/swagger/v3/core/util/Json31.java +++ b/modules/swagger-core/src/main/java/io/swagger/v3/core/util/Json31.java @@ -13,23 +13,22 @@ public class Json31 { - private static ObjectMapper mapper; - private static ObjectMapper converterMapper; + private static final class ObjectMapperHolder { + private static final ObjectMapper MAPPER = ObjectMapperFactory.createJson31(); + } + + private static final class ConverterMapperHolder { + private static final ObjectMapper MAPPER = ObjectMapperFactory.createJsonConverter(); + } static Logger LOGGER = LoggerFactory.getLogger(Json31.class); public static ObjectMapper mapper() { - if (mapper == null) { - mapper = ObjectMapperFactory.createJson31(); - } - return mapper; + return ObjectMapperHolder.MAPPER; } public static ObjectMapper converterMapper() { - if (converterMapper == null) { - converterMapper = ObjectMapperFactory.createJsonConverter(); - } - return converterMapper; + return ConverterMapperHolder.MAPPER; } public static ObjectWriter pretty() { diff --git a/modules/swagger-core/src/main/java/io/swagger/v3/core/util/Yaml.java b/modules/swagger-core/src/main/java/io/swagger/v3/core/util/Yaml.java index eaef8709fb..2a77b70305 100644 --- a/modules/swagger-core/src/main/java/io/swagger/v3/core/util/Yaml.java +++ b/modules/swagger-core/src/main/java/io/swagger/v3/core/util/Yaml.java @@ -5,13 +5,13 @@ import com.fasterxml.jackson.databind.ObjectWriter; public class Yaml { - static ObjectMapper mapper; + + private static final class ObjectMapperHolder { + private static final ObjectMapper MAPPER = ObjectMapperFactory.createYaml(); + } public static ObjectMapper mapper() { - if (mapper == null) { - mapper = ObjectMapperFactory.createYaml(); - } - return mapper; + return ObjectMapperHolder.MAPPER; } public static ObjectWriter pretty() { diff --git a/modules/swagger-core/src/main/java/io/swagger/v3/core/util/Yaml31.java b/modules/swagger-core/src/main/java/io/swagger/v3/core/util/Yaml31.java index ba16433896..75d9819a84 100644 --- a/modules/swagger-core/src/main/java/io/swagger/v3/core/util/Yaml31.java +++ b/modules/swagger-core/src/main/java/io/swagger/v3/core/util/Yaml31.java @@ -11,15 +11,16 @@ import java.util.Map; public class Yaml31 { - static ObjectMapper mapper; + + private static final class ObjectMapperHolder { + private static final ObjectMapper MAPPER = ObjectMapperFactory.createYaml31(); + } + static Logger LOGGER = LoggerFactory.getLogger(Yaml31.class); public static ObjectMapper mapper() { - if (mapper == null) { - mapper = ObjectMapperFactory.createYaml31(); - } - return mapper; + return ObjectMapperHolder.MAPPER; } public static ObjectWriter pretty() {