Skip to main content

Create FastAPI routes from OpenAPI spec

Project description

Reasoning

FastAPI is an awesome framework that simplifies the process of creating APIs. One of the most exciting features is that it can generate OpenAPI specs out of the box. But what if.. you have an OpenAPI spec and you need to create an API from it?

One day we faced that problem — we had to create an API from multiple OpenAPI specs, and make sure that the incoming requests and the outgoing responses were aligned with the models defined the specs.

⚠️ This library was created to cover only our own needs first. So for now it's not suitable for everyone and has a lot of technical restrictions. Please consider it as experimental stuff

Installation

The package is available on PyPi:

pip install openapi-to-fastapi

Generating FastAPI routes

The main purpose of this library is to generate FastAPI routes from OpenAPI specs. This is done by:

from pathlib import Path
from openapi_to_fastapi.routes import SpecRouter

specs = Path("./specs")

router = SpecRouter(specs).to_fastapi_router()

The code above will create a FastAPI router that can be either included into the main router, or used as the default one.

Imagine you have a following spec (some parts are cut off for brevity):

{
  "openapi": "3.0.2",
  "paths": {
    "/Company/BasicInfo": {
      "post": {
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/BasicCompanyInfoRequest",
                "responses": {
                  "200": {
                    "content": {
                      "application/json": {
                        "schema": {
                          "$ref": "#/components/schemas/BasicCompanyInfoResponse"
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      },
      "components": {
        "schemas": {
          "BasicCompanyInfoRequest": {
            "title": "BasicCompanyInfoRequest",
            "required": ["companyId"],
            "type": "object",
            "properties": {
              "companyId": {
                "title": "Company Id",
                "type": "string",
                "example": "2464491-9"
              }
            }
          },
          "BasicCompanyInfoResponse": {
            "title": "BasicCompanyInfoResponse",
            "required": ["name", "companyId", "companyForm"],
            "type": "object",
            "properties": {
              "name": {
                "title": "Name of the company",
                "type": "string"
              },
              "companyId": {
                "title": "ID of the company",
                "type": "string"
              },
              "companyForm": {
                "title": "The company form of the company",
                "type": "string"
              }
            }
          }
        }
      }
    }
  }
}

The FastAPI route equivalent could look like this:

class BasicCompanyInfoRequest(pydantic.BaseModel):
    companyId: str

class BasicCompanyInfoResponse(pydantic.BaseModel):
    name: str
    companyId: str
    companyForm: str


@router.post("/Company/BasicInfo", response_model=BasicCompanyInfoResponse)
def _route(request: BasicCompanyInfoRequest):
    return {}

And openapi-to-fastapi can create it automagically.

Custom routes

In most cases it makes no sense to create an API without any business logic.

Here's how to define it:

from fastapi import Header, HTTPException
from openapi_to_fastapi.routes import SpecRouter

spec_router = SpecRouter("./specs")

# Default handler for all POST endpoints found in the spec
@spec_router.post()
def hello_world(params, x_my_token: str = Header(...)):
    if x_my_token != "my_token":
        raise HTTPException(status_code=403, detail="Sorry")
    return {"Hello": "World"}

# Specific endpoint for a "/pet" route
@spec_router.post("/pet")
def create_pet(params):
    pet = db.make_pet(name=params.name)
    return pet.to_dict()

router = spec_router.to_fastapi_router()

API Documentation

Now after you have a lot of routes, you might want to leverage another great feature of FastAPI — auto documentation.

Request and response models are already handled. But to display documentation nicely, FastAPI needs to assign a name for each endpoint. Here is how you can provide such name:

from openapi_to_fastapi.routes import SpecRouter

spec_router = SpecRouter("./specs")

@spec_router.post(
    "/pet",
    name="Create a pet",
    description="Create a pet",
    response_description="A Pet",
    tags=["pets"],
)
def create_pet(params):
    return {}

# Or you can set the dynamic name based on API path
def name_factory(path: str, **kwargs):
    return path.replace("/", " ")

@spec_router.post(name_factory=name_factory)
def create_pet(params):
    return {}

OpenAPI validation

This package also provides a CLI entrypoint to validate OpenAPI specs. It's especially useful when you need to define you own set of rules for validation.

Imagine your API specs are stored in a separate repository and maintained by another team. You also expect that all OpenAPI specs have only one endpoint defined (some internal agreement).

Now you can set up a CI check and validate them on every push.

Firstly create a file with a custom validator:

# my_validator.py

from openapi_to_fastapi.validator import BaseValidator, OpenApiValidationError

class CustomError(OpenApiValidationError):
    pass

# important: must be inherited from BaseValidator
class MyValidator(BaseValidator):

    # implement this single method
    def validate_spec(self, spec: dict):
        if len(spec["paths"]) != 1:
            raise CustomError("Only one endpoint allowed")

Then run the tool:

openapi-validator --path ./standards -m my_validator.py -v MyValidator

===============================================================================
OpenAPI specs root path: ./standards
Validators: DefaultValidator, MyValidator
===============================================================================
File: ./standards/Current.json
[PASSED]
-------------------------------------------------------------------------------
File: ./standards/Metric.json
[PASSED]
-------------------------------------------------------------------------------
File: ./standards/BasicInfo.json
[PASSED]
-------------------------------------------------------------------------------
===============================================================================
Summary:
Total : 3
Passed: 3
Failed: 0
===============================================================================

This validator can also be reused when generating routes:

router = SpecRouter(specs, validators=[MyValidator])

Development

You will need:

  • Python 3.8+ (though 3.11+ might have some issues with dependencies)
  • pre-commit

Before working on the project, make sure you run:

pre-commit install

After making changes you can run tests:

poetry run invoke test

License

This code is released under the BSD 3-Clause license. Details in the LICENSE file.

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_to_fastapi-0.13.0.tar.gz (18.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

openapi_to_fastapi-0.13.0-py3-none-any.whl (22.8 kB view details)

Uploaded Python 3

File details

Details for the file openapi_to_fastapi-0.13.0.tar.gz.

File metadata

  • Download URL: openapi_to_fastapi-0.13.0.tar.gz
  • Upload date:
  • Size: 18.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.0 CPython/3.9.18 Linux/6.2.0-1015-azure

File hashes

Hashes for openapi_to_fastapi-0.13.0.tar.gz
Algorithm Hash digest
SHA256 4cebe5693c0c1355e17687b0dcc63ad4eab81cf82fb1a552be6ce080fe7d2485
MD5 2cdd783f8bbf23cd54d4525686776a14
BLAKE2b-256 fcf94816a9627b064f69dc1ff870b0afb767389843f500f83a63f3874cb68daf

See more details on using hashes here.

File details

Details for the file openapi_to_fastapi-0.13.0-py3-none-any.whl.

File metadata

  • Download URL: openapi_to_fastapi-0.13.0-py3-none-any.whl
  • Upload date:
  • Size: 22.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.0 CPython/3.9.18 Linux/6.2.0-1015-azure

File hashes

Hashes for openapi_to_fastapi-0.13.0-py3-none-any.whl
Algorithm Hash digest
SHA256 44af68f21e070604c6675864bb641eff32e590d60b512da30da7a6299a4e13c6
MD5 3268c7897f92d5e405d6eb560d264158
BLAKE2b-256 73105b94ab097b3a2c11b968456629ec3a0580e55fc90b95556ede9e6cc80364

See more details on using hashes here.

Supported by

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