Search in sources :

Example 11 with ParameterSchema

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

the class ResourceSchemaToResourceSpecTranslator method toActionMetadata.

// this is dynamic, don't have concrete classes for the FieldDef
@SuppressWarnings({ "unchecked", "rawtypes" })
private ActionMetadata toActionMetadata(ActionSchema action) {
    ArrayList<FieldDef<?>> fieldDefs = new ArrayList<FieldDef<?>>();
    if (action.hasParameters()) {
        for (ParameterSchema parameterSchema : action.getParameters()) {
            DataSchema dataSchema = RestSpecCodec.textToSchema(parameterSchema.getType(), _schemaResolver);
            Class<?> paramClass = toType(dataSchema);
            FieldDef<?> fieldDef = new FieldDef(parameterSchema.getName(), paramClass, dataSchema);
            fieldDefs.add(fieldDef);
        }
    }
    Collection<FieldDef<?>> response;
    if (action.hasReturns()) {
        DataSchema returnType = RestSpecCodec.textToSchema(action.getReturns(), _schemaResolver);
        Class<?> returnClass = toType(returnType);
        response = Collections.<FieldDef<?>>singletonList(new FieldDef("value", returnClass, returnType));
    } else {
        response = Collections.emptyList();
    }
    return new ActionMetadata(action.getName(), new DynamicRecordMetadata(action.getName(), fieldDefs), new DynamicRecordMetadata(action.getName(), response));
}
Also used : DataSchema(com.linkedin.data.schema.DataSchema) EnumDataSchema(com.linkedin.data.schema.EnumDataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) FieldDef(com.linkedin.data.template.FieldDef) DynamicRecordMetadata(com.linkedin.data.template.DynamicRecordMetadata) ArrayList(java.util.ArrayList) ParameterSchema(com.linkedin.restli.restspec.ParameterSchema)

Example 12 with ParameterSchema

use of com.linkedin.restli.restspec.ParameterSchema 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) {
            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) {
            for (AssocKeySchema key : associationSchema.getAssocKeys()) {
                final NamedDataSchema keyTypeSchema = extractSchema(key.getType());
                if (keyTypeSchema != null) {
                    connectSchemaToResource(visitContext, keyTypeSchema);
                }
            }
        }

        @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 visitFinder(VisitContext visitContext, RecordTemplate parentResource, FinderSchema finderSchema) {
            final MetadataSchema metadata = finderSchema.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) {
            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);
            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, new DataSchemaTraverse.Callback() {

                @Override
                public void callback(List<String> path, DataSchema 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);
        }
    };
    ResourceSchemaCollection.visitResources(_resourceSchemas.getResources().values(), visitor);
}
Also used : ResourceSchema(com.linkedin.restli.restspec.ResourceSchema) ResourceLevel(com.linkedin.restli.server.ResourceLevel) MapDataSchema(com.linkedin.data.schema.MapDataSchema) ParameterSchema(com.linkedin.restli.restspec.ParameterSchema) FinderSchema(com.linkedin.restli.restspec.FinderSchema) StringArray(com.linkedin.data.template.StringArray) IdentifierSchema(com.linkedin.restli.restspec.IdentifierSchema) RecordTemplate(com.linkedin.data.template.RecordTemplate) AssociationSchema(com.linkedin.restli.restspec.AssociationSchema) CollectionSchema(com.linkedin.restli.restspec.CollectionSchema) MetadataSchema(com.linkedin.restli.restspec.MetadataSchema) ActionSchema(com.linkedin.restli.restspec.ActionSchema) AssocKeySchema(com.linkedin.restli.restspec.AssocKeySchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) MapDataSchema(com.linkedin.data.schema.MapDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) DataSchemaTraverse(com.linkedin.data.schema.DataSchemaTraverse)

Example 13 with ParameterSchema

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

the class ExampleRequestResponseGenerator method addParams.

private void addParams(ActionRequestBuilder<?, ?> request, DynamicRecordMetadata requestMetadata, ParameterSchemaArray parameters) {
    if (parameters != null) {
        for (ParameterSchema parameter : parameters) {
            FieldDef<?> fieldDef = requestMetadata.getFieldDef(parameter.getName());
            Object value = generateFieldDefValue(fieldDef);
            // to the dereferenced type so the example values can be set on the request without coercing.
            if (fieldDef.getDataSchema().getType() == Type.TYPEREF) {
                FieldDef<?> deRefFieldDef = new FieldDef<>(fieldDef.getName(), fieldDef.getDataClass(), fieldDef.getDataSchema().getDereferencedDataSchema());
                deRefFieldDef.getField().setRecord(fieldDef.getField().getRecord());
                fieldDef = deRefFieldDef;
            }
            request.setParam(fieldDef, value);
        }
    }
}
Also used : FieldDef(com.linkedin.data.template.FieldDef) ParameterSchema(com.linkedin.restli.restspec.ParameterSchema)

Aggregations

ParameterSchema (com.linkedin.restli.restspec.ParameterSchema)13 DataSchema (com.linkedin.data.schema.DataSchema)5 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)4 ParameterSchemaArray (com.linkedin.restli.restspec.ParameterSchemaArray)4 DynamicRecordMetadata (com.linkedin.data.template.DynamicRecordMetadata)3 ActionSchema (com.linkedin.restli.restspec.ActionSchema)3 JClass (com.sun.codemodel.JClass)3 EnumDataSchema (com.linkedin.data.schema.EnumDataSchema)2 MapDataSchema (com.linkedin.data.schema.MapDataSchema)2 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)2 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)2 TyperefDataSchema (com.linkedin.data.schema.TyperefDataSchema)2 FieldDef (com.linkedin.data.template.FieldDef)2 ClassTemplateSpec (com.linkedin.pegasus.generator.spec.ClassTemplateSpec)2 MetadataSchema (com.linkedin.restli.restspec.MetadataSchema)2 JVar (com.sun.codemodel.JVar)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 DataMap (com.linkedin.data.DataMap)1 DataSchemaTraverse (com.linkedin.data.schema.DataSchemaTraverse)1