Skip to content

Annotation expression template processing should not fail on Class parameter types #15711

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

Merged
merged 1 commit into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -998,6 +998,15 @@ public void adviseWhenPrePostEnabledThenEachInterceptorRunsExactlyOnce() {
verify(expressionHandler, times(4)).createEvaluationContext(any(Supplier.class), any());
}

// gh-15721
@Test
@WithMockUser(roles = "uid")
public void methodWhenMetaAnnotationPropertiesHasClassProperties() {
this.spring.register(MetaAnnotationPlaceholderConfig.class).autowire();
MetaAnnotationService service = this.spring.getContext().getBean(MetaAnnotationService.class);
assertThat(service.getIdPath("uid")).isEqualTo("uid");
}

private static Consumer<ConfigurableWebApplicationContext> disallowBeanOverriding() {
return (context) -> ((AnnotationConfigWebApplicationContext) context).setAllowBeanDefinitionOverriding(false);
}
Expand Down Expand Up @@ -1376,6 +1385,27 @@ List<String> resultsContainDave(List<String> list) {
return list;
}

@RestrictedAccess(entityClass = EntityClass.class)
String getIdPath(String id) {
return id;
}

}

@Retention(RetentionPolicy.RUNTIME)
@PreAuthorize("hasRole({idPath})")
@interface RestrictedAccess {

String idPath() default "#id";

Class<?> entityClass();

String[] recipes() default {};

}

static class EntityClass {

}

@Retention(RetentionPolicy.RUNTIME)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,20 @@
import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;

import org.springframework.core.annotation.AnnotationConfigurationException;
import org.springframework.core.annotation.MergedAnnotation;
import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.core.annotation.MergedAnnotations.SearchStrategy;
import org.springframework.core.annotation.RepeatableContainers;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.GenericConverter;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.util.PropertyPlaceholderHelper;

Expand All @@ -55,6 +59,12 @@
*/
final class AuthorizationAnnotationUtils {

private static final DefaultConversionService conversionService = new DefaultConversionService();

static {
conversionService.addConverter(new ClassToStringConverter());
}

static <A extends Annotation> Function<AnnotatedElement, A> withDefaults(Class<A> type,
PrePostTemplateDefaults defaults) {
Function<MergedAnnotation<A>, A> map = (mergedAnnotation) -> {
Expand All @@ -70,7 +80,7 @@ static <A extends Annotation> Function<AnnotatedElement, A> withDefaults(Class<A
String key = property.getKey();
Object value = property.getValue();
String asString = (value instanceof String) ? (String) value
: DefaultConversionService.getSharedInstance().convert(value, String.class);
: conversionService.convert(value, String.class);
stringProperties.put(key, asString);
}
AnnotatedElement annotatedElement = (AnnotatedElement) mergedAnnotation.getSource();
Expand Down Expand Up @@ -156,4 +166,18 @@ private AuthorizationAnnotationUtils() {

}

static class ClassToStringConverter implements GenericConverter {

@Override
public Set<ConvertiblePair> getConvertibleTypes() {
return Collections.singleton(new ConvertiblePair(Class.class, String.class));
}

@Override
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
return (source != null) ? source.toString() : null;
}

}

}