Inheritance: PartialSchema
 public HttpParameterDescriptor Map(Parameter swaggerParameter, int parameterIndex, HttpActionDescriptor actionDescriptor)
 {
     // Maybe the parameter is a key parameter, e.g., where Id in the URI path maps to a parameter named 'key'
     if (swaggerParameter.description != null && swaggerParameter.description.StartsWith("key:"))
     {
         // Find either a single 'key' in the route or composite keys
         // which take the form of key<parameter name>
         var keyParameterName = swaggerParameter
                                 .description
                                 .Replace(FindKeyReplacementSubStr, 
                                             String.Empty)
                                 .ToLower();
         var parameterDescriptor = 
             actionDescriptor
                 .GetParameters()?
                 .SingleOrDefault(descriptor =>
                     descriptor.ParameterName.ToLower() == KeyName
                     || descriptor.ParameterName.ToLower().Equals(keyParameterName)
                 );
         if (parameterDescriptor != null && !parameterDescriptor.IsODataLibraryType())
         {
             var httpControllerDescriptor = actionDescriptor.ControllerDescriptor;
             Contract.Assume(httpControllerDescriptor != null);
             return new ODataParameterDescriptor(swaggerParameter.name, parameterDescriptor.ParameterType, parameterDescriptor.IsOptional, parameterDescriptor)
             {
                 Configuration = httpControllerDescriptor.Configuration,
                 ActionDescriptor = actionDescriptor,
                 ParameterBinderAttribute = parameterDescriptor.ParameterBinderAttribute
             };
         }
     }
     return null;
 }
 public HttpParameterDescriptor Map(Parameter swaggerParameter, int parameterIndex, HttpActionDescriptor actionDescriptor)
 {
     var httpParameterDescriptors = actionDescriptor.GetParameters();
     Contract.Assume(httpParameterDescriptors != null);
     return httpParameterDescriptors
         .SingleOrDefault(descriptor => string.Equals(descriptor.ParameterName, swaggerParameter.name, StringComparison.CurrentCultureIgnoreCase));
 }
        public HttpParameterDescriptor Map(Parameter swaggerParameter, int parameterIndex, HttpActionDescriptor actionDescriptor)
        {
            Contract.Requires(swaggerParameter != null);
            Contract.Requires(parameterIndex >= 0);
            Contract.Requires(actionDescriptor != null);
            Contract.Requires(actionDescriptor.GetParameters() != null);
            //Contract.Ensures(Contract.Result<HttpParameterDescriptor>() == null || Contract.Result<HttpParameterDescriptor>().Configuration != null);

            throw new NotImplementedException();
        }
        public RestierParameterDescriptor(Parameter parameter)
        {
            Contract.Requires(parameter != null);

            DefaultValue = null;
            Prefix = null;
            ParameterName = parameter.name;
            IsOptional = !parameter.required ?? false;
            ParameterType = parameter.GetClrType();
        }
        public HttpParameterDescriptor Map(Parameter swaggerParameter, int parameterIndex, HttpActionDescriptor actionDescriptor)
        {
            var required = swaggerParameter.required;
            Contract.Assume(required != null);

            return new ODataParameterDescriptor(swaggerParameter.name, swaggerParameter.GetClrType(), !required.Value, null)
            {
                Configuration = actionDescriptor.ControllerDescriptor.Configuration,
                ActionDescriptor = actionDescriptor
            };
        }
 public HttpParameterDescriptor Map(Parameter swaggerParameter, int parameterIndex, HttpActionDescriptor actionDescriptor)
 {
     if (actionDescriptor.ControllerDescriptor.ControllerName == "Restier")
     {
         return new RestierParameterDescriptor(swaggerParameter)
         {
             Configuration = actionDescriptor.ControllerDescriptor.Configuration
         };
     }
     return null;
 }
        private ApiParameterDescription GetParameterDescription(Parameter parameter, int index, HttpActionDescriptor actionDescriptor)
        {
            var httpParameterDescriptor = GetHttpParameterDescriptor(parameter, index, actionDescriptor);

            return new SwaggerApiParameterDescription
            {
                ParameterDescriptor = httpParameterDescriptor,
                Name = httpParameterDescriptor.Prefix ?? httpParameterDescriptor.ParameterName,
                Documentation = GetApiParameterDocumentation(parameter, httpParameterDescriptor),
                SwaggerSource = parameter.MapToSwaggerSource(),
                Source = parameter.MapToApiParameterSource()
            };
        }
        private static string GetApiParameterDocumentation(Parameter parameter, HttpParameterDescriptor parameterDescriptor)
        {
            Contract.Requires(parameter != null);
            Contract.Requires(parameterDescriptor != null);

            Contract.Assume(parameterDescriptor.Configuration != null);

            var documentationProvider = parameterDescriptor.Configuration.Services.GetDocumentationProvider();

            return documentationProvider != null
                ? documentationProvider.GetDocumentation(parameterDescriptor)
                : parameter.description;
        }
        private HttpParameterDescriptor GetHttpParameterDescriptor(Parameter parameter, int index, HttpActionDescriptor actionDescriptor)
        {
            Contract.Requires(_parameterMappers != null);
            Contract.Ensures(Contract.Result<HttpParameterDescriptor>() != null);
            Contract.Ensures(Contract.Result<HttpParameterDescriptor>().Configuration != null);

            var result = _parameterMappers
                .Select(mapper => mapper.Map(parameter, index, actionDescriptor))
                .FirstOrDefault(httpParameterDescriptor => httpParameterDescriptor != null);

            Contract.Assume(result != null);
            Contract.Assume(result.Configuration != null);
            return result;
        }
        public HttpParameterDescriptor Map(Parameter swaggerParameter, int parameterIndex, HttpActionDescriptor actionDescriptor)
        {
            var required = swaggerParameter.required;
            Contract.Assume(required != null);

            if (swaggerParameter.@in == "body" && swaggerParameter.schema != null && swaggerParameter.schema.type == "object")
            {
                var odataActionParametersDescriptor = actionDescriptor.GetParameters().SingleOrDefault(descriptor => descriptor.ParameterType == typeof (ODataActionParameters));
                Contract.Assume(odataActionParametersDescriptor != null);
                return new ODataActionParameterDescriptor(odataActionParametersDescriptor.ParameterName, typeof(ODataActionParameters), !required.Value, swaggerParameter.schema, odataActionParametersDescriptor)
                {
                    Configuration = actionDescriptor.ControllerDescriptor.Configuration,
                    ActionDescriptor = actionDescriptor
                };
            }
            return null;
        }
        private void ExtractAndAddQueryParams(
            Schema sourceSchema,
            string sourceQualifier,
            bool? sourceRequired,
            SchemaRegistry schemaRegistry,
            IList<Parameter> operationParams)
        {
            foreach (var entry in sourceSchema.properties)
            {
                var propertySchema = entry.Value;
                if (propertySchema.readOnly == true) continue;

                var required = (sourceRequired == true)
                    && sourceSchema.required != null && sourceSchema.required.Contains(entry.Key);

                if (propertySchema.@ref != null)
                {
                    var schema = schemaRegistry.Definitions[[email protected]("#/definitions/", "")];

                    ExtractAndAddQueryParams(
                        schema,
                        sourceQualifier + entry.Key.ToCamelCase() + ".",
                        required,
                        schemaRegistry,
                        operationParams);
                }
                else
                {
                    var param = new Parameter
                    {
                        name = sourceQualifier + entry.Key.ToCamelCase(),
                        @in = "query",
                        required = required,
                        description = entry.Value.description,
                        example = entry.Value.example
                    };

                    param.PopulateFrom(entry.Value);
                    if (param.type == "array")
                        param.collectionFormat = "multi";
                    operationParams.Add(param);
                }
            }
        }
 public HttpParameterDescriptor Map(Parameter swaggerParameter, int parameterIndex, HttpActionDescriptor actionDescriptor)
 {
     if (swaggerParameter.@in != "query" && parameterIndex < actionDescriptor.GetParameters().Count)
     {
         var parameterDescriptor = actionDescriptor.GetParameters()[parameterIndex];
         if (parameterDescriptor != null && !parameterDescriptor.IsODataLibraryType())
         {
             var httpControllerDescriptor = actionDescriptor.ControllerDescriptor;
             Contract.Assume(httpControllerDescriptor != null);
             return new ODataParameterDescriptor(swaggerParameter.name, parameterDescriptor.ParameterType, parameterDescriptor.IsOptional, parameterDescriptor)
             {
                 Configuration = httpControllerDescriptor.Configuration,
                 ActionDescriptor = actionDescriptor,
                 ParameterBinderAttribute = parameterDescriptor.ParameterBinderAttribute
             };
         }
     }
     return null;
 }
 public HttpParameterDescriptor Map(Parameter swaggerParameter, int parameterIndex, HttpActionDescriptor actionDescriptor)
 {
     // Maybe the parameter is a key parameter, e.g., where Id in the URI path maps to a parameter named 'key'
     if (swaggerParameter.description != null && swaggerParameter.description.StartsWith("key:"))
     {
         var parameterDescriptor = actionDescriptor.GetParameters()?.SingleOrDefault(descriptor => descriptor.ParameterName == "key");
         if (parameterDescriptor != null && !parameterDescriptor.IsODataLibraryType())
         {
             var httpControllerDescriptor = actionDescriptor.ControllerDescriptor;
             Contract.Assume(httpControllerDescriptor != null);
             return new ODataParameterDescriptor(swaggerParameter.name, parameterDescriptor.ParameterType, parameterDescriptor.IsOptional, parameterDescriptor)
             {
                 Configuration = httpControllerDescriptor.Configuration,
                 ActionDescriptor = actionDescriptor,
                 ParameterBinderAttribute = parameterDescriptor.ParameterBinderAttribute
             };
         }
     }
     return null;
 }
Exemple #14
0
            public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
            {
                if (operation.parameters is null)
                {
                    operation.parameters = new List <Swashbuckle.Swagger.Parameter>();
                }
                var param = new Swashbuckle.Swagger.Parameter()
                {
                    name        = "Authorization",
                    @in         = "header",
                    description = "JWT Token",
                    required    = false,
                    type        = "string",
                    @default    = "Bearer "
                };

                if (apiDescription.ActionDescriptor.GetCustomAttributes <AuthorizeAttribute>().Any() || apiDescription.ActionDescriptor.ControllerDescriptor.GetCustomAttributes <AuthorizeAttribute>().Any())
                {
                    param.required = true;
                    operation.parameters.Add(param);
                }
            }
        private void ExtractAndAddQueryParams(
            Schema sourceSchema,
            string sourceQualifier,
            bool sourceRequired,
            SchemaRegistry schemaRegistry,
            IList<Parameter> operationParams)
        {
            foreach (var entry in sourceSchema.properties)
            {
                var propertySchema = entry.Value;
                var required = sourceRequired
                    && sourceSchema.required != null && sourceSchema.required.Contains(entry.Key); 

                if (propertySchema.@ref != null)
                {
                    var schema = schemaRegistry.Definitions[[email protected]("#/definitions/", "")];
                    ExtractAndAddQueryParams(
                        schema,
                        sourceQualifier + entry.Key.ToLowerInvariant() + ".",
                        required,
                        schemaRegistry,
                        operationParams);
                }
                else
                {
                    var param = new Parameter
                    {
                        name =  sourceQualifier + entry.Key.ToLowerInvariant(),
                        @in = "query",
                        required = required,
                        description = entry.Value.description
                    };
                    param.PopulateFrom(entry.Value);
                    operationParams.Add(param);
                }
            }
        }
        private Parameter CreateParameter(ApiParameterDescription paramDesc, bool inPath, SchemaRegistry schemaRegistry)
        {
            var @in = (inPath)
                ? "path"
                : (paramDesc.Source == ApiParameterSource.FromUri) ? "query" : "body";

            var parameter = new Parameter
            {
                name = paramDesc.Name,
                @in = @in
            };

            if (paramDesc.ParameterDescriptor == null)
            {
                parameter.type = "string";
                parameter.required = true;
                return parameter; 
            }

            parameter.required = !paramDesc.ParameterDescriptor.IsOptional;

            var schema = schemaRegistry.GetOrRegister(paramDesc.ParameterDescriptor.ParameterType);
            if (parameter.@in == "body")
                parameter.schema = schema;
            else
                parameter.PopulateFrom(schema);

            return parameter;
        }
        private static Parameter CreateParameter(SwaggerApiParameterDescription paramDesc, bool inPath, SchemaRegistry schemaRegistry, IEdmModel edmModel)
        {
            Contract.Requires(paramDesc != null);
            Contract.Requires(schemaRegistry != null);
            Contract.Assume(paramDesc.ParameterDescriptor != null);

            var @in = inPath
                ? "path"
                : MapToSwaggerParameterLocation(paramDesc.SwaggerSource);

            var parameter = new Parameter
            {
                name = paramDesc.Name,
                description = paramDesc.Documentation,
                @in = @in,
                required = inPath || !paramDesc.ParameterDescriptor.IsOptional,
                @default = paramDesc.ParameterDescriptor.DefaultValue
            };


            var parameterType = paramDesc.ParameterDescriptor.ParameterType;
            Contract.Assume(parameterType != null);
            var schema = schemaRegistry.GetOrRegisterParameterType(edmModel, paramDesc.ParameterDescriptor);
            if (parameter.@in == "body")
                parameter.schema = schema;
            else
                parameter.PopulateFrom(schema);

            return parameter;
        }
        private string GetSummaryForParameterAndAddDefaultValue(ApiDescription apiDescription, Parameter param)
        {
            var parameters = apiDescription.ActionDescriptor.GetParameters();
            if (parameters != null)
            {
                foreach (HttpParameterDescriptor paramDesc in parameters)
                {
                    if (paramDesc.ParameterName.Equals(param.name, StringComparison.InvariantCultureIgnoreCase))
                    {
                        if (param.@enum != null && paramDesc.DefaultValue != null)
                        {
                            param.@default = param.@enum[(int)paramDesc.DefaultValue];
                        }
                        else
                        {
                            param.@default = paramDesc.DefaultValue;
                        }

                        var customSummaryAttribute = paramDesc.GetCustomAttributes<CustomSummaryAttribute>();
                        if (customSummaryAttribute != null && customSummaryAttribute.Count > 0)
                        {
                            return customSummaryAttribute[0].Summary;
                        }
                    }
                }
            }

            return null;
        }
Exemple #19
0
        private Parameter CreateParameter(string location, ApiParameterDescription paramDesc, SchemaRegistry schemaRegistry)
        {
            var parameter = new Parameter
            {
                @in = location,
                name = paramDesc.Name
            };

            if (paramDesc.ParameterDescriptor == null)
            {
                parameter.type = "string";
                parameter.required = true;
                return parameter;
            }

            parameter.required = location == "path" || !paramDesc.ParameterDescriptor.IsOptional;
            parameter.@default = paramDesc.ParameterDescriptor.DefaultValue;

            var schema = schemaRegistry.GetOrRegister(paramDesc.ParameterDescriptor.ParameterType);
            if (parameter.@in == "body")
                parameter.schema = schema;
            else
                parameter.PopulateFrom(schema);

            return parameter;
        }
        private static Type GetEntityTypeForBodyParameter(Parameter parameter)
        {
            Contract.Requires(parameter != null);
            Contract.Requires(parameter.schema != null);
            Contract.Requires(!string.IsNullOrWhiteSpace(parameter.schema.@ref));
            Contract.Requires(parameter.@in == "body");

            return parameter.schema.GetReferencedType();
        }
        private Parameter CreateParameter(ApiParameterDescription paramDesc, bool inPath, SchemaRegistry schemaRegistry)
        {
            var @in = (inPath)
                ? "path"
                : (paramDesc.Source == ApiParameterSource.FromUri) ? "query" : "body";

            var parameter = new Parameter
            {
                name = paramDesc.Name,
                @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.GetOrRegister(paramDesc.ParameterDescriptor.ParameterType);

            var reflectedDescriptor = (ReflectedHttpParameterDescriptor)paramDesc.ParameterDescriptor;
            foreach (var attribute in reflectedDescriptor.ParameterInfo.GetCustomAttributes(true))
            {
                SchemaExtensions.SetSchemaDetails(schema, attribute);
            }

            //if (paramDesc.ParameterDescriptor.ActionDescriptor != null)
            //{
            //    var actionIgnore = paramDesc.ParameterDescriptor.ActionDescriptor.GetCustomAttributes<SwaggerIgnore>();

            //    if (actionIgnore != null)
            //    {
            //        parameter.ignore = true;
            //    }
            //}

            if (parameter.@in == "body")
                parameter.schema = schema;
            else
                parameter.PopulateFrom(schema);

            return parameter;
        }
        private Parameter CreateParameter(ApiParameterDescription apiParamDesc, string apiPath)
        {
            var paramType = "";
            switch (apiParamDesc.Source)
            {
                case ApiParameterSource.FromBody:
                    paramType = "body";
                    break;
                case ApiParameterSource.FromUri:
                    paramType = apiPath.Contains(String.Format("{{{0}}}", apiParamDesc.Name)) ? "path" : "query";
                    break;
            }

            var parameter = new Parameter
            {
                ParamType = paramType,
                Name = apiParamDesc.Name,
                Description = apiParamDesc.Documentation,
                Required = !apiParamDesc.ParameterDescriptor.IsOptional
            };

            var dataType = _dataTypeRegistry.GetOrRegister(apiParamDesc.ParameterDescriptor.ParameterType);
            if (dataType.Type == "object")
            {
                parameter.Type = dataType.Id;
            }
            else
            {
                parameter.Type = dataType.Type;
                parameter.Format = dataType.Format;
                parameter.Items = dataType.Items;
                parameter.Enum = dataType.Enum;
            }

            return parameter;
        }