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
Add IsRequired and DefaultValue to ApiParameterDescription #7957
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
33 changes: 33 additions & 0 deletions
33
src/Microsoft.AspNetCore.Mvc.ApiExplorer/ApiParameterContext.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,33 @@ | ||
// 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.Mvc.Controllers; | ||
using Microsoft.AspNetCore.Mvc.ModelBinding; | ||
using Microsoft.AspNetCore.Routing.Template; | ||
|
||
namespace Microsoft.AspNetCore.Mvc.ApiExplorer | ||
{ | ||
internal class ApiParameterContext | ||
{ | ||
public ApiParameterContext( | ||
IModelMetadataProvider metadataProvider, | ||
ControllerActionDescriptor actionDescriptor, | ||
IReadOnlyList<TemplatePart> routeParameters) | ||
{ | ||
MetadataProvider = metadataProvider; | ||
ActionDescriptor = actionDescriptor; | ||
RouteParameters = routeParameters; | ||
|
||
Results = new List<ApiParameterDescription>(); | ||
} | ||
|
||
public ControllerActionDescriptor ActionDescriptor { get; } | ||
|
||
public IModelMetadataProvider MetadataProvider { get; } | ||
|
||
public IList<ApiParameterDescription> Results { get; } | ||
|
||
public IReadOnlyList<TemplatePart> RouteParameters { get; } | ||
} | ||
} |
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 |
---|---|---|
|
@@ -4,8 +4,6 @@ | |
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc.Abstractions; | ||
using Microsoft.AspNetCore.Mvc.Controllers; | ||
using Microsoft.AspNetCore.Mvc.Formatters; | ||
|
@@ -177,7 +175,7 @@ private IList<ApiParameterDescription> GetParameters(ApiParameterContext context | |
{ | ||
var visitor = new PseudoModelBindingVisitor(context, actionParameter); | ||
|
||
ModelMetadata metadata = null; | ||
ModelMetadata metadata; | ||
if (_mvcOptions.AllowValidatingTopLevelNodes && | ||
actionParameter is ControllerParameterDescriptor controllerParameterDescriptor && | ||
_modelMetadataProvider is ModelMetadataProvider provider) | ||
|
@@ -232,6 +230,21 @@ actionParameter is ControllerParameterDescriptor controllerParameterDescriptor & | |
} | ||
|
||
// Next, we want to join up any route parameters with those discovered from the action's parameters. | ||
// This will result us in creating a parameter representation for each route parameter that does not | ||
// have a mapping parameter or bound property. | ||
ProcessRouteParameters(context); | ||
|
||
// Set IsRequired=true | ||
ProcessIsRequired(context); | ||
|
||
// Set DefaultValue | ||
ProcessParameterDefaultValue(context); | ||
|
||
return context.Results; | ||
} | ||
|
||
private void ProcessRouteParameters(ApiParameterContext context) | ||
{ | ||
var routeParameters = new Dictionary<string, ApiParameterRouteInfo>(StringComparer.OrdinalIgnoreCase); | ||
foreach (var routeParameter in context.RouteParameters) | ||
{ | ||
|
@@ -271,8 +284,46 @@ actionParameter is ControllerParameterDescriptor controllerParameterDescriptor & | |
Source = BindingSource.Path, | ||
}); | ||
} | ||
} | ||
|
||
return context.Results; | ||
internal static void ProcessIsRequired(ApiParameterContext context) | ||
{ | ||
foreach (var parameter in context.Results) | ||
{ | ||
if (parameter.Source == BindingSource.Body) | ||
{ | ||
parameter.IsRequired = true; | ||
} | ||
|
||
if (parameter.ModelMetadata != null && parameter.ModelMetadata.IsBindingRequired) | ||
{ | ||
parameter.IsRequired = true; | ||
} | ||
|
||
if (parameter.Source == BindingSource.Path && parameter.RouteInfo != null && !parameter.RouteInfo.IsOptional) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When would There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unit tests |
||
{ | ||
parameter.IsRequired = true; | ||
} | ||
} | ||
} | ||
|
||
internal static void ProcessParameterDefaultValue(ApiParameterContext context) | ||
{ | ||
foreach (var parameter in context.Results) | ||
{ | ||
if (parameter.Source == BindingSource.Path) | ||
{ | ||
parameter.DefaultValue = parameter.RouteInfo?.DefaultValue; | ||
} | ||
else | ||
{ | ||
if (parameter.ParameterDescriptor is ControllerParameterDescriptor controllerParameter && | ||
ParameterDefaultValues.TryGetDeclaredParameterDefaultValue(controllerParameter.ParameterInfo, out var defaultValue)) | ||
{ | ||
parameter.DefaultValue = defaultValue; | ||
} | ||
} | ||
} | ||
} | ||
|
||
private ApiParameterRouteInfo CreateRouteInfo(TemplatePart routeParameter) | ||
|
@@ -416,29 +467,6 @@ private IApiRequestMetadataProvider[] GetRequestMetadataAttributes(ControllerAct | |
.ToArray(); | ||
} | ||
|
||
private class ApiParameterContext | ||
{ | ||
public ApiParameterContext( | ||
IModelMetadataProvider metadataProvider, | ||
ControllerActionDescriptor actionDescriptor, | ||
IReadOnlyList<TemplatePart> routeParameters) | ||
{ | ||
MetadataProvider = metadataProvider; | ||
ActionDescriptor = actionDescriptor; | ||
RouteParameters = routeParameters; | ||
|
||
Results = new List<ApiParameterDescription>(); | ||
} | ||
|
||
public ControllerActionDescriptor ActionDescriptor { get; } | ||
|
||
public IModelMetadataProvider MetadataProvider { get; } | ||
|
||
public IList<ApiParameterDescription> Results { get; } | ||
|
||
public IReadOnlyList<TemplatePart> RouteParameters { get; } | ||
} | ||
|
||
private class ApiParameterDescriptionContext | ||
{ | ||
public ModelMetadata ModelMetadata { get; set; } | ||
|
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When would
ModelMetadata
property benull
e.g. is this only for route parameters that don't match an actual parameter (but are added toResults
)?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unit tests