public static IReadOnlyList <KeyValuePair <String, Object?> > GetParameters(IEdmModel edmModel, ODataPathSegment segment, IDictionary <string, SingleValueNode> parameterAliasNodes, Stream?requestStream = null, String?contentType = null) { var parameters = new List <KeyValuePair <String, Object?> >(); IEdmOperation operation; IEnumerable <OperationSegmentParameter> segmentParameters; if (segment is OperationSegment operationSegment) { segmentParameters = operationSegment.Parameters; operation = operationSegment.Operations.Single(); } else if (segment is OperationImportSegment operationImportSegment) { segmentParameters = operationImportSegment.Parameters; operation = (EdmOperation)operationImportSegment.OperationImports.Single().Operation; } else { throw new InvalidOperationException("Not supported segment type " + segment.GetType().Name); } foreach (OperationSegmentParameter segmentParameter in segmentParameters) { Object?value; if (segmentParameter.Value is ConstantNode constantNode) { value = OeEdmClrHelper.GetValue(edmModel, constantNode.Value); } else if (segmentParameter.Value is ConvertNode convertNode) { value = OeEdmClrHelper.GetValue(edmModel, ((ConstantNode)convertNode.Source).Value); } else if (segmentParameter.Value is ParameterAliasNode parameterAliasNode) { value = OeEdmClrHelper.GetValue(edmModel, ((ConstantNode)parameterAliasNodes[parameterAliasNode.Alias]).Value); } else { value = OeEdmClrHelper.GetValue(edmModel, segmentParameter.Value); } parameters.Add(new KeyValuePair <String, Object?>(segmentParameter.Name, value)); } if (parameters.Count == 0 && requestStream != null) { FillParameters(edmModel, parameters, requestStream, operation, contentType); } OrderParameters(operation.Parameters, parameters); return(parameters); }
/// <summary> /// Parses incoming feed/entry/property queries, resolves against the data store and formulates the response. /// </summary> /// <returns>Stream containing the query results if successful, otherwise an error.</returns> public Stream ProcessGetQuery(string requestUri) { object queryResults = null; QueryContext queryContext; try { queryContext = this.GetDefaultQueryContext(); queryResults = queryContext.ResolveQuery(this.Model, this.DataContext); } catch (Exception error) { return(this.WriteErrorResponse(400, error)); } return(this.WriteResponse(200, (messageWriter, writerSettings, message) => { IEdmEntitySet entitySet = queryContext.ResolveEntitySet(); ODataPathSegment lastSegment = queryContext.QueryPath.LastSegment; var expandedProperties = Enumerable.Empty <string>(); if (lastSegment is EntitySetSegment) { ODataWriter resultWriter = messageWriter.CreateODataFeedWriter(entitySet); ResponseWriter.WriteFeed(resultWriter, queryResults as IQueryable, entitySet, this.Model, writerSettings.Version.GetValueOrDefault(), expandedProperties); resultWriter.Flush(); } else if (lastSegment is KeySegment) { ODataWriter resultWriter = messageWriter.CreateODataEntryWriter(entitySet); ResponseWriter.WriteEntry(resultWriter, queryResults, entitySet, this.Model, writerSettings.Version.GetValueOrDefault(), expandedProperties); resultWriter.Flush(); } else if (lastSegment is PropertySegment) { ODataProperty property = ODataObjectModelConverter.CreateODataProperty(queryResults, (lastSegment as PropertySegment).Property.Name); messageWriter.WriteProperty(property); } else { throw new ODataErrorException("Unsupported URI segment " + lastSegment.GetType()); } })); }
private void BuildSelections( SelectExpandClause selectExpandClause, HashSet<IEdmStructuralProperty> allStructuralProperties, HashSet<IEdmStructuralProperty> allNestedProperties, HashSet<IEdmNavigationProperty> allNavigationProperties, HashSet<IEdmAction> allActions, HashSet<IEdmFunction> allFunctions) { foreach (SelectItem selectItem in selectExpandClause.SelectedItems) { if (selectItem is ExpandedNavigationSelectItem) { continue; } PathSelectItem pathSelectItem = selectItem as PathSelectItem; if (pathSelectItem != null) { ValidatePathIsSupported(pathSelectItem.SelectedPath); ODataPathSegment segment = pathSelectItem.SelectedPath.LastSegment; NavigationPropertySegment navigationPropertySegment = segment as NavigationPropertySegment; if (navigationPropertySegment != null) { IEdmNavigationProperty navigationProperty = navigationPropertySegment.NavigationProperty; if (allNavigationProperties.Contains(navigationProperty)) { SelectedNavigationProperties.Add(navigationProperty); } continue; } PropertySegment structuralPropertySegment = segment as PropertySegment; if (structuralPropertySegment != null) { IEdmStructuralProperty structuralProperty = structuralPropertySegment.Property; if (allStructuralProperties.Contains(structuralProperty)) { SelectedStructuralProperties.Add(structuralProperty); } else if (allNestedProperties.Contains(structuralProperty)) { SelectedComplexProperties.Add(structuralProperty); } continue; } OperationSegment operationSegment = segment as OperationSegment; if (operationSegment != null) { AddOperations(allActions, allFunctions, operationSegment); continue; } DynamicPathSegment dynamicPathSegment = segment as DynamicPathSegment; if (dynamicPathSegment != null) { SelectedDynamicProperties.Add(dynamicPathSegment.Identifier); continue; } throw new ODataException(Error.Format(SRResources.SelectionTypeNotSupported, segment.GetType().Name)); } WildcardSelectItem wildCardSelectItem = selectItem as WildcardSelectItem; if (wildCardSelectItem != null) { SelectedStructuralProperties = allStructuralProperties; SelectedComplexProperties = allNestedProperties; SelectedNavigationProperties = allNavigationProperties; SelectAllDynamicProperties = true; continue; } NamespaceQualifiedWildcardSelectItem wildCardActionSelection = selectItem as NamespaceQualifiedWildcardSelectItem; if (wildCardActionSelection != null) { SelectedActions = allActions; SelectedFunctions = allFunctions; continue; } throw new ODataException(Error.Format(SRResources.SelectionTypeNotSupported, selectItem.GetType().Name)); } }
/// <summary> /// Build the $select item, it maybe $select=complex/abc, $select=abc, $select=nav, etc. /// </summary> /// <param name="pathSelectItem">The expanded reference select item.</param> /// <param name="currentLevelPropertiesInclude">The current properties to include at current level.</param> /// <param name="structuralTypeInfo">The structural type properties.</param> private void BuildSelectItem(PathSelectItem pathSelectItem, IDictionary <IEdmStructuralProperty, SelectExpandIncludedProperty> currentLevelPropertiesInclude, EdmStructuralTypeInfo structuralTypeInfo) { Contract.Assert(pathSelectItem != null && pathSelectItem.SelectedPath != null); Contract.Assert(currentLevelPropertiesInclude != null); Contract.Assert(structuralTypeInfo != null); // Verify and process the $select=abc/xyz/.... ODataSelectPath selectPath = pathSelectItem.SelectedPath; IList <ODataPathSegment> remainingSegments; ODataPathSegment segment = selectPath.GetFirstNonTypeCastSegment(out remainingSegments); PropertySegment firstPropertySegment = segment as PropertySegment; if (firstPropertySegment != null) { if (structuralTypeInfo.IsStructuralPropertyDefined(firstPropertySegment.Property)) { // $select=abc/xyz/... SelectExpandIncludedProperty newPropertySelectItem; if (!currentLevelPropertiesInclude.TryGetValue(firstPropertySegment.Property, out newPropertySelectItem)) { newPropertySelectItem = new SelectExpandIncludedProperty(firstPropertySegment); currentLevelPropertiesInclude[firstPropertySegment.Property] = newPropertySelectItem; } newPropertySelectItem.AddSubSelectItem(remainingSegments, pathSelectItem); } return; } // If the first segment is not a property segment, // that segment must be the last segment, so the remainging segments should be null. Contract.Assert(remainingSegments == null); NavigationPropertySegment navigationSegment = segment as NavigationPropertySegment; if (navigationSegment != null) { // for example: $select=NavigationProperty or $select=NS.VipCustomer/VipNav if (structuralTypeInfo.IsNavigationPropertyDefined(navigationSegment.NavigationProperty)) { if (SelectedNavigationProperties == null) { SelectedNavigationProperties = new HashSet <IEdmNavigationProperty>(); } SelectedNavigationProperties.Add(navigationSegment.NavigationProperty); } return; } OperationSegment operationSegment = segment as OperationSegment; if (operationSegment != null) { // for example: $select=NS.Operation, or, $select=NS.VipCustomer/NS.Operation AddOperations(operationSegment, structuralTypeInfo.AllActions, structuralTypeInfo.AllFunctions); return; } DynamicPathSegment dynamicPathSegment = segment as DynamicPathSegment; if (dynamicPathSegment != null) { if (SelectedDynamicProperties == null) { SelectedDynamicProperties = new HashSet <string>(); } SelectedDynamicProperties.Add(dynamicPathSegment.Identifier); return; } // In fact, we should never be here, because it's verified above throw new ODataException(Error.Format(SRResources.SelectionTypeNotSupported, segment.GetType().Name)); }
private void BuildSelections(SelectExpandClause selectExpandClause, HashSet <IEdmStructuralProperty> allStructuralProperties, HashSet <IEdmNavigationProperty> allNavigationProperties, HashSet <IEdmFunctionImport> allActions) { foreach (SelectItem selectItem in selectExpandClause.SelectedItems) { if (selectItem is ExpandedNavigationSelectItem) { continue; } PathSelectItem pathSelectItem = selectItem as PathSelectItem; if (pathSelectItem != null) { ValidatePathIsSupported(pathSelectItem.SelectedPath); ODataPathSegment segment = pathSelectItem.SelectedPath.LastSegment; NavigationPropertySegment navigationPropertySegment = segment as NavigationPropertySegment; if (navigationPropertySegment != null) { IEdmNavigationProperty navigationProperty = navigationPropertySegment.NavigationProperty; if (allNavigationProperties.Contains(navigationProperty)) { SelectedNavigationProperties.Add(navigationProperty); } continue; } PropertySegment structuralPropertySegment = segment as PropertySegment; if (structuralPropertySegment != null) { IEdmStructuralProperty structuralProperty = structuralPropertySegment.Property; if (allStructuralProperties.Contains(structuralProperty)) { SelectedStructuralProperties.Add(structuralProperty); } continue; } OperationSegment operationSegment = segment as OperationSegment; if (operationSegment != null) { foreach (IEdmFunctionImport action in operationSegment.Operations) { if (allActions.Contains(action)) { SelectedActions.Add(action); } } continue; } throw new ODataException(Error.Format(SRResources.SelectionTypeNotSupported, segment.GetType().Name)); } WildcardSelectItem wildCardSelectItem = selectItem as WildcardSelectItem; if (wildCardSelectItem != null) { SelectedStructuralProperties = allStructuralProperties; SelectedNavigationProperties = allNavigationProperties; continue; } ContainerQualifiedWildcardSelectItem wildCardActionSelection = selectItem as ContainerQualifiedWildcardSelectItem; if (wildCardActionSelection != null) { IEnumerable <IEdmFunctionImport> actionsInThisContainer = allActions.Where(a => a.Container == wildCardActionSelection.Container); foreach (IEdmFunctionImport action in actionsInThisContainer) { SelectedActions.Add(action); } continue; } throw new ODataException(Error.Format(SRResources.SelectionTypeNotSupported, selectItem.GetType().Name)); } }