Skip to main content

Maps an OpenAPI schema to SQLAlchemy models.

Project description

OpenAPI-SQLAlchemy

Translates an OpenAPI schema to SQLAlchemy models.

Installation

python3 -m pip install OpenAPI-SQLAlchemy
# To be able to load yaml file
python3 -m pip install PyYAML

Example

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")

Documentation

Read the Docs

Features

Supported

The following features are supported:

  • integer (32 and 64 bit),
  • number (float only),
  • boolean,
  • string,
  • $ref references for columns and models,
  • foreign keys,
  • many to one relationships and
  • allOf inheritance for columns and models.

Not Supported

The following features are on the backlog:

  • one to many relationships.

Contributing

Fork and checkout the repository. To install:

python3 -m venv venv
source ./venv/bin/activate
python3 -m pip install -e .[dev]

To run tests:

tox

Make your changes and raise a pull request.

Compiling Docs

python3 -m venv venv
cd docs
make html

This creates the index.html file in docs/build/html/index.html.

Release Commands

rm -r dist/*
python setup.py sdist bdist_wheel
python -m twine upload dist/*

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.6.3.tar.gz (15.2 kB view details)

Uploaded Source

Built Distribution

OpenAPI_SQLAlchemy-0.6.3-py3-none-any.whl (24.8 kB view details)

Uploaded Python 3

File details

Details for the file OpenAPI-SQLAlchemy-0.6.3.tar.gz.

File metadata

  • Download URL: OpenAPI-SQLAlchemy-0.6.3.tar.gz
  • Upload date:
  • Size: 15.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.8.0

File hashes

Hashes for OpenAPI-SQLAlchemy-0.6.3.tar.gz
Algorithm Hash digest
SHA256 e837a5e7f3dcaab2bcf9a6e5aa98360b7a6ab8ff95231cf32a00c9bf341f51e8
MD5 11563f14fc29b466eda823e8bec5810b
BLAKE2b-256 c7c9dd1de48086636d650afdd07897605cd8bf12a2f5d9957ba29bfd3a55062c

See more details on using hashes here.

File details

Details for the file OpenAPI_SQLAlchemy-0.6.3-py3-none-any.whl.

File metadata

  • Download URL: OpenAPI_SQLAlchemy-0.6.3-py3-none-any.whl
  • Upload date:
  • Size: 24.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.8.0

File hashes

Hashes for OpenAPI_SQLAlchemy-0.6.3-py3-none-any.whl
Algorithm Hash digest
SHA256 baa7137a81b858adff4a6c40d49f6da03ab7c9df02a8068d9e3a131df62b4b9d
MD5 da8023608d289dd55cd3b793aa066c8a
BLAKE2b-256 38652baab984684d203608406e40aab463a62ded4827e3f30e879753058ca21f

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