// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Reflection;
using Microsoft.AspNetCore.JsonPatch;
using Microsoft.AspNetCore.JsonPatch.Operations;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.AspNetCore.Mvc.ModelBinding;
namespace Microsoft.AspNetCore.Mvc.Formatters.Json
{
///
/// Implements a provider of to change parameters of
/// type to an array of .
///
public class JsonPatchOperationsArrayProvider : IApiDescriptionProvider
{
private readonly IModelMetadataProvider _modelMetadataProvider;
///
/// Creates a new instance of .
///
/// The .
public JsonPatchOperationsArrayProvider(IModelMetadataProvider modelMetadataProvider)
{
_modelMetadataProvider = modelMetadataProvider;
}
///
///
/// The order -999 ensures that this provider is executed right after the Microsoft.AspNetCore.Mvc.ApiExplorer.DefaultApiDescriptionProvider.
///
public int Order => -999;
///
public void OnProvidersExecuting(ApiDescriptionProviderContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
foreach (var result in context.Results)
{
foreach (var parameterDescription in result.ParameterDescriptions)
{
if (typeof(IJsonPatchDocument).GetTypeInfo().IsAssignableFrom(parameterDescription.Type))
{
parameterDescription.Type = typeof(Operation[]);
parameterDescription.ModelMetadata = _modelMetadataProvider.GetMetadataForType(typeof(Operation[]));
}
}
}
}
///
public void OnProvidersExecuted(ApiDescriptionProviderContext context)
{
}
}
}