Skip to main content

openapi_nexus_core/data/
parameter_info.rs

1//! Parameter information with raw OpenAPI schema
2
3use serde::{Deserialize, Serialize};
4
5use openapi_nexus_spec::oas31::spec::{ObjectOrReference, ObjectSchema, ParameterIn};
6
7// Type alias for compatibility
8pub type RefOr<T> = ObjectOrReference<T>;
9
10/// Parameter location for conflict resolution
11///
12/// Indicates where a parameter is used in an HTTP request.
13///
14/// # Variants
15///
16/// - `Path`: URL path segment parameter (e.g., `{id}` in `/users/{id}`)
17/// - `Query`: Query string parameter (e.g., `?page=1&limit=10`)
18/// - `Header`: HTTP header parameter (e.g., `Authorization: Bearer ...`)
19/// - `Body`: Request body parameter (for POST/PUT/PATCH requests)
20#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
21pub enum ParameterLocation {
22    /// URL path segment parameter
23    Path,
24    /// Query string parameter
25    Query,
26    /// HTTP header parameter
27    Header,
28    /// Request body parameter
29    Body,
30}
31
32impl From<ParameterIn> for ParameterLocation {
33    fn from(param_in: ParameterIn) -> Self {
34        match param_in {
35            ParameterIn::Path => ParameterLocation::Path,
36            ParameterIn::Query => ParameterLocation::Query,
37            ParameterIn::Header => ParameterLocation::Header,
38            ParameterIn::Cookie => ParameterLocation::Header, // Treat cookie as header
39        }
40    }
41}
42
43/// Parameter information for code generation
44///
45/// This struct represents a parameter extracted from an OpenAPI operation
46/// after name conflict resolution. It contains both the original parameter
47/// name (used for HTTP requests) and the resolved parameter name (used in
48/// generated code).
49///
50/// # Fields
51///
52/// - `original_name`: The original parameter name from the OpenAPI spec,
53///   used when constructing HTTP requests (e.g., query strings, headers, path segments)
54/// - `param_name`: The resolved parameter name after conflict resolution,
55///   used in generated code (may have location prefixes if conflicts exist)
56/// - `schema`: The OpenAPI schema definition for the parameter (if available)
57/// - `required`: Whether the parameter is required
58/// - `deprecated`: Whether the parameter is deprecated
59/// - `description`: Parameter description from the OpenAPI spec (if available)
60/// - `default_value`: Default value for the parameter (if specified in the schema)
61/// - `location`: The parameter location (path, query, header, or body)
62#[derive(Debug, Clone, Serialize, Deserialize)]
63pub struct ParameterInfo {
64    /// Original parameter name (for HTTP usage)
65    ///
66    /// This is the parameter name as specified in the OpenAPI spec.
67    /// It is used when constructing HTTP requests (e.g., in query strings,
68    /// headers, or path segments).
69    pub original_name: String,
70    /// Resolved parameter name (for code generation)
71    ///
72    /// This is the parameter name after conflict resolution. If multiple
73    /// parameters from different locations have the same camelCase name,
74    /// they will be prefixed with their location (e.g., "pathId", "queryId").
75    /// This name is used in generated code.
76    pub param_name: String,
77    /// OpenAPI schema definition for the parameter
78    ///
79    /// Contains the type information and validation rules from the OpenAPI spec.
80    /// May be `None` if not needed for template rendering.
81    pub schema: Option<ObjectOrReference<ObjectSchema>>,
82    /// Whether the parameter is required
83    ///
84    /// If `true`, the parameter must be provided when calling the API.
85    pub required: bool,
86    /// Whether the parameter is deprecated
87    ///
88    /// If `true`, the parameter is marked as deprecated in the OpenAPI spec
89    /// and should be avoided in new code.
90    pub deprecated: bool,
91    /// Parameter description
92    ///
93    /// Human-readable description of the parameter from the OpenAPI spec.
94    /// May be `None` if no description is provided.
95    pub description: Option<String>,
96    /// Default value for the parameter
97    ///
98    /// The default value extracted from the parameter's schema as a JSON value.
99    /// Preserves the original type from the OpenAPI spec (string, number, boolean, null, array, object).
100    /// May be `None` if no default value is specified.
101    pub default_value: Option<serde_json::Value>,
102    /// Parameter location
103    ///
104    /// Indicates where the parameter is used in the HTTP request:
105    /// - `Path`: URL path segment (e.g., `/users/{id}`)
106    /// - `Query`: Query string parameter (e.g., `?page=1`)
107    /// - `Header`: HTTP header (e.g., `Authorization: Bearer ...`)
108    /// - `Body`: Request body (for POST/PUT/PATCH requests)
109    pub location: ParameterLocation,
110}