Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hypergraph update breaks parsing into code generator #55

Open
rtbs-dev opened this issue Dec 20, 2021 · 1 comment · May be fixed by #56
Open

Hypergraph update breaks parsing into code generator #55

rtbs-dev opened this issue Dec 20, 2021 · 1 comment · May be fixed by #56

Comments

@rtbs-dev
Copy link

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'
    )
@rtbs-dev
Copy link
Author

rtbs-dev commented Aug 3, 2022

Update: based on some digging and problem-solving here, along with the openapi spec docs, I've realized that there's not actually a problem with the schema itself, unless #56 is what is desired, but rather that the validators are unaware of the treatment of bools inside additionalProperties, items, etc.

By substituting false-> {"not":{}} in v2.1, the code generator I referenced works just fine. Feel free to close, unless you want to keep the reference alive for #56! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant