Skip to main content

Usage

From command line

spec2sdk --input-file path/to/api.yml --output-dir path/to/output-dir/

From the code

from pathlib import Path
from spec2sdk.main import generate

generate(url=Path("path/to/api.yml").absolute().as_uri(), output_dir=Path("path/to/output-dir/"))

Open API specification requirements

Operation ID

operationId must be specified for each endpoint to generate meaningful method names. It must be unique among all operations described in the API.

Input

paths:
  /health:
    get:
      operationId: healthCheck
      responses:
        '200':
          description: Successful response

Output

class APIClient:
    def health_check(self) -> None:
        ...

Inline schemas

Inline schemas should be annotated with the schema name in the x-schema-name field that doesn't overlap with the existing schema names in the specification.

Input

paths:
  /me:
    get:
      operationId: getMe
      responses:
        '200':
          description: Successful response
          content:
            application/json:
              schema:
                x-schema-name: User
                type: object
                properties:
                  name:
                    type: string
                  email:
                    type: string

Output

class User(Model):
    name: str | None = Field(default=None)
    email: str | None = Field(default=None)

Enum variable names

Variable names for enums can be specified by the x-enum-varnames field.

Input

components: 
  schemas:
    Direction:
      x-enum-varnames: [ NORTH, SOUTH, WEST, EAST ]
      type: string
      enum: [ N, S, W, E ]

Output

from enum import StrEnum

class Direction(StrEnum):
    NORTH = "N"
    SOUTH = "S"
    WEST = "W"
    EAST = "E"

Custom types

Register Python converters and renderers to implement custom types.

Input

components: 
  schemas: 
    User:
      type: object
      properties:
        name:
          type: string
        email:
          type: string
          format: email
from pathlib import Path

from spec2sdk.parsers.entities import DataType, StringDataType
from spec2sdk.generators.converters import converters
from spec2sdk.generators.entities import PythonType
from spec2sdk.generators.predicates import is_instance
from spec2sdk.generators.imports import Import
from spec2sdk.generators.models.entities import TypeRenderer
from spec2sdk.generators.models.renderers import render_root_model, renderers
from spec2sdk.main import generate


class EmailPythonType(PythonType):
    pass


def is_email_format(data_type: DataType) -> bool:
    return isinstance(data_type, StringDataType) and data_type.format == "email"


@converters.register(predicate=is_email_format)
def convert_email_field(data_type: StringDataType) -> EmailPythonType:
    return EmailPythonType(
        name=None,
        type_hint="EmailStr",
        description=data_type.description,
        default_value=data_type.default_value,
    )


@renderers.register(predicate=is_instance(EmailPythonType))
def render_email_field(py_type: EmailPythonType) -> TypeRenderer:
    return render_root_model(
        py_type,
        extra_imports=(Import(name="EmailStr", package="pydantic"),),
        content="EmailStr",
    )


if __name__ == "__main__":
    generate(url=Path("api.yml").absolute().as_uri(), output_dir=Path("output"))

Output

from pydantic import EmailStr, Field

class User(Model):
    name: str | None = Field(default=None)
    email: EmailStr | None = Field(default=None)

Metadata

Release files for spec2sdk 1.0.202410140618

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

Source distribution (sdist)

Source distribution for spec2sdk 1.0.202410140618
File Size Uploaded
spec2sdk-1.0.202410140618.tar.gz 15.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for spec2sdk 1.0.202410140618
File Interpreter ABI Platform
spec2sdk-1.0.202410140618-py3-none-any.whl Python 3 none any Details

Total release size: 37.4 kB

Release files / spec2sdk-1.0.202410140618.tar.gz

Download URL spec2sdk-1.0.202410140618.tar.gz
Size 15.3 kB
Tags Source
SHA-256 checksum
How to use checksums
97e097c45d72992f0180d0106b2fe64d60edd157f7b30bdfe512c16573611783
BLAKE2b-256 checksum
How to use checksums
d4b97a9ceefbb8ad0f779555e582c38076786f1a96b7967c5b685c915d7ac0b6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.12.3 Linux/6.8.0-1015-azure

Release files / spec2sdk-1.0.202410140618-py3-none-any.whl

Download URL spec2sdk-1.0.202410140618-py3-none-any.whl
Size 22.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c22130efb302c5c0b26c30a373b9ffed78a4d585eb7b1c4658586d2ebfa187c1
BLAKE2b-256 checksum
How to use checksums
c9e7d9281b525e1e3334eb3404e733e59c4f1c31e775584038e64d8b3b6046b3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.12.3 Linux/6.8.0-1015-azure

Release history Release notifications | RSS feed

This release

1.0.202410140618 This release

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