"""
Tests for OpenAPI parsing functionality.
"""
import pytest
from fastmcp_openapi.parser import OpenAPIParser
from fastmcp_openapi.models import OpenAPISpec, HTTPMethod
class TestOpenAPIParser:
"""Test OpenAPI parsing functionality."""
def setup_method(self):
"""Set up test fixtures."""
self.parser = OpenAPIParser()
def test_parse_simple_spec(self):
"""Test parsing a simple OpenAPI specification."""
spec_data = {
"openapi": "3.0.0",
"info": {
"title": "Test API",
"version": "1.0.0",
"description": "A test API"
},
"servers": [
{"url": "https://api.example.com"}
],
"paths": {
"/users": {
"get": {
"operationId": "listUsers",
"summary": "List users",
"description": "Get a list of users",
"parameters": [
{
"name": "limit",
"in": "query",
"required": False,
"schema": {"type": "integer"}
}
],
"responses": {
"200": {
"description": "Successful response",
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {"type": "object"}
}
}
}
}
}
}
}
}
}
spec = self.parser.parse_spec(spec_data)
assert isinstance(spec, OpenAPISpec)
assert spec.info.title == "Test API"
assert spec.info.version == "1.0.0"
assert spec.info.description == "A test API"
assert len(spec.servers) == 1
assert spec.servers[0].url == "https://api.example.com"
assert len(spec.operations) == 1
operation = spec.operations[0]
assert operation.operation_id == "listUsers"
assert operation.method == HTTPMethod.GET
assert operation.path == "/users"
assert operation.summary == "List users"
assert len(operation.parameters) == 1
param = operation.parameters[0]
assert param.name == "limit"
assert param.param_type == "query"
assert not param.required
assert param.schema["type"] == "integer"
def test_parse_with_request_body(self):
"""Test parsing operation with request body."""
spec_data = {
"openapi": "3.0.0",
"info": {"title": "Test API", "version": "1.0.0"},
"paths": {
"/users": {
"post": {
"operationId": "createUser",
"requestBody": {
"required": True,
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"name": {"type": "string"},
"email": {"type": "string"}
},
"required": ["name"]
}
}
}
},
"responses": {"201": {"description": "Created"}}
}
}
}
}
spec = self.parser.parse_spec(spec_data)
operation = spec.operations[0]
assert operation.method == HTTPMethod.POST
assert operation.request_body is not None
assert operation.request_body.required
assert operation.request_body.content_type == "application/json"
assert "properties" in operation.request_body.schema
def test_parse_with_path_parameters(self):
"""Test parsing operation with path parameters."""
spec_data = {
"openapi": "3.0.0",
"info": {"title": "Test API", "version": "1.0.0"},
"paths": {
"/users/{id}": {
"get": {
"operationId": "getUser",
"parameters": [
{
"name": "id",
"in": "path",
"required": True,
"schema": {"type": "string"}
}
],
"responses": {"200": {"description": "Success"}}
}
}
}
}
spec = self.parser.parse_spec(spec_data)
operation = spec.operations[0]
assert operation.path == "/users/{id}"
assert len(operation.parameters) == 1
param = operation.parameters[0]
assert param.name == "id"
assert param.param_type == "path"
assert param.required