X Tutup
Skip to content
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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 @@ -320,6 +320,26 @@ private boolean elementHasAnnotationSafe(Class<? extends Annotation> annotation,
return false;
}

public int numberOfElementParameterHasAnnotation(ExecutableElement element, Class<? extends Annotation> annotation) {
int count = 0;
for (VariableElement parameter : element.getParameters()) {
if (parameter.getAnnotation(annotation) != null) {
count++;
}
}
return count;
}

public int numberOfNotAnnotatedElementParameter(ExecutableElement element) {
int count = 0;
for (VariableElement parameter : element.getParameters()) {
if (parameter.getAnnotationMirrors().size() == 0) {
count++;
}
}
return count;
}

public void doesntThrowException(Element element, ElementValidation valid) {
ExecutableElement executableElement = (ExecutableElement) element;
if (executableElement.getThrownTypes().size() > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
*
* &#064;Post(&quot;/entity&quot;)
* &#064;Accept(<b>MediaType.APPLICATION_XML</b>)
* Event addEvent(Event event);
* Event addEvent(&#064;Body Event event);
* }
* </pre>
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Copyright (C) 2010-2015 eBusiness Information, Excilys Group
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed To in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package org.androidannotations.rest.spring.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* This annotation can be used to add a method body to the POST, PUT, or
* PATCH request from a method parameter.
*
* <b>Example :</b>
*
* <pre>
* &#064;Rest(rootUrl = &quot;http://myserver&quot;, converters = FormHttpMessageConverter.class)
* public interface RestClient {
*
* &#064;Post(&quot;/events/{id}&quot;)
* EventList addEvent(String id, <b>&#064;Body</b> Event event);
* }
* </pre>
*
* @see Rest
* @see Post
* @see Put
* @see Patch
* @see Field
*/
@Retention(RetentionPolicy.CLASS)
@Target(ElementType.PARAMETER)
public @interface Body {
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
* Event deleteEvent();
*
* &#064;Delete(&quot;/events/new/<b>{id}</b>&quot;)
* void deleteEvent(Event <i>event</i>, int <b>id</b>);
* void deleteEvent(&#064;Body Event <i>event</i>, int <b>id</b>);
* }
* </pre>
*
Expand All @@ -52,6 +52,7 @@
* @see Put
* @see Head
* @see Options
* @see Body
*/
@Retention(RetentionPolicy.CLASS)
@Target(ElementType.METHOD)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
* @see Put
* @see Patch
* @see Part
* @see Body
*/
@Retention(RetentionPolicy.CLASS)
@Target(ElementType.PARAMETER)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
* @see Put
* @see Patch
* @see Field
* @see Body
*/
@Retention(RetentionPolicy.CLASS)
@Target(ElementType.PARAMETER)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
* Event updateEvent();
*
* &#064;Patch(&quot;/events/update/<b>{id}</b>&quot;)
* void updateEvent(Event <i>event</i>, int <b>id</b>);
* void updateEvent(&#064;Body Event <i>event</i>, int <b>id</b>);
* }
* </pre>
*
Expand All @@ -53,6 +53,7 @@
* @see Delete
* @see Head
* @see Options
* @see Body
*/
@Retention(RetentionPolicy.CLASS)
@Target(ElementType.METHOD)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
* Event newEvent();
*
* &#064;Post(&quot;/events/new/<b>{id}</b>&quot;)
* void newEvent(Event <i>event</i>, int <b>id</b>);
* void newEvent(&#064;Body Event <i>event</i>, int <b>id</b>);
* }
* </pre>
*
Expand All @@ -56,6 +56,7 @@
* @see Delete
* @see Head
* @see Options
* @see Body
*/
@Retention(RetentionPolicy.CLASS)
@Target(ElementType.METHOD)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
* Event updateEvent();
*
* &#064;Put(&quot;/events/update/<b>{id}</b>&quot;)
* void updateEvent(Event <i>event</i>, int <b>id</b>);
* void updateEvent(&#064;Body Event <i>event</i>, int <b>id</b>);
* }
* </pre>
*
Expand All @@ -52,6 +52,7 @@
* @see Delete
* @see Head
* @see Options
* @see Body
*/
@Retention(RetentionPolicy.CLASS)
@Target(ElementType.METHOD)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
* Event getEvent(long id);
*
* &#064;Post(&quot;/events/new&quot;)
* void newEvent(Event event);
* void newEvent(&#064;Body Event event);
* }
*
* &#064;EBean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
*
* &#064;Put(&quot;/events/update/&quot;)
* &#064;RequiresCookie(<b>&quot;token&quot;</b>)
* void updateEvent(Event event);
* void updateEvent(&#064;Body Event event);
* }
* </pre>
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Set;

import org.androidannotations.rest.spring.annotations.Accept;
import org.androidannotations.rest.spring.annotations.Body;
import org.androidannotations.rest.spring.annotations.Delete;
import org.androidannotations.rest.spring.annotations.Field;
import org.androidannotations.rest.spring.annotations.Get;
Expand Down Expand Up @@ -141,102 +142,100 @@ public interface MyService {
@Post("http://company.com/client/ping")
void ping();

// There should be max 1 parameter that is not mapped to an attribute. This
// parameter will be used as the post entity.
@Post("/events/")
@Accept(MediaType.APPLICATION_JSON)
Event addEvent(Event event);
Event addEvent(@Body String event);

@Post("/events/{year}/")
Event addEvent(Event event, int year);
Event addEvent(@Body Event event, int year);

@Post("/events/{year}/")
Event addEvent(int year);

@Post("/events/")
ResponseEntity<Event> addEvent2(Event event);
ResponseEntity<Event> addEvent2(@Body Event event);

@Post("/events/{date}")
@Post("/events/{date}?myCookieInUrl={myCookieInUrl}")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am little bit confused here. Why this was working before the cookie url variable in the url?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was really surprised when I notice this, too. This is why I asked you before. There was no validation that checks if @RequiresCookieInUrl parameter is in url or not. I had to fix these test cases because I added hasAnnotatedAllParameters method that validates the number of url variables in url is equals with the sum of non-annotated parameters, parameters with @Path and @RequiresCookieInUrl

...ok I felt I have to add more validations on hasAnnotatedAllParameters.

Do you want to remove current hasAnnotatedAllParameters validation and send me another pull request for validating @RequiresCookieInUrl? which will block this pull request. It won't take that long.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, but we validate the sum, or we also validate that the actually declared url variable exists in the url now with @RequiresCookieInUrl?

@Post("/events/{date}?myCookieInUrl={myCookieInUrl}")
@RequiresCookieInUrl("badNamed")

So does this invalidate now?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We only validate the sum, not actual name. You are right. AA has to handle that. I will add the validation in this pull request.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@RequiresHeader("SomeFancyHeader")
@RequiresCookie("myCookie")
@RequiresCookieInUrl("myCookieInUrl")
void addEventWithParameters(String date, @Field String parameter, @Field String otherParameter);

@Post("/events/{date}")
@Post("/events/{date}?myCookieInUrl={myCookieInUrl}")
@RequiresHeader("SomeFancyHeader")
@RequiresCookie("myCookie")
@RequiresCookieInUrl("myCookieInUrl")
void addEventWithParts(String date, @Part String parameter, @Part String otherParameter);

@Post("/events/{date}")
@Post("/events/{date}?myCookieInUrl={myCookieInUrl}")
@RequiresHeader("SomeFancyHeader")
@RequiresCookie("myCookie")
@RequiresCookieInUrl("myCookieInUrl")
void addEventWithPathParameters(@Path("date") String pathParam, @Field String parameter);

@Post("/events/{date}")
@Post("/events/{date}?myCookieInUrl={myCookieInUrl}")
@RequiresHeader("SomeFancyHeader")
@Header(name = "SomeFancyHeader", value = "fancy")
@RequiresCookie("myCookie")
@RequiresCookieInUrl("myCookieInUrl")
void addEventWithHeaders(String date, String parameter);
void addEventWithHeaders(String date, @Body String parameter);

@Post("/events/{date}")
@Post("/events/{date}?myCookieInUrl={myCookieInUrl}")
@Headers(@Header(name = "SomeFancyHeader", value = "fancy"))
@RequiresCookie("myCookie")
@RequiresCookieInUrl("myCookieInUrl")
void addEventWithHeadersHeadersAnnotation(String date, String parameter);
void addEventWithHeadersHeadersAnnotation(String date, @Body String parameter);

/**
* Output different then input
*/
@Post("/events/")
ResponseEntity<String> addEvent3(Event event);
ResponseEntity<String> addEvent3(@Body Event event);

@Post("/events/")
List<? extends Event> addEventGenericsListWildcardExtends(Event event);
List<? extends Event> addEventGenericsListWildcardExtends(@Body Event event);

@Post("/events/")
List<Event> addEventGenericsList(Event event);
List<Event> addEventGenericsList(@Body Event event);

// TODO: Handle generics in params
// @Post("/events/")
// List<Event> addEventGenericsList(List<Event> events);

@Post("/events/")
Set<Event> addEventGenericsSet(Event event);
Set<Event> addEventGenericsSet(@Body Event event);

@Post("/events/")
GenericEvent<GenericEvent<GenericEvent<String>>> addEventGenericsInception(Event event);
GenericEvent<GenericEvent<GenericEvent<String>>> addEventGenericsInception(@Body Event event);

@Post("/events/")
Map<String, Event> addEventGenericsMap(Event event);
Map<String, Event> addEventGenericsMap(@Body Event event);

/**
* Output different then input
*/
@Post("/events/")
String addEvent4(Event event);
String addEvent4(@Body Event event);

@Post("/events/")
void addEvent5(Event event);
void addEvent5(@Body Event event);

@Post("/events/{year}/")
@Accept(MediaType.APPLICATION_JSON)
ResponseEntity<Event> addEvent2(Event event, int year);
ResponseEntity<Event> addEvent2(@Body Event event, int year);

// *** PUT ***

@Put("/events/{id}")
void updateEvent(Event event, int id);
void updateEvent(@Body Event event, int id);

@Put("/events/{date}")
void updateEvent(long date);

@Put("/events/{date}")
Event updateEventWithResponse(long date);

@Put("/events/{date}")
@Put("/events/{date}?myCookieInUrl={myCookieInUrl}")
@RequiresHeader("SomeFancyHeader")
@RequiresCookie("myCookie")
@RequiresCookieInUrl("myCookieInUrl")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.androidannotations.rest.spring.test;

import org.androidannotations.rest.spring.annotations.Body;
import org.androidannotations.rest.spring.annotations.Field;
import org.androidannotations.rest.spring.annotations.Part;
import org.androidannotations.rest.spring.annotations.Post;
Expand All @@ -30,4 +31,7 @@ public interface PostRestService extends RestClientSupport {

@Post("/")
void multipart(@Part("otherParam") String postParam, @Part("postParam") String otherParam, @Part String thirdParam);

@Post("/")
void postRequests(@Body String postParam);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.androidannotations.AndroidAnnotationsEnvironment;
import org.androidannotations.handler.AnnotationHandler;
import org.androidannotations.plugin.AndroidAnnotationsPlugin;
import org.androidannotations.rest.spring.handler.BodyHandler;
import org.androidannotations.rest.spring.handler.DeleteHandler;
import org.androidannotations.rest.spring.handler.FieldHandler;
import org.androidannotations.rest.spring.handler.GetHandler;
Expand Down Expand Up @@ -51,6 +52,7 @@ public List<AnnotationHandler<?>> getHandlers(AndroidAnnotationsEnvironment andr
annotationHandlers.add(new RestHandler(androidAnnotationEnv));
annotationHandlers.add(new FieldHandler(androidAnnotationEnv));
annotationHandlers.add(new PartHandler(androidAnnotationEnv));
annotationHandlers.add(new BodyHandler(androidAnnotationEnv));
annotationHandlers.add(new GetHandler(androidAnnotationEnv));
annotationHandlers.add(new PostHandler(androidAnnotationEnv));
annotationHandlers.add(new PutHandler(androidAnnotationEnv));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ public void validate(Element element, ElementValidation validation) {

validatorHelper.doesNotReturnPrimitive((ExecutableElement) element, validation);

restSpringValidatorHelper.urlVariableNamesExistInParametersAndHasOnlyOneEntityParameterOrOneOrMoreParameter((ExecutableElement) element, validation);

restSpringValidatorHelper.doesNotMixPartAndFieldAnnotations((ExecutableElement) element, validation);
restSpringValidatorHelper.doesNotHaveDuplicateFieldAndPartName((ExecutableElement) element, validation);
restSpringValidatorHelper.hasOneOrZeroBodyParameter((ExecutableElement) element, validation);
restSpringValidatorHelper.doesNotMixRequestEntityAnnotations((ExecutableElement) element, validation);
}

@Override
Expand Down
Loading
X Tutup