|
| 1 | +import 'package:collection/collection.dart'; |
| 2 | +import 'package:pharaoh/src/_next/router.dart'; |
| 3 | + |
| 4 | +class OpenApiGenerator { |
| 5 | + static Map<String, dynamic> generateOpenApi( |
| 6 | + List<OpenApiRoute> routes, { |
| 7 | + required String apiName, |
| 8 | + required List<String> serverUrls, |
| 9 | + }) { |
| 10 | + return { |
| 11 | + "openapi": "3.0.0", |
| 12 | + "info": {"title": apiName, "version": "1.0.0"}, |
| 13 | + "servers": serverUrls.map((e) => {'url': e}).toList(), |
| 14 | + "paths": _generatePaths(routes), |
| 15 | + "components": {"schemas": _generateSchemas(routes)} |
| 16 | + }; |
| 17 | + } |
| 18 | + |
| 19 | + static Map<String, dynamic> _generatePaths(List<OpenApiRoute> routes) { |
| 20 | + final paths = <String, Map<String, dynamic>>{}; |
| 21 | + |
| 22 | + for (final route in routes) { |
| 23 | + final pathParams = route.args.where((e) => e.meta is Param).toList(); |
| 24 | + final bodyParam = route.args.firstWhereOrNull((e) => e is Body); |
| 25 | + |
| 26 | + var path = route.route; |
| 27 | + // Convert Express-style path params (:id) to OpenAPI style ({id}) |
| 28 | + for (final param in pathParams) { |
| 29 | + path = path.replaceAll('<${param.name}>', '{${param.name}}'); |
| 30 | + } |
| 31 | + |
| 32 | + 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 | + "responses": { |
| 37 | + "200": {"description": "Successful response"} |
| 38 | + } |
| 39 | + }; |
| 40 | + |
| 41 | + if (bodyParam != null) { |
| 42 | + paths[path]![route.method.name.toLowerCase()]["requestBody"] = { |
| 43 | + "required": !bodyParam.optional, |
| 44 | + "content": { |
| 45 | + "application/json": {"schema": _generateSchema(bodyParam)} |
| 46 | + } |
| 47 | + }; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + return paths; |
| 52 | + } |
| 53 | + |
| 54 | + static List<Map<String, dynamic>> _generateParameters( |
| 55 | + List<ControllerMethodParam> args, |
| 56 | + ) { |
| 57 | + final parameters = <Map<String, dynamic>>[]; |
| 58 | + |
| 59 | + for (final arg in args) { |
| 60 | + final parameterLocation = _getParameterLocation(arg.meta); |
| 61 | + if (parameterLocation == null) continue; |
| 62 | + |
| 63 | + final parameterSchema = _generateSchema(arg); |
| 64 | + |
| 65 | + // Add default value if available and not a path parameter |
| 66 | + if (arg.defaultValue != null && parameterLocation != "path") { |
| 67 | + parameterSchema["default"] = arg.defaultValue; |
| 68 | + } |
| 69 | + |
| 70 | + final param = { |
| 71 | + "name": arg.name, |
| 72 | + "in": parameterLocation, |
| 73 | + "required": parameterLocation == "path" ? true : !arg.optional, |
| 74 | + "schema": parameterSchema, |
| 75 | + }; |
| 76 | + |
| 77 | + parameters.add(param); |
| 78 | + } |
| 79 | + |
| 80 | + return parameters; |
| 81 | + } |
| 82 | + |
| 83 | + static String? _getParameterLocation(RequestAnnotation? annotation) { |
| 84 | + return switch (annotation) { |
| 85 | + const Header() => "header", |
| 86 | + const Query() => "query", |
| 87 | + const Param() => "path", |
| 88 | + _ => null, |
| 89 | + }; |
| 90 | + } |
| 91 | + |
| 92 | + static Map<String, dynamic> _generateSchema(ControllerMethodParam param) { |
| 93 | + if (param.dto != null) { |
| 94 | + return { |
| 95 | + "\$ref": "#/components/schemas/${param.dto.runtimeType.toString()}" |
| 96 | + }; |
| 97 | + } |
| 98 | + |
| 99 | + return _typeToOpenApiType(param.type); |
| 100 | + } |
| 101 | + |
| 102 | + static Map<String, dynamic> _typeToOpenApiType(Type type) { |
| 103 | + switch (type.toString()) { |
| 104 | + case "String": |
| 105 | + return {"type": "string"}; |
| 106 | + case "int": |
| 107 | + return {"type": "integer", "format": "int32"}; |
| 108 | + case "double": |
| 109 | + return {"type": "number", "format": "double"}; |
| 110 | + case "bool": |
| 111 | + return {"type": "boolean"}; |
| 112 | + case "DateTime": |
| 113 | + return {"type": "string", "format": "date-time"}; |
| 114 | + default: |
| 115 | + return {"type": "object"}; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + static Map<String, dynamic> _generateSchemas(List<OpenApiRoute> routes) { |
| 120 | + final schemas = <String, dynamic>{}; |
| 121 | + |
| 122 | + for (final route in routes) { |
| 123 | + for (final arg in route.args) { |
| 124 | + final dto = arg.dto; |
| 125 | + if (dto == null) continue; |
| 126 | + |
| 127 | + schemas[dto.runtimeType.toString()] = { |
| 128 | + "type": "object", |
| 129 | + "properties": dto.properties.fold({}, |
| 130 | + (preV, curr) => preV..[curr.name] = _typeToOpenApiType(curr.type)) |
| 131 | + }; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + return schemas; |
| 136 | + } |
| 137 | +} |
0 commit comments