From 1036956603d1288ec908aca6f8720a2c352b2a73 Mon Sep 17 00:00:00 2001 From: Demis Bellot Date: Fri, 12 Oct 2018 14:08:49 +0930 Subject: [PATCH] Catch Native Types Attribute Property errors --- .../NativeTypes/NativeTypesMetadata.cs | 40 ++++++++++++------- .../NativeTypeIssuesService.cs | 7 ++-- .../Check.ServiceModel.csproj | 1 + tests/Check.ServiceModel/NativeTypeIssues.cs | 11 ++++- tests/CheckWeb/Global.asax.cs | 4 +- 5 files changed, 42 insertions(+), 21 deletions(-) diff --git a/src/ServiceStack/NativeTypes/NativeTypesMetadata.cs b/src/ServiceStack/NativeTypes/NativeTypesMetadata.cs index 3f9b9291d43..fe493fe9046 100644 --- a/src/ServiceStack/NativeTypes/NativeTypesMetadata.cs +++ b/src/ServiceStack/NativeTypes/NativeTypesMetadata.cs @@ -7,6 +7,7 @@ using ServiceStack.Configuration; using ServiceStack.DataAnnotations; using ServiceStack.Host; +using ServiceStack.Logging; using ServiceStack.Text; using ServiceStack.Web; @@ -86,6 +87,8 @@ internal MetadataTypesGenerator GetMetadataTypesGenerator(MetadataTypesConfig co public class MetadataTypesGenerator { + private static ILog log = LogManager.GetLogger(typeof(MetadataTypesGenerator)); + private readonly ServiceMetadata meta; private readonly MetadataTypesConfig config; @@ -639,25 +642,32 @@ public MetadataPropertyType ToProperty(PropertyInfo pi, object instance = null) if (instance != null) { - var value = pi.GetValue(instance, null); - if (value != null - && !value.Equals(pi.PropertyType.GetDefaultValue())) + try { - if (pi.PropertyType.IsEnum) + var value = pi.GetValue(instance, null); + if (value != null + && !value.Equals(pi.PropertyType.GetDefaultValue())) { - property.Value = "{0}.{1}".Fmt(pi.PropertyType.Name, value); - } - else if (pi.PropertyType == typeof(Type)) - { - var type = (Type)value; - property.Value = $"typeof({type.FullName})"; - } - else - { - var strValue = value as string; - property.Value = strValue ?? value.ToJson(); + if (pi.PropertyType.IsEnum) + { + property.Value = "{0}.{1}".Fmt(pi.PropertyType.Name, value); + } + else if (pi.PropertyType == typeof(Type)) + { + var type = (Type)value; + property.Value = $"typeof({type.FullName})"; + } + else + { + var strValue = value as string; + property.Value = strValue ?? value.ToJson(); + } } } + catch (Exception ex) + { + log.Warn($"Could not get value for property '{pi.PropertyType}.{pi.Name}'", ex); + } if (pi.GetSetMethod() == null) //ReadOnly is bool? to minimize serialization property.ReadOnly = true; diff --git a/tests/Check.ServiceInterface/NativeTypeIssuesService.cs b/tests/Check.ServiceInterface/NativeTypeIssuesService.cs index 88b7ae7090d..f9e14690350 100644 --- a/tests/Check.ServiceInterface/NativeTypeIssuesService.cs +++ b/tests/Check.ServiceInterface/NativeTypeIssuesService.cs @@ -5,9 +5,8 @@ namespace Check.ServiceInterface { public class NativeTypeIssuesService : Service { - public object Any(Issue221Long request) - { - return request; - } + public object Any(Issue221Long request) => request; + + public object Any(TestAttributeExport request) => request; } } \ No newline at end of file diff --git a/tests/Check.ServiceModel/Check.ServiceModel.csproj b/tests/Check.ServiceModel/Check.ServiceModel.csproj index 34a47c2c283..95f224fe8df 100644 --- a/tests/Check.ServiceModel/Check.ServiceModel.csproj +++ b/tests/Check.ServiceModel/Check.ServiceModel.csproj @@ -32,6 +32,7 @@ + diff --git a/tests/Check.ServiceModel/NativeTypeIssues.cs b/tests/Check.ServiceModel/NativeTypeIssues.cs index 0ff1477b250..6c588943901 100644 --- a/tests/Check.ServiceModel/NativeTypeIssues.cs +++ b/tests/Check.ServiceModel/NativeTypeIssues.cs @@ -1,4 +1,6 @@ -using ServiceStack.DataAnnotations; +using System.ComponentModel.DataAnnotations; +using ServiceStack; +using ServiceStack.DataAnnotations; namespace Check.ServiceModel { @@ -20,4 +22,11 @@ public Issue221Long(long id) : base(id) { } } + + + public class TestAttributeExport : IReturn + { + [Display(AutoGenerateField = true, AutoGenerateFilter = true, ShortName = "UnitMeasKey")] + public int UnitMeasKey { get; set; } + } } \ No newline at end of file diff --git a/tests/CheckWeb/Global.asax.cs b/tests/CheckWeb/Global.asax.cs index c4936d13aa4..20d7810f28e 100644 --- a/tests/CheckWeb/Global.asax.cs +++ b/tests/CheckWeb/Global.asax.cs @@ -64,8 +64,10 @@ public override void Configure(Container container) var nativeTypes = this.GetPlugin(); nativeTypes.MetadataTypesConfig.ExportTypes.Add(typeof(DayOfWeek)); nativeTypes.MetadataTypesConfig.IgnoreTypes.Add(typeof(IgnoreInMetadataConfig)); - //nativeTypes.MetadataTypesConfig.GlobalNamespace = "Check.ServiceInterface"; + nativeTypes.MetadataTypesConfig.ExportAttributes.Add(typeof(System.ComponentModel.DataAnnotations.DisplayAttribute)); + //nativeTypes.MetadataTypesConfig.GlobalNamespace = "Check.ServiceInterface"; + // Change ServiceStack configuration this.SetConfig(new HostConfig {