Exemplo n.º 1
0
        public ProblemDetailsApplicationModelProvider()
        {
            var defaultErrorResponseType = new ProducesErrorResponseTypeAttribute(typeof(MvcProblemDetails));

            ActionModelConventions = new List <IActionModelConvention>
            {
                new ApiConventionApplicationModelConvention(defaultErrorResponseType),
                new ProblemDetailsResultFilterConvention()
            };
        }
Exemplo n.º 2
0
        public ApiBehaviorApplicationModelProvider(
            IOptions <ApiBehaviorOptions> apiBehaviorOptions,
            IModelMetadataProvider modelMetadataProvider,
            IClientErrorFactory clientErrorFactory,
            ILoggerFactory loggerFactory)
        {
            var options = apiBehaviorOptions.Value;

            ActionModelConventions = new List <IActionModelConvention>()
            {
                new ApiVisibilityConvention(),
            };

            if (!options.SuppressMapClientErrors)
            {
                ActionModelConventions.Add(new ClientErrorResultFilterConvention());
            }

            if (!options.SuppressModelStateInvalidFilter)
            {
                ActionModelConventions.Add(new InvalidModelStateFilterConvention());
            }

            if (!options.SuppressConsumesConstraintForFormFileParameters)
            {
                ActionModelConventions.Add(new ConsumesConstraintForFormFileParameterConvention());
            }

            var defaultErrorType          = options.SuppressMapClientErrors ? typeof(void) : typeof(ProblemDetails);
            var defaultErrorTypeAttribute = new ProducesErrorResponseTypeAttribute(defaultErrorType);

            ActionModelConventions.Add(new ApiConventionApplicationModelConvention(defaultErrorTypeAttribute));

            if (!options.SuppressInferBindingSourcesForParameters)
            {
                var convention = new InferParameterBindingInfoConvention(modelMetadataProvider)
                {
                    AllowInferringBindingSourceForCollectionTypesAsFromQuery = options.AllowInferringBindingSourceForCollectionTypesAsFromQuery,
                };

                ActionModelConventions.Add(convention);
            }
        }
    public ApiBehaviorApplicationModelProvider(
        IOptions <ApiBehaviorOptions> apiBehaviorOptions,
        IModelMetadataProvider modelMetadataProvider,
        IClientErrorFactory clientErrorFactory,
        ILoggerFactory loggerFactory,
        IServiceProvider serviceProvider)
    {
        var options = apiBehaviorOptions.Value;

        ActionModelConventions = new List <IActionModelConvention>()
        {
            new ApiVisibilityConvention(),
        };

        if (!options.SuppressMapClientErrors)
        {
            ActionModelConventions.Add(new ClientErrorResultFilterConvention());
        }

        if (!options.SuppressModelStateInvalidFilter)
        {
            ActionModelConventions.Add(new InvalidModelStateFilterConvention());
        }

        if (!options.SuppressConsumesConstraintForFormFileParameters)
        {
            ActionModelConventions.Add(new ConsumesConstraintForFormFileParameterConvention());
        }

        var defaultErrorType          = options.SuppressMapClientErrors ? typeof(void) : typeof(ProblemDetails);
        var defaultErrorTypeAttribute = new ProducesErrorResponseTypeAttribute(defaultErrorType);

        ActionModelConventions.Add(new ApiConventionApplicationModelConvention(defaultErrorTypeAttribute));

        if (!options.SuppressInferBindingSourcesForParameters)
        {
            var serviceProviderIsService = serviceProvider.GetService <IServiceProviderIsService>();
            var convention = options.DisableImplicitFromServicesParameters || serviceProviderIsService is null ?
                             new InferParameterBindingInfoConvention(modelMetadataProvider) :
                             new InferParameterBindingInfoConvention(modelMetadataProvider, serviceProviderIsService);
            ActionModelConventions.Add(convention);
        }
    }
Exemplo n.º 4
0
    /// <summary>
    ///     Initializes a new instance of the <see cref="UmbracoApiBehaviorApplicationModelProvider" /> class.
    /// </summary>
    public UmbracoApiBehaviorApplicationModelProvider(IModelMetadataProvider modelMetadataProvider)
    {
        // see see https://docs.microsoft.com/en-us/aspnet/core/web-api/?view=aspnetcore-3.1#apicontroller-attribute
        // for what these things actually do
        // NOTE: we don't have attribute routing requirements and we cannot use ApiVisibilityConvention without attribute routing
        _actionModelConventions = new List <IActionModelConvention>
        {
            new ClientErrorResultFilterConvention(),                // Ensures the responses without any body is converted into a simple json object with info instead of a string like "Status Code: 404; Not Found"
            new ConsumesConstraintForFormFileParameterConvention(), // If an controller accepts files, it must accept multipart/form-data.

            // This ensures that all parameters of type BindingSource.Body and those of complex type are bound
            // using our own UmbracoJsonModelBinder
            new UmbracoJsonModelBinderConvention(modelMetadataProvider),
        };

        Type defaultErrorType          = typeof(ProblemDetails);
        var  defaultErrorTypeAttribute = new ProducesErrorResponseTypeAttribute(defaultErrorType);

        _actionModelConventions.Add(new ApiConventionApplicationModelConvention(defaultErrorTypeAttribute));
    }
Exemplo n.º 5
0
 /// <summary>
 /// Initializes a new instance of <see cref="ApiConventionApplicationModelConvention"/>.
 /// </summary>
 /// <param name="defaultErrorResponseType">The error type to be used. Use <see cref="void" />
 /// when no default error type is to be inferred.
 /// </param>
 public ApiConventionApplicationModelConvention(ProducesErrorResponseTypeAttribute defaultErrorResponseType)
 {
     DefaultErrorResponseType = defaultErrorResponseType ?? throw new ArgumentNullException(nameof(defaultErrorResponseType));
 }