Skip to content

Commit 2218711

Browse files
authored
Merge pull request #1 from S3wnkin/dev
fix: correctly detect msgspec types nested inside builtin containers
2 parents e2fb22c + a80b804 commit 2218711

1 file changed

Lines changed: 17 additions & 10 deletions

File tree

fastapi_msgspec_openapi/updater.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717
def _update_method_response(
1818
path: str,
1919
method: str,
20-
struct: type[msgspec.Struct],
20+
return_type: Any,
2121
status_code: int,
2222
paths: dict[str, Any],
23+
openapi_schema: dict[str, Any],
2324
) -> None:
2425
"""
2526
Update a single method response schema to reference a msgspec.Struct.
@@ -29,9 +30,10 @@ def _update_method_response(
2930
Args:
3031
path: Route path
3132
method: HTTP method (lowercase)
32-
struct: msgspec.Struct type to reference
33+
return_type: Full return type annotation
3334
status_code: HTTP status code
3435
paths: OpenAPI paths dictionary
36+
openapi_schema: Full OpenAPI schema for components updates
3537
"""
3638
if method not in paths[path]:
3739
logger.debug("Method %s not in path %s", method.upper(), path)
@@ -52,21 +54,25 @@ def _update_method_response(
5254

5355
description = responses[status_str].get("description", "Successful Response")
5456

57+
(schema,), components = msgspec.json.schema_components(
58+
(return_type,),
59+
ref_template="#/components/schemas/{name}",
60+
)
61+
62+
if components:
63+
schemas = openapi_schema.setdefault("components", {}).setdefault("schemas", {})
64+
schemas.update(components)
65+
5566
endpoint_schema["responses"][status_str] = {
5667
"description": description,
57-
"content": {
58-
"application/json": {
59-
"schema": {"$ref": f"#/components/schemas/{struct.__name__}"}
60-
}
61-
},
68+
"content": {"application/json": {"schema": schema}},
6269
}
6370

6471
logger.debug(
65-
"Updated %s %s response %s to reference %s",
72+
"Updated %s %s response %s with msgspec schema",
6673
method.upper(),
6774
path,
6875
status_str,
69-
struct.__name__,
7076
)
7177

7278

@@ -106,9 +112,10 @@ def update_route_responses(
106112
_update_method_response(
107113
path=path,
108114
method=method.lower(),
109-
struct=struct,
115+
return_type=return_type,
110116
status_code=status_code,
111117
paths=paths,
118+
openapi_schema=openapi_schema,
112119
)
113120

114121
except (NameError, AttributeError) as e:

0 commit comments

Comments
 (0)