Skip to main content

Generate SiLA2 Feature Definitions and proxy servers from OpenAPI specifications

Project description

openapi-to-sila2

PyPI version Python 3.10+ License: Apache 2.0 SiLA2 Standard

Transform REST APIs into SiLA2 Laboratory Automation Services

Automatically generate SiLA2 Feature Definition Language (FDL) and gRPC-based proxy servers from OpenAPI specifications. Bridge the gap between RESTful lab APIs and standardized laboratory automation.


What is openapi-to-sila2?

openapi-to-sila2 is a code generator that converts OpenAPI specifications into SiLA2-compliant services. Instead of manually writing SiLA2 feature definitions, you describe your API in OpenAPI format and let this tool generate production-ready SiLA2 servers.

This enables:

  • Immediate SiLA2 compliance without rewriting your API
  • Seamless integration with existing OpenAPI-based systems
  • Proxy servers that connect legacy REST APIs to modern lab automation
  • Type-safe Python code with automatic class generation

The Workflow

┌─────────────────────┐
│  OpenAPI Spec       │
│  (JSON/YAML)        │
└──────────┬──────────┘
           │
    ┌──────▼──────────────────┐
    │ openapi-to-sila2        │
    │ FDL Generator           │
    │                         │
    │ • Parse OpenAPI         │
    │ • Map to SiLA2 concepts │
    │ • Validate against XSD  │
    └──────┬──────────────────┘
           │
    ┌──────▼──────────────────┐
    │ SiLA2 Feature Defs      │
    │ (*.xml)                 │
    └──────┬──────────────────┘
           │
    ┌──────▼──────────────────┐
    │ sila2-codegen           │
    │ (Official Tool)         │
    │                         │
    │ • Generate .proto files │
    │ • Generate gRPC stubs   │
    └──────┬──────────────────┘
           │
    ┌──────▼──────────────────┐
    │ openapi-to-sila2        │
    │ Class Generator         │
    │                         │
    │ • Parse .proto files    │
    │ • Create dataclasses    │
    │ • Extract custom types  │
    └──────┬──────────────────┘
           │
    ┌──────▼──────────────────┐
    │ Python Code             │
    │ • Feature classes       │
    │ • Custom type classes   │
    │ • Server/Client stubs   │
    │ • gRPC services         │
    └──────────────────────────┘

Quick Start

1. Install

pip install openapi-to-sila2

2. Generate from OpenAPI

openapi-to-sila2 generate \
  --input your-api.openapi.json \
  --output ./generated \
  --codegen \
  --types

3. Implement Features

Implement feature logic in Python by extending generated base classes. See the examples/ directory for complete working examples, or refer to the Python SiLA2 Framework Documentation for server implementation patterns.


Mapping Reference

How OpenAPI concepts map to SiLA2:

OpenAPI Concept SiLA2 Concept Details
Tag Feature Groups operations into reusable features
GET (no parameters) Property Returns a single read-only value
GET (with query/path params) Command Parameterized query operation
POST/PUT/DELETE Command State-modifying operations
string String UTF-8 text
integer / int32 / int64 Integer Whole numbers
number / float / double Real Decimal numbers
boolean Boolean True/false values
array List[T] Ordered collection of type T
object Structure Named collection of typed fields
security scheme Header parameter Authentication tokens, API keys
response object Response type Named struct with operation outputs
error response ExecutionError Standardized error reporting

Parameter Mapping

  • Query parameters → Command parameters (Structure.QueryParameters)
  • Path parameters → Command parameters (Structure.PathParameters)
  • Request body → Command parameters (Structure.RequestBody)
  • Header authorization → Command parameters (Structure.HeaderParameters)

CLI Reference

openapi-to-sila2 generate

Generate SiLA2 Feature Definition Language files from an OpenAPI specification.

Usage:

openapi-to-sila2 generate [OPTIONS]

Options:

Option Shorthand Description Default
--input PATH -i Required. Path to OpenAPI spec (JSON or YAML)
--output PATH -o Output directory for generated FDL files .
--codegen Run sila2-codegen after FDL generation false
--types Generate Python type classes (requires --codegen) false

Examples:

# Basic: Generate FDL only
openapi-to-sila2 generate -i api.json -o ./generated

# Full pipeline: FDL → gRPC → Python types
openapi-to-sila2 generate \
  -i api.json \
  -o ./generated \
  --codegen \
  --types

openapi-to-sila2 version

Display the installed version.

openapi-to-sila2 version
# Output: openapi-to-sila2 version: 0.1.0

Python API

Use openapi-to-sila2 programmatically in your build scripts:

FDL Generation

from openapi_to_sila2 import FDLGenerator

generator = FDLGenerator()
generator.generate_fdl_from_openapi(
    openapi_spec_path="./api.openapi.json",
    output_directory="./generated"
)

Custom Type Generation

from openapi_to_sila2 import Sila2ClassGenerator

generator = Sila2ClassGenerator()
class_code = generator.generate_classes_from_proto(
    proto_file_path="./generated/myfeature/myfeature.proto"
)

with open("./generated/myfeature/types.py", "w") as f:
    f.write(class_code)

How It Works

1. OpenAPI Parsing

  • Reads OpenAPI specification (JSON/YAML)
  • Validates against OpenAPI 3.0+ schema
  • Normalizes tags and operation structure

2. SiLA2 Mapping

  • Each tag becomes a Feature
  • GET endpoints (no params) become Properties
  • Other methods become Commands
  • Response schemas become Custom Data Types

3. FDL Generation

  • Generates XML Feature Definition files
  • Validates against SiLA2 XSD schemas
  • Includes error definitions and type constraints

4. Code Generation (via sila2-codegen)

  • Generates .proto files for gRPC
  • Creates server and client base classes
  • Produces type definitions

5. Type Refinement

  • Extracts types from generated .proto files
  • Creates native Python dataclasses
  • Replaces auto-generated Any types with specific types

Known Limitations

Be aware of these limitations when designing your OpenAPI specification:

Type Composition

  • Complex type unions (allOf, oneOf, anyOf) are converted to SiLA2 Any type
    • Workaround: Flatten schemas or use single allOf with discriminator
    • SiLA2 AllowedTypes constraint exists but only supports built-in types

Error Handling

  • Multiple error schemas cannot be represented in SiLA2
    • SiLA2 supports only one ExecutionError per feature
    • HTTP error responses are mapped to a generic error with response details
    • Workaround: Document error details in error description

Parameter Structure

  • Request parameters are unified into a single Parameters structure
    • Path parameters, query parameters, and body are combined
    • Cannot reuse parameter types across different endpoints
    • Workaround: Use consistent parameter naming across similar endpoints

Empty/Dynamic Objects

  • Objects with no defined properties become SiLA2 Any type
    • Prevents strongly-typed access to arbitrary JSON objects
    • Workaround: Define explicit properties in OpenAPI schema

Observable Streams

  • SiLA2 Properties return single values, not streams
    • Streaming APIs must be converted to commands if possible
    • True observable properties require custom SiLA2 feature design

Examples

See the examples/ directory for working demonstrations:

  • Authentication: JWT token-based login and authorization
  • CRUD Operations: Complete instrument lifecycle management
  • Observable Streams: Real-time measurement data subscriptions
  • Proxy Server: Full proxy from REST API to SiLA2
  • Discovery: Automatic SiLA2 server discovery via mDNS

To run examples:

cd examples/
just install

See examples/README.md for detailed instructions.


System Requirements

  • Python: 3.10 or higher
  • sila2: 0.13.0+ with codegen support (installs automatically)
  • gRPC: Installed as dependency via sila2
  • mDNS: For server discovery (requires system support)

Architecture

The tool is organized into three main components:

FDL Generator

File: src/openapi_to_sila2/fdl_generator.py

Converts OpenAPI specifications to SiLA2 Feature Definition Language XML files. Handles:

  • Tag-to-feature mapping
  • Operation-to-command/property conversion
  • Data type extraction and definition
  • Error handling and validation

Class Generator

File: src/openapi_to_sila2/class_generator.py

Generates native Python dataclasses from generated .proto files. Provides:

  • Type-safe request/response objects
  • Automatic import management
  • Dependency ordering

CLI

File: src/openapi_to_sila2/cli.py

Command-line interface orchestrating the full pipeline and user interaction.


Contributing

We welcome contributions! Please see CONTRIBUTING.md for:

  • Development setup
  • Code style guidelines
  • Testing procedures
  • Submitting pull requests

License

© 2024 QPillars GmbH

Licensed under the Apache License 2.0. See LICENSE for details.


Learn More


Questions or issues? Open an issue or check the examples/ for common patterns.

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_sila2-0.1.1.tar.gz (183.0 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_sila2-0.1.1-py3-none-any.whl (23.9 kB view details)

Uploaded Python 3

File details

Details for the file openapi_to_sila2-0.1.1.tar.gz.

File metadata

  • Download URL: openapi_to_sila2-0.1.1.tar.gz
  • Upload date:
  • Size: 183.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for openapi_to_sila2-0.1.1.tar.gz
Algorithm Hash digest
SHA256 7caa4642797c671903274fb28e05fb528fd22cff1e52cd267deaacfda3e3906e
MD5 20f3319729f23467fdb16d1564804601
BLAKE2b-256 9e346c7027072924fb260031e33c95788b0e26caa2537a3794a24cccca57872c

See more details on using hashes here.

Provenance

The following attestation bundles were made for openapi_to_sila2-0.1.1.tar.gz:

Publisher: publish.yml on qpillars/openapi-to-sila2

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file openapi_to_sila2-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for openapi_to_sila2-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7bc33b3407491a810481c988e7bdc884e0794e074111e677a3979485973a37f2
MD5 d3fbd78f93cf62a5d88a128a1d787e34
BLAKE2b-256 94806631d6eb0be9ef9637b2cc7d833df0ba9fd99d2495ce410d621cb98170ab

See more details on using hashes here.

Provenance

The following attestation bundles were made for openapi_to_sila2-0.1.1-py3-none-any.whl:

Publisher: publish.yml on qpillars/openapi-to-sila2

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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