Skip to content

Commit

Permalink
Catch Native Types Attribute Property errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Oct 12, 2018
1 parent c3fc9d9 commit 1036956
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 21 deletions.
40 changes: 25 additions & 15 deletions src/ServiceStack/NativeTypes/NativeTypesMetadata.cs
Expand Up @@ -7,6 +7,7 @@
using ServiceStack.Configuration;
using ServiceStack.DataAnnotations;
using ServiceStack.Host;
using ServiceStack.Logging;
using ServiceStack.Text;
using ServiceStack.Web;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down
7 changes: 3 additions & 4 deletions tests/Check.ServiceInterface/NativeTypeIssuesService.cs
Expand Up @@ -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;
}
}
1 change: 1 addition & 0 deletions tests/Check.ServiceModel/Check.ServiceModel.csproj
Expand Up @@ -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" />
Expand Down
11 changes: 10 additions & 1 deletion tests/Check.ServiceModel/NativeTypeIssues.cs
@@ -1,4 +1,6 @@
using ServiceStack.DataAnnotations;
using System.ComponentModel.DataAnnotations;
using ServiceStack;
using ServiceStack.DataAnnotations;

namespace Check.ServiceModel
{
Expand All @@ -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; }
}
}
4 changes: 3 additions & 1 deletion tests/CheckWeb/Global.asax.cs
Expand Up @@ -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
{
Expand Down

0 comments on commit 1036956

Please sign in to comment.