@@ -47,6 +47,13 @@ def get_schema_from_ref(openapi_data, ref):
4747 return node
4848
4949
50+ def make_decription (child_name : str , parent_name : str , relation_name : str ):
51+ if relation_name .endswith ("s" ):
52+ return f"Fetch all related { kebab_to_camel (child_name )} s to this { parent_name } "
53+ else :
54+ return f"Fetch { parent_name } 's { kebab_to_camel (child_name )} "
55+
56+
5057class Command (BaseCommand ):
5158
5259 def add_arguments (self , parser ):
@@ -78,30 +85,29 @@ def handle(self, *args, **options):
7885 if "get" in path_item
7986 }
8087
81- for path , path_item in paths .items ():
88+ for path , parent_path_item in paths .items ():
8289 if path .endswith (f"/{{{ RELATED_FIELD_PARAM_NAME } }}/" ):
8390 print (f" -> Found generic relationship path: { path } " )
8491 paths_to_delete .append (path )
8592
8693 base_path = path .rsplit ("/" , 2 )[0 ] + "/"
87- primary_resource_path = f"{ base_path } "
8894
89- if primary_resource_path not in paths :
95+ if base_path not in paths :
9096 print (
91- f" [!] Warning: Could not find parent path '{ primary_resource_path } ' to infer relationships. Skipping."
97+ f" [!] Warning: Could not find parent path '{ base_path } ' to infer relationships. Skipping."
9298 )
9399 continue
94100
95101 try :
96- schema_ref : str = paths [primary_resource_path ]["get" ]["responses" ][
97- "200 "
98- ]["content" ][ JSON_API_CONTENT_TYPE ]["schema" ]["$ref" ]
102+ schema_ref : str = paths [base_path ]["get" ]["responses" ][ "200 " ][
103+ "content "
104+ ][JSON_API_CONTENT_TYPE ]["schema" ]["$ref" ]
99105 primary_resource_schema = get_schema_from_ref (
100106 data , schema_ref .removesuffix ("Response" )
101107 )
102108 except KeyError :
103109 print (
104- f" [!] Warning: Could not find a valid 200 OK schema reference for '{ primary_resource_path } '. Skipping."
110+ f" [!] Warning: Could not find a valid 200 OK schema reference for '{ base_path } '. Skipping."
105111 )
106112 continue
107113
@@ -117,7 +123,7 @@ def handle(self, *args, **options):
117123 ]["properties" ]
118124 except KeyError :
119125 print (
120- f" [!] Warning: No 'properties.relationships.properties' found in schema for '{ primary_resource_path } '. Skipping."
126+ f" [!] Warning: No 'properties.relationships.properties' found in schema for '{ base_path } '. Skipping."
121127 )
122128 continue
123129
@@ -133,19 +139,26 @@ def handle(self, *args, **options):
133139 )["type" ]["enum" ][0 ]
134140 resource = resource .removesuffix ("s" )
135141 schema_name = f"{ kebab_to_camel (resource )} Response"
136- new_schema_ref = f"#/components/schemas/{ schema_name } "
142+ relationship_schema_ref = f"#/components/schemas/{ schema_name } "
137143 new_path_item = copy .deepcopy (
138144 reverse_path_index .get (f"{ fix_resource (resource )} s-retrieve" )
139145 )
140- if not new_path_item :
141- new_path_item = copy .deepcopy (path_item )
142146
143147 method = "get"
144148 operation = new_path_item [method ]
145149 operation ["operationId" ] = (
146- f"{ path_item [method ]['operationId' ]} _related_{ rel_name } "
150+ f"{ parent_path_item [method ]['operationId' ]} _related_{ rel_name } "
151+ )
152+ parent_name = schema_ref .rsplit ("/" , maxsplit = 1 )[1 ].removesuffix (
153+ "Response"
154+ )
155+ operation ["description" ] = (
156+ f"Fetch all related { kebab_to_camel (resource )} s to this { parent_name } "
157+ )
158+ operation ["description" ] = make_decription (
159+ resource , parent_name , rel_name
147160 )
148- operation ["tags" ] = path_item [method ]["tags" ]
161+ operation ["tags" ] = parent_path_item [method ]["tags" ]
149162 if "parameters" in operation :
150163 operation ["parameters" ] = [
151164 p
@@ -156,11 +169,13 @@ def handle(self, *args, **options):
156169 try :
157170 operation ["responses" ]["200" ]["content" ][JSON_API_CONTENT_TYPE ][
158171 "schema"
159- ] = {"$ref" : new_schema_ref }
172+ ] = {"$ref" : relationship_schema_ref }
160173 print (
161174 f" ✓ Creating endpoint '{ method .upper ()} { new_path_str } '"
162175 )
163- print (f" - Pointing schema to: { new_schema_ref } " )
176+ print (
177+ f" - Pointing schema to: { relationship_schema_ref } "
178+ )
164179
165180 except KeyError :
166181 print (
@@ -169,7 +184,12 @@ def handle(self, *args, **options):
169184
170185 new_paths [new_path_str ] = {"get" : operation }
171186
172- output_file = options ["output" ]
187+ self .collect_and_write_output (
188+ data , new_paths , options ["output" ], paths_to_delete
189+ )
190+
191+ @staticmethod
192+ def collect_and_write_output (data , new_paths , output_file , paths_to_delete ):
173193 if paths_to_delete :
174194 print ("\n 🔄 Updating OpenAPI structure..." )
175195 for path in paths_to_delete :
0 commit comments