Product OS : Openapi
Product OS : OpenAPI provides a set of structs for defining the structure of an OpenAPI / Swagger specification. This crate is no_std compatible and requires the 'openapi' feature for serialization/deserialization support. Intended to be used with Product OS : Connector.
What is Product OS?
Product OS is a collection of packages that provide different tools and features that can work together to build products more easily for the Rust ecosystem.
Feature Flags
| Feature | Default | Description |
|---|---|---|
openapi |
yes | Enables: serde, serde/derive, serde_json, serde_json/alloc |
std |
yes | Enables: no-std-compat/std, openapi |
default features: std, openapi
Installation
[]
= "0.0.1"
Pin the version to match the crate Cargo.toml when using path or git dependencies.
Documentation
Full API documentation is available at docs.rs/product-os-openapi.
Usage
Overview
A no_std compatible Rust library for working with OpenAPI/Swagger specifications.
Features
- Dual Version Support: Works with both Swagger 2.0 and OpenAPI 3.x specifications
no_stdCompatible: Can be used in embedded and constrained environments- Type Safety: Strongly typed structs with proper validation through the type system
- Serde Integration: Full JSON serialization/deserialization support
- Well Documented: Comprehensive rustdoc documentation with examples
- Zero Unsafe Code: Pure safe Rust implementation
Installation
Add this to your Cargo.toml:
[]
= "0.0.5"
Or use cargo add:
Usage
Parsing OpenAPI v3 Specification
use ProductOSOpenAPI;
use serde_json;
let json = r#"{
"openapi": "3.0.0",
"info": {
"title": "My API",
"version": "1.0.0"
},
"paths": {
"/users": {
"get": {
"summary": "List users",
"responses": {
"200": {
"description": "Success"
}
}
}
}
}
}"#;
let spec: ProductOSOpenAPI = from_str?;
assert_eq!;
assert!;
assert_eq!;
Parsing Swagger v2 Specification
use ProductOSOpenAPI;
use serde_json;
let json = r#"{
"swagger": "2.0",
"info": {
"title": "My API",
"version": "1.0.0"
},
"host": "api.example.com",
"basePath": "/v1",
"paths": {}
}"#;
let spec: ProductOSOpenAPI = from_str?;
assert!;
assert_eq!;
Creating Specifications Programmatically
use ;
let spec = ProductOSOpenAPI ;
let json = to_string_pretty?;
OpenAPI v2 vs v3
This crate supports both versions by including fields from both specifications:
OpenAPI v3 Specific Fields
openapi- Version string (e.g., "3.0.0")servers- Server connectivity informationcomponents- Reusable componentswebhooks- Incoming webhook definitions (v3.1+)json_schema_dialect- JSON Schema dialect URI (v3.1+)
Swagger v2 Specific Fields
swagger- Version string (must be "2.0")host- API hostbase_path- Base path for endpointsschemes- Transfer protocolsconsumes- Global MIME types consumedproduces- Global MIME types produceddefinitions- Data type definitions
Shared Fields (Both Versions)
info- API metadata (required)paths- API paths and operationssecurity- Security requirementstags- Tag definitionsexternal_docs- External documentation
Helper Methods
The library provides convenient helper methods:
// Check specification version
if spec.is_openapi_v3
if spec.is_swagger_v2
// Get version string
if let Some = spec.version
Performance
Benchmarks on a modern CPU show:
- Deserialization: ~6µs per operation
- Serialization: ~3µs per operation
Run benchmarks with:
no_std Support
This crate is no_std compatible. When using without the standard library, you need to enable the openapi feature which provides alloc support:
[]
= { = "0.0.5", = false, = ["openapi"] }
Features
default- Enablesstdandopenapifeaturesstd- Enables standard library supportopenapi- Enables serde serialization/deserialization (requiresalloc)
Related Crates
This crate is part of the Product OS ecosystem and is designed to work with:
product-os-connector- Framework for defining APIs using OpenAPI specs
Documentation
Full documentation is available at docs.rs/product-os-openapi.
Contributing
Contributions are not currently available but will be available on a public repository soon.
License
This project is licensed under the GNU GPLv3.