private IParameter CreateParameter( ApiDescription apiDescription, ApiParameterDescription apiParameterDescription, ISchemaRegistry schemaRegistry) { // Try to retrieve additional metadata that's not provided by ApiExplorer ParameterInfo parameterInfo = null; PropertyInfo propertyInfo = null; var customAttributes = Enumerable.Empty <object>(); if (apiParameterDescription.TryGetParameterInfo(apiDescription, out parameterInfo)) { customAttributes = parameterInfo.GetCustomAttributes(true); } else if (apiParameterDescription.TryGetPropertyInfo(out propertyInfo)) { customAttributes = propertyInfo.GetCustomAttributes(true); } var name = _settings.DescribeAllParametersInCamelCase ? apiParameterDescription.Name.ToCamelCase() : apiParameterDescription.Name; var location = ParameterLocationMap.ContainsKey(apiParameterDescription.Source) ? ParameterLocationMap[apiParameterDescription.Source] : "query"; var schema = (apiParameterDescription.Type != null) ? schemaRegistry.GetOrRegister(apiParameterDescription.Type) : null; var isRequired = customAttributes.Any(attr => new[] { typeof(RequiredAttribute), typeof(BindRequiredAttribute) }.Contains(attr.GetType())); var parameter = (location == "body") ? new BodyParameter { Name = name, Schema = schema, Required = isRequired } : CreateNonBodyParameter( name, location, schema, schemaRegistry, isRequired, customAttributes, parameterInfo); var filterContext = new ParameterFilterContext( apiParameterDescription, schemaRegistry, parameterInfo, propertyInfo); foreach (var filter in _settings.ParameterFilters) { filter.Apply(parameter, filterContext); } return(parameter); }
private IParameter CreateParameter( ApiDescription apiDescription, ApiParameterDescription apiParameterDescription, ISchemaRegistry schemaRegistry) { // Try to retrieve additional metadata that's not directly provided by ApiExplorer ParameterInfo parameterInfo = null; PropertyInfo propertyInfo = null; var customAttributes = Enumerable.Empty <object>(); if (apiParameterDescription.TryGetParameterInfo(apiDescription, out parameterInfo)) { customAttributes = parameterInfo.GetCustomAttributes(true); } else if (apiParameterDescription.TryGetPropertyInfo(out propertyInfo)) { customAttributes = propertyInfo.GetCustomAttributes(true); } var name = _options.DescribeAllParametersInCamelCase ? apiParameterDescription.Name.ToCamelCase() : apiParameterDescription.Name; var isRequired = customAttributes.Any(attr => new[] { typeof(RequiredAttribute), typeof(BindRequiredAttribute) }.Contains(attr.GetType())); var parameter = (apiParameterDescription.Source == BindingSource.Body) ? CreateBodyParameter( apiParameterDescription, name, isRequired, schemaRegistry) : CreateNonBodyParameter( apiParameterDescription, parameterInfo, customAttributes, name, isRequired, schemaRegistry); var filterContext = new ParameterFilterContext( apiParameterDescription, schemaRegistry, parameterInfo, propertyInfo); foreach (var filter in _options.ParameterFilters) { filter.Apply(parameter, filterContext); } return(parameter); }
internal static void GetAdditionalMetadata( this ApiParameterDescription parameterDescription, ApiDescription apiDescription, out ParameterInfo parameterInfo, out PropertyInfo propertyInfo, out IEnumerable <object> parameterOrPropertyAttributes) { parameterInfo = null; propertyInfo = null; parameterOrPropertyAttributes = Enumerable.Empty <object>(); if (parameterDescription.TryGetParameterInfo(apiDescription, out parameterInfo)) { parameterOrPropertyAttributes = parameterInfo.GetCustomAttributes(true); } else if (parameterDescription.TryGetPropertyInfo(out propertyInfo)) { parameterOrPropertyAttributes = propertyInfo.GetCustomAttributes(true); } }