11from collections.dict import Dict
22from emberjson import JSON , Value, Array, Object, ParseOptions, parse, to_string
33
4+
45struct OpenAPIGenerator :
56 var tags : List[Value]
67
@@ -23,53 +24,53 @@ struct OpenAPIGenerator:
2324 var path = route[" path" ][String].strip(' "' )
2425 var method = String(route[" method" ][String].strip(' "' )).lower()
2526 route_map[handler] = (String(path), method)
26-
27+
2728 for func in mojo_doc[" decl" ][Object][" functions" ][Array]._data:
2829 var func_name = str (func[][Object][" name" ]).strip(' "' )
29-
30+
3031 if func_name not in route_map:
3132 continue
32-
33+
3334 var route_info = route_map[func_name]
3435 var path = route_info.get[0 ][String]()
3536 var http_method = route_info.get[1 ][String]()
36-
37+
3738 var endpoint = self .create_endpoint(func[].object(), http_method)
38-
39+
3940 if paths.__contains__ (path):
4041 var new_path_item = JSON()
4142 var existing_path_item = paths[path][Object]
42-
43+
4344 for key in existing_path_item._data.keys():
4445 var existing_key = key[]
4546 new_path_item[existing_key] = existing_path_item[existing_key]
46-
47+
4748 new_path_item[http_method] = endpoint.object()
48-
49+
4950 paths[path] = new_path_item.object()
5051 else :
5152 var path_item = JSON()
5253 path_item[http_method] = endpoint.object()
5354 paths[path] = path_item.object()
54-
55+
5556 return paths
5657
5758 fn create_endpoint (mut self , function_data : JSON , http_method : String) raises -> JSON :
5859 var endpoint = JSON()
5960 var func_name = function_data[" name" ]
6061 endpoint[" summary" ] = String(str (func_name).strip(' "' )) + " endpoint"
6162 endpoint[" operationId" ] = func_name
62-
63+
6364 var responses = JSON()
6465 var response_200 = JSON()
65-
66+
6667 var overloads = Array(function_data[" overloads" ])._data
6768
6869 var request_description = String(" Request body" )
6970 var response_description = String(" Successful response" )
7071
7172 for i in range (len (overloads)):
72- var overload = overloads[i][Array][0 ] # first overload only for now
73+ var overload = overloads[i][Array][0 ] # first overload only for now
7374 if " returnsDoc" in overload[Object]._data:
7475 response_description = String(str (overload[Object][" returnsDoc" ]).strip(' "' ))
7576 if " summary" in overload[Object]._data:
@@ -95,14 +96,14 @@ struct OpenAPIGenerator:
9596 if " description" in arg._data:
9697 request_description = String(str (arg[" description" ]).strip(' "' ))
9798 break
98-
99+
99100 response_200[" description" ] = response_description
100-
101+
101102 var content = JSON()
102103 var text_plain = JSON()
103104 var schema = JSON()
104105 schema[" type" ] = " string"
105-
106+
106107 text_plain[" schema" ] = schema.object()
107108 content[" text/plain" ] = text_plain.object()
108109 response_200[" content" ] = content.object()
@@ -113,78 +114,78 @@ struct OpenAPIGenerator:
113114 var request_body = JSON()
114115 request_body[" required" ] = True
115116 request_body[" description" ] = request_description
116-
117+
117118 var req_content = JSON()
118119 var req_text_plain = JSON()
119120 var req_schema = JSON()
120121 req_schema[" type" ] = " string"
121-
122+
122123 req_text_plain[" schema" ] = req_schema.object()
123124 req_content[" text/plain" ] = req_text_plain.object()
124125 request_body[" content" ] = req_content.object()
125126 endpoint[" requestBody" ] = request_body.object()
126-
127+
127128 return endpoint
128129
129130 fn create_components_schema (self ) raises -> JSON :
130131 var components = JSON()
131132 var schemas = JSON()
132-
133+
133134 # Define HTTPRequest schema
134135 var http_request = JSON()
135136 var request_properties = JSON()
136-
137+
137138 var body_raw = JSON()
138139 body_raw[" type" ] = " string"
139-
140+
140141 var uri = JSON()
141142 var uri_properties = JSON()
142143 var path = JSON()
143144 path[" type" ] = " string"
144145 uri_properties[" path" ] = path.object()
145146 uri[" type" ] = " object"
146147 uri[" properties" ] = uri_properties.object()
147-
148+
148149 var method = JSON()
149150 method[" type" ] = " string"
150-
151+
151152 request_properties[" body_raw" ] = body_raw.object()
152153 request_properties[" uri" ] = uri.object()
153154 request_properties[" method" ] = method.object()
154-
155+
155156 http_request[" type" ] = " object"
156157 http_request[" properties" ] = request_properties.object()
157-
158+
158159 # Define HTTPResponse schema
159160 var http_response = JSON()
160161 var response_properties = JSON()
161162 var body = JSON()
162163 body[" type" ] = " string"
163164 response_properties[" body" ] = body.object()
164-
165+
165166 http_response[" type" ] = " object"
166167 http_response[" properties" ] = response_properties.object()
167-
168+
168169 schemas[" HTTPRequest" ] = http_request.object()
169170 schemas[" HTTPResponse" ] = http_response.object()
170171 components[" schemas" ] = schemas.object()
171-
172+
172173 return components
173174
174175 fn generate_spec (mut self , mojo_doc : JSON , router_metadata : JSON ) raises -> JSON :
175176 var spec = JSON()
176-
177+
177178 spec[" openapi" ] = " 3.0.0"
178-
179+
179180 var info = JSON()
180181 info[" title" ] = mojo_doc[" decl" ][Object][" name" ]
181182 info[" version" ] = mojo_doc[" version" ]
182183 info[" description" ] = " API generated from Mojo documentation"
183-
184+
184185 spec[" info" ] = info.object()
185186 spec[" paths" ] = self .create_paths(mojo_doc, router_metadata).object()
186187 spec[" components" ] = self .create_components_schema().object()
187-
188+
188189 return spec
189190
190191 fn read_mojo_doc (self , filename : String) raises -> JSON :
@@ -194,8 +195,7 @@ struct OpenAPIGenerator:
194195 fn read_router_metadata (self , filename : String) raises -> JSON :
195196 with open (filename, " r" ) as router_metadata:
196197 return parse[ParseOptions(fast_float_parsing = True )](router_metadata.read())
197-
198+
198199 fn save_spec (self , spec : JSON , filename : String) raises :
199200 with open (filename, " w" ) as f:
200201 f.write(to_string[pretty=True ](spec))
201-
0 commit comments