Skip to content

Commit

Permalink
crnk-project#767 fixed filterSpecMapper init, added test for unknown …
Browse files Browse the repository at this point in the history
…attribute
  • Loading branch information
JochenReinhardt committed Jul 22, 2020
1 parent e020d7a commit 2087f0a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,32 +65,7 @@ public ClientStubBase(CrnkClient client, UrlBuilder urlBuilder, Class<?> resourc
this.resourceClass = resourceClass;
this.filterCriteriaInRequestBody = filterCriteriaInRequestBody;
if (filterCriteriaInRequestBody) {
QuerySpecUrlContext context = new QuerySpecUrlContext() {
@Override
public ResourceRegistry getResourceRegistry() {
return client.getRegistry();
}

@Override
public TypeParser getTypeParser() {
return client.getModuleRegistry().getTypeParser();
}

@Override
public ObjectMapper getObjectMapper() {
return client.getObjectMapper();
}

@Override
public UrlBuilder getUrlBuilder() {
return client.getModuleRegistry().getUrlBuilder();
}
};
Map<String, FilterOperator> supportedOperators = new DefaultQuerySpecUrlMapper().getSupportedOperators().stream().collect(Collectors.toMap(
FilterOperator::getName, Function.identity()));
QueryPathResolver pathResolver = new DefaultQueryPathResolver();
pathResolver.init(context);
filterSpecMapper = new JsonFilterSpecMapper(context, supportedOperators, FilterOperator.EQ, pathResolver);
filterSpecMapper = ((DefaultQuerySpecUrlMapper) client.getUrlMapper()).getJsonParser();
compactMapper = new ObjectMapper();
} else {
filterSpecMapper = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@

import io.crnk.client.http.apache.HttpClientAdapter;
import io.crnk.core.boot.CrnkProperties;
import io.crnk.core.exception.InternalServerErrorException;
import io.crnk.core.queryspec.FilterOperator;
import io.crnk.core.queryspec.FilterSpec;
import io.crnk.core.queryspec.PathSpec;
import io.crnk.core.queryspec.QuerySpec;
import io.crnk.core.queryspec.mapper.DefaultQuerySpecUrlMapper;
import io.crnk.core.repository.ResourceRepository;
import io.crnk.core.resource.list.ResourceList;
import io.crnk.test.mock.models.Project;
import org.junit.Before;
import org.junit.Test;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import static junit.framework.TestCase.assertNotNull;
import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -43,14 +46,18 @@ private void createProjects() {
@Override
protected TestApplication configure() {
TestApplication app = super.configure();
app.setProperties(Collections.singletonMap(CrnkProperties.FILTER_CRITERIA_IN_HTTP_BODY, Boolean.TRUE.toString()));
Map<String, String> properties = new HashMap<>();
properties.put(CrnkProperties.FILTER_CRITERIA_IN_HTTP_BODY, Boolean.TRUE.toString());
properties.put(CrnkProperties.ALLOW_UNKNOWN_ATTRIBUTES, Boolean.TRUE.toString());
app.setProperties(properties);
return app;
}

@Override
protected void setupClient(CrnkClient client) {
client.setHttpAdapter(new HttpClientAdapter());
client.setFilterCriteriaInRequestBody(true);
((DefaultQuerySpecUrlMapper) client.getUrlMapper()).setAllowUnknownAttributes(true);
}

@Test
Expand Down Expand Up @@ -82,4 +89,13 @@ public void testWithCombinedFilter() {
assertNotNull(projects);
assertEquals(5, projects.size());
}

@Test(expected = InternalServerErrorException.class)
public void testUnknownFilterAttributeIsPassedToServer() {
ResourceRepository<Project, Long> projectRepo = client.getRepositoryForType(Project.class);
QuerySpec querySpec = new QuerySpec(Project.class);
String unknownPath = "unknown";
querySpec.addFilter(PathSpec.of(unknownPath).filter(FilterOperator.EQ, 1));
projectRepo.findAll(querySpec);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ public void init(QuerySpecUrlContext ctx) {
jsonParser = new JsonFilterSpecMapper(ctx, supportedOperators, defaultOperator, pathResolver);
}

public JsonFilterSpecMapper getJsonParser() {
return jsonParser;
}

/**
* @return true if attribute paths must be separated with ".". Ealier
* Crnk versions did made use of brackets
Expand Down

0 comments on commit 2087f0a

Please sign in to comment.