From 5135a0dcb69bbe6d189d72df7b5a36ca6f0aa4dd Mon Sep 17 00:00:00 2001 From: Ali Toosi Date: Fri, 27 Sep 2024 01:01:18 +1000 Subject: [PATCH 1/3] Add support for Pydantic V2 x OpenAPI 3 Pydantic V2 uses `$defs` key in its schema. --- chalice_spec/pydantic.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/chalice_spec/pydantic.py b/chalice_spec/pydantic.py index 6f17815..0935983 100644 --- a/chalice_spec/pydantic.py +++ b/chalice_spec/pydantic.py @@ -32,15 +32,16 @@ def schema_helper( # If the spec has passed, we probably have nested models to contend with. spec: Union[APISpec, None] = kwargs.pop("spec", None) - if spec and "definitions" in schema: - for k, v in schema["definitions"].items(): - try: - spec.components.schema(k, v) - except DuplicateComponentNameError: - pass - - if "definitions" in schema: - del schema["definitions"] + for key in ("definitions", "$defs"): + if spec and key in schema: + for k, v in schema[key].items(): + try: + spec.components.schema(k, v) + except DuplicateComponentNameError: + pass + + if key in schema: + del schema[key] return schema From 3f85baee0f36d25085aa13d654d6d5644b15ce19 Mon Sep 17 00:00:00 2001 From: Ali Toosi Date: Fri, 27 Sep 2024 01:46:01 +1000 Subject: [PATCH 2/3] Fix tests to use an empty model instead of BaseModel Cannot call schema directly on BaseModel in Pydantic V2 --- tests/test_blueprint.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_blueprint.py b/tests/test_blueprint.py index 5f6b1d2..0739563 100644 --- a/tests/test_blueprint.py +++ b/tests/test_blueprint.py @@ -139,7 +139,7 @@ def test_blueprint_three_with_default_docs(): "requestBody": { "content": { "multipart/form-data": { - "schema": {"$ref": "#/components/schemas/BaseModel"} + "schema": {"$ref": "#/components/schemas/EmptyModel"} } } }, @@ -148,7 +148,7 @@ def test_blueprint_three_with_default_docs(): "description": "Success", "content": { "application/json": { - "schema": {"$ref": "#/components/schemas/BaseModel"} + "schema": {"$ref": "#/components/schemas/EmptyModel"} } }, } @@ -160,7 +160,7 @@ def test_blueprint_three_with_default_docs(): "openapi": "3.0.1", "components": { "schemas": { - "BaseModel": {"title": "BaseModel", "type": "object", "properties": {}} + "EmptyModel": {"title": "EmptyModel", "type": "object", "properties": {}} } }, } From 93997c7f9bd8c23887a623561a3bd5608f019fa4 Mon Sep 17 00:00:00 2001 From: Ali Toosi Date: Fri, 27 Sep 2024 01:46:50 +1000 Subject: [PATCH 3/3] Use EmptyModel instead of BaseModel Cannot call schema directly on BaseModel in Pydantic V2 --- chalice_spec/chalice.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/chalice_spec/chalice.py b/chalice_spec/chalice.py index aa55781..76636e7 100644 --- a/chalice_spec/chalice.py +++ b/chalice_spec/chalice.py @@ -10,6 +10,10 @@ from pydantic import BaseModel +class EmptyModel(BaseModel): + pass + + def default_docs_for_methods( methods: List[str], content_types: Optional[List[str]] = None ): @@ -22,11 +26,11 @@ def default_docs_for_methods( **{ method: Operation( content_types=content_types, - response=BaseModel, + response=EmptyModel, request=( None if method in ["get", "delete", "head", "options"] - else BaseModel + else EmptyModel ), ) for method in methods