fix(yamlreaders): Improve format auto-detection in get_reader()
Now properly detects Postman and OpenAPI formats by parsing YAML content
This commit is contained in:
1 parent
805b28e344
commit
6ae350b48c
1 file changed
+16
-5
+16
-5
@@ -6,6 +6,7 @@ from .openapi_reader import OpenAPIReader
|
|||||||
|
|
||||||
def get_reader(source: str) -> BaseReader:
|
def get_reader(source: str) -> BaseReader:
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
import yaml
|
||||||
|
|
||||||
path = Path(source)
|
path = Path(source)
|
||||||
|
|
||||||
@@ -13,12 +14,22 @@ def get_reader(source: str) -> BaseReader:
|
|||||||
raise FileNotFoundError(f"File not found: {path}")
|
raise FileNotFoundError(f"File not found: {path}")
|
||||||
|
|
||||||
with open(path, "r", encoding="utf-8") as f:
|
with open(path, "r", encoding="utf-8") as f:
|
||||||
first_line = f.readline().strip()
|
content = f.read(2048)
|
||||||
|
|
||||||
if '"openapi"' in first_line or "openapi:" in first_line:
|
if "openapi:" in content:
|
||||||
return OpenAPIReader()
|
try:
|
||||||
|
data = yaml.safe_load(content)
|
||||||
|
if isinstance(data, dict) and "openapi" in data:
|
||||||
|
return OpenAPIReader()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
if '"info"' in first_line and '"item"' in first_line:
|
try:
|
||||||
return PostmanReader()
|
data = yaml.safe_load(content)
|
||||||
|
if isinstance(data, dict):
|
||||||
|
if "info" in data and "item" in data:
|
||||||
|
return PostmanReader()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
return YamlReader()
|
return YamlReader()
|
||||||
Reference in new issue
Block a user