#open-api #definition #generate #structures

openapi-struct-gen

Generate rust structures from an openapi 3.0 definition

14 releases

0.1.13 May 5, 2023
0.1.12 May 5, 2023
0.1.11 Feb 14, 2023
0.1.6 Aug 18, 2022

#2354 in Web programming

Download history 4/week @ 2024-07-22 8/week @ 2024-08-12 6/week @ 2024-08-19 16/week @ 2024-09-09 21/week @ 2024-09-16 15/week @ 2024-09-23 28/week @ 2024-09-30 87/week @ 2024-10-07 87/week @ 2024-10-14 23/week @ 2024-10-21 3/week @ 2024-11-04

124 downloads per month

Apache-2.0

21KB
508 lines

This crate generates Rust structures from OpenAPI 3.0 definitions.

Example

Cargo.toml:

[dependencies]
serde = "1.0.142"
openapi-struct-gen = "*"

[build-dependencies]
openapi-struct-gen = { version = "*", features = ["build"] }

build.rs:

use openapi_struct_gen::generate;

fn main() {
    generate(
        format!(
            "{}/{}",
            std::env::var("CARGO_MANIFEST_DIR").unwrap(),
            "api.yaml"
        ),
        format!("{}/{}", std::env::var("OUT_DIR").unwrap(), "oapi.rs"),
        Some(&["Clone", "Serialize", "Deserialize"]),
        Some(&[("serde", "Serialize"), ("serde", "Deserialize")]),
        Some(&[(r#"#[skip_serializing_none]"#, None)]),
        Some(&[(r#"#[serde(rename_all = "camelCase")]"#, Some(&["Struct"]))]),
    ).unwrap();
}

The first aparameter is path to oapi schema. The second is the target output rust file. The third is derive statements. The fourth is use statements, being tuples of the path to an object and the object the fifth is annotations that are to be put before the derive statement. Sometimes such are required, like serde_with. Each annotation consists of a tuple - the annotation itself and optional list of structs that are not to have this annotation The sixth is annotations that are to be put after the derive statement. Most annotations would be applied like that. Each annotation consists of a tuple - the annotation itself and optional list of structs that are not to have this annotation

code:

openapi_struct_gen::include!("oapi");

Goals

  • Generate Rust structures from Open API 3.0 definitions

Non Goals

  • Generate web servers and clients

Dependencies

~0–730KB
~14K SLoC