Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
310 views
in Technique[技术] by (71.8m points)

python - Using the models generated by OpenAPI Generator with sqlalchemy

I have generated a python-flask server based upon my OAS API schema (say PETS.yaml) using the OpenAPI Generator.

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

Here, is a sample of the models generated using OpenAPI generator.

# 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.

Now I would like to use the generated models directly with SQLAchemacy. However, SQLAchemacy requires that the baseclass of each model should be db.Model, and each column is defined as a Column. For example (from 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

So is it somehow possible to use the models generated by OpenAPI Generator directly with SQLAchemacy, without redefining them in the format required by SQLAchemacy?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...