Skip to main content

Maps an openapi schema to SQLAlchemy models.

Project description

openapi-SQLAlchemy

Translates an openapi schema to SQLAlchemy models. For example, given the following openapi specification:

# example-spec.yml
openapi: "3.0.0"

info:
  title: Test Schema
  description: API to illustrate openapi-SQLALchemy MVP.
  version: "0.1"

paths:
  /employee:
    get:
      summary: Used to retrieve all employees.
      responses:
        200:
          description: Return all employees from the database.
          content:
            application/json:
              schema:
                type: array
                items:
                  "$ref": "#/components/schemas/Employee"

components:
  schemas:
    Employee:
      description: Person that works for a company.
      type: object
      x-tablename: employee
      properties:
        id:
          type: integer
          description: Unique identifier for the employee.
          example: 0
          x-primary-key: true
          x-autoincrement: true
        name:
          type: string
          description: The name of the employee.
          example: David Andersson.
          x-index: true
        division:
          type: string
          description: The part of the company the employee works in.
          example: Engineering
          x-index: true
        salary:
          type: number
          description: The amount of money the employee is paid.
          example: 1000000.00
      required:
        - id
        - name
        - division

The SQLALchemy models file then becomes:

# models.py
from yaml import load, Loader
from sqlalchemy.ext.declarative import declarative_base
from openapi_sqlalchemy import init_model_factory


Base = declarative_base()
with open("example-spec.yml") as spec_file:
    SPEC = load(spec_file, Loader=Loader)
MODEL_FACTORY = init_model_factory(base=Base, spec=SPEC)


Employee = MODEL_FACTORY(name="Employee")

Features

Supported

The following features are supported:

  • integer (32 and 64 bit),
  • number (float only),
  • boolean and
  • string.

Not Supported

The following features are on the backlog:

  • allOf inheritance,
  • $ref references for columns,
  • $ref references for models,
  • foreign keys and
  • relationships.

Contributing

Fork and checkout the repository. To install:

python -m venv venv
python -m pip install -e .

To run tests:

tox

Make your changes and raise a pull request.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

openapi-SQLAlchemy-0.1.0.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

openapi_SQLAlchemy-0.1.0-py3-none-any.whl (14.3 kB view details)

Uploaded Python 3

File details

Details for the file openapi-SQLAlchemy-0.1.0.tar.gz.

File metadata

  • Download URL: openapi-SQLAlchemy-0.1.0.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.33.0 CPython/3.7.4

File hashes

Hashes for openapi-SQLAlchemy-0.1.0.tar.gz
Algorithm Hash digest
SHA256 ad4b0ec97f7db4a4ef229f431e22561f8aeec3b25dddaa81d02c36bf16ccbf5b
MD5 b60b15653055dab963b1c30cf7b0f4f4
BLAKE2b-256 dfeb6ba5ec247d7fc660f74d1f655fd326948ad21352d65445838c2c395d35c7

See more details on using hashes here.

File details

Details for the file openapi_SQLAlchemy-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: openapi_SQLAlchemy-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 14.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.33.0 CPython/3.7.4

File hashes

Hashes for openapi_SQLAlchemy-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cb605c06d77b8b0b2cc78249857692f8e58d568548f5d56eee0c585337b30db9
MD5 2043a73863d26414cfb3e83f9daeb045
BLAKE2b-256 568021fb1e49cb98aab60c6ec5fc9f473f047258d23a1f0a02cd8e12fce644eb

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page