Skip to content

Hypergraph update breaks parsing into code generator #55

Open
@rtbs-dev

Description

@rtbs-dev

Previous version (v2.0) could be read as a jsonschema object to create pydantic schemas (which fwiw, I was originally intending to submit a PR to the python api to use pydantic for a smooth automated maintenance experience).

Using datamodel-code-generator to create python objects with the (now quite powerful/popular) pydantic library worked just fine with v2.0:

datamodel-codegen \
   --url https://jsongraphformat.info/v2.0/json-graph-schema.json \
   --output model.py \
   --class-name 'JSONGraphSchema'     

works just fine, but trying the new hypergraph version throws an obscure error:

datamodel-codegen \
   --url https://raw.githubusercontent.com/jsongraph/json-graph-specification/master/json-graph-schema_v2.json \
   --output tidy/model.py \
   --class-name 'JSONGraphSchema'
...
  File "pydantic/main.py", line 406, in pydantic.main.BaseModel.__init__
pydantic.error_wrappers.ValidationError: 1 validation error for JsonSchemaObject
properties -> additionalProperties
  value is not a valid dict (type=type_error.dict)

I originally was opening an issue for the generator codebase, but came across this related issue, which toward the end looks like the problem came from a schema problem. Having some difficulty determining which change is causing this, however.


In case you're curious, the python generated is quite nice for the previous version (pasted below). Happy to open a PR or start a repo that automates this if you'd want a "standardized" pydantic-based api for python users. I'll be using this as a starting point for more general graph interop for networkx, pandas, serialization, etc.

# generated by datamodel-codegen:
#   filename:  https://jsongraphformat.info/v2.0/json-graph-schema.json
#   timestamp: 2021-12-20T19:44:51+00:00

from __future__ import annotations

from typing import Any, Dict, List, Optional, Union

from pydantic import BaseModel, Extra, Field


class Nodes(BaseModel):
    class Config:
        extra = Extra.forbid

    label: Optional[str] = None
    metadata: Optional[Dict[str, Any]] = None


class Edge(BaseModel):
    class Config:
        extra = Extra.forbid

    id: Optional[str] = None
    source: str
    target: str
    relation: Optional[str] = None
    directed: Optional[bool] = True
    label: Optional[str] = None
    metadata: Optional[Dict[str, Any]] = None


class Graph(BaseModel):
    class Config:
        extra = Extra.forbid

    id: Optional[str] = None
    label: Optional[str] = None
    directed: Optional[bool] = True
    type: Optional[str] = None
    metadata: Optional[Dict[str, Any]] = None
    nodes: Optional[Dict[str, Nodes]] = None
    edges: Optional[List[Edge]] = None


class JSONGraphSchemaItem(BaseModel):
    class Config:
        extra = Extra.forbid

    graph: Graph


class JSONGraphSchemaItem1(BaseModel):
    class Config:
        extra = Extra.forbid

    graphs: Optional[List[Graph]] = None


class JSONGraphSchema(BaseModel):
    __root__: Union[JSONGraphSchemaItem, JSONGraphSchemaItem1] = Field(
        ..., title='JSON Graph Schema'
    )

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions