jschema-to-python icon indicating copy to clipboard operation
jschema-to-python copied to clipboard

Add generation with dataclasses, and type annotations

Open BowenBao opened this issue 3 years ago • 1 comments

BowenBao avatar Oct 25 '22 00:10 BowenBao

I installed your branch via pip install git+https://github.com/BowenBao/jschema-to-python.git@dataclasses_and_type_annotations

Given:

example_schema.json

{
    "title": "Example Schema",
    "type": "object",
    "properties": {
        "firstName": {
            "type": "string"
        },
        "lastName": {
            "type": "string"
        },
        "age": {
            "description": "Age in years",
            "type": "integer",
            "minimum": 0
        },
        "dogs": {
            "type": "array",
            "items": {
                "type": "string"
            },
            "maxItems": 4
        },
        "address": {
            "type": "object",
            "properties": {
                "street": {
                    "type": "string"
                },
                "city": {
                    "type": "string"
                },
                "state": {
                    "type": "string"
                }
            },
            "required": [
                "street",
                "city"
            ]
        },
        "gender": {
            "type": "string",
            "enum": [
                "male",
                "female"
            ]
        },
        "deceased": {
            "enum": [
                "yes",
                "no",
                1,
                0,
                "true",
                "false"
            ]
        }
    },
    "required": [
        "firstName",
        "lastName"
    ]
}

And example_schema_hints.json:

{
  "ExampleSchema": {
    "properties": {
      "firstName": {
        "type": "str"
      },
      "lastName": {
        "type": "str"
      },
      "age": {
        "type": "int"
      },
      "dogs": {
        "type": "List[str]"
      },
      "address": {
        "type": "Address"
      },
      "gender": {
        "type": "str"
      },
      "deceased": {
        "type": "Union[str, int, bool]"
      }
    }
  },
  "Address": {
    "properties": {
      "street": {
        "type": "str"
      },
      "city": {
        "type": "str"
      },
      "state": {
        "type": "str"
      }
    }
  }
}

I managed to generate:

# This file was generated by jschema_to_python version 0.0.1.dev30.

import dataclasses
from __future__ import annotations
from typing import List, Any, Optional
from typing_extensions import Literal


@dataclasses.dataclass
class ExampleSchema(object):
    first_name: str = dataclasses.field(metadata={"schema_property_name": "firstName"})
    last_name: str = dataclasses.field(metadata={"schema_property_name": "lastName"})
    address: Any = dataclasses.field(default=None, metadata={"schema_property_name": "address"})
    age: Optional[int] = dataclasses.field(default=None, metadata={"schema_property_name": "age"})
    deceased: Optional[Literal['yes', 'no', 1, 0, 'true', 'false']] = dataclasses.field(default=None, metadata={"schema_property_name": "deceased"})
    dogs: Optional[List[str]] = dataclasses.field(default=None, metadata={"schema_property_name": "dogs"})
    gender: Optional[str] = dataclasses.field(default=None, metadata={"schema_property_name": "gender"})

using python -m jschema_to_python -s "./tests/input_schemas/example_schema.json" -o "./generated_python_classes/" -r "ExampleSchema" -m "generated_python_classes" -g "./tests/input_schemas/example_schema_hints.json" -f -l "dataclasses"

This is 95% what I want to do.

Question: I did not dig in details into code repo.Do you know if the code already in place it could manage to auto-generate Address and use this new type instead of Any or this should probably require a repo modification?

Greg7000 avatar Apr 03 '25 13:04 Greg7000