openapi-contract-macros 0.1.1

Proc-macro implementation for the openapi-contract crate. Not intended to be used directly — depend on `openapi-contract` instead.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
use proc_macro2::TokenStream;
use quote::{format_ident, quote};
use syn::parse::{Parse, ParseStream};
use syn::{Expr, Ident, LitStr, Token};

use std::path::PathBuf;

use crate::spec::{self, SpecIndex};
use crate::types;
fn resolve_spec_path(relative: &str) -> PathBuf {
    let p = PathBuf::from(relative);
    if p.is_absolute() && p.exists() {
        return p;
    }

    if let Ok(env_path) = std::env::var("OPENAPI_SPEC_PATH") {
        let ep = PathBuf::from(&env_path);
        if ep.is_absolute() {
            return ep;
        }
        let manifest = std::env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| ".".into());
        return PathBuf::from(manifest).join(ep);
    }

    let manifest = std::env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| ".".into());
    let candidate = PathBuf::from(&manifest).join(relative);
    if candidate.exists() {
        return candidate;
    }

    let mut dir = PathBuf::from(&manifest);
    while let Some(parent) = dir.parent() {
        let candidate = parent.join(relative);
        if candidate.exists() {
            return candidate;
        }
        dir = parent.to_path_buf();
    }

    PathBuf::from(manifest).join(relative)
}

// ─── generate_types! ────────────────────────────────────────────

struct GenerateTypesInput {
    spec_path: LitStr,
}

impl Parse for GenerateTypesInput {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let spec_path: LitStr = input.parse()?;
        Ok(Self { spec_path })
    }
}
pub fn generate_types_impl(input: TokenStream) -> syn::Result<TokenStream> {
    let parsed = syn::parse2::<GenerateTypesInput>(input)?;
    let spec_path_str = parsed.spec_path.value();

    let full_path = resolve_spec_path(&spec_path_str);

    let spec = spec::load_spec(&full_path, &spec_path_str).map_err(|e| {
        syn::Error::new_spanned(&parsed.spec_path, format!("failed to load spec: {e}"))
    })?;

    let type_tokens = types::generate_all_types(&spec.schemas);
    Ok(type_tokens)
}

// ─── api! ───────────────────────────────────────────────────────

struct ApiInput {
    method: Ident,
    path: LitStr,
    params: Vec<(Ident, Expr)>,
    query_params: Vec<(Ident, Expr)>,
    body: Option<Expr>,
}

impl Parse for ApiInput {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let method: Ident = input.parse()?;
        let path: LitStr = input.parse()?;

        let mut params = Vec::new();
        let mut query_params = Vec::new();
        let mut body = None;

        while input.peek(Token![,]) {
            let _: Token![,] = input.parse()?;

            if input.is_empty() {
                break;
            }

            if input.peek(Ident) {
                let key: Ident = input.parse()?;
                let key_str = key.to_string();

                if key_str == "query" {
                    let _: Token![=] = input.parse()?;
                    let content;
                    syn::braced!(content in input);
                    while !content.is_empty() {
                        let qk: Ident = content.parse()?;
                        let _: Token![:] = content.parse()?;
                        let qv: Expr = content.parse()?;
                        query_params.push((qk, qv));
                        if content.peek(Token![,]) {
                            let _: Token![,] = content.parse()?;
                        }
                    }
                } else if key_str == "body" {
                    let _: Token![=] = input.parse()?;
                    let expr: Expr = input.parse()?;
                    body = Some(expr);
                } else {
                    let _: Token![=] = input.parse()?;
                    let expr: Expr = input.parse()?;
                    params.push((key, expr));
                }
            }
        }

        Ok(Self {
            method,
            path,
            params,
            query_params,
            body,
        })
    }
}
pub fn api_impl(input: TokenStream) -> syn::Result<TokenStream> {
    let parsed = syn::parse2::<ApiInput>(input)?;

    let method_str = parsed.method.to_string().to_uppercase();
    let path_str = parsed.path.value();

    let spec_path = resolve_spec_path("openapi-spec.json");
    let spec = spec::load_spec(&spec_path, "openapi-spec.json")
        .map_err(|e| syn::Error::new_spanned(&parsed.path, format!("failed to load spec: {e}")))?;

    validate_endpoint(&parsed, &method_str, &path_str, &spec)?;

    let endpoint = &spec.endpoints[&(method_str.clone(), path_str.clone())];

    let method_ident = format_ident!("{}", method_str);

    let path_format = build_path_format(&path_str, &parsed.params);

    let response_type = if endpoint.is_sse {
        quote! { () }
    } else {
        match &endpoint.response_schema {
            Some(schema) => types::schema_to_rust_type(schema, &spec.schemas),
            None => quote! { () },
        }
    };

    let query_code = if parsed.query_params.is_empty() {
        quote! {}
    } else {
        let pairs: Vec<TokenStream> = parsed
            .query_params
            .iter()
            .map(|(k, v)| {
                let ks = k.to_string();
                quote! { (#ks, &#v as &dyn ToString) }
            })
            .collect();
        quote! { .query_raw(::openapi_contract::build_query_string(&[#(#pairs),*])) }
    };

    let body_code = match &parsed.body {
        Some(expr) => quote! { .body_json(#expr) },
        None => quote! {},
    };

    Ok(quote! {
        {
            let __path = #path_format;
            ::openapi_contract::ApiRequest::<#response_type>::new(
                ::openapi_contract::Method::#method_ident,
                __path,
            )
            #query_code
            #body_code
        }
    })
}
fn validate_endpoint(
    parsed: &ApiInput,
    method: &str,
    path: &str,
    spec: &SpecIndex,
) -> syn::Result<()> {
    let key = (method.to_string(), path.to_string());

    if !spec.endpoints.contains_key(&key) {
        let has_path = spec.endpoints.keys().any(|(_, p)| p == path);
        if has_path {
            let hint = spec::available_methods_hint(spec, path);
            return Err(syn::Error::new_spanned(
                &parsed.method,
                format!("{method} is not defined for \"{path}\". Available methods: {hint}"),
            ));
        }

        let prefix_end = path.char_indices().nth(8).map_or(path.len(), |(i, _)| i);
        let prefix = &path[..prefix_end];
        let hint = spec::available_paths_hint(spec, prefix);
        return Err(syn::Error::new_spanned(
            &parsed.path,
            format!("unknown API path \"{path}\". Similar paths: {hint}"),
        ));
    }

    let endpoint = &spec.endpoints[&key];

    let provided_params: Vec<String> = parsed.params.iter().map(|(k, _)| k.to_string()).collect();

    for expected in &endpoint.path_params {
        if !provided_params.contains(expected) {
            return Err(syn::Error::new_spanned(
                &parsed.path,
                format!(
                    "missing path parameter `{expected}` for {method} \"{path}\". Required: {}",
                    endpoint.path_params.join(", ")
                ),
            ));
        }
    }

    for (k, _) in &parsed.params {
        let ks = k.to_string();
        if !endpoint.path_params.contains(&ks) {
            return Err(syn::Error::new_spanned(
                k,
                format!(
                    "unexpected parameter `{ks}` for {method} \"{path}\". Expected: {}",
                    endpoint.path_params.join(", ")
                ),
            ));
        }
    }

    Ok(())
}
fn build_path_format(path: &str, params: &[(Ident, Expr)]) -> TokenStream {
    if params.is_empty() {
        return quote! { #path.to_string() };
    }

    let param_map: std::collections::HashMap<String, &Expr> = params
        .iter()
        .map(|(name, expr)| (name.to_string(), expr))
        .collect();

    let mut fmt_str = String::new();
    let mut args = Vec::new();
    let mut chars = path.chars().peekable();

    while let Some(c) = chars.next() {
        if c == '{' {
            let mut name = String::new();
            for c2 in chars.by_ref() {
                if c2 == '}' {
                    break;
                }
                name.push(c2);
            }
            fmt_str.push_str("{}");
            if let Some(expr) = param_map.get(&name) {
                args.push(quote! { #expr });
            }
        } else {
            fmt_str.push(c);
        }
    }

    quote! { format!(#fmt_str, #(#args),*) }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::{Mutex, OnceLock};

    fn env_lock() -> std::sync::MutexGuard<'static, ()> {
        static LOCK: OnceLock<Mutex<()>> = OnceLock::new();
        LOCK.get_or_init(|| Mutex::new(())).lock().unwrap()
    }

    fn write_test_spec(
        spec: &serde_json::Value,
    ) -> (std::path::PathBuf, std::sync::MutexGuard<'static, ()>) {
        let guard = env_lock();
        let tmp = std::env::temp_dir().join("openapi-contract-codegen-test-spec.json");
        std::fs::write(&tmp, spec.to_string()).unwrap();
        unsafe { std::env::set_var("OPENAPI_SPEC_PATH", tmp.to_str().unwrap()) };
        (tmp, guard)
    }

    fn cleanup_test_spec(tmp: &std::path::Path) {
        unsafe { std::env::remove_var("OPENAPI_SPEC_PATH") };
        let _ = std::fs::remove_file(tmp);
    }

    fn full_spec() -> serde_json::Value {
        serde_json::json!({
            "openapi": "3.0.3",
            "info": {"title": "T", "version": "1"},
            "paths": {
                "/items": {
                    "get": {
                        "responses": { "200": { "description": "ok", "content": {
                            "application/json": { "schema": {"type": "array", "items": {"type": "string"}} }
                        }}}
                    },
                    "post": {
                        "responses": { "200": { "description": "ok", "content": {
                            "application/json": { "schema": {"type": "string"} }
                        }}}
                    }
                },
                "/items/{id}": {
                    "get": {
                        "parameters": [{"name": "id", "in": "path", "required": true, "schema": {"type": "string"}}],
                        "responses": { "200": { "description": "ok", "content": {
                            "application/json": { "schema": {"type": "string"} }
                        }}}
                    }
                },
                "/stream": {
                    "get": {
                        "responses": { "200": { "description": "ok", "content": {
                            "text/event-stream": { "schema": {"type": "string"} }
                        }}}
                    }
                }
            }
        })
    }

    // ── Parsing ─────────────────────────────────────────────────

    #[test]
    fn parse_api_input_variants() {
        // Basic
        let p: ApiInput = syn::parse2(quote! { GET "/api/test" }).unwrap();
        assert_eq!(p.method.to_string(), "GET");
        assert_eq!(p.path.value(), "/api/test");
        assert!(p.params.is_empty() && p.body.is_none());

        // With param
        let p: ApiInput = syn::parse2(quote! { GET "/api/teams/{id}", id = &team_id }).unwrap();
        assert_eq!(p.params.len(), 1);
        assert_eq!(p.params[0].0.to_string(), "id");

        // With body
        let p: ApiInput = syn::parse2(quote! { POST "/api/teams", body = &data }).unwrap();
        assert!(p.body.is_some());

        // With query
        let p: ApiInput =
            syn::parse2(quote! { GET "/api/users", query = { limit: 10, offset: 0 } }).unwrap();
        assert_eq!(p.query_params.len(), 2);

        // Mixed: param + query + body
        let p: ApiInput = syn::parse2(quote! { POST "/api/teams/{id}/invite", id = &tid, query = { draft: true }, body = &invite }).unwrap();
        assert_eq!(p.params.len(), 1);
        assert_eq!(p.query_params.len(), 1);
        assert!(p.body.is_some());

        // Trailing comma
        let p: ApiInput = syn::parse2(quote! { GET "/api/test", }).unwrap();
        assert_eq!(p.method.to_string(), "GET");

        // Non-ident after comma fails
        assert!(syn::parse2::<ApiInput>(quote! { GET "/api/test", 123 }).is_err());

        // GenerateTypesInput
        let g: GenerateTypesInput = syn::parse2(quote! { "spec.json" }).unwrap();
        assert_eq!(g.spec_path.value(), "spec.json");
    }

    // ── build_path_format ───────────────────────────────────────

    #[test]
    fn build_path_format_variants() {
        // No params
        assert!(
            build_path_format("/api/test", &[])
                .to_string()
                .contains("to_string")
        );

        // Single param
        let ts = build_path_format(
            "/api/teams/{id}",
            &[(format_ident!("id"), syn::parse_quote! { &team_id })],
        );
        assert!(ts.to_string().contains("format"));

        // Multiple params
        let ts = build_path_format(
            "/orgs/{org}/teams/{team}",
            &[
                (format_ident!("org"), syn::parse_quote! { &org_id }),
                (format_ident!("team"), syn::parse_quote! { &team_id }),
            ],
        );
        assert!(ts.to_string().contains("format"));
    }

    // ── resolve_spec_path ───────────────────────────────────────

    #[test]
    fn resolve_spec_path_all_strategies() {
        // Absolute existing file
        let tmp = std::env::temp_dir().join("openapi-contract-abs-spec.json");
        std::fs::write(&tmp, "{}").unwrap();
        assert_eq!(resolve_spec_path(tmp.to_str().unwrap()), tmp);
        let _ = std::fs::remove_file(&tmp);

        // OPENAPI_SPEC_PATH absolute
        let _guard = env_lock();
        unsafe { std::env::set_var("OPENAPI_SPEC_PATH", "/absolute/path/spec.json") };
        assert!(
            resolve_spec_path("ignored.json")
                .to_string_lossy()
                .contains("spec.json")
        );

        // OPENAPI_SPEC_PATH relative
        unsafe { std::env::set_var("OPENAPI_SPEC_PATH", "relative/spec.json") };
        assert!(
            resolve_spec_path("ignored.json")
                .to_string_lossy()
                .contains("spec.json")
        );
        unsafe { std::env::remove_var("OPENAPI_SPEC_PATH") };

        // Candidate exists in CARGO_MANIFEST_DIR
        let dir = std::env::temp_dir().join("openapi-contract-manifest-dir-test");
        std::fs::create_dir_all(&dir).unwrap();
        let spec = dir.join("my-spec.json");
        std::fs::write(&spec, "{}").unwrap();
        unsafe { std::env::set_var("CARGO_MANIFEST_DIR", dir.to_str().unwrap()) };
        assert_eq!(resolve_spec_path("my-spec.json"), spec);
        unsafe { std::env::remove_var("CARGO_MANIFEST_DIR") };
        let _ = std::fs::remove_dir_all(&dir);

        // Parent search
        let root = std::env::temp_dir().join("openapi-contract-parent-search");
        let nested = root.join("a").join("b");
        let spec = root.join("openapi-spec.json");
        std::fs::create_dir_all(&nested).unwrap();
        std::fs::write(&spec, "{}").unwrap();
        unsafe { std::env::set_var("CARGO_MANIFEST_DIR", nested.to_str().unwrap()) };
        assert_eq!(resolve_spec_path("openapi-spec.json"), spec);
        unsafe { std::env::remove_var("CARGO_MANIFEST_DIR") };
        let _ = std::fs::remove_dir_all(&root);

        // Fallback (no match)
        unsafe { std::env::remove_var("OPENAPI_SPEC_PATH") };
        assert!(
            resolve_spec_path("test.json")
                .to_string_lossy()
                .contains("test.json")
        );
    }

    // ── generate_types_impl / api_impl ──────────────────────────

    #[test]
    fn generate_types_impl_success_and_error() {
        // Error on missing file
        let _guard = env_lock();
        unsafe { std::env::remove_var("OPENAPI_SPEC_PATH") };
        let err = generate_types_impl(quote! { "nonexistent-spec-file.json" }).unwrap_err();
        assert!(err.to_string().contains("failed to load spec"));
        drop(_guard);

        // Success
        let (tmp, _guard) = write_test_spec(&serde_json::json!({
            "openapi": "3.0.3",
            "info": {"title": "T", "version": "1"},
            "paths": {},
            "components": { "schemas": { "User": {
                "type": "object", "required": ["id"],
                "properties": { "id": {"type": "string"} }
            }}}
        }));
        let code = generate_types_impl(quote! { "ignored.json" })
            .unwrap()
            .to_string();
        assert!(code.contains("User"));
        cleanup_test_spec(&tmp);
    }

    #[test]
    fn api_impl_valid_spec_variants() {
        let (tmp, _guard) = write_test_spec(&full_spec());

        // GET with response type
        let code = api_impl(quote! { GET "/items" }).unwrap().to_string();
        assert!(code.contains("Vec"));

        // With path param
        let code = api_impl(quote! { GET "/items/{id}", id = &my_id })
            .unwrap()
            .to_string();
        assert!(code.contains("format"));

        // With query
        let code = api_impl(quote! { GET "/items", query = { limit: 10 } })
            .unwrap()
            .to_string();
        assert!(code.contains("query_raw"));

        // With body
        let code = api_impl(quote! { POST "/items", body = &data })
            .unwrap()
            .to_string();
        assert!(code.contains("body_json"));

        // SSE endpoint
        assert!(api_impl(quote! { GET "/stream" }).is_ok());

        cleanup_test_spec(&tmp);
    }

    #[test]
    fn api_impl_validation_errors() {
        let (tmp, _guard) = write_test_spec(&full_spec());

        // Wrong method
        assert!(
            api_impl(quote! { DELETE "/items" })
                .unwrap_err()
                .to_string()
                .contains("is not defined for")
        );

        // Unknown path
        assert!(
            api_impl(quote! { GET "/nonexistent" })
                .unwrap_err()
                .to_string()
                .contains("unknown API path")
        );

        // Missing path param
        assert!(
            api_impl(quote! { GET "/items/{id}" })
                .unwrap_err()
                .to_string()
                .contains("missing path parameter")
        );

        // Unexpected param
        assert!(
            api_impl(quote! { GET "/items", id = &x })
                .unwrap_err()
                .to_string()
                .contains("unexpected parameter")
        );

        cleanup_test_spec(&tmp);
    }

    #[test]
    fn api_impl_missing_spec() {
        let _guard = env_lock();
        unsafe { std::env::remove_var("OPENAPI_SPEC_PATH") };
        unsafe { std::env::set_var("CARGO_MANIFEST_DIR", "/tmp/nonexistent") };
        assert!(api_impl(quote! { GET "/api/test" }).is_err());
        unsafe { std::env::remove_var("CARGO_MANIFEST_DIR") };
    }
}