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

Add BindingSourceMetadataProvider #5750

Merged
merged 6 commits into from
Feb 17, 2017
Merged

Conversation

jbagga
Copy link
Contributor

@jbagga jbagga commented Feb 2, 2017

In progress #5673

cc @rynowak

@jbagga
Copy link
Contributor Author

jbagga commented Feb 2, 2017

@rynowak Should we add two different types of BindingSource? One for types like CancellationToken which should be 'hidden' and another for IFormFile etc. that should not be expanded into multiple ParameterDescriptions?

options.ModelMetadataDetailsProviders.Add(new SpecialParametersBindingMetadataProvider(typeof(CancellationToken)));
options.ModelMetadataDetailsProviders.Add(new SpecialParametersBindingMetadataProvider(typeof(IFormFile)));
options.ModelMetadataDetailsProviders.Add(new SpecialParametersBindingMetadataProvider(typeof(IFormCollection)));
options.ModelMetadataDetailsProviders.Add(new SpecialParametersBindingMetadataProvider(typeof(Stream)));
Copy link
Member

Choose a reason for hiding this comment

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

Why stream?

Copy link
Member

Choose a reason for hiding this comment

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

Can you figure out what the right set of types are to have Special applied?

Copy link
Member

@rynowak rynowak left a comment

Choose a reason for hiding this comment

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

@jbagga - what does swagger say now?

I would imagine that the CancellationToken one is correctly hidden, but the IFormFile one is not. Let's verify that.


namespace Microsoft.AspNetCore.Mvc.ModelBinding.Metadata
{
public class SpecialParametersBindingMetadataProvider : IBindingMetadataProvider
Copy link
Member

Choose a reason for hiding this comment

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

SpecialBindingSourceMetadataProvider

In general always avoid plurals like SpecialParameters in names.

/// Creates a new <see cref="SpecialParametersBindingMetadataProvider"/> for the given <paramref name="type"/>.
/// </summary>
/// <param name="type">
/// The <see cref="Type"/>. All properties with this <see cref="Type"/> will have
Copy link
Member

Choose a reason for hiding this comment

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

Careful here, this isn't what your code does.

@jbagga
Copy link
Contributor Author

jbagga commented Feb 3, 2017

@rynowak Updated

@jbagga jbagga changed the title [Design] Add SpecialParametersBindingMetadataProvider [Design] Add SpecialBindingSourceMetadataProvider Feb 7, 2017
@jbagga jbagga force-pushed the jbagga/ApiExplorerSpecialParams5673 branch from fbfe141 to 21f7fe1 Compare February 10, 2017 01:10
@jbagga
Copy link
Contributor Author

jbagga commented Feb 10, 2017

Added unit tests

Copy link
Member

@rynowak rynowak left a comment

Choose a reason for hiding this comment

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

This looks right. Let's move on to adding tests 👍

We should also take a look at what the output in swagger/swaggerui is


namespace Microsoft.AspNetCore.Mvc.ModelBinding.Metadata
{
public class FormFileBindingSourceMetadataProvider : IBindingMetadataProvider
Copy link
Member

Choose a reason for hiding this comment

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

Why not also take the BindingSource as a parameter and then you don't have to make this class tied to the source you're setting up.

throw new ArgumentNullException(nameof(context));
}

if (Type.IsAssignableFrom(context.Key.ModelType))
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't do what the docs say 😆 this will set the source for the given type and anything assignable to the given type.

public class FormFileBindingSourceMetadataProviderTests
{
[Fact]
public void ChecksParameterType_AssignsFormFileBindingSource()
Copy link
Member

Choose a reason for hiding this comment

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

https://github.com/aspnet/Home/wiki/Engineering-guidelines#unit-test-method-naming

Consider something like CreateBindingMetadata_ForMatchingType_SetsBindingSource.

if you don't have a lot to say then say a little.

@jbagga jbagga force-pushed the jbagga/ApiExplorerSpecialParams5673 branch from 21f7fe1 to 4974ab3 Compare February 14, 2017 20:17
@jbagga
Copy link
Contributor Author

jbagga commented Feb 14, 2017

@rynowak Updated. The swagger hides the CancellationToken and has one field for IFormFile now (not expanded out).


namespace Microsoft.AspNetCore.Mvc.ModelBinding.Metadata
{
public class SpecialBindingSourceMetadataProvider : IBindingMetadataProvider
Copy link
Member

Choose a reason for hiding this comment

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

Just call this BindingSourceMetadataProvider. It's not as special as you think

public class SpecialBindingSourceMetadataProvider : IBindingMetadataProvider
{
public Type Type { get; }
public BindingSource BindingSource { get; }
Copy link
Member

Choose a reason for hiding this comment

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

Properties after constructor


// ModelState
Assert.True(modelState.IsValid);
}
Copy link
Member

Choose a reason for hiding this comment

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

Is this just a test that the binding source has no effect on the runtime behavior?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes

@rynowak
Copy link
Member

rynowak commented Feb 14, 2017

The swagger hides the CancellationToken and has one field for IFormFile now (not expanded out).

@domaindrivendev - I believe this was your ideal behavior right?

@jbagga
Copy link
Contributor Author

jbagga commented Feb 14, 2017

@rynowak Updated

var boundPerson = Assert.IsType<CancellationTokenBundle>(modelBindingResult.Model);
Assert.NotNull(boundPerson);
Assert.Equal("Fred", boundPerson.Name);
Assert.Equal(token, boundPerson.Token);
Copy link
Member

Choose a reason for hiding this comment

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

Assert.Same

@jbagga jbagga changed the title [Design] Add SpecialBindingSourceMetadataProvider Add BindingSourceMetadataProvider Feb 16, 2017
@jbagga jbagga deleted the jbagga/ApiExplorerSpecialParams5673 branch February 22, 2017 22:26
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.

4 participants