Add mockapi source files and tests
This commit is contained in:
@@ -10,20 +10,12 @@ class OpenAPIValidator:
|
|||||||
"""Validates OpenAPI 3.x specifications."""
|
"""Validates OpenAPI 3.x specifications."""
|
||||||
|
|
||||||
def __init__(self, spec: Dict[str, Any]):
|
def __init__(self, spec: Dict[str, Any]):
|
||||||
"""Initialize the validator.
|
"""Initialize the validator."""
|
||||||
|
|
||||||
Args:
|
|
||||||
spec: The OpenAPI specification dictionary
|
|
||||||
"""
|
|
||||||
self.spec = spec
|
self.spec = spec
|
||||||
self._validation_errors: List[str] = []
|
self._validation_errors: List[str] = []
|
||||||
|
|
||||||
def validate(self) -> List[str]:
|
def validate(self) -> List[str]:
|
||||||
"""Validate the OpenAPI specification.
|
"""Validate the OpenAPI specification."""
|
||||||
|
|
||||||
Returns:
|
|
||||||
List of validation error messages (empty if valid)
|
|
||||||
"""
|
|
||||||
self._validation_errors = []
|
self._validation_errors = []
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@@ -48,49 +40,23 @@ class OpenAPIValidator:
|
|||||||
return str(error)
|
return str(error)
|
||||||
|
|
||||||
def is_valid(self) -> bool:
|
def is_valid(self) -> bool:
|
||||||
"""Check if the specification is valid.
|
"""Check if the specification is valid."""
|
||||||
|
|
||||||
Returns:
|
|
||||||
True if valid, False otherwise
|
|
||||||
"""
|
|
||||||
return len(self.validate()) == 0
|
return len(self.validate()) == 0
|
||||||
|
|
||||||
def get_paths(self) -> List[str]:
|
def get_paths(self) -> List[str]:
|
||||||
"""Get list of paths in the spec.
|
"""Get list of paths in the spec."""
|
||||||
|
|
||||||
Returns:
|
|
||||||
List of path strings
|
|
||||||
"""
|
|
||||||
return list(self.spec.get("paths", {}).keys())
|
return list(self.spec.get("paths", {}).keys())
|
||||||
|
|
||||||
def get_operations(self, path: str) -> Dict[str, Any]:
|
def get_operations(self, path: str) -> Dict[str, Any]:
|
||||||
"""Get all operations for a given path.
|
"""Get all operations for a given path."""
|
||||||
|
|
||||||
Args:
|
|
||||||
path: The path to get operations for
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
Dictionary of method -> operation
|
|
||||||
"""
|
|
||||||
path_item = self.spec.get("paths", {}).get(path, {})
|
path_item = self.spec.get("paths", {}).get(path, {})
|
||||||
methods = ["get", "post", "put", "delete", "patch", "options", "head", "trace"]
|
methods = ["get", "post", "put", "delete", "patch", "options", "head", "trace"]
|
||||||
return {m: path_item[m] for m in methods if m in path_item}
|
return {m: path_item[m] for m in methods if m in path_item}
|
||||||
|
|
||||||
def get_schema(self, schema_name: str) -> Optional[Dict[str, Any]]:
|
def get_schema(self, schema_name: str) -> Optional[Dict[str, Any]]:
|
||||||
"""Get a schema by name from components/schemas.
|
"""Get a schema by name from components/schemas."""
|
||||||
|
|
||||||
Args:
|
|
||||||
schema_name: Name of the schema
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
Schema definition or None if not found
|
|
||||||
"""
|
|
||||||
return self.spec.get("components", {}).get("schemas", {}).get(schema_name)
|
return self.spec.get("components", {}).get("schemas", {}).get(schema_name)
|
||||||
|
|
||||||
def get_all_schemas(self) -> Dict[str, Any]:
|
def get_all_schemas(self) -> Dict[str, Any]:
|
||||||
"""Get all schemas from the spec.
|
"""Get all schemas from the spec."""
|
||||||
|
return self.spec.get("components", {}).get("schemas", {})
|
||||||
Returns:
|
|
||||||
Dictionary of schema name -> schema definition
|
|
||||||
"""
|
|
||||||
return self.spec.get("components", {}).get("schemas", {})
|
|
||||||
|
|||||||
Reference in New Issue
Block a user