Skip to content

Commit 1dbaa58

Browse files
committed
Fix AddFromAssembly ServiceRoutesExtensions to use Request DTOs to define auto routes
1 parent b1ec350 commit 1dbaa58

File tree

5 files changed

+127
-69
lines changed

5 files changed

+127
-69
lines changed

src/ServiceStack.ServiceInterface/ServiceRoutesExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,12 @@ from t in assembly.GetExportedTypes()
7777
if (allowedMethods.Count == 0) continue;
7878
var allowedVerbs = string.Join(" ", allowedMethods.ToArray());
7979

80-
routes.Add(requestType, restService.Name, allowedVerbs, null);
80+
routes.Add(requestType, requestType.Name, allowedVerbs, null);
8181

8282
var hasIdField = requestType.GetProperty(IdUtils.IdField) != null;
8383
if (hasIdField)
8484
{
85-
var routePath = restService.Name + "/{" + IdUtils.IdField + "}";
85+
var routePath = requestType.Name + "/{" + IdUtils.IdField + "}";
8686
routes.Add(requestType, routePath, allowedVerbs, null);
8787
}
8888
}

src/ServiceStack/Properties/AssemblyInfo.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
[assembly: AssemblyTitle("ServiceStack")]
1010
[assembly: AssemblyDescription("")]
1111
[assembly: AssemblyConfiguration("")]
12-
[assembly: AssemblyCompany("Liquidbit")]
12+
[assembly: AssemblyCompany("ServiceStack")]
1313
[assembly: AssemblyProduct("ServiceStack")]
14-
[assembly: AssemblyCopyright("Copyright © Liquidbit 2011")]
14+
[assembly: AssemblyCopyright("Copyright © ServiceStack 2012")]
1515
[assembly: AssemblyTrademark("")]
1616
[assembly: AssemblyCulture("")]
1717

@@ -33,5 +33,5 @@
3333
// You can specify all the values or you can default the Build and Revision Numbers
3434
// by using the '*' as shown below:
3535
// [assembly: AssemblyVersion("1.0.*")]
36-
[assembly: AssemblyVersion("3.4.2.*")]
36+
[assembly: AssemblyVersion("3.4.3.*")]
3737
//[assembly: AssemblyFileVersion("1.0.*")]

tests/ServiceStack.Common.Tests/Text/AdhocJsTests.cs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@
55

66
namespace ServiceStack.Common.Tests.Text
77
{
8+
public enum EnumValues
9+
{
10+
Enum1,
11+
Enum2,
12+
Enum3,
13+
}
14+
815
[TestFixture]
916
public class AdhocJsTests
1017
{
@@ -16,5 +23,54 @@ public void Can_Deserialize()
1623

1724
Console.WriteLine(items.Dump());
1825
}
26+
27+
[Test]
28+
public void Can_Serialize_Array_of_enums()
29+
{
30+
var enumArr = new[] { EnumValues.Enum1, EnumValues.Enum2, EnumValues.Enum3, };
31+
var json = JsonSerializer.SerializeToString(enumArr);
32+
Assert.That(json, Is.EqualTo("[\"Enum1\",\"Enum2\",\"Enum3\"]"));
33+
}
34+
35+
[Test]
36+
public void Can_Serialize_Array_of_chars()
37+
{
38+
var enumArr = new[] { 'A', 'B', 'C', };
39+
var json = JsonSerializer.SerializeToString(enumArr);
40+
Assert.That(json, Is.EqualTo("[\"A\",\"B\",\"C\"]"));
41+
}
42+
43+
[Test]
44+
public void Can_Serialize_Array_with_nulls()
45+
{
46+
var t = new {
47+
Name = "MyName",
48+
Number = (int?)null,
49+
Data = new object[] { 5, null, "text" }
50+
};
51+
52+
ServiceStack.Text.JsConfig.IncludeNullValues = true;
53+
var json = ServiceStack.Text.JsonSerializer.SerializeToString(t);
54+
Assert.That(json, Is.EqualTo("{\"Name\":\"MyName\",\"Number\":null,\"Data\":[5,null,\"text\"]}"));
55+
}
56+
57+
class A
58+
{
59+
public string Value { get; set; }
60+
}
61+
62+
[Test]
63+
public void DumpFail()
64+
{
65+
var arrayOfA = new[] { new A { Value = "a" }, null, new A { Value = "b" } };
66+
Console.WriteLine(arrayOfA.Dump());
67+
}
68+
69+
[Test]
70+
public void Deserialize_array_with_null_elements()
71+
{
72+
var json = "[{\"Value\": \"a\"},null,{\"Value\": \"b\"}]";
73+
var o = JsonSerializer.DeserializeFromString<A[]>(json);
74+
}
1975
}
2076
}
Lines changed: 65 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,66 @@
1-
using System.Linq;
2-
using NUnit.Framework;
3-
using ServiceStack.ServiceClient.Web;
4-
using ServiceStack.ServiceInterface;
5-
6-
namespace ServiceStack.ServiceHost.Tests.Routes
7-
{
8-
[TestFixture]
9-
public class ServiceRoutesTests
10-
{
11-
[Test]
12-
public void Can_Register_Routes_From_Assembly()
13-
{
14-
var routes = new ServiceRoutes();
15-
routes.AddFromAssembly(typeof(RestServiceWithAllVerbsImplemented).Assembly);
16-
17-
RestPath restWithAllMethodsRoute = (from r in routes.RestPaths
18-
where r.Path == "RestServiceWithAllVerbsImplemented"
19-
select r).FirstOrDefault();
20-
21-
Assert.That(restWithAllMethodsRoute, Is.Not.Null);
22-
23-
Assert.That(restWithAllMethodsRoute.AllowedVerbs.Contains("GET"));
24-
Assert.That(restWithAllMethodsRoute.AllowedVerbs.Contains("POST"));
25-
Assert.That(restWithAllMethodsRoute.AllowedVerbs.Contains("PUT"));
26-
Assert.That(restWithAllMethodsRoute.AllowedVerbs.Contains("DELETE"));
27-
Assert.That(restWithAllMethodsRoute.AllowedVerbs.Contains("PATCH"));
28-
}
29-
30-
[Test]
31-
public void Can_Register_Routes_With_Partially_Implemented_REST_Verbs()
32-
{
33-
var routes = new ServiceRoutes();
34-
routes.AddFromAssembly(typeof(RestServiceWithSomeVerbsImplemented).Assembly);
35-
36-
RestPath restWithAFewMethodsRoute = (from r in routes.RestPaths
37-
where r.Path == "RestServiceWithSomeVerbsImplemented"
38-
select r).FirstOrDefault();
39-
40-
Assert.That(restWithAFewMethodsRoute, Is.Not.Null);
41-
42-
Assert.That(restWithAFewMethodsRoute.AllowedVerbs.Contains("GET"), Is.True);
43-
Assert.That(restWithAFewMethodsRoute.AllowedVerbs.Contains("POST"), Is.False);
44-
Assert.That(restWithAFewMethodsRoute.AllowedVerbs.Contains("PUT"), Is.True);
45-
Assert.That(restWithAFewMethodsRoute.AllowedVerbs.Contains("DELETE"), Is.False);
46-
Assert.That(restWithAFewMethodsRoute.AllowedVerbs.Contains("PATCH"), Is.False);
47-
}
48-
49-
[Test]
50-
public void Can_Register_Routes_Using_Add_Extension()
51-
{
52-
var routes = new ServiceRoutes();
53-
routes.Add<Customer>(HttpMethod.Get, "/Users/{0}/Orders/{1}", x => x.Name, x => x.OrderId);
54-
var route = routes.RestPaths[0];
55-
Assert.That(route.Path == "/Users/{Name}/Orders/{OrderId}");
56-
}
57-
}
58-
59-
public class Customer
60-
{
61-
public string Name { get; set; }
62-
public int OrderId { get; set; }
63-
}
1+
using System.Linq;
2+
using NUnit.Framework;
3+
using ServiceStack.ServiceClient.Web;
4+
using ServiceStack.ServiceInterface;
5+
6+
namespace ServiceStack.ServiceHost.Tests.Routes
7+
{
8+
[TestFixture]
9+
public class ServiceRoutesTests
10+
{
11+
[Test]
12+
public void Can_Register_Routes_From_Assembly()
13+
{
14+
var routes = new ServiceRoutes();
15+
routes.AddFromAssembly(typeof(RestServiceWithAllVerbsImplemented).Assembly);
16+
17+
RestPath restWithAllMethodsRoute =
18+
(from r in routes.RestPaths
19+
where r.Path == "RequestDto2"
20+
select r).FirstOrDefault();
21+
22+
Assert.That(restWithAllMethodsRoute, Is.Not.Null);
23+
24+
Assert.That(restWithAllMethodsRoute.AllowedVerbs.Contains("GET"));
25+
Assert.That(restWithAllMethodsRoute.AllowedVerbs.Contains("POST"));
26+
Assert.That(restWithAllMethodsRoute.AllowedVerbs.Contains("PUT"));
27+
Assert.That(restWithAllMethodsRoute.AllowedVerbs.Contains("DELETE"));
28+
Assert.That(restWithAllMethodsRoute.AllowedVerbs.Contains("PATCH"));
29+
}
30+
31+
[Test]
32+
public void Can_Register_Routes_With_Partially_Implemented_REST_Verbs()
33+
{
34+
var routes = new ServiceRoutes();
35+
routes.AddFromAssembly(typeof(RestServiceWithSomeVerbsImplemented).Assembly);
36+
37+
RestPath restWithAFewMethodsRoute =
38+
(from r in routes.RestPaths
39+
where r.Path == "RequestDto"
40+
select r).FirstOrDefault();
41+
42+
Assert.That(restWithAFewMethodsRoute, Is.Not.Null);
43+
44+
Assert.That(restWithAFewMethodsRoute.AllowedVerbs.Contains("GET"), Is.True);
45+
Assert.That(restWithAFewMethodsRoute.AllowedVerbs.Contains("POST"), Is.False);
46+
Assert.That(restWithAFewMethodsRoute.AllowedVerbs.Contains("PUT"), Is.True);
47+
Assert.That(restWithAFewMethodsRoute.AllowedVerbs.Contains("DELETE"), Is.False);
48+
Assert.That(restWithAFewMethodsRoute.AllowedVerbs.Contains("PATCH"), Is.False);
49+
}
50+
51+
[Test]
52+
public void Can_Register_Routes_Using_Add_Extension()
53+
{
54+
var routes = new ServiceRoutes();
55+
routes.Add<Customer>(HttpMethod.Get, "/Users/{0}/Orders/{1}", x => x.Name, x => x.OrderId);
56+
var route = routes.RestPaths[0];
57+
Assert.That(route.Path == "/Users/{Name}/Orders/{OrderId}");
58+
}
59+
}
60+
61+
public class Customer
62+
{
63+
public string Name { get; set; }
64+
public int OrderId { get; set; }
65+
}
6466
}

tests/ServiceStack.WebHost.IntegrationTests/ServiceStack.WebHost.IntegrationTests.csproj.user

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<PropertyGroup>
4-
<ProjectView>ProjectFiles</ProjectView>
4+
<ProjectView>ShowAllFiles</ProjectView>
55
</PropertyGroup>
66
<ProjectExtensions>
77
<VisualStudio>

0 commit comments

Comments
 (0)