From d1b874a692e64ebf93d46f76fa85f084c6590df1 Mon Sep 17 00:00:00 2001 From: Carmen Alvarez Date: Tue, 18 Jul 2023 21:05:53 +0200 Subject: [PATCH] Fix the blueprint for pydantic version 2 Use the proper `ref_template`, specifying the `{model}` in the template. An example is documented here: https://github.com/pydantic/pydantic/blob/main/docs/usage/json_schema.md#schema-customization We need to specify `ref_template = '#/components/schemas/{model}'`, not `ref_template = '#/components/schemas/'` (need that extra `{model}` at the end). `model_json_schema` generates something like this, for a `Parent` model with a `child` field of type `Child`: ```json { "$defs": { "Child": { "properties": { "foo": { "default": 5, "title": "Foo", "type": "integer" } }, "title": "Child", "type": "object" } }, "properties": { "child": { "anyOf": [ { "$ref": "#/components/schemas/Child" }, { "type": "null" } ], "default": null } }, "title": "Parent", "type": "object" } ``` The definitions are in a key `$defs` (not `definitions`), and the references start with `#/components/schemas` as expected. --- docs/blueprints/pydantic2.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/blueprints/pydantic2.py b/docs/blueprints/pydantic2.py index 6bc684d8..2837ce1d 100644 --- a/docs/blueprints/pydantic2.py +++ b/docs/blueprints/pydantic2.py @@ -14,10 +14,10 @@ def get_name(self, auto_schema, direction): def map_serializer(self, auto_schema, direction): # let pydantic generate a JSON schema - schema = model_json_schema(self.target, ref_template="#/components/schemas") + schema = model_json_schema(self.target, ref_template="#/components/schemas/{model}") # pull out potential sub-schemas and put them into component section - for sub_name, sub_schema in schema.pop("definitions", {}).items(): + for sub_name, sub_schema in schema.pop("$defs", {}).items(): component = ResolvedComponent( name=sub_name, type=ResolvedComponent.SCHEMA,