使用OpenAPI生成器生成的模型和sqlalchemy



我已经使用OpenAPI生成器基于我的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。模型,每个列都定义为一个列。例如(来自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一起使用,而无需以SQLAchemary所需的格式重新定义它们?

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

相关内容

  • 没有找到相关文章

最新更新