Skip to content

Commit

Permalink
Merge branch 'valentinabojan-#647-register-model-converters-only-once'
Browse files Browse the repository at this point in the history
  • Loading branch information
bnasslahsen committed May 5, 2020
2 parents f6802aa + 11fedf3 commit 78522f2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,54 @@

package org.springdoc.core.converters;

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
*/
public ModelConverterRegistrar(List<ModelConverter> modelConverters) {
modelConverters.forEach(ModelConverters.getInstance()::addConverter);
for (ModelConverter modelConverter : modelConverters) {
Optional<ModelConverter> registeredConverterOptional = getRegisteredConverterSameAs(modelConverter);
registeredConverterOptional.ifPresent(modelConvertersInstance::removeConverter);
modelConvertersInstance.addConverter(modelConverter);
}
}

private Optional<ModelConverter> getRegisteredConverterSameAs(ModelConverter modelConverter) {
try {
Field convertersField = FieldUtils.getDeclaredField(ModelConverters.class, "converters", true);
List<ModelConverter> modelConverters = (List<ModelConverter>) convertersField.get(modelConvertersInstance);
return modelConverters.stream()
.filter(registeredModelConverter -> isSameConverter(registeredModelConverter, modelConverter))
.findFirst();
}
catch (IllegalAccessException exception) {
LOGGER.error(exception.getMessage(), exception);
throw new RuntimeException(exception);
}
}

private boolean isSameConverter(ModelConverter modelConverter1, ModelConverter modelConverter2) {
// comparing by the converter type
Class<? extends ModelConverter> modelConverter1Class = modelConverter1.getClass();
Class<? extends ModelConverter> modelConverter2Class = modelConverter2.getClass();
return modelConverter1Class.equals(modelConverter2Class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -57,8 +54,6 @@ public abstract class AbstractSpringDocTest {

public static String className;

private static List<ModelConverter> modelConverters;

@Autowired
protected MockMvc mockMvc;

Expand All @@ -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<ModelConverter> modelConverters) {
AbstractSpringDocTest.modelConverters = modelConverters;
}

@Test
public void testApp() throws Exception {
className = getClass().getSimpleName();
Expand Down

0 comments on commit 78522f2

Please sign in to comment.