From 11fedf391b3f0d747f5b002ea60276308c83c861 Mon Sep 17 00:00:00 2001 From: bnasslahsen Date: Tue, 5 May 2020 18:55:56 +0200 Subject: [PATCH] Register model converters only if they are not registered already - fixes #647 #648 --- .../converters/ModelConverterRegistrar.java | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/springdoc-openapi-common/src/main/java/org/springdoc/core/converters/ModelConverterRegistrar.java b/springdoc-openapi-common/src/main/java/org/springdoc/core/converters/ModelConverterRegistrar.java index 34f8a7116..6cf3ef69e 100644 --- a/springdoc-openapi-common/src/main/java/org/springdoc/core/converters/ModelConverterRegistrar.java +++ b/springdoc-openapi-common/src/main/java/org/springdoc/core/converters/ModelConverterRegistrar.java @@ -20,18 +20,24 @@ package org.springdoc.core.converters; -import io.swagger.v3.core.converter.ModelConverter; -import io.swagger.v3.core.converter.ModelConverters; - import java.lang.reflect.Field; import java.util.List; import java.util.Optional; +import io.swagger.v3.core.converter.ModelConverter; +import io.swagger.v3.core.converter.ModelConverters; +import org.apache.commons.lang3.reflect.FieldUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * Wrapper for model converters to only register converters once */ public class ModelConverterRegistrar { + private static final ModelConverters modelConvertersInstance = ModelConverters.getInstance(); + private static final Logger LOGGER = LoggerFactory.getLogger(ModelConverterRegistrar.class); + /** * @param modelConverters spring registered model converter beans which have to be * registered in {@link ModelConverters} instance @@ -39,8 +45,6 @@ public class ModelConverterRegistrar { public ModelConverterRegistrar(List modelConverters) { for (ModelConverter modelConverter : modelConverters) { Optional registeredConverterOptional = getRegisteredConverterSameAs(modelConverter); - - ModelConverters modelConvertersInstance = ModelConverters.getInstance(); registeredConverterOptional.ifPresent(modelConvertersInstance::removeConverter); modelConvertersInstance.addConverter(modelConverter); } @@ -48,23 +52,22 @@ public ModelConverterRegistrar(List modelConverters) { private Optional getRegisteredConverterSameAs(ModelConverter modelConverter) { try { - Field convertersField = ModelConverters.class.getDeclaredField("converters"); - ModelConverters modelConvertersInstance = ModelConverters.getInstance(); - convertersField.setAccessible(true); + Field convertersField = FieldUtils.getDeclaredField(ModelConverters.class, "converters", true); List modelConverters = (List) convertersField.get(modelConvertersInstance); return modelConverters.stream() .filter(registeredModelConverter -> isSameConverter(registeredModelConverter, modelConverter)) .findFirst(); - } catch (NoSuchFieldException | IllegalAccessException exception) { + } + catch (IllegalAccessException exception) { + LOGGER.error(exception.getMessage(), exception); throw new RuntimeException(exception); } } private boolean isSameConverter(ModelConverter modelConverter1, ModelConverter modelConverter2) { - // for now we are comparing using the converter types which may not be what we want + // comparing by the converter type Class modelConverter1Class = modelConverter1.getClass(); Class modelConverter2Class = modelConverter2.getClass(); - return modelConverter1Class.equals(modelConverter2Class); } }