use of com.linkedin.restli.restspec.IdentifierSchema in project rest.li by linkedin.
the class ResourceSchemaToResourceSpecTranslator method collectionToResourceSpec.
@SuppressWarnings("rawtypes")
private ResourceSpec collectionToResourceSpec(ResourceSchema resourceSchema, CollectionSchema collection) {
ActionSchemaArray actions = null, entityActions = null;
StringArray supports = collection.getSupports();
if (collection.hasActions()) {
actions = collection.getActions();
}
if (collection.getEntity().hasActions()) {
entityActions = collection.getEntity().getActions();
}
String schema = resourceSchema.getSchema();
IdentifierSchema identifier = collection.getIdentifier();
if (// in this case we have a "simple" collection resource
identifier.getParams() == null) {
DataSchema key = RestSpecCodec.textToSchema(identifier.getType(), _schemaResolver);
return buildResourceSpec(supports, toTypeSpec(key), null, Collections.<String, Object>emptyMap(), schema, actions, entityActions);
} else // we have a complex collection resource
{
DataSchema keyKeyType = RestSpecCodec.textToSchema(identifier.getType(), _schemaResolver);
DataSchema keyParamsType = RestSpecCodec.textToSchema(identifier.getParams(), _schemaResolver);
ComplexKeySpec<?, ?> complexKeyType = toComplexKey(keyKeyType, keyParamsType);
return buildResourceSpec(supports, new TypeSpec<>(ComplexResourceKey.class, null), complexKeyType, Collections.<String, Object>emptyMap(), schema, actions, entityActions);
}
}
use of com.linkedin.restli.restspec.IdentifierSchema in project rest.li by linkedin.
the class SnapshotGenerator method findModelsCollection.
private void findModelsCollection(ResourceSchema resourceSchema, Map<String, NamedDataSchema> foundTypes, List<NamedDataSchema> typeOrder) {
CollectionSchema collection = resourceSchema.getCollection();
if (collection != null) {
IdentifierSchema identifier = collection.getIdentifier();
findModelsIdentifier(identifier, foundTypes, typeOrder);
findErrorDetailTypeModels(collection, foundTypes, typeOrder);
if (collection.hasFinders()) {
for (FinderSchema restMethodSchema : collection.getFinders()) {
findModels(restMethodSchema.getParameters(), restMethodSchema.getMetadata(), foundTypes, typeOrder);
findErrorDetailTypeModels(restMethodSchema, foundTypes, typeOrder);
}
}
if (collection.hasBatchFinders()) {
for (BatchFinderSchema restMethodSchema : collection.getBatchFinders()) {
findModels(restMethodSchema.getParameters(), restMethodSchema.getMetadata(), foundTypes, typeOrder);
findErrorDetailTypeModels(restMethodSchema, foundTypes, typeOrder);
}
}
if (collection.hasMethods()) {
for (RestMethodSchema restMethodSchema : collection.getMethods()) {
findModels(restMethodSchema.getParameters(), restMethodSchema.getMetadata(), foundTypes, typeOrder);
findErrorDetailTypeModels(restMethodSchema, foundTypes, typeOrder);
}
}
if (collection.hasActions()) {
for (ActionSchema actionSchema : collection.getActions()) {
findModelsAction(actionSchema, foundTypes, typeOrder);
findErrorDetailTypeModels(actionSchema, foundTypes, typeOrder);
}
}
if (collection.hasEntity()) {
EntitySchema entity = collection.getEntity();
findModelsEntity(entity, foundTypes, typeOrder);
}
}
}
use of com.linkedin.restli.restspec.IdentifierSchema in project rest.li by linkedin.
the class ResourceModelEncoder method appendIdentifierNode.
private void appendIdentifierNode(final CollectionSchema collectionNode, final ResourceModel collectionResource) {
IdentifierSchema identifierSchema = new IdentifierSchema();
identifierSchema.setName(collectionResource.getKeyName());
// Otherwise, just set the type to the key class schema type
if (collectionResource.getKeyClass().equals(ComplexResourceKey.class)) {
identifierSchema.setType(buildDataSchemaType(collectionResource.getKeyKeyClass()));
identifierSchema.setParams(buildDataSchemaType(collectionResource.getKeyParamsClass()));
} else {
Key key = collectionResource.getPrimaryKey();
identifierSchema.setType(buildDataSchemaType(key.getType(), key.getDataSchema()));
}
collectionNode.setIdentifier(identifierSchema);
}
use of com.linkedin.restli.restspec.IdentifierSchema in project rest.li by linkedin.
the class RestLiResourceRelationship method findDataModels.
private void findDataModels() {
final ResourceSchemaVisitior visitor = new BaseResourceSchemaVisitor() {
@Override
public void visitResourceSchema(VisitContext visitContext, ResourceSchema resourceSchema) {
final String schema = resourceSchema.getSchema();
// ActionSet resources do not have a schema
if (schema != null) {
final NamedDataSchema schemaSchema = extractSchema(schema);
if (schemaSchema != null) {
connectSchemaToResource(visitContext, schemaSchema);
}
}
}
@Override
public void visitCollectionResource(VisitContext visitContext, CollectionSchema collectionSchema) {
connectErrorDetailTypeToResource(visitContext, collectionSchema);
final IdentifierSchema id = collectionSchema.getIdentifier();
final NamedDataSchema typeSchema = extractSchema(id.getType());
if (typeSchema != null) {
connectSchemaToResource(visitContext, typeSchema);
}
final String params = id.getParams();
if (params != null) {
final NamedDataSchema paramsSchema = extractSchema(params);
if (paramsSchema != null) {
connectSchemaToResource(visitContext, paramsSchema);
}
}
}
@Override
public void visitAssociationResource(VisitContext visitContext, AssociationSchema associationSchema) {
connectErrorDetailTypeToResource(visitContext, associationSchema);
for (AssocKeySchema key : associationSchema.getAssocKeys()) {
final NamedDataSchema keyTypeSchema = extractSchema(key.getType());
if (keyTypeSchema != null) {
connectSchemaToResource(visitContext, keyTypeSchema);
}
}
}
@Override
public void visitSimpleResource(VisitContext visitContext, SimpleSchema simpleSchema) {
connectErrorDetailTypeToResource(visitContext, simpleSchema);
}
@Override
public void visitActionSetResource(VisitContext visitContext, ActionsSetSchema actionSetSchema) {
connectErrorDetailTypeToResource(visitContext, actionSetSchema);
}
@Override
public void visitParameter(VisitContext visitContext, RecordTemplate parentResource, Object parentMethodSchema, ParameterSchema parameterSchema) {
String parameterTypeString = parameterSchema.getType();
if (// the parameter type field contains a inline schema, so we traverse into it
isInlineSchema(parameterTypeString)) {
visitInlineSchema(visitContext, parameterTypeString);
} else {
final NamedDataSchema schema;
// grab the schema name from it
if (parameterSchema.hasItems()) {
schema = extractSchema(parameterSchema.getItems());
} else // the only remaining possibility is that the type field contains the name of a data schema
{
schema = extractSchema(parameterTypeString);
}
if (schema != null) {
connectSchemaToResource(visitContext, schema);
}
}
}
@Override
public void visitRestMethod(VisitContext visitContext, RecordTemplate parentResource, RestMethodSchema restMethodSchema) {
connectErrorDetailTypeToResource(visitContext, restMethodSchema);
}
@Override
public void visitFinder(VisitContext visitContext, RecordTemplate parentResource, FinderSchema finderSchema) {
connectErrorDetailTypeToResource(visitContext, finderSchema);
final MetadataSchema metadata = finderSchema.getMetadata();
if (metadata != null) {
final NamedDataSchema metadataTypeSchema = extractSchema(metadata.getType());
if (metadataTypeSchema != null) {
connectSchemaToResource(visitContext, metadataTypeSchema);
}
}
}
@Override
public void visitBatchFinder(VisitContext visitContext, RecordTemplate parentResource, BatchFinderSchema batchFinderSchema) {
connectErrorDetailTypeToResource(visitContext, batchFinderSchema);
final MetadataSchema metadata = batchFinderSchema.getMetadata();
if (metadata != null) {
final NamedDataSchema metadataTypeSchema = extractSchema(metadata.getType());
if (metadataTypeSchema != null) {
connectSchemaToResource(visitContext, metadataTypeSchema);
}
}
}
@Override
public void visitAction(VisitContext visitContext, RecordTemplate parentResource, ResourceLevel resourceLevel, ActionSchema actionSchema) {
connectErrorDetailTypeToResource(visitContext, actionSchema);
final String returns = actionSchema.getReturns();
if (returns != null) {
if (// the parameter type field contains a inline schema, so we traverse into it
isInlineSchema(returns)) {
visitInlineSchema(visitContext, returns);
} else // otherwise the type field contains the name of a data schema
{
final NamedDataSchema returnsSchema = extractSchema(returns);
if (returnsSchema != null) {
connectSchemaToResource(visitContext, returnsSchema);
}
}
}
final StringArray throwsArray = actionSchema.getThrows();
if (throwsArray != null) {
for (String errorName : throwsArray) {
final NamedDataSchema errorSchema = extractSchema(errorName);
if (errorSchema != null) {
connectSchemaToResource(visitContext, errorSchema);
}
}
}
}
private boolean isInlineSchema(String schemaString) {
return schemaString.startsWith("{");
}
private void visitInlineSchema(VisitContext visitContext, String schemaString) {
DataSchema schema = DataTemplateUtil.parseSchema(schemaString, _schemaResolver, SchemaFormatType.PDSC);
if (schema instanceof ArrayDataSchema) {
DataSchema itemSchema = ((ArrayDataSchema) schema).getItems();
if (itemSchema instanceof NamedDataSchema) {
connectSchemaToResource(visitContext, (NamedDataSchema) itemSchema);
}
}
if (schema instanceof MapDataSchema) {
DataSchema valueSchema = ((MapDataSchema) schema).getValues();
if (valueSchema instanceof NamedDataSchema) {
connectSchemaToResource(visitContext, (NamedDataSchema) valueSchema);
}
}
}
private void connectSchemaToResource(VisitContext visitContext, final NamedDataSchema schema) {
final Node<NamedDataSchema> schemaNode = _relationships.get(schema);
_dataModels.put(schema.getFullName(), schema);
final DataSchemaTraverse traveler = new DataSchemaTraverse();
traveler.traverse(schema, (path, nestedSchema) -> {
if (nestedSchema instanceof RecordDataSchema && nestedSchema != schema) {
final RecordDataSchema nestedRecordSchema = (RecordDataSchema) nestedSchema;
_dataModels.put(nestedRecordSchema.getFullName(), nestedRecordSchema);
final Node<RecordDataSchema> node = _relationships.get(nestedRecordSchema);
schemaNode.addAdjacentNode(node);
}
});
final Node<ResourceSchema> resourceNode = _relationships.get(visitContext.getParentSchema());
resourceNode.addAdjacentNode(schemaNode);
schemaNode.addAdjacentNode(resourceNode);
}
/**
* Given a record which includes {@link ServiceErrorsSchema}, scans for service errors and connects the error
* detail type field (if any) to the resource.
*
* @param visitContext visit context
* @param record record which includes {@link ServiceErrorsSchema}
*/
private void connectErrorDetailTypeToResource(VisitContext visitContext, RecordTemplate record) {
final ServiceErrorsSchema serviceErrorsSchema = new ServiceErrorsSchema(record.data());
if (serviceErrorsSchema.getServiceErrors() != null) {
for (ServiceErrorSchema serviceErrorSchema : serviceErrorsSchema.getServiceErrors()) {
if (serviceErrorSchema.hasErrorDetailType()) {
final NamedDataSchema errorDetailTypeSchema = extractSchema(serviceErrorSchema.getErrorDetailType());
if (errorDetailTypeSchema != null) {
connectSchemaToResource(visitContext, errorDetailTypeSchema);
}
}
}
}
}
};
ResourceSchemaCollection.visitResources(_resourceSchemas.getResources().values(), visitor);
}
Aggregations