コード例 #1
0
        private Operation CreateOperation(ApiDescription apiDescription, SchemaRegistry schemaRegistry)
        {
            Contract.Requires(apiDescription != null);
            Contract.Requires(schemaRegistry != null);
            Contract.Requires(apiDescription.ParameterDescriptions != null);


            var edmModel = ((ODataRoute)apiDescription.Route).GetEdmModel();

            var parameters = apiDescription.ParameterDescriptions
                             .Select(paramDesc =>
            {
                var inPath = apiDescription.RelativePathSansQueryString().Contains("{" + paramDesc.Name + "}");
                var swaggerApiParameterDescription = paramDesc as SwaggerApiParameterDescription;
                return(swaggerApiParameterDescription != null
                    ? CreateParameter(swaggerApiParameterDescription, inPath, schemaRegistry, edmModel)
                    : CreateParameter(paramDesc, inPath, schemaRegistry, edmModel));
            })
                             .ToList();
            var responses    = new Dictionary <string, Response>();
            var responseType = apiDescription.ResponseType();

            if (responseType == null || responseType == typeof(void))
            {
                responses.Add("204", new Response {
                    description = "No Content"
                });
            }
            else
            {
                responses.Add("200", new Response {
                    description = "OK", schema = schemaRegistry.GetOrRegisterResponseType(edmModel, responseType)
                });
            }

            var operation = new Operation
            {
                summary     = apiDescription.Documentation,
                tags        = new[] { _config.GetSwashbuckleOptions().GroupingKeySelector(apiDescription) },
                operationId = apiDescription.OperationId(),
                produces    = apiDescription.Produces()?.ToList(),
                consumes    = apiDescription.Consumes()?.ToList(),
                parameters  = parameters.Any() ? parameters : null, // parameters can be null but not empty
                responses   = responses,
                deprecated  = apiDescription.IsObsolete()
            };

            foreach (var filter in _config.GetSwashbuckleOptions().OperationFilters)
            {
                Contract.Assume(filter != null);
                filter.Apply(operation, schemaRegistry, apiDescription);
            }

            return(operation);
        }