11import 'package:collection/collection.dart' ;
2- import 'package:pharaoh/src/_next/router .dart' ;
2+ import 'package:pharaoh/pharaoh_next .dart' ;
33
44class OpenApiGenerator {
55 static Map <String , dynamic > generateOpenApi (
@@ -38,7 +38,17 @@ class OpenApiGenerator {
3838 if (parameters.isNotEmpty) "parameters" : parameters,
3939 if (route.tags.isNotEmpty) "tags" : route.tags,
4040 "responses" : {
41- "200" : {"description" : "Successful response" }
41+ "200" : {
42+ "description" : "Successful response" ,
43+ if (route.returnType != null && route.returnType != Response )
44+ "content" : {
45+ "application/json" : {
46+ "schema" : {
47+ "\$ ref" : "#/components/schemas/${route .returnType }"
48+ },
49+ },
50+ }
51+ }
4252 }
4353 };
4454
@@ -104,18 +114,31 @@ class OpenApiGenerator {
104114 }
105115
106116 static Map <String , dynamic > _typeToOpenApiType (Type type) {
107- switch (type. toString () ) {
108- case " String" :
117+ switch (type) {
118+ case const ( String ) :
109119 return {"type" : "string" };
110- case " int" :
120+ case const ( int ) :
111121 return {"type" : "integer" , "format" : "int32" };
112- case " double" :
122+ case const ( double ) :
113123 return {"type" : "number" , "format" : "double" };
114- case " bool" :
124+ case const ( bool ) :
115125 return {"type" : "boolean" };
116- case " DateTime" :
126+ case const ( DateTime ) :
117127 return {"type" : "string" , "format" : "date-time" };
118128 default :
129+ final actualType = getActualType (type);
130+ if (actualType == null ) return {"type" : "object" };
131+
132+ // final properties = <VariableMirror>[];
133+
134+ // ClassMirror? clazz = reflectType(actualType);
135+ // while (clazz?.superclass != null) {
136+ // properties.addAll(clazz!.variables);
137+ // clazz = clazz.superclass;
138+ // }
139+
140+ // print(properties);
141+
119142 return {"type" : "object" };
120143 }
121144 }
@@ -124,6 +147,7 @@ class OpenApiGenerator {
124147 final schemas = < String , dynamic > {};
125148
126149 for (final route in routes) {
150+ final returnType = route.returnType;
127151 for (final arg in route.args) {
128152 final dto = arg.dto;
129153 if (dto == null ) continue ;
@@ -134,6 +158,18 @@ class OpenApiGenerator {
134158 (preV, curr) => preV..[curr.name] = _typeToOpenApiType (curr.type))
135159 };
136160 }
161+
162+ if (returnType == null || returnType == Response ) continue ;
163+
164+ final properties = reflectType (returnType).variables;
165+
166+ schemas[returnType.toString ()] = {
167+ "type" : "object" ,
168+ "properties" : properties.fold (
169+ {},
170+ (preV, curr) => preV
171+ ..[curr.simpleName] = _typeToOpenApiType (curr.reflectedType))
172+ };
137173 }
138174
139175 return schemas;
0 commit comments