Skip to main content

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 files for OpenAPI-SQLAlchemy 0.6.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for OpenAPI-SQLAlchemy 0.6.1
File Size Uploaded
OpenAPI-SQLAlchemy-0.6.1.tar.gz 13.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for OpenAPI-SQLAlchemy 0.6.1
File Interpreter ABI Platform
OpenAPI_SQLAlchemy-0.6.1-py3-none-any.whl Python 3 none any Details

Total release size: 36.8 kB

Release files / OpenAPI-SQLAlchemy-0.6.1.tar.gz

Download URL OpenAPI-SQLAlchemy-0.6.1.tar.gz
Size 13.8 kB
Tags Source
SHA-256 checksum
How to use checksums
e114d5bb9fcd7231b4794407b4ef817c82f8e6d10da6c0a36a7a3cb78f855c65
BLAKE2b-256 checksum
How to use checksums
3656c0f0a140ab4e9a24f499742b99ba5e2a44930f35b32c6e4c3154206dc2eb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.14.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.7.4

Release files / OpenAPI_SQLAlchemy-0.6.1-py3-none-any.whl

Download URL OpenAPI_SQLAlchemy-0.6.1-py3-none-any.whl
Size 23.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
21ad722ec4d7150db77a039d43054ad283dd359fd5a9a078cf780908185c2992
BLAKE2b-256 checksum
How to use checksums
ef9aedfeacfe6f95b07a65866708a2ccd67ff8b152049081328713b937f6e791
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.14.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.7.4

Release history Release notifications | RSS feed

0.7.0

2 release files

0.6.3

2 release files

0.6.2

2 release files

This release

0.6.1 This release

2 release files

0.6.0

3 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.0

1 release file

0.2.0

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page