Files
harmony/opnsense-api/examples/test_haproxy_deser.rs
Jean-Gabriel Gill-Couture 777213288e fix(opnsense-config): use serde_json::Value for HAProxy config traversal
The hand-written HaproxyGetResponse structs used HashMap which fails
when OPNsense returns [] for empty collections. The generated types
in opnsense-api handle this via opn_map, but opnsense-config had
duplicated structs without that fix.

Replace all hand-written HAProxy response types with serde_json::Value
traversal. This avoids the duplication and handles the []/{} duality.

Also fix integration example:
- Use high ports (16443, 18443) to avoid conflicting with web UI on 443
- Skip package install if already installed
- Use harmony_cli::cli_logger::init() instead of env_logger (safe to
  call multiple times)
- Increase verification timeout to 60s

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-25 11:42:35 -04:00

48 lines
1.5 KiB
Rust

//! Test HAProxy settings deserialization against a real OPNsense instance.
//!
//! This proves whether the generated types handle a fresh HAProxy install
//! (where most collections are empty `[]` not `{}`).
//!
//! ```text
//! cargo run --example test_haproxy_deser
//! ```
mod common;
use opnsense_api::generated::haproxy::OpNsenseHaProxyResponse;
#[tokio::main]
async fn main() {
let client = common::client_from_env();
// First try with serde_json::Value to see the raw response
println!("=== Raw JSON (first 500 chars) ===");
let raw: serde_json::Value = client
.get_typed("haproxy", "settings", "get")
.await
.expect("raw get failed");
let raw_str = serde_json::to_string_pretty(&raw).unwrap();
println!("{}", &raw_str[..raw_str.len().min(500)]);
println!("...");
println!("Total JSON size: {} bytes", raw_str.len());
// Now try with the generated types
println!("\n=== Typed deserialization ===");
match client
.get_typed::<OpNsenseHaProxyResponse>("haproxy", "settings", "get")
.await
{
Ok(resp) => {
println!("SUCCESS — deserialized into OpNsenseHaProxyResponse");
println!(" general.enabled: {:?}", resp.haproxy.general.enabled);
}
Err(e) => {
println!("FAILED — {e}");
println!();
println!("This means the generated types don't handle the API response.");
println!("The codegen needs to be fixed.");
}
}
}