use product_os_openapi::*;
#[test]
fn test_info_construction() {
let info = Info {
title: "My API".to_owned(),
version: "1.0.0".to_owned(),
summary: Some("A summary".to_owned()),
description: Some("A description".to_owned()),
terms_of_service: Some("https://example.com/tos".to_owned()),
contact: Some(Contact {
name: Some("Support".to_owned()),
url: Some("https://example.com".to_owned()),
email: "support@example.com".to_owned(),
}),
license: Some(License {
name: "MIT".to_owned(),
identifier: Some("MIT".to_owned()),
url: Some("https://opensource.org/licenses/MIT".to_owned()),
}),
};
assert_eq!(info.title, "My API");
assert_eq!(info.version, "1.0.0");
assert!(info.contact.is_some());
assert!(info.license.is_some());
}
#[test]
fn test_info_serialization() {
let info = Info {
title: "Test".to_owned(),
version: "1.0.0".to_owned(),
summary: None,
description: None,
terms_of_service: None,
contact: None,
license: None,
};
let json = serde_json::to_string(&info).expect("Failed to serialize");
let deserialized: Info = serde_json::from_str(&json).expect("Failed to deserialize");
assert_eq!(deserialized.title, info.title);
assert_eq!(deserialized.version, info.version);
}
#[test]
fn test_contact_with_email() {
let json = r#"{
"name": "API Support",
"email": "support@example.com"
}"#;
let contact: Contact = serde_json::from_str(json).expect("Failed to deserialize");
assert_eq!(contact.name, Some("API Support".to_owned()));
assert_eq!(contact.email, "support@example.com");
assert_eq!(contact.url, None);
}
#[test]
fn test_license_with_identifier() {
let json = r#"{
"name": "Apache 2.0",
"identifier": "Apache-2.0"
}"#;
let license: License = serde_json::from_str(json).expect("Failed to deserialize");
assert_eq!(license.name, "Apache 2.0");
assert_eq!(license.identifier, Some("Apache-2.0".to_owned()));
}
#[test]
fn test_server_with_variables() {
let json = r#"{
"url": "https://{environment}.example.com:{port}/v1",
"description": "Multi-environment server",
"variables": {
"environment": {
"default": "production",
"enum": ["production", "staging", "development"],
"description": "Environment name"
},
"port": {
"default": "443"
}
}
}"#;
let server: Server = serde_json::from_str(json).expect("Failed to deserialize");
assert_eq!(server.url, "https://{environment}.example.com:{port}/v1");
assert!(server.variables.is_some());
let vars = server.variables.unwrap();
assert!(vars.contains_key("environment"));
assert!(vars.contains_key("port"));
let env_var = &vars["environment"];
assert_eq!(env_var.default, "production");
assert!(env_var.enumeration.is_some());
let enums = env_var.enumeration.as_ref().unwrap();
assert_eq!(enums.len(), 3);
}
#[test]
fn test_server_variable() {
let json = r#"{
"default": "8443",
"enum": ["8443", "443"]
}"#;
let var: ServerVariable = serde_json::from_str(json).expect("Failed to deserialize");
assert_eq!(var.default, "8443");
assert!(var.enumeration.is_some());
}
#[test]
fn test_schema_type_display() {
assert_eq!(SchemaType::Object.to_string(), "object");
assert_eq!(SchemaType::String.to_string(), "string");
assert_eq!(SchemaType::Integer.to_string(), "integer");
assert_eq!(SchemaType::Number.to_string(), "number");
assert_eq!(SchemaType::Boolean.to_string(), "boolean");
assert_eq!(SchemaType::Array.to_string(), "array");
}
#[test]
fn test_schema_type_serialization() {
let schema_type = SchemaType::String;
let json = serde_json::to_string(&schema_type).expect("Failed to serialize");
assert_eq!(json, r#""string""#);
let deserialized: SchemaType = serde_json::from_str(&json).expect("Failed to deserialize");
assert!(matches!(deserialized, SchemaType::String));
}
#[test]
fn test_schema_location_serialization() {
let location = SchemaLocation::Path;
let json = serde_json::to_string(&location).expect("Failed to serialize");
assert_eq!(json, r#""path""#);
let deserialized: SchemaLocation = serde_json::from_str(&json).expect("Failed to deserialize");
assert!(matches!(deserialized, SchemaLocation::Path));
}
#[test]
fn test_schema_with_properties() {
let json = r#"{
"type": "object",
"properties": {
"id": {
"type": "integer",
"minimum": 1
},
"name": {
"type": "string",
"minLength": 1
}
},
"required": ["id"]
}"#;
let schema: Schema = serde_json::from_str(json).expect("Failed to deserialize");
assert_eq!(schema.kind, Some(SchemaType::Object));
assert!(schema.properties.is_some());
assert!(schema.required.is_some());
let props = schema.properties.unwrap();
assert!(props.contains_key("id"));
assert!(props.contains_key("name"));
let id_prop = &props["id"];
assert_eq!(id_prop.kind, Some(SchemaType::Integer));
assert_eq!(id_prop.minimum, Some(1));
}
#[test]
fn test_schema_with_array_items() {
let json = r#"{
"type": "array",
"items": {
"type": "string"
},
"minItems": 1,
"maxItems": 10,
"uniqueItems": true
}"#;
let schema: Schema = serde_json::from_str(json).expect("Failed to deserialize");
assert_eq!(schema.kind, Some(SchemaType::Array));
assert!(schema.items.is_some());
assert_eq!(schema.min_items, Some(1));
assert_eq!(schema.max_items, Some(10));
assert_eq!(schema.unique_items, Some(true));
let items = schema.items.unwrap();
assert_eq!(items.kind, Some(SchemaType::String));
}
#[test]
fn test_schema_with_reference() {
let json = r##"{
"$ref": "#/components/schemas/User"
}"##;
let schema: Schema = serde_json::from_str(json).expect("Failed to deserialize");
assert_eq!(schema.reference, Some("#/components/schemas/User".to_owned()));
}
#[test]
fn test_schema_with_constraints() {
let json = r#"{
"type": "string",
"minLength": 5,
"maxLength": 100,
"format": "email"
}"#;
let schema: Schema = serde_json::from_str(json).expect("Failed to deserialize");
assert_eq!(schema.kind, Some(SchemaType::String));
assert_eq!(schema.min_length, Some(5));
assert_eq!(schema.max_length, Some(100));
assert_eq!(schema.format, Some("email".to_owned()));
}
#[test]
fn test_discriminator() {
let json = r##"{
"propertyName": "petType",
"mapping": {
"dog": "#/components/schemas/Dog",
"cat": "#/components/schemas/Cat"
}
}"##;
let discriminator: Discriminator = serde_json::from_str(json).expect("Failed to deserialize");
assert_eq!(discriminator.property_name, "petType");
assert!(discriminator.mapping.is_some());
let mapping = discriminator.mapping.unwrap();
assert_eq!(mapping.len(), 2);
}
#[test]
fn test_xml_metadata() {
let json = r#"{
"name": "animals",
"namespace": "http://example.com/schema",
"prefix": "ex",
"attribute": false,
"wrapped": true
}"#;
let xml: XML = serde_json::from_str(json).expect("Failed to deserialize");
assert_eq!(xml.name, Some("animals".to_owned()));
assert_eq!(xml.namespace, Some("http://example.com/schema".to_owned()));
assert_eq!(xml.prefix, Some("ex".to_owned()));
assert_eq!(xml.attribute, Some(false));
assert_eq!(xml.wrapped, Some(true));
}
#[test]
fn test_tag() {
let json = r#"{
"name": "users",
"description": "User management endpoints"
}"#;
let tag: Tag = serde_json::from_str(json).expect("Failed to deserialize");
assert_eq!(tag.name, "users");
assert_eq!(tag.description, Some("User management endpoints".to_owned()));
}
#[test]
fn test_external_docs() {
let json = r#"{
"url": "https://docs.example.com",
"description": "Find more info here"
}"#;
let docs: ExternalDocs = serde_json::from_str(json).expect("Failed to deserialize");
assert_eq!(docs.url, "https://docs.example.com");
assert_eq!(docs.description, Some("Find more info here".to_owned()));
}
#[test]
fn test_reference() {
let json = r##"{
"$ref": "#/components/schemas/Pet",
"summary": "A pet reference"
}"##;
let reference: Reference = serde_json::from_str(json).expect("Failed to deserialize");
assert_eq!(reference.reference, Some("#/components/schemas/Pet".to_owned()));
assert_eq!(reference.summary, Some("A pet reference".to_owned()));
}
#[test]
fn test_parameter_path() {
let json = r#"{
"name": "userId",
"in": "path",
"required": true,
"description": "User ID",
"schema": {
"type": "string"
}
}"#;
let param: Parameter = serde_json::from_str(json).expect("Failed to deserialize");
assert_eq!(param.name, Some("userId".to_owned()));
assert_eq!(param.location, Some("path".to_owned()));
assert_eq!(param.required, Some(true));
assert!(param.schema.is_some());
}
#[test]
fn test_parameter_query() {
let json = r#"{
"name": "limit",
"in": "query",
"description": "Items per page",
"schema": {
"type": "integer",
"minimum": 1,
"maximum": 100
}
}"#;
let param: Parameter = serde_json::from_str(json).expect("Failed to deserialize");
assert_eq!(param.name, Some("limit".to_owned()));
assert_eq!(param.location, Some("query".to_owned()));
}
#[test]
fn test_example() {
let json = r#"{
"summary": "A user example",
"value": {
"id": 42,
"name": "John Doe"
}
}"#;
let example: Example = serde_json::from_str(json).expect("Failed to deserialize");
assert_eq!(example.summary, Some("A user example".to_owned()));
assert!(example.value.is_some());
}
#[test]
fn test_media_type() {
let json = r#"{
"schema": {
"type": "object",
"properties": {
"name": {
"type": "string"
}
}
}
}"#;
let media: MediaType = serde_json::from_str(json).expect("Failed to deserialize");
assert!(media.schema.is_some());
}
#[test]
fn test_response() {
let json = r#"{
"description": "Successful response",
"content": {
"application/json": {
"schema": {
"type": "object"
}
}
}
}"#;
let response: Response = serde_json::from_str(json).expect("Failed to deserialize");
assert_eq!(response.description, Some("Successful response".to_owned()));
assert!(response.content.is_some());
}
#[test]
fn test_request_body() {
let json = r#"{
"required": true,
"content": {
"application/json": {
"schema": {
"type": "object"
}
}
}
}"#;
let req_body: RequestBody = serde_json::from_str(json).expect("Failed to deserialize");
assert_eq!(req_body.required, Some(true));
assert!(req_body.content.contains_key("application/json"));
}
#[test]
fn test_header() {
let json = r#"{
"description": "The number of items returned",
"schema": {
"type": "integer"
}
}"#;
let header: Header = serde_json::from_str(json).expect("Failed to deserialize");
assert_eq!(header.description, Some("The number of items returned".to_owned()));
assert!(header.schema.is_some());
}
#[test]
fn test_security_scheme_bearer() {
let json = r#"{
"type": "http",
"scheme": "bearer",
"bearerFormat": "JWT"
}"#;
let scheme: SecurityScheme = serde_json::from_str(json).expect("Failed to deserialize");
assert_eq!(scheme.kind, "http");
assert_eq!(scheme.scheme, "bearer");
assert_eq!(scheme.bearer_format, Some("JWT".to_owned()));
}
#[test]
fn test_oauth_flows() {
let json = r#"{
"implicit": {
"authorizationUrl": "https://example.com/oauth/authorize",
"tokenUrl": "https://example.com/oauth/token",
"scopes": {
"read": "Read access",
"write": "Write access"
}
}
}"#;
let flows: OAuthFlows = serde_json::from_str(json).expect("Failed to deserialize");
assert!(flows.implicit.is_some());
let implicit = flows.implicit.unwrap();
assert_eq!(implicit.scopes.len(), 2);
}
#[test]
fn test_link() {
let json = r#"{
"operationId": "getUserAddress",
"parameters": {
"userId": "$response.body#/id"
}
}"#;
let link: Link = serde_json::from_str(json).expect("Failed to deserialize");
assert_eq!(link.operation_id, Some("getUserAddress".to_owned()));
assert!(link.parameters.is_some());
}
#[test]
fn test_encoding() {
let json = r#"{
"contentType": "application/json",
"explode": true
}"#;
let encoding: Encoding = serde_json::from_str(json).expect("Failed to deserialize");
assert_eq!(encoding.content_type, Some("application/json".to_owned()));
assert_eq!(encoding.explode, Some(true));
}