Skip to content

Commit

Permalink
Register model converters only if they are not registered already - f…
Browse files Browse the repository at this point in the history
…ixes #647 #648
  • Loading branch information
bnasslahsen committed May 5, 2020
1 parent cf40851 commit 11fedf3
Showing 1 changed file with 14 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,51 +20,54 @@

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
*/
public ModelConverterRegistrar(List<ModelConverter> modelConverters) {
for (ModelConverter modelConverter : modelConverters) {
Optional<ModelConverter> registeredConverterOptional = getRegisteredConverterSameAs(modelConverter);

ModelConverters modelConvertersInstance = ModelConverters.getInstance();
registeredConverterOptional.ifPresent(modelConvertersInstance::removeConverter);
modelConvertersInstance.addConverter(modelConverter);
}
}

private Optional<ModelConverter> 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<ModelConverter> modelConverters = (List<ModelConverter>) 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<? extends ModelConverter> modelConverter1Class = modelConverter1.getClass();
Class<? extends ModelConverter> modelConverter2Class = modelConverter2.getClass();

return modelConverter1Class.equals(modelConverter2Class);
}
}

0 comments on commit 11fedf3

Please sign in to comment.