将 OpenAPI Generator 生成的模型与 sqlalchemy 结合使用

问题描述

我已经使用 PETS.yaml 基于我的 OAS API 架构(比如 OpenAPI Generator生成一个 python-flask 服务器。

docker run --rm \
  -v ${PWD}:/local openapitools/openapi-generator-cli generate \
  -i /local/petstore.yaml \
  -g go \
  -o /local/out/go

此处是使用 OpenAPI 生成生成的模型示例。

# coding: utf-8

from __future__ import absolute_import
from datetime import date,datetime  # noqa: F401

from typing import List,Dict  # noqa: F401

from openapi_server.models.base_model_ import Model
from openapi_server.models.category import Category
from openapi_server.models.tag import Tag
from openapi_server import util

from openapi_server.models.category import Category  # noqa: E501
from openapi_server.models.tag import Tag  # noqa: E501

class Pet(Model):
    """NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).

    Do not edit the class manually.
    """

    def __init__(self,id=None,category=None,name=None,photo_urls=None,tags=None,status=None):  # noqa: E501
        """Pet - a model defined in OpenAPI

        :param id: The id of this Pet.  # noqa: E501
        :type id: int
        :param category: The category of this Pet.  # noqa: E501
        :type category: Category
        :param name: The name of this Pet.  # noqa: E501
        :type name: str
        :param photo_urls: The photo_urls of this Pet.  # noqa: E501
        :type photo_urls: List[str]
        :param tags: The tags of this Pet.  # noqa: E501
        :type tags: List[Tag]
        :param status: The status of this Pet.  # noqa: E501
        :type status: str
        """
        self.openapi_types = {
            'id': int,'category': Category,'name': str,'photo_urls': List[str],'tags': List[Tag],'status': str
        }

        self.attribute_map = {
            'id': 'id','category': 'category','name': 'name','photo_urls': 'photoUrls','tags': 'tags','status': 'status'
        }

        self._id = id
        self._category = category
        self._name = name
        self._photo_urls = photo_urls
        self._tags = tags
        self._status = status

    @classmethod
    def from_dict(cls,dikt) -> 'Pet':
        """Returns the dict as a model

        :param dikt: A dict.
        :type: dict
        :return: The Pet of this Pet.  # noqa: E501
        :rtype: Pet
        """
        return util.deserialize_model(dikt,cls)

    @property
    def name(self):
        """Gets the name of this Pet.


        :return: The name of this Pet.
        :rtype: str
        """
        return self._name

    @name.setter
    def name(self,name):
        """Sets the name of this Pet.


        :param name: The name of this Pet.
        :type name: str
        """
        if name is None:
            raise ValueError("Invalid value for `name`,must not be `None`")  # noqa: E501

        self._name = name


etc.

现在我想直接将生成的模型与 sqlAchemacy 一起使用。但是sqlAchemacy要求每个模型的基类应该是db.Model,并且每一列都定义为一个Column。例如(来自SQLAlchemy):

class User(db.Model):
    id = db.Column(db.Integer,primary_key=True)
    username = db.Column(db.String(80),unique=True,nullable=False)
    email = db.Column(db.String(120),nullable=False)

    def __repr__(self):
        return '<User %r>' % self.username

那么是否有可能将 OpenAPI Generator 生成的模型直接与 sqlAchemacy 一起使用,而无需以 sqlAchemacy 要求的格式重新定义它们?

解决方法

这不完全是您要找的,但您可以使用 OpenAlchemy 使用相同的 OAS 生成 SQLAlchemy 模型。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...