为sqlalchemy模型映射jsonschema

alchemyjsonschema的Python项目详细描述


https://travis-ci.org/podhmo/alchemyjsonschema.svg

功能

alchemyjsonschema是用于将sqlalchemys的模型转换为jsonschema的库。

  • 使用alchemyjsonschema作为命令
  • 使用alchemyjsonschema作为库

作为库

有三种输出样式。

  • noforeignkeywalker–忽略关系
  • foreignkeywalker–期望有关关系的信息是外键
  • StructuralWalker–完整集输出(期望有关关系的信息是完整的JSON数据)

示例

转储具有以上三种输出样式的json。

目标模型在这里。组和用户。

# -*- coding:utf-8 -*-importsqlalchemyassaimportsqlalchemy.ormasormfromsqlalchemy.ext.declarativeimportdeclarative_baseBase=declarative_base()classGroup(Base):"""model for test"""__tablename__="Group"pk=sa.Column(sa.Integer,primary_key=True,doc="primary key")name=sa.Column(sa.String(255),default="",nullable=False)classUser(Base):__tablename__="User"pk=sa.Column(sa.Integer,primary_key=True,doc="primary key")name=sa.Column(sa.String(255),default="",nullable=True)group_id=sa.Column(sa.Integer,sa.ForeignKey(Group.pk),nullable=False)group=orm.relationship(Group,uselist=False,backref="users")

非外星键行者

importpprintasppfromalchemyjsonschemaimportSchemaFactoryfromalchemyjsonschemaimportNoForeignKeyWalkerfactory=SchemaFactory(NoForeignKeyWalker)pp.pprint(factory(User))"""
{'properties': {'name': {'maxLength': 255, 'type': 'string'},
                'pk': {'description': 'primary key', 'type': 'integer'}},
 'required': ['pk'],
 'title': 'User',
 'type': 'object'}
"""

外国键行者
importpprintasppfromalchemyjsonschemaimportSchemaFactoryfromalchemyjsonschemaimportForeignKeyWalkerfactory=SchemaFactory(ForeignKeyWalker)pp.pprint(factory(User))"""
{'properties': {'group_id': {'type': 'integer'},
                'name': {'maxLength': 255, 'type': 'string'},
                'pk': {'description': 'primary key', 'type': 'integer'}},
 'required': ['pk', 'group_id'],
 'title': 'User',
 'type': 'object'}
"""

结构步行机
importpprintasppfromalchemyjsonschemaimportSchemaFactoryfromalchemyjsonschemaimportStructuralWalkerfactory=SchemaFactory(StructuralWalker)pp.pprint(factory(User))"""
{'definitions': {'Group': {'properties': {'pk': {'description': 'primary key',
                                                 'type': 'integer'},
                                          'name': {'maxLength': 255,
                                                   'type': 'string'}},
                           'type': 'object'}},
 'properties': {'pk': {'description': 'primary key', 'type': 'integer'},
                'name': {'maxLength': 255, 'type': 'string'},
                'group': {'$ref': '#/definitions/Group'}},
 'required': ['pk'],
 'title': 'User',
 'type': 'object'}
"""pp.pprint(factory(Group))"""
{'definitions': {'User': {'properties': {'pk': {'description': 'primary key',
                                                'type': 'integer'},
                                         'name': {'maxLength': 255,
                                                  'type': 'string'}},
                          'type': 'object'}},
 'description': 'model for test',
 'properties': {'pk': {'description': 'primary key', 'type': 'integer'},
                'name': {'maxLength': 255, 'type': 'string'},
                'users': {'items': {'$ref': '#/definitions/User'},
                          'type': 'array'}},
 'required': ['pk', 'name'],
 'title': 'Group',
 'type': 'object'}
"""

作为命令

使用alchemyjsonschema as命令(命令名也是alchemyjsonschema)。

帮助

$ alchemyjsonschema --help
usage: alchemyjsonschema [-h][--walker {noforeignkey,foreignkey,structural}][--decision {default,fullset}][--depth DEPTH][--out OUT]
                         target

positional arguments:
  target                the module or class to extract schemas from

optional arguments:
  -h, --help            show this help message and exit
  --walker {noforeignkey,foreignkey,structural}
  --decision {default,fullset}
  --depth DEPTH
  --out OUT             output to file

以上两种模型定义(用户、组)存在于 AljyjjSnase.Test.Mask中。

目标是类位置或模块位置。例如,

  • 类位置–alchemyjsonschema.tests.models:user
  • 模块位置–alchemyjsonschema.tests.models

示例

通过命令行使用structural walker(–walker structural)。 当然,noforeignkeywalker是noforeignkey,而foreignkeywalker是foreignkey。

$ alchemyjsonschema --walker structural alchemyjsonschema.tests.models:Group

{"definitions": {"Group": {"properties": {"color": {"enum": ["red",
            "green",
            "yellow",
            "blue"],
          "maxLength": 6,
          "type": "string"},
        "created_at": {"format": "date-time",
          "type": "string"},
        "name": {"maxLength": 255,
          "type": "string"},
        "pk": {"description": "primary key",
          "type": "integer"},
        "users": {"items": {"$ref": "#/definitions/User"},
          "type": "array"}},
      "required": ["pk"],
      "title": "Group",
      "type": "object"},
    "User": {"properties": {"created_at": {"format": "date-time",
          "type": "string"},
        "name": {"maxLength": 255,
          "type": "string"},
        "pk": {"description": "primary key",
          "type": "integer"}},
      "required": ["pk"],
      "type": "object"}}}

直接使用walker类时,输出不相同。这是类似于swagger(openapi 2.0)工具的便捷输出。

附录:什么是“决定”?

什么是“决定”?(待办事项:温和描述)

$ alchemyjsonschema --walker structural alchemyjsonschema.tests.models:User | jq . -S > /tmp/default.json
$ alchemyjsonschema --decision useforeignkey --walker structural alchemyjsonschema.tests.models:User | jq . -S > /tmp/useforeignkey.json
$ diff -u /tmp/default.json /tmp/useforeignkey.json
--- /tmp/default.json 2017-01-02 22:49:44.000000000 +0900
+++ /tmp/useforeignkey.json   2017-01-02 22:53:13.000000000 +0900
@@ -1,43 +1,14 @@
 {
   "definitions": {
-    "Group": {
-      "properties": {
-        "color": {
-          "enum": [
-            "red",
-            "green",
-            "yellow",
-            "blue"
-          ],
-          "maxLength": 6,
-          "type": "string"
-        },
-        "created_at": {
-          "format": "date-time",
-          "type": "string"
-        },
-        "name": {
-          "maxLength": 255,
-          "type": "string"
-        },
-        "pk": {
-          "description": "primary key",
-          "type": "integer"
-        }
-      },
-      "required": [
-        "pk"
-      ],
-      "type": "object"
-    },
     "User": {
       "properties": {
         "created_at": {
           "format": "date-time",
           "type": "string"
         },
-        "group": {
-          "$ref": "#/definitions/Group"
+        "group_id": {
+          "relation": "group",
+          "type": "integer"
         },
         "name": {
           "maxLength": 255,

0.6.0

  • 修复jsonschema更新

0.4.2

  • 修复用模块(不是模型类)调用命令的错误

0.4.0

  • 删除不必要的功能(11)

0.3

  • 夸张的支持(感谢iSysd)

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java Google应用程序引擎Jsf2 welcomefilelist不工作   Java学校练习   jar如何在java应用程序中完成http请求流   java在将列表数据放入映射时遇到异常   java安卓studio谷歌前置条件   在Eclipse中使用gradle运行java项目   java CXF返回元素列表   java直线与垂线相交的精度   java将图像从磁盘加载到JSP页面   多线程在Java多线程进程中更新布尔值   java SDK目录“C:\Users\User\AppData\Local\Android\SDK”不存在   单击“确定”按钮时,java GUI登录系统不会做出反应   雅加达。网ConnectException:添加weblogicapplication后。xml   在两个用户定义的点之间进行java二进制搜索,返回输入的两个点之间的所有名称   需要Java IntelliJ帮助才能不读取我的文件吗   java在序列化过程中动态忽略JSON属性仅在某些情况下,对于其他api端点,不应进行过滤   java SpringBoot REST API MockMVC测试未成功创建用户,尽管在React项目中使用了相同的功能   调试java。jvisulavm中添加JMXConnection时的lang.SecurityException   基于java的web边缘检测浏览器