private Operation CreateOperation(ApiDescription apiDescription, SchemaRegistry schemaRegistry) { Contract.Requires(apiDescription != null); Contract.Requires(schemaRegistry != null); Contract.Requires(apiDescription.ParameterDescriptions != null); 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) : CreateParameter(paramDesc, inPath, schemaRegistry)); }) .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.GetOrRegisterODataType(responseType) }); } var operation = new Operation { summary = apiDescription.Documentation, tags = new[] { _options.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 _options.OperationFilters) { Contract.Assume(filter != null); filter.Apply(operation, schemaRegistry, apiDescription); } return(operation); }
private static Parameter CreateParameter(SwaggerApiParameterDescription paramDesc, bool inPath, SchemaRegistry schemaRegistry) { Contract.Requires(paramDesc != null); var @in = inPath ? "path" : MapToSwaggerParameterLocation(paramDesc.SwaggerSource); var parameter = new Parameter { name = paramDesc.Name, description = paramDesc.Documentation, @in = @in }; if (paramDesc.ParameterDescriptor == null) { parameter.type = "string"; parameter.required = true; return(parameter); } parameter.required = inPath || !paramDesc.ParameterDescriptor.IsOptional; parameter.@default = paramDesc.ParameterDescriptor.DefaultValue; var schema = schemaRegistry.GetOrRegisterODataType(paramDesc.ParameterDescriptor.ParameterType); if (parameter.@in == "body") { parameter.schema = schema; } else { parameter.PopulateFrom(schema); } return(parameter); }