【问题标题】:Using the models generated by OpenAPI Generator with sqlalchemy将 OpenAPI Generator 生成的模型与 sqlalchemy 一起使用
【发布时间】:2020-12-30 05:28:47
【问题描述】:

我已经使用OpenAPI Generator根据我的 OAS API 架构(比如PETS.yaml)生成了一个 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), unique=True, nullable=False)

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

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

【问题讨论】:

    标签: python flask flask-sqlalchemy openapi-generator connexion


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-26
      • 2022-07-23
      • 2017-08-20
      • 1970-01-01
      • 2021-08-08
      • 2022-10-13
      • 1970-01-01
      • 2020-02-13
      相关资源
      最近更新 更多