public void GetAttributesForType_PropertyAttributes_IsNull() { // Arrange & Act var attributes = ModelAttributes.GetAttributesForType(typeof(BaseViewModel)); // Assert Assert.Null(attributes.PropertyAttributes); }
protected (ModelMetadataIdentity, ModelAttributes) GetMockedDependencies() { var modelType = typeof(CompositeModel); var key = ModelMetadataIdentity.ForType(modelType); var attributes = ModelAttributes.GetAttributesForType(modelType); return(key, attributes); }
public void GetAttributesForType_IncludesMetadataAttributes() { // Arrange & Act var attributes = ModelAttributes.GetAttributesForType(typeof(BaseViewModel)); // Assert Assert.Single(attributes.Attributes.OfType <ClassValidator>()); Assert.Single(attributes.TypeAttributes.OfType <ClassValidator>()); }
public void CreateModelMetadata_NullContainerType_DoesNotSetDisplayName() { DefaultMetadataDetails metaDetails = new DefaultMetadataDetails( new ModelMetadataIdentity(), ModelAttributes.GetAttributesForType(typeof(RoleView))); ICompositeMetadataDetailsProvider details = Substitute.For <ICompositeMetadataDetailsProvider>(); DisplayNameMetadataProviderProxy provider = new DisplayNameMetadataProviderProxy(details); String actual = provider.BaseCreateModelMetadata(metaDetails).DisplayName; Assert.Null(actual); }
public void CreateDisplayMetadata_NullContainerType_DoesNotSetDisplayName() { DisplayMetadataProvider provider = new DisplayMetadataProvider(); DisplayMetadataProviderContext context = new DisplayMetadataProviderContext( ModelMetadataIdentity.ForType(typeof(RoleView)), ModelAttributes.GetAttributesForType(typeof(RoleView))); provider.CreateDisplayMetadata(context); Assert.Null(context.DisplayMetadata.DisplayName); }
public void CreateDisplayMetadata_NullResource_DoesNotSetDisplayName() { PropertyInfo property = typeof(AllTypesView).GetProperty(nameof(AllTypesView.StringField)) !; DisplayMetadataProviderContext context = new( ModelMetadataIdentity.ForProperty(property, typeof(String), typeof(AllTypesView)), ModelAttributes.GetAttributesForType(typeof(AllTypesView))); new DisplayMetadataProvider().CreateDisplayMetadata(context); Assert.Null(context.DisplayMetadata.DisplayName); }
private ModelMetadata GetRequiredMetadata() { var details = new DefaultMetadataDetails(ModelMetadataIdentity.ForType(typeof(string)), ModelAttributes.GetAttributesForType(typeof(string))) { ValidationMetadata = new ValidationMetadata { IsRequired = true } }; return(new DefaultModelMetadata(new EmptyModelMetadataProvider(), new DefaultCompositeMetadataDetailsProvider(new IMetadataDetailsProvider[0]), details)); }
private IEnumerable <ApiParameterDescription> GetParametersDescription(MethodInfo methodInfo, string route) { foreach (var parameter in methodInfo.GetParameters()) { var requestBodyTypeAttribute = parameter.GetCustomAttribute(typeof(RequestBodyTypeAttribute)) as RequestBodyTypeAttribute; if ((parameter.ParameterType == typeof(HttpRequestMessage) || parameter.ParameterType == typeof(HttpRequest)) && requestBodyTypeAttribute == null) { continue; } if (IgnoreParameter(parameter)) { continue; } var hasHttpTriggerAttribute = parameter.GetCustomAttributes() .Any(attr => attr is HttpTriggerAttribute); var hasFromUriAttribute = false; var type = hasHttpTriggerAttribute && requestBodyTypeAttribute != null ? requestBodyTypeAttribute.Type : parameter.ParameterType; var regex = GetRoutePathParamRegex(parameter.Name); var match = regex.Match(route); var bindingSource = match.Success ? BindingSource.Path : hasFromUriAttribute ? BindingSource.Query : BindingSource.Body; var optional = bindingSource == BindingSource.Query || match.Value.Contains("?"); yield return(new ApiParameterDescription { Name = parameter.Name, Type = type, Source = bindingSource, RouteInfo = new ApiParameterRouteInfo { IsOptional = optional }, ModelMetadata = new DefaultModelMetadata(_modelMetadataProvider, _compositeMetadataDetailsProvider, new DefaultMetadataDetails( ModelMetadataIdentity.ForType(type), ModelAttributes.GetAttributesForType(type))) }); } }
public void CreateDisplayMetadata_SetsDisplayName() { PropertyInfo property = typeof(RoleView).GetProperty(nameof(RoleView.Title)) !; DisplayMetadataProviderContext context = new( ModelMetadataIdentity.ForProperty(property, typeof(String), typeof(RoleView)), ModelAttributes.GetAttributesForType(typeof(RoleView))); new DisplayMetadataProvider().CreateDisplayMetadata(context); String expected = Resource.ForProperty(typeof(RoleView), nameof(RoleView.Title)); String actual = context.DisplayMetadata.DisplayName(); Assert.Equal(expected, actual); }
public void CreateDisplayMetadata_SetsDisplayName() { DisplayMetadataProvider provider = new DisplayMetadataProvider(); DisplayMetadataProviderContext context = new DisplayMetadataProviderContext( ModelMetadataIdentity.ForProperty(typeof(String), "Title", typeof(RoleView)), ModelAttributes.GetAttributesForType(typeof(RoleView))); provider.CreateDisplayMetadata(context); String expected = Resource.ForProperty(typeof(RoleView), "Title"); String actual = context.DisplayMetadata.DisplayName(); Assert.Equal(expected, actual); }
private static DefaultMetadataDetails CreateMetadataDetails(ActionExecutionModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } var modelMetadataIdentity = ModelMetadataIdentity.ForType(model.GetType()); var modelAttributes = ModelAttributes.GetAttributesForType(model.GetType()); var modelMetadataDetails = new DefaultMetadataDetails(modelMetadataIdentity, modelAttributes); return(modelMetadataDetails); }
public static IEnumerable <KeyValuePair <Type, ModelMetadata> > CreateMetadataForTypes(params Type[] types) { var provider = new EmptyModelMetadataProvider(); var detailsProvider = new DefaultCompositeMetadataDetailsProvider( Enumerable.Empty <IMetadataDetailsProvider>()); foreach (var type in types) { var key = ModelMetadataIdentity.ForType(type); var cache = new DefaultMetadataDetails(key, ModelAttributes.GetAttributesForType(type)); var metadata = new DefaultModelMetadata(provider, detailsProvider, cache); Xunit.Assert.Equal(type, metadata.ModelType); Xunit.Assert.Null(metadata.PropertyName); Xunit.Assert.Null(metadata.ContainerType); yield return(KeyValuePair.Create <Type, ModelMetadata>(type, metadata)); } }
public static DefaultModelMetadata CreateModelMetadata <T>() { var metadataDetailProvider = new Mock <ICompositeMetadataDetailsProvider>().Object; var modelMetadataProvider = new DefaultModelMetadataProvider(metadataDetailProvider); var details = new DefaultMetadataDetails(ModelMetadataIdentity.ForType(typeof(T)), ModelAttributes.GetAttributesForType(typeof(T))); return(new DefaultModelMetadata(modelMetadataProvider, metadataDetailProvider, details)); }
/// <summary> /// Creates the <see cref="DefaultMetadataDetails"/> entry for a model <see cref="Type"/>. /// </summary> /// <param name="key"> /// The <see cref="ModelMetadataIdentity"/> identifying the model <see cref="Type"/>. /// </param> /// <returns>A details object for the model <see cref="Type"/>.</returns> /// <remarks> /// The results of this method will be cached and used to satisfy calls to /// <see cref="GetMetadataForType(Type)"/>. Override this method to provide a different /// set of attributes. /// </remarks> protected virtual DefaultMetadataDetails CreateTypeDetails(ModelMetadataIdentity key) { return(new DefaultMetadataDetails(key, ModelAttributes.GetAttributesForType(key.ModelType))); }
private static async Task <InputFormatterResult> ReadRequestBody(string sampleJson, ModelStateDictionary modelStateDictionary) { var formatter = new FhirJsonInputFormatter(new FhirJsonParser(), ArrayPool <char> .Shared); var metaData = new DefaultModelMetadata( new EmptyModelMetadataProvider(), Substitute.For <ICompositeMetadataDetailsProvider>(), new DefaultMetadataDetails(ModelMetadataIdentity.ForType(typeof(Observation)), ModelAttributes.GetAttributesForType(typeof(Observation)))); var context = new InputFormatterContext( new DefaultHttpContext(), KnownActionParameterNames.Resource, modelStateDictionary, metaData, (stream, encoding) => new StreamReader(new MemoryStream(encoding.GetBytes(sampleJson)))); return(await formatter.ReadRequestBodyAsync(context)); }
public static ModelMetadata ForType(Type modelType) { var mockModelMetadataProvider = new Mock <IModelMetadataProvider>(); var mockCompositeMetadataDetailsProvider = new Mock <ICompositeMetadataDetailsProvider>(); var defaultMetadataDetails = new DefaultMetadataDetails(ModelMetadataIdentity.ForType(modelType), ModelAttributes.GetAttributesForType(typeof(List <object>))); return(new DefaultModelMetadata(mockModelMetadataProvider.Object, mockCompositeMetadataDetailsProvider.Object, defaultMetadataDetails)); }
private static async Task <InputFormatterResult> ReadRequestBody(string sampleXml, ModelStateDictionary modelStateDictionary) { var formatter = new FhirXmlInputFormatter(new FhirXmlParser()); var metaData = new DefaultModelMetadata(new EmptyModelMetadataProvider(), new DefaultCompositeMetadataDetailsProvider(new IMetadataDetailsProvider[0]), new DefaultMetadataDetails(ModelMetadataIdentity.ForType(typeof(Observation)), ModelAttributes.GetAttributesForType(typeof(Observation)))); var defaultHttpContext = new DefaultHttpContext(); defaultHttpContext.Request.Body = new MemoryStream(Encoding.UTF8.GetBytes(sampleXml)); var context = new InputFormatterContext( defaultHttpContext, KnownActionParameterNames.Resource, modelStateDictionary, metaData, Substitute.For <Func <Stream, Encoding, TextReader> >()); return(await formatter.ReadRequestBodyAsync(context)); }
/// <summary> /// Creates the <see cref="DefaultMetadataDetailsCache"/> entry for a model <see cref="Type"/>. /// </summary> /// <param name="key"> /// The <see cref="ModelMetadataIdentity"/> identifying the model <see cref="Type"/>. /// </param> /// <returns>A cache object for the model <see cref="Type"/>.</returns> /// <remarks> /// The results of this method will be cached and used to satisfy calls to /// <see cref="GetMetadataForType(Type)"/>. Override this method to provide a different /// set of attributes. /// </remarks> protected virtual DefaultMetadataDetailsCache CreateTypeCacheEntry([NotNull] ModelMetadataIdentity key) { var attributes = new List <object>(ModelAttributes.GetAttributesForType(key.ModelType)); return(new DefaultMetadataDetailsCache(key, attributes)); }
public void CreateModelMetadata_SetsDisplayName() { DefaultMetadataDetails metaDetails = new DefaultMetadataDetails( ModelMetadataIdentity.ForProperty(typeof(String), "Title", typeof(RoleView)), ModelAttributes.GetAttributesForType(typeof(RoleView))); ICompositeMetadataDetailsProvider details = Substitute.For <ICompositeMetadataDetailsProvider>(); DisplayNameMetadataProviderProxy provider = new DisplayNameMetadataProviderProxy(details); String expected = ResourceProvider.GetPropertyTitle(typeof(RoleView), "Title"); String actual = provider.BaseCreateModelMetadata(metaDetails).DisplayName; Assert.Equal(expected, actual); }