예제 #1
0
        public Operation ApiDescriptionToOperation(ApiDescription apiDescription)
        {
            var apiPath    = apiDescription.RelativePathSansQueryString();
            var parameters = apiDescription.ParameterDescriptions
                             .Select(paramDesc => CreateParameter(paramDesc, apiPath))
                             .ToList();

            var operation = new Operation
            {
                Method           = apiDescription.HttpMethod.Method,
                Nickname         = apiDescription.Nickname(),
                Summary          = apiDescription.Documentation ?? "",
                Parameters       = parameters,
                ResponseMessages = new List <ResponseMessage>(),
                Produces         = apiDescription.SupportedResponseFormatters.SelectMany(d => d.SupportedMediaTypes.Select(t => t.MediaType)).ToList(),
                Consumes         = apiDescription.SupportedRequestBodyFormatters.SelectMany(d => d.SupportedMediaTypes.Select(t => t.MediaType)).ToList()
            };

            var responseType = apiDescription.ActualResponseType();

            if (responseType == null)
            {
                operation.Type = "void";
            }
            else
            {
                var dataType = _dataTypeRegistry.GetOrRegister(responseType);
                if (dataType.Type == "object")
                {
                    operation.Type = dataType.Id;
                }
                else
                {
                    operation.Type   = dataType.Type;
                    operation.Format = dataType.Format;
                    operation.Items  = dataType.Items;
                    operation.Enum   = dataType.Enum;
                }
            }

            foreach (var filter in _operationFilters)
            {
                filter.Apply(operation, _dataTypeRegistry, apiDescription);
            }

            return(operation);
        }
예제 #2
0
        /// <summary>
        /// Creates an api operation
        /// </summary>
        /// <param name="api">Description of the api via the ApiExplorer</param>
        /// <param name="docProvider">Access to the XML docs written in code</param>
        /// <returns>An api operation</returns>
        public static ResourceApiOperation CreateResourceApiOperation(ApiDescription api, XmlCommentDocumentationProvider docProvider, DataTypeRegistry dataTypeRegistry)
        {
            ResourceApiOperation rApiOperation = new ResourceApiOperation()
            {
                httpMethod       = api.HttpMethod.ToString(),
                nickname         = docProvider.GetNickname(api.ActionDescriptor),
                responseClass    = docProvider.GetResponseClass(api.ActionDescriptor),
                summary          = api.Documentation,
                notes            = docProvider.GetNotes(api.ActionDescriptor),
                type             = docProvider.GetResponseClass(api.ActionDescriptor),
                parameters       = new List <ResourceApiOperationParameter>(),
                responseMessages = new List <ResourceApiOperationResponseMessage>(),
            };

            var responseType = api.ActualResponseType();

            if (responseType == null)
            {
                rApiOperation.type = "void";
            }
            else
            {
                var dataType = dataTypeRegistry.GetOrRegister(responseType);
                if (dataType.Type == "object")
                {
                    rApiOperation.type = dataType.Id;
                }
                else
                {
                    rApiOperation.type   = dataType.Type;
                    rApiOperation.format = dataType.Format;
                    rApiOperation.items  = dataType.Items;
                    rApiOperation.Enum   = dataType.Enum;
                }
            }

            return(rApiOperation);
        }