okapi-operation 0.3.0

Procedural macro for generating OpenAPI operation specification (using okapi)
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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
# `okapi-operation`

- [`okapi-operation`]#-okapi-operation-
  - [Example (using axum, but without axum_integration feature)]#example-using-axum-but-without-axum_integration-feature
  - [`openapi` macro]#openapi-macro
    - [Minimal example]#minimal-example
    - [Operation attributes]#operation-attributes
    - [External documentation]#external-documentation
    - [Request parameters]#request-parameters
      - [Header]#header
      - [Query]#query
      - [Path]#path
      - [Cookie]#cookie
      - [Reference]#reference
    - [Multiple parameters]#multiple-parameters
    - [Request body]#request-body
      - [Request body detection]#request-body-detection
    - [Responses]#responses
      - [From return type]#from-return-type
      - [Ignore return type]#ignore-return-type
      - [Manual definition]#manual-definition
        - [Single response]#single-response
        - [From type]#from-type
      - [Reference]#reference-1
      - [Multiple responses]#multiple-responses
    - [Security scheme]#security-scheme
  - [Building OpenAPI specification]#building-openapi-specification
  - [Features]#features
  - [TODO]#todo

Crate which allow to generate OpenAPI's operation definitions (using types from [`okapi`] crate) with procedural macro [`openapi`].

## Example (using axum, but without axum_integration feature)

```ignore
use axum::{
    extract::Query,
    http::Method,
    routing::{get, post},
    Json, Router,
};
use okapi_operation::*;
use serde::Deserialize;

#[derive(Deserialize, JsonSchema)]
struct Request {
    /// Echo data
    data: String,
}

#[openapi(
    summary = "Echo using GET request",
    operation_id = "echo_get",
    tags = "echo",
    parameters(
        query(name = "echo-data", required = true, schema = "std::string::String",),
        header(name = "x-request-id", schema = "std::string::String",)
    )
)]
async fn echo_get(query: Query<Request>) -> Json<String> {
    Json(query.0.data)
}

#[openapi(
    summary = "Echo using POST request",
    operation_id = "echo_post",
    tags = "echo"
)]
async fn echo_post(
    #[body(description = "Echo data", required = true)] body: Json<Request>,
) -> Json<String> {
    Json(body.0.data)
}

async fn openapi_spec() -> Json<OpenApi> {
    let generate_spec = || {
        OpenApiBuilder::new("Echo API", "1.0.0")
            .try_operation("/echo/get", Method::GET, echo_get__openapi)?
            .try_operation("/echo/post", Method::POST, echo_post__openapi)?
            .build()
    };
    generate_spec().map(Json).expect("Should not fail")
}

#[tokio::main]
async fn main() {
    let app = Router::new()
        .route("/echo/get", get(echo_get))
        .route("/echo/post", post(echo_post))
        .route("/openapi", get(openapi_spec));

    let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
    axum::serve(listener, app.into_make_service()).await.unwrap()
}
```

## [`openapi`] macro

This macro generate function with name `<original_name>__openapi` of type `fn(&mut Components) -> Result<Operation, anyhow::Error>` ([`OperationGenerator`]), which generate [`okapi::openapi3::Operation`], storing type definitions in provided [`Components`].

If any attribute is missing, it is set to None/false.

Since most attributes taken from OpenAPI specification directly, refer to [OpenAPI website](https://swagger.io/docs/specification/about/) for additional information.

### Minimal example

Macro doesn't have any mandatory attributes.

```compile
# use okapi_operation::*;
#[openapi]
async fn handler() {}
```

### Operation attributes

All attributes is translated into same fields of [`okapi::openapi3::Operation`].

Tags is provided as single string, which later is separated by comma.

```no_run
# use okapi_operation::*;
#[openapi(
    summary = "Simple handler",
    description = "Simple handler, demonstrating how to use operation attributes",
    operation_id = "simple",
    tags = "examples,handlers",
    deprecated = false
)]
async fn handler() {}
```

### External documentation

External documentation can be set for operation. It is translated to [`okapi::openapi3::ExternalDocs`].

```no_run
# use okapi_operation::*;
#[openapi(
    external_docs(
        url = "https://example.com",
        description = "Example Domain"
    )
)]
async fn handler() {}
```

### Request parameters

Request parameters can be:

- HTTP header (`location: header`);
- query parameter (`?param=value`) (`location: query`);
- part of the path (`/api/user/:id`, where `:id` is parameter) (`location: path`);
- reference to one of the above.

Parameters is defined in `[openapi]` macro. Inferring header from fucntion signature is not supported currently.

This definition translated to [`okapi::openapi3::Parameter`] with [`okapi::openapi3::ParameterValue::Schema`].

#### Header

`header` have following attributes:

- name (string, mandatory);
- description (string, optional);
- required (bool, optional);
- deprecated (bool, optional);
- style (string, optional) - how parameter is serialized (see [OpenAPI docs](https://swagger.io/docs/specification/serialization/));
- schema (path, mandatory) - path to type of parameter.

```no_run
# use okapi_operation::*;
#[openapi(
    parameters(
        header(
            name = "x-custom-header",
            description = "Custom header description",
            required = true,
            deprecated = false,
            style = "simple",
            schema = "std::string::String",
        )
    )
)]
async fn handler() {}
```

#### Query

`query` have following attributes:

- name (string, mandatory);
- description (string, optional);
- required (bool, optional);
- deprecated (bool, optional);
- style (string, optional) - how parameter is serialized (https://swagger.io/docs/specification/serialization/);
- explode (bool, optional) - specifies whether arrays and objects should generate separate parameters for each array item or object property;
- allow_empty_value (bool, optional) - allow empty value for this parameter;
- allow_reserved (bool, optional) - allow reserved characters `:/?#[]@!$&'()*+,;=` in parameter;
- schema (path, mandatory) - path to type of parameter.

```no_run
# use okapi_operation::*;
#[openapi(
    parameters(
        query(
            name = "page",
            description = "Which page to return",
            required = true,
            deprecated = false,
            style = "simple",
            explode = true,
            allow_empty_value = false,
            allow_reserved = false,
            schema = "std::string::String",
        )
    )
)]
async fn handler() {}
```

#### Path

`path` have following attributes:

- name (string, mandatory);
- description (string, optional);
- deprecated (bool, optional);
- style (string, optional) - how parameter is serialized (https://swagger.io/docs/specification/serialization/);
- schema (path, mandatory) - path to type of parameter.

Unlike header and query parameters, all path parameters is mandatory.

```no_run
# use okapi_operation::*;
#[openapi(
    parameters(
        path(
            name = "user_id",
            description = "ID of user",
            deprecated = false,
            style = "simple",
            schema = "std::string::String",
        )
    )
)]
async fn handler() {}
```

#### Cookie

`cookie` have following attributes:

- name (string, mandatory);
- description (string, optional);
- required (bool, optional);
- deprecated (bool, optional);
- explode (bool, optional) - specifies whether arrays and objects should generate separate parameters for each array item or object property;
- allow_empty_value (bool, optional) - allow empty value for this parameter;
- schema (path, mandatory) - path to type of parameter.

```no_run
# use okapi_operation::*;
#[openapi(
    parameters(
        cookie(
            name = "session_id",
            description = "Session ID",
            required = false,
            deprecated = false,
            explode = true,
            allow_empty_value = false,
            schema = "std::string::String",
        )
    )
)]
async fn handler() {}
```

#### Reference

```no_run
# use okapi_operation::*;
#[openapi(
    parameters(
        reference = "#/components/parameters/ReusableHeader"
    )
)]
async fn handler() {}
```

### Multiple parameters

Specifying multiple parameters is supported:

```no_run
# use okapi_operation::*;
#[openapi(
    parameters(
        header(
            name = "x-request-id",
            description = "ID of request for logging",
            required = true,
            deprecated = false,
            style = "simple",
            schema = "std::string::String",
        ),
        header(
            name = "traceparent",
            description = "ID of parent span",
            required = true,
            deprecated = false,
            style = "simple",
            schema = "std::string::String",
        ),
        path(
            name = "user_id",
            description = "ID of user",
            deprecated = false,
            style = "simple",
            schema = "std::string::String",
        ),
        reference = "#/components/parameters/ReusableHeader"
    ),
)]
async fn handler() {}
```

### Request body

Request body is associated with one of function arguments and _by default_ it's schema is inferred from argument type.

Request body definition have following attributes:

- description (string, optional);
- required (bool, optional);
- content (path, optional) - path to type, which schema should be used. If not speified, argument's type is used.

```no_run
# use okapi_operation::*;
# use okapi::schemars::*;
# struct Json<T>(T);
# impl_to_media_types_for_wrapper!(Json<T>, "application/json");
#[derive(JsonSchema)]
struct Request {
    user_id: String
}


#[openapi]
async fn handler(
    #[body(
        description = "JSON with user ID",
        required = true,
    )] body: Json<Request>
) {}

#[openapi]
async fn handler_with_request_body_override(
    #[body(
        description = "JSON with user ID",
        required = true,
        content = "Json<std::string::String>",
    )] body: Json<Request>
) {}
```

#### Request body detection

Request body can be automatically detected from well known types of supported frameworks. Refer to specific framework integration module for details.

TODO: allow disabling this behaviour

### Responses

Responses can be:

- inferred from return type;
- specified in [`openapi`] macro.

#### From return type

Return type should implement [`ToResponses`] trait.

```no_run
# use okapi_operation::*;
# use okapi::schemars::*;
# struct Json<T>(T);
# impl_to_media_types_for_wrapper!(Json<T>, "application/json");
# impl_to_responses_for_wrapper!(Json<T>);
#[derive(JsonSchema)]
struct Response {
    data: String
}

#[openapi]
async fn handler() -> Json<Response> {
# todo!()
}
```

#### Ignore return type

If return type doesn't implement [`ToResponses`], it can be ignored with special attribute `ignore_return_type`:

```no_run
# use okapi_operation::*;
#[openapi(
    responses(
        ignore_return_type = true,
    )
)]
async fn handler() -> String {
# todo!()
}
```

#### Manual definition

Manual definition is helpful when you type for some reason doesn't implement [`ToResponses`] or
if you need to specify some responses, which can occur outside handler (in middleware, for example).

##### Single response

Single response define response for a single HTTP status (or pattern). Schema of this response should implement [`ToMediaTypes`].

Single response have following attributes:

- status (string, mandatory) - HTTP status (or pattern like 2XX, 3XX). To define defautl fallback type, use special `default` value;
- description (string, optional);
- content (path, mandatory) - path to type, which provide schemas for this response;
- headers (list, optional) - list of headers (definition is the same as in request parameters). References to header is also allowed.

```no_run
# use okapi_operation::*;
# use okapi::schemars::*;
# struct Json<T>(T);
# impl_to_media_types_for_wrapper!(Json<T>, "application/json");
# impl_to_responses_for_wrapper!(Json<T>);
#[derive(JsonSchema)]
struct Response {
    data: String
}

#[openapi(
    responses(
        response(
            status = "200",
            description = "Success",
            content = "Json<Response>",
            headers(
                header(
                    name = "x-custom-message", 
                    description = "Description",
                    required = true,
                    deprecated = false,
                    style = "simple",
                    schema = "std::string::String",
                ),
                reference = "#/components/headers/ReusableHeader"
            ),
        ),
    )
)]
async fn handler() {
# todo!()
}
```

##### From type

Responses can be generated from type, which implement [`ToResponses`]:

```no_run
# use okapi_operation::*;
# use okapi::schemars::*;
# struct Json<T>(T);
# impl_to_media_types_for_wrapper!(Json<T>, "application/json");
# impl_to_responses_for_wrapper!(Json<T>);
#[derive(JsonSchema)]
struct Response {
    data: String
}

#[openapi(
    responses(
        from_type = "Json<String>",
    )
)]
async fn handler() {
# todo!()
}
```

`Json<String>` generates single 200 response with JSON with single string.

#### Reference

Reference to response have following attributes:

- status (string, mandatory) - HTTP status (or pattern like 2XX, 3XX). To define defautl fallback type, use special `default` value;
- reference (string, mandatory).

```no_run
# use okapi_operation::*;
#[openapi(
    responses(
        reference(
            status = "200",
            reference = "#/components/responses/Reference"
        )
    )
)]
async fn handler() {
# todo!()
}
```

#### Multiple responses

If mutliple manual responses is specified (or specified both return type and manual responses),
they are all merged using [`okapi::merge::merge_responses`]. If multiple responses specified for same HTTP status,
first occurence is used. Responses merged in following order:

- from return type;
- manual single responses;
- references;
- from types.

```no_run
# use okapi_operation::*;
# use okapi::schemars::*;
# struct Json<T>(T);
# impl_to_media_types_for_wrapper!(Json<T>, "application/json");
# impl_to_responses_for_wrapper!(Json<T>);
#[derive(JsonSchema)]
struct Response {
    data: String
}

#[openapi(
    responses(
        response(
            status = "500",
            description = "Internal server error",
            content = "Json<String>",
        ),
        reference(
            status = "401",
            reference = "#/components/responses/AuthError"
        ),
        reference(
            status = "403",
            reference = "#/components/responses/AuthError"
        )
    )
)]
async fn handler() -> Json<Response> {
# todo!()
}
```

### Security scheme

Security scheme have following attributes:

- name (string, mandatory) - name of used security scheme;
- scopes (string, optional) - comma separated list of scopes. Have meaning only for `OAuth2` and `OpenID Connect`.

If multiple schemes specified, they are combined as OR. AND is not currently supported.

```no_run
# use okapi_operation::*;
#[openapi(
    security(
        security_scheme(
            name = "BasicAuth",
        ),
        security_scheme(
            name = "OAuth2",
            scopes = "scope1,scope2",
        ),
    ),
)]
async fn handler() {}
```

## Building OpenAPI specification

For convenience this crate provide builder-like [`OpenApiBuilder`] type for creating OpenAPI specification:

```rust
# use okapi_operation::*;
# use okapi::schemars::*;
# use http::Method;
# struct Json<T>(T);
# impl_to_media_types_for_wrapper!(Json<T>, "application/json");
# impl_to_responses_for_wrapper!(Json<T>);
#[derive(JsonSchema)]
struct Request {
    user_id: String
}


#[openapi]
async fn handler1(
    #[body(
        description = "JSON with user ID",
        required = true,
    )] body: Json<Request>
) {
# todo!()
}

#[openapi]
async fn handler2() -> Json<String> {
# todo!()
}

fn generate_openapi_specification() -> Result<OpenApi, anyhow::Error> {
    OpenApiBuilder::new("Demo", "1.0.0")
        .operation("/handle/1", Method::POST, handler1__openapi)
        .operation("/handle/2", Method::GET, handler2__openapi)
        .build()
}

assert!(generate_openapi_specification().is_ok());
```

## Features

- `macro`: enables re-import of [`openapi`] macro (enabled by default);
- `axum`: enables integration with [`axum`]https://github.com/tokio-rs/axum crate (implement traits for certain `axum` types). See [`crate::axum_integration`] for details.

## TODO

- [ ] support examples on MediaType or Parameter (examples supported on types via `JsonSchema` macro)
- [ ] support inferring schemas of parameters from function definitions
- [ ] support for renaming or changing paths to okapi/schemars/okapi-operations in macro
- [ ] more examples
- [ ] ...