Beispiel #1
0
        private OpenApiParameter GenerateParameter(
            ApiParameterDescription apiParameter,
            SchemaRepository schemaRepository)
        {
            var name = _options.DescribeAllParametersInCamelCase
                ? apiParameter.Name.ToCamelCase()
                : apiParameter.Name;

            var location = (apiParameter.Source != null && ParameterLocationMap.ContainsKey(apiParameter.Source))
                ? ParameterLocationMap[apiParameter.Source]
                : ParameterLocation.Query;

            var isRequired = (apiParameter.IsFromPath()) ||
                             apiParameter.CustomAttributes().Any(attr => RequiredAttributeTypes.Contains(attr.GetType()));

            var schema = (apiParameter.ModelMetadata != null)
                ? _schemaGenerator.GenerateSchema(apiParameter.ModelMetadata.ModelType, schemaRepository)
                : new OpenApiSchema {
                Type = "string"
            };

            // If it's NOT a reference schema, apply contextual metadata (i.e. from custom attributes)
            if (schema.Reference == null)
            {
                // Honor default value for optional action parameters
                var parameterInfo = apiParameter.ParameterInfo();
                if (parameterInfo != null && parameterInfo.HasDefaultValue)
                {
                    schema.Default = OpenApiAnyFactory.TryCreateFor(schema, parameterInfo.DefaultValue, out IOpenApiAny openApiAny)
                        ? openApiAny
                        : null;
                }

                schema.ApplyCustomAttributes(apiParameter.CustomAttributes());
            }

            var parameter = new OpenApiParameter
            {
                Name     = name,
                In       = location,
                Required = isRequired,
                Schema   = schema
            };

            var filterContext = new ParameterFilterContext(
                apiParameter,
                _schemaGenerator,
                schemaRepository,
                apiParameter.ParameterInfo(),
                apiParameter.PropertyInfo());

            foreach (var filter in _options.ParameterFilters)
            {
                filter.Apply(parameter, filterContext);
            }

            return(parameter);
        }
        private OpenApiParameter GenerateParameter(
            ApiParameterDescription apiParameter,
            SchemaRepository schemaRepository)
        {
            var name = _options.DescribeAllParametersInCamelCase
                ? apiParameter.Name.ToCamelCase()
                : apiParameter.Name;

            var location = ParameterLocationMap.ContainsKey(apiParameter.Source)
                ? ParameterLocationMap[apiParameter.Source]
                : ParameterLocation.Query;

            var isRequired = (apiParameter.IsFromPath()) ||
                             apiParameter.CustomAttributes().Any(attr => RequiredAttributeTypes.Contains(attr.GetType()));

            var schema = (apiParameter.ModelMetadata != null)
                ? _schemaGenerator.GenerateSchema(apiParameter.ModelMetadata.ModelType, schemaRepository)
                : new OpenApiSchema {
                Type = "string"
            };

            var defaultValue = apiParameter.CustomAttributes().OfType <DefaultValueAttribute>().FirstOrDefault()?.Value
                               ?? apiParameter.ParameterInfo()?.DefaultValue;

            // NOTE: Oddly, ParameterInfo.DefaultValue returns DBNull if not optional, hence the additional check below
            if (schema.Reference == null && defaultValue != null && defaultValue != DBNull.Value)
            {
                schema.Default = OpenApiAnyFactory.TryCreateFor(schema, defaultValue, out IOpenApiAny openApiAny)
                    ? openApiAny
                    : null;
            }

            var parameter = new OpenApiParameter
            {
                Name     = name,
                In       = location,
                Required = isRequired,
                Schema   = schema
            };

            var filterContext = new ParameterFilterContext(
                apiParameter,
                _schemaGenerator,
                schemaRepository,
                apiParameter.ParameterInfo(),
                apiParameter.PropertyInfo());

            foreach (var filter in _options.ParameterFilters)
            {
                filter.Apply(parameter, filterContext);
            }

            return(parameter);
        }
Beispiel #3
0
        private OpenApiRequestBody GenerateRequestBodyFromBodyParameter(
            ApiDescription apiDescription,
            SchemaRepository schemaRepository,
            ApiParameterDescription bodyParameter)
        {
            var contentTypes = InferRequestContentTypes(apiDescription);

            var isRequired = bodyParameter.CustomAttributes().Any(attr => RequiredAttributeTypes.Contains(attr.GetType()));

            var schema = _schemaGenerator.GenerateSchema(
                bodyParameter.ModelMetadata.ModelType,
                schemaRepository,
                bodyParameter.PropertyInfo(),
                bodyParameter.ParameterInfo());

            return(new OpenApiRequestBody
            {
                Content = contentTypes
                          .ToDictionary(
                    contentType => contentType,
                    contentType => new OpenApiMediaType
                {
                    Schema = schema
                }
                    ),
                Required = isRequired
            });
        }
        public static bool IsRequiredParameter(this ApiParameterDescription apiParameter)
        {
            // From the OpenAPI spec:
            // If the parameter location is "path", this property is REQUIRED and its value MUST be true.
            if (apiParameter.IsFromPath())
            {
                return(true);
            }

            // This is the default logic for IsRequired
            bool IsRequired() => apiParameter.CustomAttributes().Any(attr => RequiredAttributeTypes.Contains(attr.GetType()));

            // This is to keep compatibility with MVC controller logic that has existed in the past
            if (apiParameter.ParameterDescriptor is ControllerParameterDescriptor)
            {
                return(IsRequired());
            }

            // For non-controllers, prefer the IsRequired flag if we're not on netstandard 2.0, otherwise fallback to the default logic.
            return
                (#if !NETSTANDARD2_0
                 apiParameter.IsRequired);
#else
                 IsRequired();
#endif
        }
 internal static void GetAdditionalMetadata(
     this ApiParameterDescription apiParameter,
     ApiDescription apiDescription,
     out ParameterInfo parameterInfo,
     out PropertyInfo propertyInfo,
     out IEnumerable <object> parameterOrPropertyAttributes)
 {
     parameterInfo = apiParameter.ParameterInfo();
     propertyInfo  = apiParameter.PropertyInfo();
     parameterOrPropertyAttributes = apiParameter.CustomAttributes();
 }
Beispiel #6
0
        private OpenApiParameter GenerateParameter(
            ApiParameterDescription apiParameter,
            SchemaRepository schemaRepository)
        {
            var name = _options.DescribeAllParametersInCamelCase
                ? apiParameter.Name.ToCamelCase()
                : apiParameter.Name;

            var location = (apiParameter.Source != null && ParameterLocationMap.ContainsKey(apiParameter.Source))
                ? ParameterLocationMap[apiParameter.Source]
                : ParameterLocation.Query;

            var isRequired = (apiParameter.IsFromPath()) ||
                             apiParameter.CustomAttributes().Any(attr => RequiredAttributeTypes.Contains(attr.GetType()));

            var schema = (apiParameter.ModelMetadata != null)
                ? _schemaGenerator.GenerateSchema(
                apiParameter.ModelMetadata.ModelType,
                schemaRepository,
                apiParameter.PropertyInfo(),
                apiParameter.ParameterInfo())
                : new OpenApiSchema {
                Type = "string"
            };

            var parameter = new OpenApiParameter
            {
                Name     = name,
                In       = location,
                Required = isRequired,
                Schema   = schema
            };

            var filterContext = new ParameterFilterContext(
                apiParameter,
                _schemaGenerator,
                schemaRepository,
                apiParameter.PropertyInfo(),
                apiParameter.ParameterInfo());

            foreach (var filter in _options.ParameterFilters)
            {
                filter.Apply(parameter, filterContext);
            }

            return(parameter);
        }
 public static bool IsRequired(
     this ApiParameterDescription apiParameter)
 {
     return((apiParameter.IsFromPath()) ||
            apiParameter.CustomAttributes().Any(attr => attr is BindRequiredAttribute || attr is RequiredAttribute));
 }