Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Add IsRequired and DefaultValue to ApiParameterDescription #7957

Closed
wants to merge 2 commits into from

Conversation

pranavkm
Copy link
Contributor

Fixes #7563

/// A parameter is considered required if
/// a) If it's bound from the request body (<see cref="BindingSource.Body"/>).
/// b) If it's a required route value.
/// c) If it has annotations (e.g. BindRequiredAttribute) that indicate it's required.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. This list will be all in one line unless you switch to <list type="bullet"> or <list type="number"> syntax.
  2. Don't repeat "If" in these items.

@@ -324,7 +324,7 @@ public override bool IsReadOnly

/// <inheritdoc />
public override bool IsRequired
{
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this file changing?

@@ -23,7 +21,7 @@ namespace Microsoft.AspNetCore.Mvc.ApiExplorer
/// Implements a provider of <see cref="ApiDescription"/> for actions represented
/// by <see cref="ControllerActionDescriptor"/>.
/// </summary>
public class DefaultApiDescriptionProvider : IApiDescriptionProvider
public partial class DefaultApiDescriptionProvider : IApiDescriptionProvider
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why partial?

parameter.IsRequired = true;
}

if (parameter.ModelMetadata != null && parameter.ModelMetadata.IsBindingRequired)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When would ModelMetadata property be null e.g. is this only for route parameters that don't match an actual parameter (but are added to Results)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unit tests

parameter.IsRequired = true;
}

if (parameter.Source == BindingSource.Path && parameter.RouteInfo != null && !parameter.RouteInfo.IsOptional)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When would RouteInfo property be null e.g. is this only for BindingSource.Path parameters that don't match actual route parameters?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unit tests

else
{
if (parameter.ParameterDescriptor is ControllerParameterDescriptor controllerParameter &&
ParameterDefaultValues.TryGetDeclaredParameterDefaultValue(controllerParameter.ParameterInfo, out var defaultValue))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line should be indented.

}

[Fact]
public void ProcessIsRequired_SetsTrue_ForParametersWithBindRequired()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test is contradictory, description is for a parameter but metadata is for a property.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ApiParameters cover parameters and properties

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🆗 but this test still covers a C# property, making the name confusing. Suggest changing the name to ..._ForApiParameterWithBindRequired or ..._ForPropertyWithBindRequired.

}

[Fact]
public void ProcessIsRequired_DoesNotSetToTrue_ForParametersWithValidationRequired()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test is contradictory, description is for a parameter but metadata is for a property.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ApiParameters cover both parameters and properties

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🆗 but this test still covers a C# property, making the name confusing. Suggest changing the name to ..._ForApiParameterWithValidationRequired or ..._ForPropertyWithValidationRequired.

}

[Fact]
public void ProcessDefaultValue_SetsDefaultRouteValue_FromParameterInfo()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename because test is about setting the ApiParameterDescription.DefaultValue property when the parameter is bound to a Query value.

Copy link
Contributor Author

@pranavkm pranavkm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🆙 📅

parameter.IsRequired = true;
}

if (parameter.ModelMetadata != null && parameter.ModelMetadata.IsBindingRequired)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unit tests

parameter.IsRequired = true;
}

if (parameter.Source == BindingSource.Path && parameter.RouteInfo != null && !parameter.RouteInfo.IsOptional)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unit tests

}

[Fact]
public void ProcessIsRequired_SetsTrue_ForParametersWithBindRequired()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ApiParameters cover parameters and properties

}

[Fact]
public void ProcessIsRequired_DoesNotSetToTrue_ForParametersWithValidationRequired()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ApiParameters cover both parameters and properties

}

[Fact]
public void ProcessIsRequired_SetsTrue_ForParametersWithBindRequired()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🆗 but this test still covers a C# property, making the name confusing. Suggest changing the name to ..._ForApiParameterWithBindRequired or ..._ForPropertyWithBindRequired.

}

[Fact]
public void ProcessIsRequired_DoesNotSetToTrue_ForParametersWithValidationRequired()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🆗 but this test still covers a C# property, making the name confusing. Suggest changing the name to ..._ForApiParameterWithValidationRequired or ..._ForPropertyWithValidationRequired.

@pranavkm pranavkm closed this Jun 22, 2018
@dougbu dougbu deleted the prkrishn/7563 branch July 15, 2018 03:41
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants