use of io.crnk.legacy.queryParams.include.Inclusion in project crnk-framework by crnk-project.
the class JsonApiQueryParamsParserTest method onGivenIncludedRelationsParserShouldReturnOnlyRequestParamsWithIncludedRelations.
@Test
public void onGivenIncludedRelationsParserShouldReturnOnlyRequestParamsWithIncludedRelations() {
// GIVEN
queryParams.put("include", Collections.singleton("author,comments.author"));
queryParams.put("random", Collections.singleton("author"));
// WHEN
QueryParams result = parseQueryParams();
// THEN
assertThat(result.getIncludedRelations().getParams().size()).isEqualTo(1);
assertThat(result.getIncludedRelations().getParams().get(TestResource.class.getSimpleName()).getParams()).containsOnly(new Inclusion("author"), new Inclusion("comments.author"));
}
use of io.crnk.legacy.queryParams.include.Inclusion in project crnk-framework by crnk-project.
the class DocumentMapperUtil method computeRequestedFields.
private static List<ResourceField> computeRequestedFields(IncludedFieldsParams includedFields, boolean relation, QueryAdapter queryAdapter, ResourceInformation resourceInformation, List<ResourceField> fields) {
Set<String> includedFieldNames = includedFields.getParams();
if (relation) {
// for relations consider both "include" and "fields"
TypedParams<IncludedRelationsParams> includedRelationsSet = queryAdapter.getIncludedRelations();
IncludedRelationsParams includedRelations = includedRelationsSet != null ? includedRelationsSet.getParams().get(resourceInformation.getResourceType()) : null;
if (includedRelations != null) {
includedFieldNames = new HashSet<>(includedFieldNames);
for (Inclusion include : includedRelations.getParams()) {
includedFieldNames.add(include.getPath());
}
}
}
List<ResourceField> results = new ArrayList<>();
for (ResourceField field : fields) {
if (includedFieldNames.contains(field.getJsonName())) {
results.add(field);
}
}
return results;
}
use of io.crnk.legacy.queryParams.include.Inclusion in project crnk-framework by crnk-project.
the class DefaultQuerySpecConverter method applyRelatedFields.
protected void applyRelatedFields(QuerySpec rootQuerySpec, QueryParams queryParams) {
TypedParams<IncludedRelationsParams> includes = queryParams.getIncludedRelations();
if (includes != null && !includes.getParams().isEmpty()) {
for (Entry<String, IncludedRelationsParams> typeEntry : includes.getParams().entrySet()) {
IncludedRelationsParams includeParams = typeEntry.getValue();
Class<?> resourceClass = getResourceClass(typeEntry.getKey());
QuerySpec querySpec = rootQuerySpec.getOrCreateQuerySpec(resourceClass);
for (Inclusion inclusion : includeParams.getParams()) {
List<String> attributePath = splitPath(inclusion.getPath());
querySpec.includeRelation(attributePath);
}
}
}
}
use of io.crnk.legacy.queryParams.include.Inclusion in project crnk-framework by crnk-project.
the class DefaultQueryParamsParser method parseIncludedRelationsParameters.
/**
* <strong>Important!</strong> Crnk implementation differs form JSON API
* <a href="http://jsonapi.org/format/#fetching-includes">definition of includes</a>
* in order to fit standard query legacy serializing strategy and maximize effective processing of data.
* <p>
* Included field set params can be send with following format: <br>
* <strong>include[ResourceType] = "property(.property)*"</strong><br>
* <p>
* Examples of accepted sparse field sets of resources:
* <ul>
* <li>{@code GET /tasks/?include[tasks]=author}</li>
* <li>{@code GET /tasks/?include[tasks][]=author&include[tasks][]=comments}</li>
* <li>{@code GET /projects/?include[projects]=task&include[tasks]=comments}</li>
* </ul>
*
* @param context Don't know, didn't write the code
* @return {@link TypedParams} Map of sparse field set params passed to a request grouped by type of document
*/
protected TypedParams<IncludedRelationsParams> parseIncludedRelationsParameters(QueryParamsParserContext context) {
String includeKey = RestrictedQueryParamsMembers.include.name();
Map<String, Set<String>> inclusions = filterQueryParamsByKey(context, includeKey);
Map<String, Set<Inclusion>> temporaryInclusionsMap = new LinkedHashMap<>();
for (Map.Entry<String, Set<String>> entry : inclusions.entrySet()) {
List<String> propertyList = buildPropertyListFromEntry(entry, includeKey);
if (propertyList.size() > 1) {
throw new ParametersDeserializationException("Exceeded maximum level of nesting of 'include' " + "legacy (1)");
}
String resourceType = propertyList.get(0);
Set<Inclusion> resourceParams;
if (temporaryInclusionsMap.containsKey(resourceType)) {
resourceParams = temporaryInclusionsMap.get(resourceType);
} else {
resourceParams = new LinkedHashSet<>();
}
for (String path : entry.getValue()) {
resourceParams.add(new Inclusion(path));
}
temporaryInclusionsMap.put(resourceType, resourceParams);
}
Map<String, IncludedRelationsParams> decodedInclusions = new LinkedHashMap<>();
for (Map.Entry<String, Set<Inclusion>> resourceTypesMap : temporaryInclusionsMap.entrySet()) {
Set<Inclusion> inclusionSet = Collections.unmodifiableSet(resourceTypesMap.getValue());
decodedInclusions.put(resourceTypesMap.getKey(), new IncludedRelationsParams(inclusionSet));
}
return new TypedParams<>(Collections.unmodifiableMap(decodedInclusions));
}
use of io.crnk.legacy.queryParams.include.Inclusion in project crnk-framework by crnk-project.
the class JsonApiQueryParamsParser method parseIncludedRelationsParameters.
@Override
protected TypedParams<IncludedRelationsParams> parseIncludedRelationsParameters(QueryParamsParserContext context) {
String includeKey = RestrictedQueryParamsMembers.include.name();
Map<String, Set<String>> inclusions = filterQueryParamsByKey(context, includeKey);
Map<String, IncludedRelationsParams> decodedInclusions = new LinkedHashMap<>();
if (inclusions.containsKey(RestrictedQueryParamsMembers.include.name())) {
Set<Inclusion> inclusionSet = new LinkedHashSet<>();
for (String inclusion : inclusions.get(RestrictedQueryParamsMembers.include.name())) {
inclusionSet.add(new Inclusion(inclusion));
}
decodedInclusions.put(context.getRequestedResourceInformation().getResourceType(), new IncludedRelationsParams(inclusionSet));
}
return new TypedParams<>(Collections.unmodifiableMap(decodedInclusions));
}
Aggregations