Search in sources :

Example 6 with Inclusion

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"));
}
Also used : Inclusion(io.crnk.legacy.queryParams.include.Inclusion) Test(org.junit.Test)

Example 7 with Inclusion

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;
}
Also used : ResourceField(io.crnk.core.engine.information.resource.ResourceField) Inclusion(io.crnk.legacy.queryParams.include.Inclusion) ArrayList(java.util.ArrayList) IncludedRelationsParams(io.crnk.legacy.queryParams.params.IncludedRelationsParams)

Example 8 with Inclusion

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);
            }
        }
    }
}
Also used : Inclusion(io.crnk.legacy.queryParams.include.Inclusion)

Example 9 with Inclusion

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));
}
Also used : Inclusion(io.crnk.legacy.queryParams.include.Inclusion) ParametersDeserializationException(io.crnk.core.exception.ParametersDeserializationException)

Example 10 with Inclusion

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));
}
Also used : Inclusion(io.crnk.legacy.queryParams.include.Inclusion) TypedParams(io.crnk.legacy.queryParams.params.TypedParams) IncludedRelationsParams(io.crnk.legacy.queryParams.params.IncludedRelationsParams)

Aggregations

Inclusion (io.crnk.legacy.queryParams.include.Inclusion)10 IncludedRelationsParams (io.crnk.legacy.queryParams.params.IncludedRelationsParams)4 Test (org.junit.Test)3 IncludeRelationSpec (io.crnk.core.queryspec.IncludeRelationSpec)2 HashSet (java.util.HashSet)2 ResourceField (io.crnk.core.engine.information.resource.ResourceField)1 ParametersDeserializationException (io.crnk.core.exception.ParametersDeserializationException)1 TypedParams (io.crnk.legacy.queryParams.params.TypedParams)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 Set (java.util.Set)1