private IParameter CreateNonBodyParameter(
            ApiParameterDescription apiParameterDescription,
            ParameterInfo parameterInfo,
            IEnumerable <object> customAttributes,
            string name,
            bool isRequired,
            ISchemaRegistry schemaRegistry)
        {
            var location = ParameterLocationMap.ContainsKey(apiParameterDescription.Source)
                ? ParameterLocationMap[apiParameterDescription.Source]
                : "query";

            var nonBodyParam = new NonBodyParameter
            {
                Name     = name,
                In       = location,
                Required = (location == "path") ? true : isRequired,
            };

            if (apiParameterDescription.Type == null)
            {
                nonBodyParam.Type = "string";
            }
            else if (typeof(IFormFile).IsAssignableFrom(apiParameterDescription.Type))
            {
                nonBodyParam.Type = "file";
            }
            else
            {
                // Retrieve a Schema object for the type and copy common fields onto the parameter
                var schema = schemaRegistry.GetOrRegister(apiParameterDescription.Type);

                // NOTE: While this approach enables re-use of SchemaRegistry logic, it introduces complexity
                // and constraints elsewhere (see below) and needs to be refactored!

                if (schema.Ref != null)
                {
                    // The registry created a referenced Schema that needs to be located. This means it's not neccessarily
                    // exclusive to this parameter and so, we can't assign any parameter specific attributes or metadata.
                    schema = schemaRegistry.Definitions[schema.Ref.Replace("#/definitions/", string.Empty)];
                }
                else
                {
                    // It's a value Schema. This means it's exclusive to this parameter and so, we can assign
                    // parameter specific attributes and metadata. Yep - it's hacky!
                    schema.AssignAttributeMetadata(customAttributes);
                    schema.Default = (parameterInfo != null && parameterInfo.IsOptional)
                        ? parameterInfo.DefaultValue
                        : null;
                }

                nonBodyParam.PopulateFrom(schema);
            }

            return(nonBodyParam);
        }
示例#2
0
        private IParameter CreateNonBodyParameter(ServiceEntry serviceEntry, ParameterInfo parameterInfo,
                                                  ISchemaRegistry schemaRegistry)
        {
            var reg          = @"(?<={)[^{}]*(?=})";
            var nonBodyParam = new NonBodyParameter
            {
                Name     = parameterInfo.Name,
                In       = "query",
                Required = true
            };

            if (Regex.IsMatch(serviceEntry.RoutePath, reg) &&
                GetParameters(serviceEntry.RoutePath).Contains(parameterInfo.Name))
            {
                nonBodyParam.In = "path";
            }

            if (parameterInfo.ParameterType == null)
            {
                nonBodyParam.Type = "string";
            }
            else if (typeof(IEnumerable <KeyValuePair <string, StringValues> >).IsAssignableFrom(parameterInfo
                                                                                                 .ParameterType) &&
                     parameterInfo.ParameterType.Name == "HttpFormCollection")
            {
                nonBodyParam.Type = "file";
                nonBodyParam.In   = "formData";
            }
            else
            {
                // Retrieve a Schema object for the type and copy common fields onto the parameter
                var schema = schemaRegistry.GetOrRegister(parameterInfo.ParameterType);

                // NOTE: While this approach enables re-use of SchemaRegistry logic, it introduces complexity
                // and constraints elsewhere (see below) and needs to be refactored!

                if (schema.Ref != null)
                {
                    // The registry created a referenced Schema that needs to be located. This means it's not neccessarily
                    // exclusive to this parameter and so, we can't assign any parameter specific attributes or metadata.
                    schema = schemaRegistry.Definitions[schema.Ref.Replace("#/definitions/", string.Empty)];
                }
                else
                {
                    // It's a value Schema. This means it's exclusive to this parameter and so, we can assign
                    // parameter specific attributes and metadata. Yep - it's hacky!
                    schema.Default = parameterInfo != null && parameterInfo.IsOptional
                        ? parameterInfo.DefaultValue
                        : null;
                }

                nonBodyParam.PopulateFrom(schema);
            }

            return(nonBodyParam);
        }
示例#3
0
        private IParameter CreateParameter(
            ApiDescription apiDescription,
            ApiParameterDescription paramDescription,
            ISchemaRegistry schemaRegistry)
        {
            var location = GetParameterLocation(apiDescription, paramDescription);

            if (location == "path" && !paramDescription.IsRequired())
            {
                throw new NotSupportedException(string.Format(
                                                    "Route contains optional parameters for action - {0}. " +
                                                    "Optional route parameters are not allowed in Swagger 2.0",
                                                    apiDescription.ActionDescriptor.DisplayName));
            }

            var name = _settings.DescribeAllParametersInCamelCase
                ? paramDescription.Name.ToCamelCase()
                : paramDescription.Name;

            var schema = (paramDescription.Type == null) ? null : schemaRegistry.GetOrRegister(paramDescription.Type);

            if (location == "body")
            {
                return(new BodyParameter
                {
                    Name = name,
                    Schema = schema
                });
            }

            var nonBodyParam = new NonBodyParameter
            {
                Name     = name,
                In       = location,
                Required = paramDescription.IsRequired()
            };

            if (schema == null)
            {
                nonBodyParam.Type = "string";
            }
            else
            {
                nonBodyParam.PopulateFrom(schema);
            }

            if (nonBodyParam.Type == "array")
            {
                nonBodyParam.CollectionFormat = "multi";
            }

            return(nonBodyParam);
        }
        private IParameter CreateParameter(
            ApiDescription apiDescription,
            ApiParameterDescription paramDescription,
            ISchemaRegistry schemaRegistry)
        {
            var location = GetParameterLocation(apiDescription, paramDescription);

            var name = _settings.DescribeAllParametersInCamelCase
                ? paramDescription.Name.ToCamelCase()
                : paramDescription.Name;

            var schema = (paramDescription.Type == null) ? null : schemaRegistry.GetOrRegister(paramDescription.Type);

            if (location == "body")
            {
                return(new BodyParameter
                {
                    Name = name,
                    Schema = schema
                });
            }

            var nonBodyParam = new NonBodyParameter
            {
                Name     = name,
                In       = location,
                Required = (location == "path") || paramDescription.IsRequired()
            };

            if (schema == null)
            {
                nonBodyParam.Type = "string";
            }
            else
            {
                // In some cases (e.g. enum types), the schemaRegistry may return a reference instead of a
                // full schema. Retrieve the full schema before populating the parameter description
                var fullSchema = (schema.Ref != null)
                    ? schemaRegistry.Definitions[schema.Ref.Replace("#/definitions/", string.Empty)]
                    : schema;

                nonBodyParam.PopulateFrom(schema);
            }

            if (nonBodyParam.Type == "array")
            {
                nonBodyParam.CollectionFormat = "multi";
            }

            return(nonBodyParam);
        }
        private IParameter CreateServiceKeyParameter()
        {
            var nonBodyParam = new NonBodyParameter
            {
                Name     = "servicekey",
                In       = "query",
                Required = false,
            };
            var schema = new Schema();

            schema.Description = "ServiceKey";
            nonBodyParam.PopulateFrom(schema);
            return(nonBodyParam);
        }
示例#6
0
        private IParameter CreateNonBodyParameter(
            string name,
            string location,
            Schema schema,
            ISchemaRegistry schemaRegistry,
            bool isRequired,
            IEnumerable <object> customAttributes,
            ParameterInfo parameterInfo)
        {
            var nonBodyParam = new NonBodyParameter
            {
                Name     = name,
                In       = location,
                Required = (location == "path") ? true : isRequired,
            };

            if (schema == null)
            {
                nonBodyParam.Type = "string";
            }
            else
            {
                if (schema.Ref != null)
                {
                    // It's a referenced Schema and therefore needs to be located. This also means it's not neccessarily
                    // exclusive to this parameter and so, we can't assign any parameter specific attributes or metadata.
                    schema = schemaRegistry.Definitions[schema.Ref.Replace("#/definitions/", string.Empty)];
                }
                else
                {
                    // It's a value Schema. This means it's exclusive to this parameter and so, we can assign
                    // parameter specific attributes and metadata.
                    // Yep, it's hacky and needs to be refactored - SchemaRegistry should be stateless
                    schema.AssignAttributeMetadata(customAttributes);
                    schema.Default = (parameterInfo != null && parameterInfo.IsOptional)
                        ? parameterInfo.DefaultValue
                        : null;
                }

                nonBodyParam.PopulateFrom(schema);
            }

            if (nonBodyParam.Type == "array")
            {
                nonBodyParam.CollectionFormat = "multi";
            }

            return(nonBodyParam);
        }
示例#7
0
        private IParameter CreateParameter(
            ApiDescription apiDescription,
            ApiParameterDescription paramDescription,
            ISchemaRegistry schemaRegistry)
        {
            var location = GetParameterLocation(apiDescription, paramDescription);

            var name = _settings.DescribeAllParametersInCamelCase
                ? paramDescription.Name.ToCamelCase()
                : paramDescription.Name;

            var schema = (paramDescription.Type == null) ? null : schemaRegistry.GetOrRegister(paramDescription.Type);

            if (location == "body")
            {
                return(new BodyParameter
                {
                    Name = name,
                    Schema = schema
                });
            }

            var nonBodyParam = new NonBodyParameter
            {
                Name     = name,
                In       = location,
                Required = (location == "path") || paramDescription.IsRequired()
            };

            if (schema == null)
            {
                nonBodyParam.Type = "string";
            }
            else
            {
                nonBodyParam.PopulateFrom(schema);
            }

            if (nonBodyParam.Type == "array")
            {
                nonBodyParam.CollectionFormat = "multi";
            }

            return(nonBodyParam);
        }
        /// <summary>
        /// 创建接口参数
        /// </summary>
        /// <param name="pams"></param>
        /// <param name="schemaRegistry"></param>
        /// <returns></returns>
        public IParameter CreateParamters(ControllerParameterDescriptor pams, ISchemaRegistry schemaRegistry)
        {
            var location = GetParameterLocation(pams);

            var schema = (pams.ParameterType == null) ? null : schemaRegistry.GetOrRegister(pams.ParameterType);

            if (location == "body")
            {
                return(new BodyParameter
                {
                    Name = pams.Name,
                    Description = pams.Name,
                    Schema = schema
                });
            }

            if (location != "header" && location != "query")
            {
                location = "path";
            }

            var nonBodyParam = new NonBodyParameter
            {
                Name     = pams.Name,
                In       = location,
                Required = (location == "path"),
            };

            if (schema == null)
            {
                nonBodyParam.Type = "string";
            }
            else
            {
                nonBodyParam.PopulateFrom(schema);
            }

            if (nonBodyParam.Type == "array")
            {
                nonBodyParam.CollectionFormat = "multi";
            }

            return(nonBodyParam);
        }
        private IParameter CreateNonBodyParameter(
            string name,
            string location,
            Schema schema,
            bool isRequired,
            ISchemaRegistry schemaRegistry)
        {
            var nonBodyParam = new NonBodyParameter
            {
                Name     = name,
                In       = location,
                Required = (location == "path") ? true : isRequired
            };

            if (schema == null)
            {
                nonBodyParam.Type = "string";
            }
            else
            {
                // In some cases (e.g. enum types), the schemaRegistry may return a reference instead of a
                // full schema. Retrieve the full schema before populating the parameter description
                var fullSchema = (schema.Ref != null)
                    ? schemaRegistry.Definitions[schema.Ref.Replace("#/definitions/", string.Empty)]
                    : schema;

                nonBodyParam.PopulateFrom(schema);
            }

            if (nonBodyParam.Type == "array")
            {
                nonBodyParam.CollectionFormat = "multi";
            }

            return(nonBodyParam);
        }