schemadoc-diff 0.1.20

OpenApi diff library and breaking changes detector
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
use serde::{Deserialize, Serialize};
use std::fmt::Debug;

use indexmap::IndexMap;
use serde_json::Value;

use crate::core::{Either, Keyed, MayBeRefCore, ReferenceDescriptor};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HttpSchemaRef {
    #[serde(rename = "$ref")]
    pub reference: String,
}

impl ReferenceDescriptor for HttpSchemaRef {
    fn reference(&self) -> &str {
        &self.reference
    }
}

pub type MayBeRef<T> = MayBeRefCore<T, HttpSchemaRef>;

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct HttpSchema {
    pub version: String,

    pub schema_version: String,
    pub schema_source: String,
    pub schema_source_version: String,

    pub info: Option<Info>,
    pub servers: Option<Vec<Server>>,
    pub paths: Option<IndexMap<String, MayBeRef<Path>>>,
    pub components: Option<Components>,
    // TODO:
    // pub security:
    pub tags: Option<Vec<Tag>>,
    pub external_docs: Option<ExternalDoc>,
}

impl HttpSchema {
    pub fn schema_version() -> &'static str {
        "0.4.1"
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Info {
    pub title: Option<String>,
    pub description: Option<String>,
    pub terms_of_service: Option<String>,

    pub contact: Option<Contact>,
    pub license: Option<License>,

    pub version: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Contact {
    pub name: Option<String>,
    pub url: Option<String>,
    pub email: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct License {
    pub name: Option<String>,
    pub url: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Server {
    pub url: Option<String>,
    pub description: Option<String>,
    pub variables: Option<IndexMap<String, ServerVariable>>,
}

impl Keyed<usize> for Server {
    fn key(&self, key: usize) -> String {
        self.url.clone().unwrap_or_else(|| key.to_string())
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ServerVariable {
    pub r#enum: Option<Vec<String>>,
    pub default: Option<Value>,
    pub description: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Components {
    pub schemas: Option<IndexMap<String, MayBeRef<Schema>>>,
    pub responses: Option<IndexMap<String, MayBeRef<Response>>>,
    pub parameters: Option<IndexMap<String, MayBeRef<Parameter>>>,
    pub examples: Option<IndexMap<String, MayBeRef<Example>>>,
    pub request_bodies: Option<IndexMap<String, MayBeRef<RequestBody>>>,
    pub headers: Option<IndexMap<String, MayBeRef<Header>>>,
    pub security_schemes: Option<IndexMap<String, MayBeRef<SecurityScheme>>>,
    pub links: Option<IndexMap<String, MayBeRef<Link>>>,
    // pub callbacks: Option<IndexMap<String, MayBeRef<T>>>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ExternalDoc {
    pub url: Option<String>,
    pub description: Option<String>,
}

impl Keyed<usize> for ExternalDoc {
    fn key(&self, _: usize) -> String {
        format!("{:?}", self.url)
    }
}

#[derive(Serialize, Deserialize, Debug, Clone, Default)]
#[serde(rename_all = "camelCase")]
pub struct Parameter {
    pub name: String,
    pub r#in: String,

    pub description: Option<String>,

    pub required: Option<bool>,
    pub deprecated: Option<bool>,
    pub allow_empty_value: Option<bool>,

    pub style: Option<String>,
    pub explode: Option<bool>,
    pub allow_reserved: Option<bool>,

    pub schema: Option<MayBeRef<Schema>>,

    pub examples: Option<IndexMap<String, MayBeRef<Value>>>,

    pub content: Option<IndexMap<String, MediaType>>,

    #[serde(flatten)]
    pub custom_fields: IndexMap<String, Value>,
}

impl Keyed<usize> for Parameter {
    fn key(&self, _: usize) -> String {
        format!("{}.{}", self.name, self.r#in)
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RequestBody {
    pub description: Option<String>,
    pub content: Option<IndexMap<String, MediaType>>,
    pub required: Option<bool>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MediaType {
    pub schema: Option<MayBeRef<Schema>>,
    pub examples: Option<IndexMap<String, MayBeRef<Example>>>,
    pub encoding: Option<IndexMap<String, Encoding>>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Encoding {
    pub content_type: Option<String>,
    pub headers: Option<IndexMap<String, MayBeRef<Header>>>,
    pub style: Option<String>,
    pub explode: Option<bool>,
    pub allow_reserved: Option<bool>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Link {
    pub operation_ref: Option<String>,
    pub operation_id: Option<String>,
    pub parameters: Option<IndexMap<String, Value>>,
    pub request_body: Option<Value>,
    pub description: Option<String>,
    pub server: Option<Server>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Response {
    pub description: Option<String>,
    pub content: Option<IndexMap<String, MediaType>>,
    pub links: Option<IndexMap<String, MayBeRef<Link>>>,
    pub headers: Option<IndexMap<String, MayBeRef<Header>>>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Example {
    pub summary: Option<String>,
    pub description: Option<String>,
    pub value: Option<Value>,
    pub external_value: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Discriminator {
    pub property_name: Option<String>,
    pub mapping: Option<IndexMap<String, String>>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Xml {
    pub name: Option<String>,
    pub namespace: Option<String>,
    pub prefix: Option<String>,
    pub attribute: Option<bool>,
    pub wrapped: Option<bool>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SecurityScheme {
    pub r#type: Option<String>,
    pub description: Option<String>,
    pub name: Option<String>,
    pub r#in: Option<String>,
    pub scheme: Option<String>,
    pub bearer_format: Option<String>,
    pub flows: Option<OAuthFlows>,
    pub open_id_connect_url: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct OAuthFlows {
    pub implicit: Option<OAuthFlow>,
    pub password: Option<OAuthFlow>,
    pub client_credentials: Option<OAuthFlow>,
    pub authorization_code: Option<OAuthFlow>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct OAuthFlow {
    pub authorization_url: Option<String>,
    pub token_url: Option<String>,
    pub refresh_url: Option<String>,
    pub scopes: Option<IndexMap<String, String>>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Tag {
    pub name: Option<String>,
    pub description: Option<String>,
    pub external_doc: Option<ExternalDoc>,
}

impl Keyed<usize> for Tag {
    fn key(&self, _: usize) -> String {
        format!("{:?}", self.name)
    }
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Schema {
    // https://json-schema.org/draft/2020-12/json-schema-core.html

    // #[serde(rename = "$schema")]
    // schema_: Option<String>,

    // #[serde(rename = "id")]
    // id_: Option<String>,
    // #[serde(rename = "$anchor")]
    // anchor_: Option<String>,
    //
    // #[serde(rename = "$dynamicAnchor")]
    // dynamic_anchor_: Option<String>,
    //
    // #[serde(rename = "$dynamicRef")]
    // dynamic_ref_: Option<String>,
    //
    // #[serde(rename = "$def")]
    // defs_: Option<IndexMap<String, Schema>>,
    //
    // #[serde(rename = "$comment")]
    // comment_: Option<IndexMap<String, Schema>>,

    // if
    // then
    // else
    // dependentSchemas

    // contains
    pub title: Option<String>,
    pub multiple_of: Option<f32>,
    pub maximum: Option<f32>,
    pub exclusive_maximum: Option<bool>,
    pub minimum: Option<f32>,
    pub exclusive_minimum: Option<bool>,
    pub max_length: Option<usize>,
    pub min_length: Option<usize>,
    pub pattern: Option<String>,
    pub max_items: Option<usize>,
    pub min_items: Option<usize>,
    pub unique_items: Option<bool>,
    pub max_properties: Option<usize>,
    pub min_properties: Option<usize>,
    pub required: Option<Vec<String>>,
    pub r#enum: Option<Vec<Value>>,

    // r#const: Option<Vec>,

    // pub contains: Box<Option<MayBeRef<Schema>>>
    // pub min_contains: Option<usize>,
    // pub max_contains: Option<bool>,

    // dependentRequired

    // https://json-schema.org/draft/2020-12/json-schema-validation.html#:~:text=to%20these%20keywords.-,8.3.,-contentEncoding
    // content_encoding
    // contentMediaType
    // contentSchema
    pub r#type: Option<Either<String, Vec<String>>>,
    pub all_of: Option<Vec<MayBeRef<Schema>>>,
    pub one_of: Option<Vec<MayBeRef<Schema>>>,
    pub any_of: Option<Vec<MayBeRef<Schema>>>,
    pub not: Option<Vec<MayBeRef<Schema>>>,

    pub items: Box<Option<MayBeRef<Schema>>>,

    // prefix_items: Option<Vec<MayBeRef<Schema>>>,

    // unevaluated_items: Box<Option<MayBeRef<Schema>>>
    pub properties: Option<IndexMap<String, MayBeRef<Schema>>>,
    pub additional_properties: Option<Either<bool, MayBeRef<Schema>>>, // TODO: Can be Bool in 3.1??
    // pattern_properties: Option<IndexMap<String, MayBeRef<Schema>>>,

    // property_names: Box<Option<MayBeRef<Schema>>>,

    // unevaluated_properties: Box<Option<MayBeRef<Schema>>>
    pub description: Option<String>,
    pub format: Option<String>,
    pub default: Option<Value>,

    pub discriminator: Option<Discriminator>,
    pub read_only: Option<bool>,
    pub write_only: Option<bool>,
    pub xml: Option<Xml>,
    pub external_docs: Option<ExternalDoc>,
    pub example: Option<Value>,

    // examples: Option<Vec<Value>>,
    pub deprecated: Option<bool>,

    #[serde(flatten)]
    pub custom_fields: IndexMap<String, Value>,
}

impl Keyed<usize> for Schema {
    fn key(&self, idx: usize) -> String {
        if let Some(kind) = &self.r#type {
            if let Some(title) = &self.title {
                return format!("{kind:?}{title:?}");
            }

            // if let Some(items) = &*self.items {
            //     return items.key(idx);
            // }

            // return kind.to_string();
        }

        idx.to_string()
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Header {
    pub description: Option<String>,

    pub required: Option<bool>,
    pub deprecated: Option<bool>,
    pub allow_empty_value: Option<bool>,

    pub style: Option<String>,
    pub explode: Option<bool>,
    pub allow_reserved: Option<bool>,

    pub schema: Option<MayBeRef<Schema>>,

    pub examples: Option<IndexMap<String, MayBeRef<Value>>>,

    pub content: Option<IndexMap<String, MediaType>>,

    #[serde(flatten)]
    pub custom_fields: IndexMap<String, Value>,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Operation {
    pub tags: Option<Vec<String>>,
    pub summary: Option<String>,
    pub description: Option<String>,

    pub external_docs: Option<ExternalDoc>,

    pub operation_id: Option<String>,

    pub responses: Option<IndexMap<String, MayBeRef<Response>>>,

    pub request_body: Option<MayBeRef<RequestBody>>,

    pub servers: Option<Vec<Server>>,
    pub parameters: Option<Vec<MayBeRef<Parameter>>>,

    pub security: Option<Vec<IndexMap<String, Vec<String>>>>,

    // TODO:
    // pub callbacks
    pub deprecated: Option<bool>,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Path {
    pub get: Option<Operation>,
    pub put: Option<Operation>,
    pub post: Option<Operation>,
    pub delete: Option<Operation>,
    pub options: Option<Operation>,
    pub head: Option<Operation>,
    pub patch: Option<Operation>,
    pub trace: Option<Operation>,

    pub servers: Option<Vec<Server>>,

    pub summary: Option<String>,
    pub description: Option<String>,
}

#[cfg(test)]
mod tests {
    use crate::schema::*;

    #[test]
    fn check_operation() {
        let op_def = r#"{
      "post": {
        "tags": ["Nodes"],
        "summary": "Export Xlsx Template",
        "description": "Generate XLSX-template for aggregated node data editing",
        "operationId": "gen_xlsx_aggr_node",
        "parameters": [
          {
            "required": true,
            "schema": { "title": "Path", "type": "string" },
            "name": "path",
            "in": "path"
          },
          {
            "required": false,
            "schema": { "title": "Update Sender", "type": "string" },
            "name": "update_sender",
            "in": "query"
          },
          {
            "required": false,
            "schema": { "title": "Force", "type": "boolean", "default": false },
            "name": "force",
            "in": "query"
          },
          {
            "required": false,
            "schema": { "title": "Compound Amount", "type": "integer" },
            "name": "compound_amount",
            "in": "query"
          },
          {
            "required": false,
            "schema": {
              "allOf": [{ "$ref": "/components/schemas/ExportFmt" }],
              "default": "xlsx"
            },
            "name": "export_format",
            "in": "query"
          }
        ],
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "/components/schemas/Body_export_xlsx_template_api_v2_nodes__path__template_generate__post"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Successful Response",
            "content": {
              "application/json": { "schema": {} },
              "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": {}
            }
          },
          "422": {
            "description": "Validation Error",
            "content": {
              "application/json": {
                "schema": { "$ref": "/components/schemas/HTTPValidationError" }
              }
            }
          }
        },
        "security": [{ "OAuth2PasswordBearer": [] }]
      }
    }"#;

        let _: Path = serde_json::from_str(op_def).unwrap();
    }

    #[test]
    fn check_schema_additional_properties() {
        let op_def = r#"{
            "title": "AdditionalProperties",
            "type": "object",
            "additionalProperties": {
              "$ref": "/components/schemas/AdditionalProperties"
            }
          }"#;

        let op: Schema = serde_json::from_str(op_def).unwrap();
        assert!(matches!(op.additional_properties, Some(Either::Right(_))));

        let op_def = r#"{
            "title": "AdditionalProperties",
            "type": "object",
            "additionalProperties": false
          }"#;

        let op: Schema = serde_json::from_str(op_def).unwrap();
        assert!(matches!(op.additional_properties, Some(Either::Left(_))));

        let sc_def = r#"
        {
        "type": "object",
        "discriminator": { "propertyName": "type" },
        "properties": {
          "type": {
            "type": "string",
            "description": "The type of context being attached to the entity.",
            "enum": ["link", "image"]
          }
        },
        "required": ["type"]
      }
        "#;
        let op: Schema = serde_json::from_str(sc_def).unwrap();
        assert!(matches!(op.discriminator, Some(_)))
    }
}