Skip to content

Unable to parse models under $defs tag, output from pydantic v2's models_json_schema #1578

@mweidman-fn

Description

@mweidman-fn

Describe the bug
Hello, I've been unable to parse json within "$defs"

The json schema below is representative of the output from a call to Pydantic v2's models_json_schema. The resulting python model doesn't include the ArrayDataset model.

Replacing "$defs" with "definitions" gives the expected output.

To Reproduce

Example schema:

{
    "$defs": {
        "ArrayDataset": {
            "description": "mini_schema",
            "properties": {
                "creation_datetime": {
                    "anyOf": [
                        {
                            "format": "date-time",
                            "type": "string"
                        },
                        {
                            "type": "null"
                        }
                    ],
                    "default": null,
                    "title": "Creation Datetime"
                },
                "id": {
                    "anyOf": [
                        {
                            "type": "string"
                        },
                        {
                            "type": "null"
                        }
                    ],
                    "default": null,
                    "title": "Id"
                },
                "schema_version": {
                    "anyOf": [
                        {
                            "type": "string"
                        },
                        {
                            "type": "null"
                        }
                    ],
                    "default": null,
                    "title": "Schema Version"
                },
                
            },
            "required": [],
            "title": "mini_schema",
            "type": "object"
        }
    },
    "title": "Metadata Schema"
}

Actual Output:

# generated by datamodel-codegen:
#   filename:  mini_migration_schema.json
#   timestamp: 2023-09-27T23:09:31+00:00

from __future__ import annotations

from typing import Any

from pydantic import Field, RootModel


class MetadataSchema(RootModel):
    root: Any = Field(..., title='Metadata Schema')

Used commandline:

$ datamodel-codegen --input mini_migration_schema.json --input-file-type  jsonschema --output mini_migration_schema_v2.py --output-model-type pydantic_v2.BaseModel

Expected behavior

# generated by datamodel-codegen:
#   filename:  mini_migration_schema.json
#   timestamp: 2023-09-27T23:03:13+00:00

from __future__ import annotations

from datetime import datetime
from typing import Any, Optional

from pydantic import BaseModel, Field, RootModel


class MetadataSchema(RootModel):
    root: Any = Field(..., title='Metadata Schema')


class ArrayDataset(BaseModel):
    creation_datetime: Optional[datetime] = Field(None, title='Creation Datetime')
    id: Optional[str] = Field(None, title='Id')
    schema_version: Optional[str] = Field(None, title='Schema Version')

Version:

  • OS: OSX Ventura 13.4
  • Python version: 3.11.4
  • datamodel-code-generator version: 0.21.5 and 0.22.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions