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

Features

Supported

The following features are supported:

  • integer (32 and 64 bit),
  • number (float only),
  • boolean,
  • string and
  • $ref references for columns.

Not Supported

The following features are on the backlog:

  • allOf inheritance,
  • $ref references for models,
  • foreign keys and
  • 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.

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

Uploaded Source

Built Distribution

openapi_SQLAlchemy-0.2.0-py3-none-any.whl (15.2 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for openapi-SQLAlchemy-0.2.0.tar.gz
Algorithm Hash digest
SHA256 d075441ffb7c0b08f65fb07afea044851548423198ff95a3821ee3b5fe3f0527
MD5 acd1ebfe43ea01395b72c0125b3ff319
BLAKE2b-256 47dd0cbdb26353b4e2c1116867d50ae6b23f0e3fcda7ea3d7e823b91f0f46b36

See more details on using hashes here.

File details

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

File metadata

  • Download URL: openapi_SQLAlchemy-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 15.2 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/40.8.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.7.4

File hashes

Hashes for openapi_SQLAlchemy-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 58cf6a8d0358fba03f69963d75a0653b2ae0cbb8f248b904e3425e2716e5fc42
MD5 c941a1261c2634b2e26f6abf85f27835
BLAKE2b-256 f1d2fdd72d001c1fac54aabb0dae6603a5ecb6a1d15f462b54a21efe7335e8c9

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