Search in sources :

Example 6 with FinderSchema

use of com.linkedin.restli.restspec.FinderSchema in project rest.li by linkedin.

the class TestResourceSchemaToResourceSpecTranslator method compareResourceSpecs.

public void compareResourceSpecs(ResourceSpec actual, ResourceSpec expected, RichResourceSchema resourceSchema) {
    Assert.assertEquals(actual.getSupportedMethods(), expected.getSupportedMethods());
    compareTypes(actual.getKeyType(), expected.getKeyType());
    compareTypes(actual.getValueType(), expected.getValueType());
    compareComplexKey(actual.getComplexKeyType(), expected.getComplexKeyType());
    compareKeyParts(actual.getKeyParts(), expected.getKeyParts());
    ActionSchemaArray actions = resourceSchema.getActions();
    compareActions(actual, expected, actions);
    ActionSchemaArray entityActions = resourceSchema.getEntityActions();
    compareActions(actual, expected, entityActions);
    FinderSchemaArray finders = resourceSchema.getFinders();
    for (FinderSchema finder : finders) {
        compareParameters(actual.getRequestMetadata(finder.getName()), expected.getRequestMetadata(finder.getName()));
    }
}
Also used : FinderSchemaArray(com.linkedin.restli.restspec.FinderSchemaArray) ActionSchemaArray(com.linkedin.restli.restspec.ActionSchemaArray) FinderSchema(com.linkedin.restli.restspec.FinderSchema)

Example 7 with FinderSchema

use of com.linkedin.restli.restspec.FinderSchema in project rest.li by linkedin.

the class ResourceModelEncoder method createFinders.

private FinderSchemaArray createFinders(final ResourceModel resourceModel) {
    FinderSchemaArray findersArray = new FinderSchemaArray();
    List<ResourceMethodDescriptor> resourceMethodDescriptors = resourceModel.getResourceMethodDescriptors();
    Collections.sort(resourceMethodDescriptors, RESOURCE_METHOD_COMPARATOR);
    for (ResourceMethodDescriptor resourceMethodDescriptor : resourceMethodDescriptors) {
        if (ResourceMethod.FINDER.equals(resourceMethodDescriptor.getType())) {
            FinderSchema finder = new FinderSchema();
            finder.setName(resourceMethodDescriptor.getFinderName());
            String doc = _docsProvider.getMethodDoc(resourceMethodDescriptor.getMethod());
            if (doc != null) {
                finder.setDoc(doc);
            }
            ParameterSchemaArray parameters = createParameters(resourceMethodDescriptor);
            if (parameters.size() > 0) {
                finder.setParameters(parameters);
            }
            StringArray assocKeys = createAssocKeyParameters(resourceMethodDescriptor);
            if (assocKeys.size() > 0) {
                finder.setAssocKeys(assocKeys);
            }
            if (resourceMethodDescriptor.getFinderMetadataType() != null) {
                Class<?> metadataType = resourceMethodDescriptor.getFinderMetadataType();
                MetadataSchema metadataSchema = new MetadataSchema();
                metadataSchema.setType(buildDataSchemaType(metadataType));
                finder.setMetadata(metadataSchema);
            }
            final DataMap customAnnotation = resourceMethodDescriptor.getCustomAnnotationData();
            String deprecatedDoc = _docsProvider.getMethodDeprecatedTag(resourceMethodDescriptor.getMethod());
            if (deprecatedDoc != null) {
                customAnnotation.put(DEPRECATED_ANNOTATION_NAME, deprecateDocToAnnotationMap(deprecatedDoc));
            }
            if (!customAnnotation.isEmpty()) {
                finder.setAnnotations(new CustomAnnotationContentSchemaMap(customAnnotation));
            }
            if (resourceMethodDescriptor.isPagingSupported()) {
                finder.setPagingSupported(true);
            }
            findersArray.add(finder);
        }
    }
    return findersArray;
}
Also used : StringArray(com.linkedin.data.template.StringArray) FinderSchemaArray(com.linkedin.restli.restspec.FinderSchemaArray) MetadataSchema(com.linkedin.restli.restspec.MetadataSchema) ParameterSchemaArray(com.linkedin.restli.restspec.ParameterSchemaArray) FinderSchema(com.linkedin.restli.restspec.FinderSchema) CustomAnnotationContentSchemaMap(com.linkedin.restli.restspec.CustomAnnotationContentSchemaMap) DataMap(com.linkedin.data.DataMap)

Example 8 with FinderSchema

use of com.linkedin.restli.restspec.FinderSchema in project rest.li by linkedin.

the class SnapshotGenerator method findModelsAssocation.

private void findModelsAssocation(ResourceSchema resourceSchema, Map<String, NamedDataSchema> foundTypes, List<NamedDataSchema> typeOrder) {
    AssociationSchema association = resourceSchema.getAssociation();
    if (association != null) {
        for (AssocKeySchema assocKeySchema : association.getAssocKeys()) {
            String type = assocKeySchema.getType();
            recordType(type, foundTypes, typeOrder);
        }
        if (association.hasFinders()) {
            for (FinderSchema restMethodSchema : association.getFinders()) {
                findModelsFinder(restMethodSchema, foundTypes, typeOrder);
            }
        }
        if (association.hasMethods()) {
            for (RestMethodSchema restMethodSchema : association.getMethods()) {
                findModelsMethod(restMethodSchema, foundTypes, typeOrder);
            }
        }
        if (association.hasActions()) {
            for (ActionSchema actionSchema : association.getActions()) {
                findModelsAction(actionSchema, foundTypes, typeOrder);
            }
        }
        if (association.hasEntity()) {
            EntitySchema entitySchema = association.getEntity();
            findModelsEntity(entitySchema, foundTypes, typeOrder);
        }
    }
}
Also used : AssociationSchema(com.linkedin.restli.restspec.AssociationSchema) RestMethodSchema(com.linkedin.restli.restspec.RestMethodSchema) FinderSchema(com.linkedin.restli.restspec.FinderSchema) EntitySchema(com.linkedin.restli.restspec.EntitySchema) ActionSchema(com.linkedin.restli.restspec.ActionSchema) AssocKeySchema(com.linkedin.restli.restspec.AssocKeySchema)

Example 9 with FinderSchema

use of com.linkedin.restli.restspec.FinderSchema in project rest.li by linkedin.

the class RequestBuilderSpecGenerator method generateFinders.

private List<RootBuilderMethodSpec> generateFinders(RootBuilderSpec rootBuilderSpec, FinderSchemaArray finderSchemas, String keyClass, String valueClass, String resourceName, List<String> pathKeys, Map<String, String> pathKeyTypes) {
    List<RootBuilderMethodSpec> finderSpecList = new ArrayList<RootBuilderMethodSpec>();
    if (finderSchemas != null) {
        String baseBuilderClass = getBuilderBase(ResourceMethod.FINDER);
        for (FinderSchema finder : finderSchemas) {
            String finderName = finder.getName();
            String builderName = CodeUtil.capitalize(resourceName) + "FindBy" + CodeUtil.capitalize(finderName) + getMethodBuilderSuffix();
            FinderBuilderSpec finderBuilderClass = generateFinderRequestBuilder(rootBuilderSpec.getResource(), baseBuilderClass, keyClass, valueClass, builderName, rootBuilderSpec.getNamespace(), finderName, finder);
            generatePathKeyBindingMethods(pathKeys, finderBuilderClass, pathKeyTypes);
            if (finder.getParameters() != null) {
                generateQueryParamBindingMethods(finder.getParameters(), finderBuilderClass);
            }
            // process custom metadata
            if (finder.getMetadata() != null) {
                String metadataClass = finder.getMetadata().getType();
                ClassTemplateSpec metadataClassSpec = classToTemplateSpec(metadataClass);
                finderBuilderClass.setMetadataType(metadataClassSpec);
            }
            String finderMethod = "findBy" + CodeUtil.capitalize(finderName);
            RootBuilderMethodSpec methodSpec = new RootBuilderMethodSpec(finderMethod, finder.getDoc(), finderBuilderClass, rootBuilderSpec);
            finderBuilderClass.setRootBuilderMethod(methodSpec);
            finderSpecList.add(methodSpec);
        }
    }
    return finderSpecList;
}
Also used : ClassTemplateSpec(com.linkedin.pegasus.generator.spec.ClassTemplateSpec) RootBuilderMethodSpec(com.linkedin.restli.tools.clientgen.builderspec.RootBuilderMethodSpec) ArrayList(java.util.ArrayList) FinderSchema(com.linkedin.restli.restspec.FinderSchema) FinderBuilderSpec(com.linkedin.restli.tools.clientgen.builderspec.FinderBuilderSpec)

Example 10 with FinderSchema

use of com.linkedin.restli.restspec.FinderSchema in project rest.li by linkedin.

the class RestLiHTMLDocumentationRenderer method renderResource.

@Override
public void renderResource(String resourceName, OutputStream out) {
    final ResourceSchema resourceSchema = _resourceSchemas.getResource(resourceName);
    final List<ResourceSchema> parentResources = _resourceSchemas.getParentResources(resourceSchema);
    ExampleRequestResponseGenerator generator = new ExampleRequestResponseGenerator(parentResources, resourceSchema, _schemaResolver);
    if (resourceSchema == null) {
        throw new RoutingException(String.format("Resource \"%s\" does not exist", resourceName), HttpStatus.S_404_NOT_FOUND.getCode());
    }
    final Map<String, Object> pageModel = createPageModel();
    pageModel.put("resource", resourceSchema);
    pageModel.put("resourceName", resourceName);
    pageModel.put("resourceFullName", ResourceSchemaUtil.getFullName(resourceSchema));
    pageModel.put("resourceType", getResourceType(resourceSchema));
    pageModel.put("subResources", _resourceSchemas.getSubResources(resourceSchema));
    final List<ResourceMethodDocView> restMethods = new ArrayList<ResourceMethodDocView>();
    final List<ResourceMethodDocView> finders = new ArrayList<ResourceMethodDocView>();
    final List<ResourceMethodDocView> actions = new ArrayList<ResourceMethodDocView>();
    final MethodGatheringResourceSchemaVisitor visitor = new MethodGatheringResourceSchemaVisitor(resourceName);
    ResourceSchemaCollection.visitResources(_resourceSchemas.getResources().values(), visitor);
    for (RecordTemplate methodSchema : visitor.getAllMethods()) {
        final ExampleRequestResponse capture;
        if (methodSchema instanceof RestMethodSchema) {
            RestMethodSchema restMethodSchema = (RestMethodSchema) methodSchema;
            capture = generator.method(ResourceMethod.valueOf(restMethodSchema.getMethod().toUpperCase()));
        } else if (methodSchema instanceof FinderSchema) {
            FinderSchema finderMethodSchema = (FinderSchema) methodSchema;
            capture = generator.finder(finderMethodSchema.getName());
        } else if (methodSchema instanceof ActionSchema) {
            ActionSchema actionMethodSchema = (ActionSchema) methodSchema;
            final ResourceLevel resourceLevel = (visitor.getCollectionActions().contains(methodSchema) ? ResourceLevel.COLLECTION : ResourceLevel.ENTITY);
            capture = generator.action(actionMethodSchema.getName(), resourceLevel);
        } else {
            capture = null;
        }
        String requestEntity = null;
        String responseEntity = null;
        if (capture != null) {
            try {
                DataMap entityMap;
                if (capture.getRequest().getEntity().length() > 0) {
                    entityMap = DataMapUtils.readMap(capture.getRequest());
                    requestEntity = new String(_codec.mapToBytes(entityMap));
                }
                if (capture.getResponse() != null && capture.getResponse().getEntity() != null && capture.getResponse().getEntity().length() > 0) {
                    entityMap = DataMapUtils.readMap(capture.getResponse());
                    responseEntity = new String(_codec.mapToBytes(entityMap));
                }
            } catch (IOException e) {
                throw new RestLiInternalException(e);
            }
        }
        final ResourceMethodDocView docView = new ResourceMethodDocView(methodSchema, capture, getDoc(methodSchema, resourceSchema.hasSimple()), requestEntity, responseEntity);
        if (methodSchema instanceof RestMethodSchema) {
            restMethods.add(docView);
        } else if (methodSchema instanceof FinderSchema) {
            finders.add(docView);
        } else if (methodSchema instanceof ActionSchema) {
            actions.add(docView);
        }
    }
    pageModel.put("restMethods", restMethods);
    pageModel.put("finders", finders);
    pageModel.put("actions", actions);
    addRelated(resourceSchema, pageModel);
    _templatingEngine.render("resource.vm", pageModel, out);
}
Also used : RoutingException(com.linkedin.restli.server.RoutingException) ResourceSchema(com.linkedin.restli.restspec.ResourceSchema) ExampleRequestResponse(com.linkedin.restli.docgen.examplegen.ExampleRequestResponse) ResourceLevel(com.linkedin.restli.server.ResourceLevel) ArrayList(java.util.ArrayList) RestMethodSchema(com.linkedin.restli.restspec.RestMethodSchema) FinderSchema(com.linkedin.restli.restspec.FinderSchema) IOException(java.io.IOException) ActionSchema(com.linkedin.restli.restspec.ActionSchema) DataMap(com.linkedin.data.DataMap) ExampleRequestResponseGenerator(com.linkedin.restli.docgen.examplegen.ExampleRequestResponseGenerator) RecordTemplate(com.linkedin.data.template.RecordTemplate) RestLiInternalException(com.linkedin.restli.internal.server.RestLiInternalException)

Aggregations

FinderSchema (com.linkedin.restli.restspec.FinderSchema)11 ActionSchema (com.linkedin.restli.restspec.ActionSchema)4 FinderSchemaArray (com.linkedin.restli.restspec.FinderSchemaArray)4 AssociationSchema (com.linkedin.restli.restspec.AssociationSchema)3 CollectionSchema (com.linkedin.restli.restspec.CollectionSchema)3 ResourceSchema (com.linkedin.restli.restspec.ResourceSchema)3 RestMethodSchema (com.linkedin.restli.restspec.RestMethodSchema)3 DataMap (com.linkedin.data.DataMap)2 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)2 RecordTemplate (com.linkedin.data.template.RecordTemplate)2 StringArray (com.linkedin.data.template.StringArray)2 AssocKeySchema (com.linkedin.restli.restspec.AssocKeySchema)2 EntitySchema (com.linkedin.restli.restspec.EntitySchema)2 IdentifierSchema (com.linkedin.restli.restspec.IdentifierSchema)2 MetadataSchema (com.linkedin.restli.restspec.MetadataSchema)2 ResourceLevel (com.linkedin.restli.server.ResourceLevel)2 ArrayList (java.util.ArrayList)2 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)1 DataSchema (com.linkedin.data.schema.DataSchema)1 DataSchemaTraverse (com.linkedin.data.schema.DataSchemaTraverse)1