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