Skip to content

Commit 2fcf58f

Browse files
committed
bugfixes - schemas and paths
1 parent 9607d16 commit 2fcf58f

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

src/serverless_openapi_generator/pydantic_handler.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@ def patch_token_region_request_schema(schema_file_path):
4747
def generate_dto_schemas(source_dir: Path, output_dir: Path, project_root: Path):
4848
"""Generates JSON schemas for Pydantic DTOs and returns a dict of successful ones."""
4949
rprint(f"[bold]Searching for DTOs in: {source_dir}[/bold]")
50-
sys.path.insert(0, str(project_root))
50+
51+
# Add the parent of the source directory to the path to allow for package-level imports
52+
import_root = source_dir.parent
53+
sys.path.insert(0, str(import_root))
54+
5155
output_dir.mkdir(parents=True, exist_ok=True)
5256

5357
discovered_models = []
@@ -60,7 +64,7 @@ def generate_dto_schemas(source_dir: Path, output_dir: Path, project_root: Path)
6064
processed_dto_files.add(dto_file_path)
6165

6266
rprint(f" [cyan]Processing DTO file: {dto_file_path}[/cyan]")
63-
relative_path = dto_file_path.relative_to(project_root)
67+
relative_path = dto_file_path.relative_to(import_root)
6468
module_name_parts = list(relative_path.parts)
6569
if module_name_parts[-1] == "dtos.py":
6670
module_name_parts[-1] = "dtos"
@@ -193,21 +197,20 @@ def generate_serverless_config(successfully_generated_schemas, project_meta, pro
193197

194198
model_entries = []
195199
if successfully_generated_schemas:
196-
for model_name, schema_file_name in sorted(successfully_generated_schemas.items()):
197-
description = f"Schema for {model_name}"
198-
try:
199-
with open(project_root / "openapi_models" / schema_file_name, "r") as sf:
200-
schema_content = json.load(sf)
201-
if "description" in schema_content and schema_content["description"]:
202-
description = schema_content["description"]
203-
except Exception: # nosec
204-
pass
200+
for schema_info in sorted(successfully_generated_schemas, key=lambda x: x['name']):
201+
model_name = schema_info['name']
202+
description = schema_info.get('description', f"Schema for {model_name}")
203+
204+
# The schema is already loaded, so we can embed it directly or reference it
205+
# For this implementation, we'll stick to the file reference model
206+
schema_file_name = f"{model_name}.json"
207+
205208
model_entries.append(
206209
{
207210
"name": model_name,
208211
"description": description,
209212
"contentType": "application/json",
210-
"schema": "${file(openapi_models/" + schema_file_name + ")}",
213+
"schema": f"${{file(openapi_models/{schema_file_name})}}",
211214
}
212215
)
213216

0 commit comments

Comments
 (0)