private static IEnumerable <KeyValuePair <string, object> > GetOperationExtensions(ApiDescription apiDescription)
        {
            var metadataAttribute      = apiDescription.ActionAttributes().OfType <MetadataAttribute>().SingleOrDefault();
            var dynamicSchemaAttribute = apiDescription.ActionAttributes().OfType <DynamicSchemaLookupAttribute>().SingleOrDefault();
            var extensions             = metadataAttribute.GetSwaggerOperationExtensions();

            return(extensions.Concat(dynamicSchemaAttribute.GetSwaggerExtensions()));
        }
        private static IEnumerable <SwaggerResponseAttribute> GetActionAttributes(ApiDescription apiDesc)
        {
            var controllerAttributes = apiDesc.ControllerAttributes().OfType <SwaggerResponseAttribute>();
            var actionAttributes     = apiDesc.ActionAttributes().OfType <SwaggerResponseAttribute>();

            return(controllerAttributes.Union(actionAttributes));
        }
示例#3
0
        private static void SetResponseModelDescriptions(Operation operation, ISchemaRegistry schemaRegistry, ApiDescription apiDescription)
        {
            var actionAttributes = apiDescription.ActionAttributes();
            var swaggerResponseExampleAttributes = actionAttributes.Where(r => r.GetType() == typeof(SwaggerResponseAttribute));

            foreach (var attribute in swaggerResponseExampleAttributes)
            {
                var attr       = (SwaggerResponseAttribute)attribute;
                var statusCode = attr.StatusCode.ToString();

                var response = operation.Responses.FirstOrDefault(r => r.Key == statusCode);

                if (response.Equals(default(KeyValuePair <string, Response>)) == false)
                {
                    if (response.Value != null)
                    {
                        var definition = schemaRegistry.Definitions[attr.Type.Name];

                        var propertiesWithDescription = attr.Type.GetProperties().Where(prop => prop.IsDefined(typeof(DescriptionAttribute), false));

                        foreach (var prop in propertiesWithDescription)
                        {
                            var descriptionAttribute = (DescriptionAttribute)prop.GetCustomAttributes(typeof(DescriptionAttribute), false).First();
                            var propName             = ToCamelCase(prop.Name);
                            definition.Properties[propName].Description = descriptionAttribute.Description;
                        }
                    }
                }
            }
        }
示例#4
0
        private static void SetResponseModelExamples(Operation operation, ApiDescription apiDescription)
        {
            var responseAttributes = apiDescription
                                     .ActionAttributes()
                                     .OfType <SwaggerResponseExampleAttribute>()
                                     .ToList();

            responseAttributes.AddRange(apiDescription.ControllerAttributes().OfType <SwaggerResponseExampleAttribute>());

            foreach (var attr in responseAttributes)
            {
                var statusCode = attr.StatusCode.ToString();

                var response = operation.Responses.FirstOrDefault(r => r.Key == statusCode);

                if (response.Equals(default(KeyValuePair <string, Response>)) == false)
                {
                    if (response.Value != null)
                    {
                        var provider = ExamplesProvider(_services, attr.ExamplesProviderType);

                        var serializerSettings = SerializerSettings(attr.ContractResolver, attr.JsonConverter);

                        response.Value.Examples = FormatJson(provider, serializerSettings, true);
                    }
                }
            }
        }
        private static void SetRequestModelExamples(Operation operation, ISchemaRegistry schemaRegistry, ApiDescription apiDescription)
        {
            var actionAttributes         = apiDescription.ActionAttributes();
            var swaggerRequestAttributes = actionAttributes.Where(r => r.GetType() == typeof(SwaggerRequestExampleAttribute));

            foreach (var attribute in swaggerRequestAttributes)
            {
                var attr   = (SwaggerRequestExampleAttribute)attribute;
                var schema = schemaRegistry.GetOrRegister(attr.RequestType);

                var request = operation.Parameters.FirstOrDefault(p => p.In == "body" /* && p.schema.@ref == schema.@ref */);

                if (request != null)
                {
                    var provider = (IExamplesProvider)Activator.CreateInstance(attr.ExamplesProviderType);

                    var parts = schema.Ref.Split('/');
                    var name  = parts.Last();

                    var definitionToUpdate = schemaRegistry.Definitions[name];

                    if (definitionToUpdate != null)
                    {
                        var formatter = (IContractResolver)Activator.CreateInstance(attr.JsonResolver);
                        definitionToUpdate.Example = ((dynamic)FormatAsJson(provider, formatter))["application/json"];
                    }
                }
            }
        }
示例#6
0
        private static void SetResponseModelExamples(Operation operation, ApiDescription apiDescription)
        {
            var actionAttributes = apiDescription.ActionAttributes();
            var swaggerResponseExampleAttributes = actionAttributes.Where(r => r.GetType() == typeof(SwaggerResponseExampleAttribute));

            foreach (var attribute in swaggerResponseExampleAttributes)
            {
                var attr       = (SwaggerResponseExampleAttribute)attribute;
                var statusCode = attr.StatusCode.ToString();

                var response = operation.Responses.FirstOrDefault(r => r.Key == statusCode);

                if (response.Equals(default(KeyValuePair <string, Response>)) == false)
                {
                    if (response.Value != null)
                    {
                        var provider           = (IExamplesProvider)Activator.CreateInstance(attr.ExamplesProviderType);
                        var serializerSettings = new JsonSerializerSettings {
                            ContractResolver = attr.ContractResolver
                        };
                        response.Value.Examples = FormatAsJson(provider, serializerSettings);
                    }
                }
            }
        }
示例#7
0
        private static void SetRequestModelExamples(Operation operation, ISchemaRegistry schemaRegistry, ApiDescription apiDescription)
        {
            var actionAttributes         = apiDescription.ActionAttributes();
            var swaggerRequestAttributes = actionAttributes.Where(r => r.GetType() == typeof(SwaggerRequestExampleAttribute));

            foreach (var attribute in swaggerRequestAttributes)
            {
                var attr   = (SwaggerRequestExampleAttribute)attribute;
                var schema = schemaRegistry.GetOrRegister(attr.RequestType);

                var request = operation.Parameters.FirstOrDefault(p => p.In == "body" /* && p.schema.@ref == schema.@ref */);

                if (request != null)
                {
                    var provider = (IExamplesProvider)Activator.CreateInstance(attr.ExamplesProviderType);

                    var parts = schema.Ref.Split('/');
                    var name  = parts.Last();

                    var definitionToUpdate = schemaRegistry.Definitions[name];

                    if (definitionToUpdate != null)
                    {
                        var serializerSettings = new JsonSerializerSettings
                        {
                            ContractResolver  = attr.ContractResolver,
                            NullValueHandling = NullValueHandling.Ignore // ignore null values because swagger does not support null objects https://github.com/OAI/OpenAPI-Specification/issues/229
                        };

                        definitionToUpdate.Example = ((dynamic)FormatAsJson(provider, serializerSettings))["application/json"];
                    }
                }
            }
        }
        private static bool IsFileResponse(ApiDescription apiDescription)
        {
            var result = apiDescription.ActionAttributes().OfType <SwaggerFileResponseAttribute>().Any();

            if (!result)
            {
                result = apiDescription.SupportedResponseTypes.Any(r => r.Type == typeof(Stream));
            }
            return(result);
        }
        private static bool IsExcluded(ApiDescription description)
        {
            var controllerExcluded = description.ControllerAttributes().OfType <AuthorizeAttribute>()
                                     .Any(IsAdministratorRole);

            if (controllerExcluded)
            {
                return(true);
            }

            var actionExcluded = description.ActionAttributes().OfType <AuthorizeAttribute>().Any(IsAdministratorRole);

            if (actionExcluded)
            {
                return(true);
            }

            return(false);
        }
示例#10
0
        public void Apply(Operation operation, OperationFilterContext context)
        {
            ISchemaRegistry schemaRegistry = context.SchemaRegistry;
            ApiDescription  apiDescription = context.ApiDescription;

            var overrideAttribute = apiDescription.ActionAttributes().FirstOrDefault(a => a is SwaggerOperationAttribute);

            // If the method is overwritten, return immediately
            if (overrideAttribute != null)
            {
                return;
            }

            // Configure the name according to the method name
            var controllerDescriptor = apiDescription.ActionDescriptor as Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor;

            if (controllerDescriptor != null)
            {
                operation.OperationId = controllerDescriptor.ControllerName + "Service_" + controllerDescriptor.ActionName;
            }
        }
        private static void SetResponseModelDescriptions(Operation operation, ISchemaRegistry schemaRegistry, ApiDescription apiDescription)
        {
            var swaggerResponseAttributes = apiDescription
                                            .ActionAttributes()
                                            .OfType <SwaggerResponseAttribute>()
                                            .ToList();

            foreach (var attribute in swaggerResponseAttributes)
            {
                var statusCode = attribute.StatusCode.ToString();

                var response = operation.Responses.FirstOrDefault(r => r.Key == statusCode);

                if (response.Equals(default(KeyValuePair <string, Response>)) == false)
                {
                    if (response.Value != null)
                    {
                        UpdateDescriptions(schemaRegistry, attribute.Type, true);
                    }
                }
            }
        }
示例#12
0
        private static void SetResponseModelExamples(Operation operation, ApiDescription apiDescription)
        {
            var responseAttributes = apiDescription.ActionAttributes().OfType <SwaggerResponseExampleAttribute>();

            foreach (var attr in responseAttributes)
            {
                var statusCode = ((int)attr.StatusCode).ToString();

                var response = operation.Responses.FirstOrDefault(r => r.Key == statusCode);

                if (response.Equals(default(KeyValuePair <string, Response>)) == false)
                {
                    if (response.Value != null)
                    {
                        IExamplesProvider provider = (IExamplesProvider)Activator.CreateInstance(attr.ExamplesProviderType);

                        var serializerSettings = new JsonSerializerSettings {
                            ContractResolver = new CamelCasePropertyNamesContractResolver(), Formatting = Formatting.Indented, NullValueHandling = NullValueHandling.Ignore, DefaultValueHandling = DefaultValueHandling.Ignore
                        };
                        response.Value.Examples = FormatJson(provider, serializerSettings, true);
                    }
                }
            }
        }
示例#13
0
        private static void SetRequestModelExamples(Operation operation, ISchemaRegistry schemaRegistry, ApiDescription apiDescription)
        {
            var actionAttributes = apiDescription
                                   .ActionAttributes()
                                   .OfType <SwaggerRequestExampleAttribute>();

            foreach (var attr in actionAttributes)
            {
                var schema = schemaRegistry.GetOrRegister(attr.RequestType);

                var bodyParameters = operation.Parameters.Where(p => p.In == "body").Cast <BodyParameter>();
                var request        = bodyParameters.FirstOrDefault(p => p.Schema.Ref == schema.Ref || p.Schema.Items.Ref == schema.Ref);

                if (request != null)
                {
                    var provider = ExamplesProvider(_services, attr.ExamplesProviderType);

                    // var name = attr.RequestType.Name; // this doesn't work for generic types, so need to to schema.ref split
                    var parts = schema.Ref?.Split('/');
                    if (parts == null)
                    {
                        continue;
                    }

                    var name = parts.Last();

                    if (schemaRegistry.Definitions.ContainsKey(name))
                    {
                        var definitionToUpdate = schemaRegistry.Definitions[name];
                        var serializerSettings = SerializerSettings(attr.ContractResolver, attr.JsonConverter);

                        definitionToUpdate.Example = FormatJson(provider, serializerSettings, false);
                    }
                }
            }
        }
示例#14
0
 internal static bool IsObsolete(this ApiDescription apiDescription)
 {
     return(apiDescription.ActionAttributes().OfType <ObsoleteAttribute>().Any());
 }
示例#15
0
        private static void SetRequestModelExamples(Operation operation, ISchemaRegistry schemaRegistry, ApiDescription apiDescription)
        {
            var actionAttributes = apiDescription
                                   .ActionAttributes()
                                   .OfType <SwaggerRequestExampleAttribute>();

            foreach (var attr in actionAttributes)
            {
                var schema = schemaRegistry.GetOrRegister(attr.RequestType);

                var bodyParameters = operation.Parameters.Where(p => p.In == "body").Cast <BodyParameter>();
                var request        = bodyParameters.FirstOrDefault(p => p?.Schema.Ref == schema.Ref || p.Schema?.Items?.Ref == schema.Ref);

                if (request == null)
                {
                    continue; // The type in their [SwaggerRequestExample(typeof(requestType), ...] is not passed to their controller action method
                }

                var    provider = ExamplesProvider(_services, attr.ExamplesProviderType);
                string name     = null;
                // var name = attr.RequestType.Name; // this doesn't work for generic types, so need to to schema.ref split
                var parts = schema.Ref?.Split('/');

                if (parts != null)
                {
                    name = parts.Last();
                }
                else
                {
                    // schema.Ref can be null for some types, so we have to try get it by attr.RequestType.Name
                    if (attr.RequestType.GetTypeInfo().IsGenericType)
                    {
                        // remove `# from the generic type name
                        var friendlyName = attr.RequestType.Name.Remove(attr.RequestType.Name.IndexOf('`'));
                        // for generic, Schema will be TypeName[GenericTypeName]
                        name = $"{friendlyName}[{string.Join(",", attr.RequestType.GetGenericArguments().Select(a => a.Name).ToList())}]";
                    }
                    else
                    {
                        name = attr.RequestType.Name;
                    }
                }

                if (string.IsNullOrEmpty(name))
                {
                    continue;
                }

                var serializerSettings = SerializerSettings(attr.ContractResolver, attr.JsonConverter);

                if (schemaRegistry.Definitions.ContainsKey(name))
                {
                    var definitionToUpdate = schemaRegistry.Definitions[name];
                    definitionToUpdate.Example = FormatJson(provider, serializerSettings, false);
                }
                else
                {
                    // schema not found in registry, so put example directly on request schema
                    request.Schema.Example = FormatJson(provider, serializerSettings, false);
                }
            }
        }