Skip to main content

Crate product_os_openapi

Crate product_os_openapi 

Source
Expand description

§Product OS OpenAPI

A no_std compatible library for working with OpenAPI/Swagger specifications.

This crate provides strongly-typed data structures for both OpenAPI v2 (Swagger) and OpenAPI v3.x specifications, with full serialization and deserialization support via serde.

§Features

  • Dual Version Support: Works with both Swagger 2.0 and OpenAPI 3.x specifications
  • no_std Compatible: 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

§Cargo Features

FeatureDefaultDescription
stdYesStandard library support via no-std-compat/std
openapiYesSerde serialization/deserialization for OpenAPI structs

§Usage Examples

§Parsing OpenAPI v3 Specification

use product_os_openapi::{ProductOSOpenAPI, Info};
use serde_json;

let json = r#"{
    "openapi": "3.0.0",
    "info": {
        "title": "My API",
        "version": "1.0.0"
    }
}"#;

let spec: ProductOSOpenAPI = serde_json::from_str(json)
    .expect("Failed to parse OpenAPI spec");

assert_eq!(spec.info.title, "My API");

§Parsing Swagger v2 Specification

use product_os_openapi::ProductOSOpenAPI;
use serde_json;

let json = r#"{
    "swagger": "2.0",
    "info": {
        "title": "My API",
        "version": "1.0.0"
    },
    "host": "api.example.com",
    "basePath": "/v1"
}"#;

let spec: ProductOSOpenAPI = serde_json::from_str(json)
    .expect("Failed to parse Swagger spec");

assert_eq!(spec.swagger, Some("2.0".to_owned()));

§Creating Specifications Programmatically

use product_os_openapi::{ProductOSOpenAPI, Info};
use serde_json;

let spec = ProductOSOpenAPI {
    openapi: Some("3.0.0".to_owned()),
    info: Info {
        title: "My API".to_owned(),
        version: "1.0.0".to_owned(),
        description: Some("A test API".to_owned()),
        summary: None,
        terms_of_service: None,
        contact: None,
        license: None,
    },
    json_schema_dialect: None,
    servers: None,
    paths: None,
    webhooks: None,
    components: None,
    definitions: None,
    security: None,
    tags: None,
    external_docs: None,
    swagger: None,
    host: None,
    base_path: None,
    schemes: None,
    consumes: None,
    produces: None,
};

let json = serde_json::to_string_pretty(&spec).unwrap();

§OpenAPI v2 vs v3

This crate supports both versions by including fields from both specifications in the main ProductOSOpenAPI struct:

  • OpenAPI v3 uses: openapi, servers, components, webhooks, json_schema_dialect
  • Swagger v2 uses: swagger, host, base_path, schemes, consumes, produces, definitions
  • Both versions share: info, paths, security, tags, external_docs

When deserializing, the appropriate fields will be populated based on the specification version.

Structs§

Components
Reusable components for an OpenAPI specification.
Contact
Contact information for the API.
Discriminator
Discriminator for polymorphism support.
Encoding
Property encoding information for multipart and form-urlencoded request bodies.
Example
Example value for a parameter, request body, or response.
ExternalDocs
External documentation reference.
Header
HTTP header definition.
Info
Metadata about the API.
License
License information for the API.
Link
Link to an operation that can be followed from a response.
MediaType
Media type and schema for request/response content.
OAuthFlow
Single OAuth2 flow configuration.
OAuthFlows
OAuth2 flow configurations.
Operation
A single API operation on a path.
Parameter
Operation parameter.
PathItem
Describes a single API path and its operations.
ProductOSOpenAPI
Root structure for OpenAPI/Swagger specifications.
Reference
Reference to a reusable component.
RequestBody
Request body for an operation.
Response
Response from an API operation.
Schema
JSON Schema definition for data models.
SchemaProperty
Property definition within a Schema.
SecurityScheme
Security scheme definition.
Server
Server connectivity information.
ServerVariable
Variable substitution information for a Server URL.
Tag
Tag for grouping operations.
XML
XML serialization metadata.

Enums§

SchemaLocation
Parameter or schema location within an API operation.
SchemaType
JSON Schema data types supported by OpenAPI.

Type Aliases§

Callbacks
Type alias for callback definitions.