This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Mutate API description parameter type from JsonPatchDocument to Opera…
…tion[] Addresses #5464
- Loading branch information
Showing
17 changed files
with
236 additions
and
1 deletion.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/Microsoft.AspNetCore.Mvc.ApiExplorer/exceptions.net45.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
[ | ||
{ | ||
"OldTypeId": "public class Microsoft.AspNetCore.Mvc.ApiExplorer.ApiDescription", | ||
"Kind": "Removal" | ||
}, | ||
{ | ||
"OldTypeId": "public class Microsoft.AspNetCore.Mvc.ApiExplorer.ApiDescriptionProviderContext", | ||
"Kind": "Removal" | ||
}, | ||
{ | ||
"OldTypeId": "public class Microsoft.AspNetCore.Mvc.ApiExplorer.ApiParameterDescription", | ||
"Kind": "Removal" | ||
}, | ||
{ | ||
"OldTypeId": "public class Microsoft.AspNetCore.Mvc.ApiExplorer.ApiParameterRouteInfo", | ||
"Kind": "Removal" | ||
}, | ||
{ | ||
"OldTypeId": "public class Microsoft.AspNetCore.Mvc.ApiExplorer.ApiRequestFormat", | ||
"Kind": "Removal" | ||
}, | ||
{ | ||
"OldTypeId": "public class Microsoft.AspNetCore.Mvc.ApiExplorer.ApiResponseFormat", | ||
"Kind": "Removal" | ||
}, | ||
{ | ||
"OldTypeId": "public class Microsoft.AspNetCore.Mvc.ApiExplorer.ApiResponseType", | ||
"Kind": "Removal" | ||
}, | ||
{ | ||
"OldTypeId": "public interface Microsoft.AspNetCore.Mvc.ApiExplorer.IApiDescriptionProvider", | ||
"Kind": "Removal" | ||
} | ||
] |
34 changes: 34 additions & 0 deletions
34
src/Microsoft.AspNetCore.Mvc.ApiExplorer/exceptions.netcore.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
[ | ||
{ | ||
"OldTypeId": "public class Microsoft.AspNetCore.Mvc.ApiExplorer.ApiDescription", | ||
"Kind": "Removal" | ||
}, | ||
{ | ||
"OldTypeId": "public class Microsoft.AspNetCore.Mvc.ApiExplorer.ApiDescriptionProviderContext", | ||
"Kind": "Removal" | ||
}, | ||
{ | ||
"OldTypeId": "public class Microsoft.AspNetCore.Mvc.ApiExplorer.ApiParameterDescription", | ||
"Kind": "Removal" | ||
}, | ||
{ | ||
"OldTypeId": "public class Microsoft.AspNetCore.Mvc.ApiExplorer.ApiParameterRouteInfo", | ||
"Kind": "Removal" | ||
}, | ||
{ | ||
"OldTypeId": "public class Microsoft.AspNetCore.Mvc.ApiExplorer.ApiRequestFormat", | ||
"Kind": "Removal" | ||
}, | ||
{ | ||
"OldTypeId": "public class Microsoft.AspNetCore.Mvc.ApiExplorer.ApiResponseFormat", | ||
"Kind": "Removal" | ||
}, | ||
{ | ||
"OldTypeId": "public class Microsoft.AspNetCore.Mvc.ApiExplorer.ApiResponseType", | ||
"Kind": "Removal" | ||
}, | ||
{ | ||
"OldTypeId": "public interface Microsoft.AspNetCore.Mvc.ApiExplorer.IApiDescriptionProvider", | ||
"Kind": "Removal" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
src/Microsoft.AspNetCore.Mvc.Formatters.Json/JsonPatchOperationsArrayProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// 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 | ||
{ | ||
/// <summary> | ||
/// Implements a provider of <see cref="ApiDescription"/> to change parameters of | ||
/// type <see cref="IJsonPatchDocument"/> to an array of <see cref="Operation"/>. | ||
/// </summary> | ||
public class JsonPatchOperationsArrayProvider : IApiDescriptionProvider | ||
{ | ||
private readonly IModelMetadataProvider _modelMetadataProvider; | ||
|
||
/// <summary> | ||
/// Creates a new instance of <see cref="JsonPatchOperationsArrayProvider"/>. | ||
/// </summary> | ||
/// <param name="modelMetadataProvider">The <see cref="IModelMetadataProvider"/>.</param> | ||
public JsonPatchOperationsArrayProvider(IModelMetadataProvider modelMetadataProvider) | ||
{ | ||
_modelMetadataProvider = modelMetadataProvider; | ||
} | ||
|
||
/// <inheritdoc /> | ||
/// <remarks> | ||
/// The order -999 ensures that this provider is executed right after the <c>Microsoft.AspNetCore.Mvc.ApiExplorer.DefaultApiDescriptionProvider</c>. | ||
/// </remarks> | ||
public int Order | ||
{ | ||
get { return -999; } | ||
} | ||
|
||
/// <inheritdoc /> | ||
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[])); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/// <inheritdoc /> | ||
public void OnProvidersExecuted(ApiDescriptionProviderContext context) | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
test/Microsoft.AspNetCore.Mvc.Formatters.Json.Test/JsonPatchOperationsArrayProviderTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// 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.Collections.Generic; | ||
using Microsoft.AspNetCore.JsonPatch; | ||
using Microsoft.AspNetCore.JsonPatch.Operations; | ||
using Microsoft.AspNetCore.Mvc.Abstractions; | ||
using Microsoft.AspNetCore.Mvc.ApiExplorer; | ||
using Microsoft.AspNetCore.Mvc.ModelBinding; | ||
using Xunit; | ||
|
||
namespace Microsoft.AspNetCore.Mvc.Formatters.Json | ||
{ | ||
public class JsonPatchOperationsArrayProviderTests | ||
{ | ||
[Fact] | ||
public void OnProvidersExecuting_FindsJsonPatchDocuments_ProvidesOperationsArray() | ||
{ | ||
// Arrange | ||
var metadataprovider = new TestModelMetadataProvider(); | ||
var provider = new JsonPatchOperationsArrayProvider(metadataprovider); | ||
var jsonPatchParameterDescription = new ApiParameterDescription | ||
{ | ||
Type = typeof(JsonPatchDocument) | ||
}; | ||
|
||
var stringParameterDescription = new ApiParameterDescription | ||
{ | ||
Type = typeof(string), | ||
}; | ||
|
||
var apiDescription = new ApiDescription(); | ||
apiDescription.ParameterDescriptions.Add(jsonPatchParameterDescription); | ||
apiDescription.ParameterDescriptions.Add(stringParameterDescription); | ||
|
||
var actionDescriptorList = new List<ActionDescriptor>(); | ||
var apiDescriptionProviderContext = new ApiDescriptionProviderContext(actionDescriptorList); | ||
apiDescriptionProviderContext.Results.Add(apiDescription); | ||
|
||
// Act | ||
provider.OnProvidersExecuting(apiDescriptionProviderContext); | ||
|
||
// Assert | ||
Assert.Collection(apiDescription.ParameterDescriptions, | ||
description => | ||
{ | ||
Assert.Equal(typeof(Operation[]), description.Type); | ||
Assert.Equal(typeof(Operation[]), description.ModelMetadata.ModelType); | ||
}, | ||
description => | ||
{ | ||
Assert.Equal(typeof(string), description.Type); | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters