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

Closes caelum/vraptor#581. Only instantiate objects with same type #355

Merged
merged 3 commits into from
Feb 5, 2014
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -38,7 +38,8 @@ public Object instantiate(Target<?> target, Parameters params) {

@Override
public boolean isAbleToInstantiate(Target<?> target) {
return request.getAttribute(target.getName()) != null;
Object value = request.getAttribute(target.getName());
return value != null && target.getClassType().isInstance(value);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.sameInstance;
Expand All @@ -43,18 +44,22 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

import org.junit.Test;

import br.com.caelum.iogi.parameters.Parameter;
import br.com.caelum.iogi.parameters.Parameters;
import br.com.caelum.iogi.reflection.Target;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.controller.ControllerMethod;
import br.com.caelum.vraptor.controller.DefaultControllerMethod;
import br.com.caelum.vraptor.http.ParametersProvider;
import br.com.caelum.vraptor.http.ParametersProviderTest;
import br.com.caelum.vraptor.validator.Message;

public class IogiParametersProviderTest extends ParametersProviderTest {

Expand Down Expand Up @@ -166,11 +171,28 @@ public void isCapableOfDealingWithSetsOfObjects() throws Exception {
assertThat(abc.iterator().next().getX(), is(1l));
}

@Test
public void shouldInjectOnlyAttributesWithSameType() throws Exception {
Object result = mock(Result.class);
when(request.getAttribute("result")).thenReturn(result);
when(request.getParameter("result")).thenReturn("buggy");

ControllerMethod method = DefaultControllerMethod.instanceFor(OtherResource.class,
OtherResource.class.getDeclaredMethod("logic", String.class));

List<Message> errors = new ArrayList<Message>();
Object[] out = provider.getParametersFor(method, errors);
Copy link
Member Author

Choose a reason for hiding this comment

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

I've changed the test because I think that we need to check if IOGI got the right parameter too. @lucascs right?


assertThat(out[0], not(is(result)));
Copy link
Member

Choose a reason for hiding this comment

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

is(not(result))?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, because Result is a vraptor object that we can't get. We want to get only parameters, not vraptor objects. This is the propose of the issue.

Copy link
Member

Choose a reason for hiding this comment

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

is(not(result)) reads better than not(is(result)) ;) 🍂

Copy link
Member Author

Choose a reason for hiding this comment

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

Done by a7990e8

}

//----------

class OtherResource {
void logic(NeedsMyResource param) {
}
void logic(String result) {
}
}

static class NeedsMyResource {
Expand Down