Skip to content

Commit 3a6caed

Browse files
committed
_
1 parent 9e77df1 commit 3a6caed

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

packages/pharaoh/lib/src/_next/_router/definition.dart

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class RouteMapping {
2424
}
2525

2626
typedef OpenApiRoute = ({
27+
List<String> tags,
2728
HTTPMethod method,
2829
String route,
2930
List<ControllerMethodParam> args,
@@ -33,6 +34,8 @@ abstract class RouteDefinition {
3334
late RouteMapping route;
3435
final RouteDefinitionType type;
3536

37+
String? group;
38+
3639
RouteDefinition(this.type);
3740

3841
void commit(Spanner spanner);
@@ -61,7 +64,8 @@ class UseAliasedMiddleware {
6164

6265
RouteGroupDefinition routes(List<RouteDefinition> routes) {
6366
return RouteGroupDefinition._(
64-
BASE_PATH,
67+
alias,
68+
prefix: BASE_PATH,
6569
definitions: routes,
6670
)..middleware(mdw);
6771
}
@@ -135,7 +139,12 @@ class ControllerRouteMethodDefinition extends RouteDefinition {
135139

136140
@override
137141
List<OpenApiRoute> get openAPIRoutes => route.methods
138-
.map((e) => (route: route.path, method: e, args: method.params.toList()))
142+
.map((e) => (
143+
route: route.path,
144+
method: e,
145+
args: method.params.toList(),
146+
tags: <String>[if (group != null) group!]
147+
))
139148
.toList();
140149
}
141150

@@ -162,12 +171,12 @@ class RouteGroupDefinition extends RouteDefinition {
162171
void _unwrapRoutes(Iterable<RouteDefinition> routes) {
163172
for (final subRoute in routes) {
164173
if (subRoute is! RouteGroupDefinition) {
165-
defns.add(subRoute._prefix(route.path));
174+
defns.add(subRoute._prefix(route.path)..group = name);
166175
continue;
167176
}
168177

169178
for (var e in subRoute.defns) {
170-
defns.add(e._prefix(route.path));
179+
defns.add(e._prefix(route.path)..group = subRoute.name);
171180
}
172181
}
173182
}
@@ -232,6 +241,12 @@ class FunctionalRouteDefinition extends RouteDefinition {
232241
}
233242

234243
@override
235-
List<OpenApiRoute> get openAPIRoutes =>
236-
[(args: [], method: method, route: route.path)];
244+
List<OpenApiRoute> get openAPIRoutes => [
245+
(
246+
args: [],
247+
method: method,
248+
route: route.path,
249+
tags: <String>[if (group != null) group!]
250+
)
251+
];
237252
}

packages/pharaoh/lib/src/_next/openapi.dart

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,29 @@ class OpenApiGenerator {
2121

2222
for (final route in routes) {
2323
final pathParams = route.args.where((e) => e.meta is Param).toList();
24-
final bodyParam = route.args.firstWhereOrNull((e) => e is Body);
24+
final bodyParam = route.args.firstWhereOrNull((e) => e.meta is Body);
25+
final parameters = _generateParameters(route.args);
26+
final routeMethod = route.method.name.toLowerCase();
2527

2628
var path = route.route;
29+
2730
// Convert Express-style path params (:id) to OpenAPI style ({id})
2831
for (final param in pathParams) {
2932
path = path.replaceAll('<${param.name}>', '{${param.name}}');
3033
}
3134

3235
paths[path] = paths[path] ?? {};
33-
paths[path]![route.method.name.toLowerCase()] = {
34-
"summary": "", // Could be added as a parameter
35-
"parameters": _generateParameters(route.args),
36+
paths[path]![routeMethod] = {
37+
"summary": "",
38+
if (parameters.isNotEmpty) "parameters": parameters,
39+
if (route.tags.isNotEmpty) "tags": route.tags,
3640
"responses": {
3741
"200": {"description": "Successful response"}
3842
}
3943
};
4044

4145
if (bodyParam != null) {
42-
paths[path]![route.method.name.toLowerCase()]["requestBody"] = {
46+
paths[path]![routeMethod]["requestBody"] = {
4347
"required": !bodyParam.optional,
4448
"content": {
4549
"application/json": {"schema": _generateSchema(bodyParam)}

0 commit comments

Comments
 (0)