Exemple #1
0
        public void SwaggerPathHandlerWorksForSwaggerMetadataUri(string swaggerMetadataUri)
        {
            SwaggerPathHandler handler = new SwaggerPathHandler();
            IEdmModel          model   = new EdmModel();

            ODataPath path = handler.Parse(model, "http://any", swaggerMetadataUri);

            ODataPathSegment segment = path.Segments.Last();

            Assert.NotNull(path);
            Assert.Null(path.NavigationSource);
            Assert.Null(path.EdmType);
            Assert.Equal("$swagger", segment.ToString());
            Assert.IsType <SwaggerPathSegment>(segment);
        }
        /// <summary>Creates the first <see cref="SegmentInfo"/> for a request.</summary>
        /// <param name="segment">The text of the segment.</param>
        /// <returns>A description of the information on the segment.</returns>
        private SegmentInfo CreateFirstSegment(ODataPathSegment segment)
        {
            Debug.Assert(segment != null, "identifier != null");

            // Look for well-known system entry points.
            if (segment is MetadataSegment)
            {
                return new SegmentInfo { Identifier = XmlConstants.UriMetadataSegment, TargetKind = RequestTargetKind.Metadata };
            }

            if (segment is BatchSegment)
            {
                return new SegmentInfo { Identifier = XmlConstants.UriBatchSegment, TargetKind = RequestTargetKind.Batch };
            }

            if (segment is CountSegment)
            {
                // $count on root: throw
                throw DataServiceException.CreateResourceNotFound(Strings.RequestUriProcessor_CountOnRoot);
            }

            // Look for a service operation.
            OperationImportSegment serviceOperation = segment as OperationImportSegment;
            if (serviceOperation != null)
            {
                Debug.Assert(serviceOperation.OperationImports.Count() == 1, "Operation import segment should only ever have exactly one operation. Was a change made to how MetadataProviderEdmModel finds actions/service operations");
                var operationImport = serviceOperation.OperationImports.Single();
                var operation = ((MetadataProviderEdmOperationImport)operationImport).ServiceOperation;
                Debug.Assert(operation != null, "operation != null");

                if (operation.Kind == OperationKind.ServiceOperation)
                {
                    return CreateSegmentForServiceOperation(operation);
                }

                Debug.Assert(operation.Kind == OperationKind.Action, "serviceAction.Kind == OperationKind.Action");
                return this.CreateSegmentForServiceAction(null /*previousSegment*/, operation);
            }

            var batchReferenceSegment = segment as BatchReferenceSegment;
            if (batchReferenceSegment != null)
            {
                SegmentInfo referencedSegmentInfo = this.crossReferenceCallback(batchReferenceSegment.ContentId);
                Debug.Assert(referencedSegmentInfo != null, "Could not find SegmentInfo for content-id: " + batchReferenceSegment.ContentId);
                referencedSegmentInfo.Identifier = batchReferenceSegment.ContentId;
                return referencedSegmentInfo;
            }

            // Look for an entity set.
            EntitySetSegment entitySetSegment = segment as EntitySetSegment;
            if (entitySetSegment != null)
            {
                var container = ((IResourceSetBasedEdmEntitySet)entitySetSegment.EntitySet).ResourceSet;
                Debug.Assert(container != null, "container != null");
                SegmentInfo segmentInfo = new SegmentInfo
                {
                    Identifier = container.Name,
                    TargetResourceSet = container,
                    TargetResourceType = container.ResourceType,
                    TargetSource = RequestTargetSource.EntitySet,
                    TargetKind = RequestTargetKind.Resource,
                    SingleResult = false
                };

                return segmentInfo;
            }

            WebUtil.CheckResourceExists(false, segment.ToString());
            return null;
        }
        /// <summary>Creates the first <see cref="SegmentInfo"/> for a request.</summary>
        /// <param name="segment">The text of the segment.</param>
        /// <returns>A description of the information on the segment.</returns>
        private SegmentInfo CreateFirstSegment(ODataPathSegment segment)
        {
            Debug.Assert(segment != null, "identifier != null");

            // Look for well-known system entry points.
            if (segment is MetadataSegment)
            {
                return(new SegmentInfo {
                    Identifier = XmlConstants.UriMetadataSegment, TargetKind = RequestTargetKind.Metadata
                });
            }

            if (segment is BatchSegment)
            {
                return(new SegmentInfo {
                    Identifier = XmlConstants.UriBatchSegment, TargetKind = RequestTargetKind.Batch
                });
            }

            if (segment is CountSegment)
            {
                // $count on root: throw
                throw DataServiceException.CreateResourceNotFound(Strings.RequestUriProcessor_CountOnRoot);
            }

            // Look for a service operation.
            OperationImportSegment serviceOperation = segment as OperationImportSegment;

            if (serviceOperation != null)
            {
                Debug.Assert(serviceOperation.OperationImports.Count() == 1, "Operation import segment should only ever have exactly one operation. Was a change made to how MetadataProviderEdmModel finds actions/service operations");
                var operationImport = serviceOperation.OperationImports.Single();
                var operation       = ((MetadataProviderEdmOperationImport)operationImport).ServiceOperation;
                Debug.Assert(operation != null, "operation != null");

                if (operation.Kind == OperationKind.ServiceOperation)
                {
                    return(CreateSegmentForServiceOperation(operation));
                }

                Debug.Assert(operation.Kind == OperationKind.Action, "serviceAction.Kind == OperationKind.Action");
                return(this.CreateSegmentForServiceAction(null /*previousSegment*/, operation));
            }

            var batchReferenceSegment = segment as BatchReferenceSegment;

            if (batchReferenceSegment != null)
            {
                SegmentInfo referencedSegmentInfo = this.crossReferenceCallback(batchReferenceSegment.ContentId);
                Debug.Assert(referencedSegmentInfo != null, "Could not find SegmentInfo for content-id: " + batchReferenceSegment.ContentId);
                referencedSegmentInfo.Identifier = batchReferenceSegment.ContentId;
                return(referencedSegmentInfo);
            }

            // Look for an entity set.
            EntitySetSegment entitySetSegment = segment as EntitySetSegment;

            if (entitySetSegment != null)
            {
                var container = ((IResourceSetBasedEdmEntitySet)entitySetSegment.EntitySet).ResourceSet;
                Debug.Assert(container != null, "container != null");
                SegmentInfo segmentInfo = new SegmentInfo
                {
                    Identifier         = container.Name,
                    TargetResourceSet  = container,
                    TargetResourceType = container.ResourceType,
                    TargetSource       = RequestTargetSource.EntitySet,
                    TargetKind         = RequestTargetKind.Resource,
                    SingleResult       = false
                };

                return(segmentInfo);
            }

            WebUtil.CheckResourceExists(false, segment.ToString());
            return(null);
        }