Skip to content

Commit 44b61bb

Browse files
committed
Add user_route.format endpoint route for primary verb in user-defined routes
1 parent f620bcd commit 44b61bb

5 files changed

Lines changed: 61 additions & 23 deletions

File tree

ServiceStack/src/ServiceStack/AppHostBase.NetCore.cs

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -348,30 +348,31 @@ Task HandleRequestAsync(Type requestType, HttpContext httpContext)
348348
{
349349
handler(pathBuilder, operation, routeVerb, route.Path);
350350
}
351-
}
352-
353-
// Add /custom/path.{format} routes for GET requests
354-
if (routeVerbs.Contains(HttpMethods.Get) && !route.Path.Contains('.') && !route.Path.Contains('*'))
355-
{
356-
var routePath = route.Path + ".{format}";
357-
if (existingRoutes.TryGetValue(routePath, out var prevRoute))
358-
{
359-
LogManager.GetLogger(GetType()).WarnFormat("Ignoring registering duplicate route: {0} for {1} and {2}",
360-
routePath,
361-
route.RequestType.FullName,
362-
prevRoute.RequestType.FullName);
363-
}
364-
else
351+
352+
// Add /custom/path.{format} routes
353+
if (!route.Path.Contains('.') && !route.Path.Contains('*'))
365354
{
366-
routeRule = $"[GET] {routePath}";
367-
existingRoutes[routePath] = route;
368-
var pathBuilder = routeBuilder.MapMethods(routePath, EndpointVerbs[HttpMethods.Get],
369-
(string format, HttpResponse response, HttpContext httpContext) =>
370-
HandleRequestAsync(requestType, httpContext));
355+
var routePath = route.Path + ".{format}";
356+
if (existingRoutes.TryGetValue(routePath, out var prevRoute))
357+
{
358+
LogManager.GetLogger(GetType()).WarnFormat("Ignoring registering duplicate route: {0} for {1} and {2}",
359+
routePath,
360+
route.RequestType.FullName,
361+
prevRoute.RequestType.FullName);
362+
}
363+
else
364+
{
365+
routeRule = $"[{verb}] {routePath}";
366+
existingRoutes[routePath] = route;
367+
var pathBuilderFmt = routeBuilder.MapMethods(routePath, verb,
368+
(string format, HttpResponse response, HttpContext httpContext) =>
369+
HandleRequestAsync(requestType, httpContext));
371370

372-
ConfigureOperationEndpoint(pathBuilder, operation, options)
373-
.WithMetadata<string>(routePath);
371+
ConfigureOperationEndpoint(pathBuilderFmt, operation, options)
372+
.WithMetadata<string>(routePath);
373+
}
374374
}
375+
375376
}
376377
}
377378
catch (Exception e)

ServiceStack/tests/OpenApi3Swashbuckle/Program.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
app.UseHttpsRedirection();
3333
app.UseAuthorization();
3434

35-
app.UseServiceStack(new AppHost());
35+
app.UseServiceStack(new AppHost(), options => {
36+
options.MapEndpoints();
37+
});
3638

3739
app.Run();

ServiceStack/tests/OpenApi3Swashbuckle/ServiceInterface/MyServices.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,10 @@ public class MyServices : Service
1919
{
2020
Result = $"Hello, {request.Name ?? "World"}!"
2121
};
22+
23+
public object Any(HelloPost request) => new HelloResponse
24+
{
25+
Result = $"Hello, {request.Name ?? "World"}!"
26+
};
2227
}
2328

ServiceStack/tests/OpenApi3Swashbuckle/ServiceModel/Hello.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.Runtime.Serialization;
2+
13
using ServiceStack;
24

35
namespace MyApp.ServiceModel;
@@ -29,4 +31,12 @@ public class HelloSecure : IGet, IReturn<HelloResponse>
2931
public class HelloApiKey : IGet, IReturn<HelloResponse>
3032
{
3133
public string? Name { get; set; }
32-
}
34+
}
35+
36+
[Tag("hello")]
37+
[Route("/hello_post", "POST")]
38+
public class HelloPost : IPost, IReturn<HelloResponse>
39+
{
40+
[DataMember]
41+
public string? Name { get; set; }
42+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
### POST hello_post.xml
2+
POST https://localhost:5001/hello_post.xml
3+
Content-Type: application/xml
4+
5+
<HelloPost xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
6+
xmlns="http://schemas.datacontract.org/2004/07/MyApp.ServiceModel">
7+
<Name>John Doe</Name>
8+
</HelloPost>
9+
###
10+
11+
### POST /api/HelloPost.xml
12+
POST https://localhost:5001/api/HelloPost.xml
13+
Content-Type: application/xml
14+
15+
<HelloPost xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
16+
xmlns="http://schemas.datacontract.org/2004/07/MyApp.ServiceModel">
17+
<Name>John Doe</Name>
18+
</HelloPost>
19+
###
20+

0 commit comments

Comments
 (0)