11import argparse
22import yaml
3+ import json
34import re
45
56
@@ -35,7 +36,7 @@ def gen_pydantic_model(name, schema, models):
3536
3637
3738def collect_models (components ):
38- """收集所有 schema 并生成 Pydantic model"""
39+ """collect all schema and generate the Pydantic model"""
3940 models = {}
4041 for name , schema in components .items ():
4142 models [name ] = gen_pydantic_model (name , schema , models )
@@ -53,7 +54,7 @@ def method_decorator(method):
5354
5455
5556def get_req_model (operation ):
56- # 只支持 application/json,且仅取第一个
57+ # only support application/json
5758 req_body = operation .get ("requestBody" , {})
5859 content = req_body .get ("content" , {})
5960 for ct , obj in content .items ():
@@ -64,7 +65,7 @@ def get_req_model(operation):
6465
6566
6667def get_resp_model (operation ):
67- # 只分析 2xx 且 application/json 响应
68+ # only 2xx and application/json
6869 for code , resp in operation .get ("responses" , {}).items ():
6970 if not code .startswith ("2" ):
7071 continue
@@ -119,7 +120,10 @@ def main():
119120 args = parser .parse_args ()
120121
121122 with open (args .file , "r" , encoding = "utf-8" ) as f :
122- raw = yaml .safe_load (f )
123+ if args .file .endswith (".json" ):
124+ raw = json .load (f )
125+ else :
126+ raw = yaml .safe_load (f )
123127
124128 # 1. 解析 models
125129 if "components" in raw and "schemas" in raw ["components" ]:
0 commit comments