Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle Optional<?> @RequestParam() when generating URL using MvcUriComponentsBuilder #22656

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -97,6 +97,7 @@ public static void addDefaultConverters(ConverterRegistry converterRegistry) {
converterRegistry.addConverter(new IdToEntityConverter((ConversionService) converterRegistry));
converterRegistry.addConverter(new FallbackObjectToStringConverter());
converterRegistry.addConverter(new ObjectToOptionalConverter((ConversionService) converterRegistry));
converterRegistry.addConverter(new OptionalUnwrappingConverter((ConversionService) converterRegistry));
}

/**
Expand Down
@@ -0,0 +1,64 @@
package org.springframework.core.convert.support;

import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.ConditionalGenericConverter;

import java.util.*;

final class OptionalUnwrappingConverter implements ConditionalGenericConverter {

private final ConversionService conversionService;

public OptionalUnwrappingConverter(final ConversionService conversionService)
{
this.conversionService = conversionService;
}

@Override
public Set<ConvertiblePair> getConvertibleTypes()
{
return null;
}

@Override
public boolean matches(final TypeDescriptor sourceType, final TypeDescriptor targetType)
{
if (!Optional.class.isAssignableFrom(sourceType.getObjectType())) {
return false;
}

if (!sourceType.getResolvableType().hasGenerics()) {
return false;
}

return conversionService.canConvert(new GenericTypeDescriptor(sourceType), targetType);
}

@Override
public Object convert(final Object source, final TypeDescriptor sourceType, final TypeDescriptor targetType)
{
if (source == null) {
return null;
}

Optional<?> optionalSource = Optional.class.cast(source);
if (!optionalSource.isPresent()) {
return null;
}

return conversionService.convert(optionalSource.get(), new GenericTypeDescriptor(sourceType), targetType);
}

@SuppressWarnings("serial")
private static class GenericTypeDescriptor extends TypeDescriptor
{

GenericTypeDescriptor(final TypeDescriptor typeDescriptor)
{
super(typeDescriptor.getResolvableType().getGeneric(), null, typeDescriptor.getAnnotations());
}

}

}
Expand Up @@ -20,6 +20,7 @@
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;

Expand Down Expand Up @@ -224,6 +225,9 @@ public void contributeMethodArgument(MethodParameter parameter, @Nullable Object
}
builder.queryParam(name);
}
else if (value instanceof Optional && !((Optional) value).isPresent()) {
return;
}
else if (value instanceof Collection) {
for (Object element : (Collection<?>) value) {
element = formatUriValue(conversionService, TypeDescriptor.nested(parameter, 1), element);
Expand Down