Skip to content
This repository was archived by the owner on May 28, 2018. It is now read-only.

Commit 12ee22a

Browse files
committed
bug id 3731 fix; do not allow resource methods to be annotated with @XXXParam
Change-Id: Ic71beb071954a1c05e7ed1412d5cbdb0253bf54e
1 parent 9fece62 commit 12ee22a

File tree

3 files changed

+84
-3
lines changed

3 files changed

+84
-3
lines changed

core-server/src/main/java/org/glassfish/jersey/server/model/ResourceMethodValidator.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
33
*
4-
* Copyright (c) 2012-2017 Oracle and/or its affiliates. All rights reserved.
4+
* Copyright (c) 2012-2018 Oracle and/or its affiliates. All rights reserved.
55
*
66
* The contents of this file are subject to the terms of either the GNU
77
* General Public License Version 2 only ("GPL") or the Common Development
@@ -171,6 +171,23 @@ private void checkMethod(ResourceMethod method) {
171171
}
172172
}
173173

174+
// Prevent PARAM_ANNOTATION_SET annotations on a resource method
175+
if (httpMethodAnnotations.size() != 0) {
176+
checkUnexpectedAnnotations(method);
177+
}
178+
}
179+
180+
private void checkUnexpectedAnnotations(ResourceMethod resourceMethod) {
181+
Invocable invocable = resourceMethod.getInvocable();
182+
for (Annotation annotation : invocable.getHandlingMethod().getDeclaredAnnotations()) {
183+
if (PARAM_ANNOTATION_SET.contains(annotation.annotationType())) {
184+
Errors.fatal(resourceMethod, LocalizationMessages.METHOD_UNEXPECTED_ANNOTATION(
185+
invocable.getHandlingMethod().getName(),
186+
invocable.getHandler().getHandlerClass().getName(),
187+
annotation.annotationType().getName())
188+
);
189+
}
190+
}
174191
}
175192

176193
private void checkValueProviders(ResourceMethod method) {
@@ -191,6 +208,11 @@ private void visitSubResourceLocator(ResourceMethod locator) {
191208
if (void.class == invocable.getRawResponseType()) {
192209
Errors.fatal(locator, LocalizationMessages.SUBRES_LOC_RETURNS_VOID(invocable.getHandlingMethod()));
193210
}
211+
212+
// Prevent PARAM_ANNOTATION_SET annotations on a resource locator
213+
if (invocable.getHandlingMethod().getAnnotation(Path.class) != null) {
214+
checkUnexpectedAnnotations(locator);
215+
}
194216
}
195217

196218
private void checkParameters(ResourceMethod method) {

core-server/src/main/resources/org/glassfish/jersey/server/internal/localization.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#
22
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
33
#
4-
# Copyright (c) 2012-2017 Oracle and/or its affiliates. All rights reserved.
4+
# Copyright (c) 2012-2018 Oracle and/or its affiliates. All rights reserved.
55
#
66
# The contents of this file are subject to the terms of either the GNU
77
# General Public License Version 2 only ("GPL") or the Common Development
@@ -164,6 +164,7 @@ method.empty.path.annotation=The (sub)resource method {0} in {1} contains empty
164164
method.invocable.from.prematch.filters.only=Method can only be invoked from pre-matching request filters.
165165
method.parameter.cannot.be.null.or.empty=Method parameter "{0}" cannot be null or empty.
166166
method.parameter.cannot.be.null=Method parameter "{0}" cannot be null.
167+
method.unexpected.annotation=(Sub)resource method {0} in {1} contains unexpected annotation {2}.
167168
multiple.http.method.designators=A (sub-)resource method, {0}, should have only one HTTP method designator. It currently has the following designators defined: {1}
168169
new.ar.created.by.introspection.modeler=A new abstract resource created by IntrospectionModeler: {0}
169170
non.instantiable.component=Component of class {0} cannot be instantiated and will be ignored.

core-server/src/test/java/org/glassfish/jersey/server/model/ValidatorTest.java

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
33
*
4-
* Copyright (c) 2010-2017 Oracle and/or its affiliates. All rights reserved.
4+
* Copyright (c) 2010-2018 Oracle and/or its affiliates. All rights reserved.
55
*
66
* The contents of this file are subject to the terms of either the GNU
77
* General Public License Version 2 only ("GPL") or the Common Development
@@ -1150,6 +1150,62 @@ public void testVoidReturnType() throws Exception {
11501150
assertEquals(Severity.HINT, issues.get(0).getSeverity());
11511151
}
11521152

1153+
@Path("paramonsetter")
1154+
public static class ResourceWithParamOnSetter {
1155+
@QueryParam("id")
1156+
public void setId(String id) {
1157+
}
1158+
1159+
@GET
1160+
public String get() {
1161+
return "get";
1162+
}
1163+
}
1164+
1165+
@Test
1166+
public void testParamOnSetterIsOk() {
1167+
LOGGER.info("Validation should report no issues.");
1168+
List<ResourceModelIssue> issues = testResourceValidation(ResourceWithParamOnSetter.class);
1169+
1170+
assertEquals(0, issues.size());
1171+
}
1172+
1173+
@Path("paramonresourcepath")
1174+
public static class ResourceWithParamOnResourcePathAnnotatedMethod {
1175+
@QueryParam("id")
1176+
@Path("fail")
1177+
public String query() {
1178+
return "post";
1179+
}
1180+
}
1181+
1182+
@Test
1183+
public void testParamOnResourcePathAnnotatedMethodFails() {
1184+
LOGGER.info("Should report fatal during validation as @Path method should not be annotated with parameter annotation");
1185+
List<ResourceModelIssue> issues = testResourceValidation(ResourceWithParamOnResourcePathAnnotatedMethod.class);
1186+
1187+
assertEquals(1, issues.size());
1188+
assertEquals(Severity.FATAL, issues.get(0).getSeverity());
1189+
}
1190+
1191+
@Path("paramonresourceget")
1192+
public static class ResourceGETMethodFails {
1193+
@QueryParam("id")
1194+
@GET
1195+
public String get(@PathParam("abc") String id) {
1196+
return "get";
1197+
}
1198+
}
1199+
1200+
@Test
1201+
public void testParamOnResourceGETMethodFails() {
1202+
LOGGER.info("Should report fatal during validation as @GET method should not be annotated with parameter annotation");
1203+
List<ResourceModelIssue> issues = testResourceValidation(ResourceGETMethodFails.class);
1204+
1205+
assertEquals(1, issues.size());
1206+
assertEquals(Severity.FATAL, issues.get(0).getSeverity());
1207+
}
1208+
11531209
/**
11541210
* Test of disabled validation failing on errors.
11551211
*/
@@ -1180,11 +1236,13 @@ public void testDisableFailOnErrors() throws ExecutionException, InterruptedExce
11801236
PercentEncodedCaseSensitiveTest.class,
11811237
PercentEncodedTest.class,
11821238
ResourceAsProvider.class,
1239+
ResourceGETMethodFails.class,
11831240
ResourceMethodWithVoidReturnType.class,
11841241
ResourceRoot.class,
11851242
ResourceRootNotUnique.class,
11861243
ResourceSubPathRoot.class,
11871244
ResourceWithMultipleScopes.class,
1245+
ResourceWithParamOnResourcePathAnnotatedMethod.class,
11881246
TestAmbiguousParams.class,
11891247
TestAsyncGetRMReturningVoid.class,
11901248
TestEmptyPathSegment.class,

0 commit comments

Comments
 (0)