Skip to content

Commit c94dccd

Browse files
committed
support parse json file
1 parent 62c396c commit c94dccd

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

pydantic_client/cli.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import argparse
22
import yaml
3+
import json
34
import re
45

56

@@ -35,7 +36,7 @@ def gen_pydantic_model(name, schema, models):
3536

3637

3738
def 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

5556
def 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

6667
def 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

Comments
 (0)