From 0aecf53c535ee11ff9a9645f7b71c3227a7c3d19 Mon Sep 17 00:00:00 2001 From: Valentina Bojan Date: Tue, 5 May 2020 14:01:24 +0300 Subject: [PATCH 1/2] Register model converters only if they are not registered already (otherwise replace them) - fix #647 --- .../converters/ModelConverterRegistrar.java | 41 +++++++++++++++++-- .../springdoc/api/AbstractSpringDocTest.java | 11 ----- 2 files changed, 38 insertions(+), 14 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 c46466767..82bc4eff7 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,11 +20,13 @@ package org.springdoc.core.converters; -import java.util.List; - 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; + /** * Wrapper for model converters to only register converters once */ @@ -35,6 +37,39 @@ public class ModelConverterRegistrar { * registered in {@link ModelConverters} instance */ public ModelConverterRegistrar(List modelConverters) { - modelConverters.forEach(ModelConverters.getInstance()::addConverter); + for (ModelConverter modelConverter : modelConverters) { + Optional registeredConverterOptional = getRegisteredConverterSameAs(modelConverter); + + if (!registeredConverterOptional.isPresent()) { + ModelConverters.getInstance().addConverter(modelConverter); + } else { + registeredConverterOptional.ifPresent(alreadyRegisteredModelConverter -> { + ModelConverters.getInstance().removeConverter(alreadyRegisteredModelConverter); + ModelConverters.getInstance().addConverter(modelConverter); + }); + } + } + } + + private Optional getRegisteredConverterSameAs(ModelConverter modelConverter) { + try { + Field convertersField = ModelConverters.class.getDeclaredField("converters"); + ModelConverters modelConvertersInstance = ModelConverters.getInstance(); + convertersField.setAccessible(true); + List modelConverters = (List) convertersField.get(modelConvertersInstance); + return modelConverters.stream() + .filter(registeredModelConverter -> isSameConverter(registeredModelConverter, modelConverter)) + .findFirst(); + } catch (NoSuchFieldException | IllegalAccessException 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 + Class modelConverter1Class = modelConverter1.getClass(); + Class modelConverter2Class = modelConverter2.getClass(); + + return modelConverter1Class.equals(modelConverter2Class); } } diff --git a/springdoc-openapi-data-rest/src/test/java/test/org/springdoc/api/AbstractSpringDocTest.java b/springdoc-openapi-data-rest/src/test/java/test/org/springdoc/api/AbstractSpringDocTest.java index a01c13ab2..f5aef11e3 100644 --- a/springdoc-openapi-data-rest/src/test/java/test/org/springdoc/api/AbstractSpringDocTest.java +++ b/springdoc-openapi-data-rest/src/test/java/test/org/springdoc/api/AbstractSpringDocTest.java @@ -22,10 +22,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.List; -import io.swagger.v3.core.converter.ModelConverter; -import io.swagger.v3.core.converter.ModelConverters; import nonapi.io.github.classgraph.utils.FileUtils; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.Test; @@ -57,8 +54,6 @@ public abstract class AbstractSpringDocTest { public static String className; - private static List modelConverters; - @Autowired protected MockMvc mockMvc; @@ -75,16 +70,10 @@ public static String getContent(String fileName) throws Exception { @AfterAll public static void afterClass() { - modelConverters.forEach(ModelConverters.getInstance()::removeConverter); System.clearProperty("spring.hateoas.use-hal-as-default-json-media-type"); getConfig().replaceWithClass(org.springframework.data.domain.Pageable.class, Pageable.class); } - @Autowired - private void setModelConverters(List modelConverters) { - AbstractSpringDocTest.modelConverters = modelConverters; - } - @Test public void testApp() throws Exception { className = getClass().getSimpleName(); From 4ca4dce95fa7f9954e6a8c36c9f68caa9fcd6d75 Mon Sep 17 00:00:00 2001 From: Valentina Bojan Date: Tue, 5 May 2020 16:48:36 +0300 Subject: [PATCH 2/2] apply some code review changes --- .../core/converters/ModelConverterRegistrar.java | 11 +++-------- 1 file changed, 3 insertions(+), 8 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 82bc4eff7..34f8a7116 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 @@ -40,14 +40,9 @@ public ModelConverterRegistrar(List modelConverters) { for (ModelConverter modelConverter : modelConverters) { Optional registeredConverterOptional = getRegisteredConverterSameAs(modelConverter); - if (!registeredConverterOptional.isPresent()) { - ModelConverters.getInstance().addConverter(modelConverter); - } else { - registeredConverterOptional.ifPresent(alreadyRegisteredModelConverter -> { - ModelConverters.getInstance().removeConverter(alreadyRegisteredModelConverter); - ModelConverters.getInstance().addConverter(modelConverter); - }); - } + ModelConverters modelConvertersInstance = ModelConverters.getInstance(); + registeredConverterOptional.ifPresent(modelConvertersInstance::removeConverter); + modelConvertersInstance.addConverter(modelConverter); } }