Skip to content
Permalink
Browse files

Catch Native Types Attribute Property errors

  • Loading branch information...
mythz committed Oct 12, 2018
1 parent c3fc9d9 commit 1036956603d1288ec908aca6f8720a2c352b2a73
@@ -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;
@@ -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;
}
}
@@ -32,6 +32,7 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Core" />
<Reference Include="System.Net" />
<Reference Include="System.Runtime.Serialization" />
@@ -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<TestAttributeExport>
{
[Display(AutoGenerateField = true, AutoGenerateFilter = true, ShortName = "UnitMeasKey")]
public int UnitMeasKey { get; set; }
}
}
@@ -64,8 +64,10 @@ public override void Configure(Container container)
var nativeTypes = this.GetPlugin<NativeTypesFeature>();
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
{

0 comments on commit 1036956

Please sign in to comment.
You can’t perform that action at this time.