odcs 0.7.0

Reference implementation of the Open Data Contract Standard (ODCS)
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
//! Duplicate key detection for JSON and YAML documents.

use std::cell::RefCell;
use std::collections::HashSet;
use std::fmt;
use std::mem::MaybeUninit;
use std::rc::Rc;
use std::slice;

use serde::de::{self, DeserializeSeed, Deserializer, MapAccess, SeqAccess, Visitor};
#[allow(clippy::unsafe_removed_from_name)]
use unsafe_libyaml as sys;

/// A duplicate mapping key and its location in the document.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DuplicateKeyFinding {
    /// The duplicated key name.
    pub key: String,
    /// JSON-path-style reference (e.g. `schema[0].name`).
    pub object_ref: String,
}

/// Returns the first duplicate key found in a JSON document, if any.
pub fn find_json_duplicate_key(content: &[u8]) -> Option<DuplicateKeyFinding> {
    let finding = Rc::new(RefCell::new(None));
    let mut de = serde_json::Deserializer::from_slice(content);
    let visitor = DupeDetectVisitor {
        finding: Rc::clone(&finding),
        path: Vec::new(),
    };
    if de.deserialize_any(visitor).is_err() {
        return finding.borrow().clone();
    }
    None
}

/// Result of scanning a YAML document for duplicate mapping keys.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum YamlDuplicateKeyScan {
    /// Scan completed; no duplicate keys found.
    Ok,
    /// First duplicate key found.
    Duplicate(DuplicateKeyFinding),
    /// The libyaml event walk failed (malformed YAML or scanner error).
    ScanFailed,
}

/// Returns the first duplicate key found in a YAML document, if any.
///
/// Uses an `unsafe-libyaml` event walk so duplicates are detected before
/// `serde_yaml` deserialization (which silently overwrites duplicate keys).
/// Flow-style mappings and anchor/alias resolution are not fully validated.
pub fn find_yaml_duplicate_key(content: &str) -> YamlDuplicateKeyScan {
    let bytes = content.as_bytes();
    let mut parser = MaybeUninit::<sys::yaml_parser_t>::uninit();

    unsafe {
        let parser = parser.as_mut_ptr();
        if sys::yaml_parser_initialize(parser).fail {
            return YamlDuplicateKeyScan::ScanFailed;
        }

        struct ParserGuard(*mut sys::yaml_parser_t);
        impl Drop for ParserGuard {
            fn drop(&mut self) {
                unsafe {
                    sys::yaml_parser_delete(self.0);
                }
            }
        }
        let _guard = ParserGuard(parser);

        sys::yaml_parser_set_encoding(parser, sys::YAML_UTF8_ENCODING);
        sys::yaml_parser_set_input_string(parser, bytes.as_ptr(), bytes.len() as u64);

        let mut state = YamlDupeState::default();
        let mut event = MaybeUninit::<sys::yaml_event_t>::uninit();

        loop {
            let event_ptr = event.as_mut_ptr();
            if sys::yaml_parser_parse(parser, event_ptr).fail {
                return YamlDuplicateKeyScan::ScanFailed;
            }

            let event_type = (*event_ptr).type_;
            if let Some(finding) = state.handle_event(event_ptr, event_type) {
                sys::yaml_event_delete(event_ptr);
                return YamlDuplicateKeyScan::Duplicate(finding);
            }

            sys::yaml_event_delete(event_ptr);
            if event_type == sys::YAML_STREAM_END_EVENT {
                break;
            }
        }
    }

    YamlDuplicateKeyScan::Ok
}

fn format_path(segments: &[String]) -> String {
    let mut result = String::new();
    for segment in segments {
        if !segment.starts_with('[') && !result.is_empty() {
            result.push('.');
        }
        result.push_str(segment);
    }
    result
}

fn object_ref(path: &[String], key: &str) -> String {
    let base = format_path(path);
    if base.is_empty() {
        key.to_string()
    } else {
        format!("{base}.{key}")
    }
}

struct DupeDetectVisitor {
    finding: Rc<RefCell<Option<DuplicateKeyFinding>>>,
    path: Vec<String>,
}

impl DupeDetectVisitor {
    fn record_duplicate(&self, key: String) {
        *self.finding.borrow_mut() = Some(DuplicateKeyFinding {
            object_ref: object_ref(&self.path, &key),
            key,
        });
    }
}

impl<'de> Visitor<'de> for DupeDetectVisitor {
    type Value = ();

    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        formatter.write_str("JSON value")
    }

    fn visit_map<M>(mut self, mut access: M) -> Result<Self::Value, M::Error>
    where
        M: MapAccess<'de>,
    {
        let mut keys = HashSet::new();
        while let Some(key) = access.next_key::<String>()? {
            if !keys.insert(key.clone()) {
                self.record_duplicate(key);
                return Err(de::Error::custom("duplicate key detected"));
            }

            self.path.push(key);
            access.next_value_seed(DupeDetectSeed {
                finding: Rc::clone(&self.finding),
                path: self.path.clone(),
            })?;
            self.path.pop();
        }
        Ok(())
    }

    fn visit_seq<M>(self, mut access: M) -> Result<Self::Value, M::Error>
    where
        M: SeqAccess<'de>,
    {
        let mut index = 0usize;
        while access
            .next_element_seed(DupeDetectSeed {
                finding: Rc::clone(&self.finding),
                path: {
                    let mut path = self.path.clone();
                    path.push(format!("[{index}]"));
                    path
                },
            })?
            .is_some()
        {
            index += 1;
        }
        Ok(())
    }

    fn visit_bool<E>(self, _: bool) -> Result<Self::Value, E> {
        Ok(())
    }

    fn visit_i64<E>(self, _: i64) -> Result<Self::Value, E> {
        Ok(())
    }

    fn visit_u64<E>(self, _: u64) -> Result<Self::Value, E> {
        Ok(())
    }

    fn visit_f64<E>(self, _: f64) -> Result<Self::Value, E> {
        Ok(())
    }

    fn visit_str<E>(self, _: &str) -> Result<Self::Value, E> {
        Ok(())
    }

    fn visit_string<E>(self, _: String) -> Result<Self::Value, E> {
        Ok(())
    }

    fn visit_none<E>(self) -> Result<Self::Value, E> {
        Ok(())
    }

    fn visit_some<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
    where
        D: Deserializer<'de>,
    {
        DeserializeSeed::deserialize(
            DupeDetectSeed {
                finding: Rc::clone(&self.finding),
                path: self.path,
            },
            deserializer,
        )
    }

    fn visit_unit<E>(self) -> Result<Self::Value, E> {
        Ok(())
    }
}

struct DupeDetectSeed {
    finding: Rc<RefCell<Option<DuplicateKeyFinding>>>,
    path: Vec<String>,
}

impl<'de> DeserializeSeed<'de> for DupeDetectSeed {
    type Value = ();

    fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_any(DupeDetectVisitor {
            finding: self.finding,
            path: self.path,
        })
    }
}

#[derive(Default)]
struct YamlDupeState {
    frames: Vec<YamlFrame>,
    path: Vec<String>,
    pending_key: Option<String>,
}

enum YamlFrame {
    Mapping {
        keys_seen: HashSet<String>,
        expect_key: bool,
    },
    Sequence {
        next_index: usize,
    },
}

impl YamlDupeState {
    fn handle_event(
        &mut self,
        event: *const sys::yaml_event_t,
        event_type: sys::yaml_event_type_t,
    ) -> Option<DuplicateKeyFinding> {
        match event_type {
            sys::YAML_MAPPING_START_EVENT => self.on_mapping_start(),
            sys::YAML_MAPPING_END_EVENT => self.on_mapping_end(),
            sys::YAML_SEQUENCE_START_EVENT => self.on_sequence_start(),
            sys::YAML_SEQUENCE_END_EVENT => self.on_sequence_end(),
            sys::YAML_SCALAR_EVENT => self.on_scalar(event),
            sys::YAML_ALIAS_EVENT => self.on_alias(),
            _ => None,
        }
    }

    fn on_mapping_start(&mut self) -> Option<DuplicateKeyFinding> {
        if let Some(key) = self.pending_key.take() {
            self.path.push(key);
        }

        if matches!(self.frames.last(), Some(YamlFrame::Sequence { .. })) {
            if let Some(YamlFrame::Sequence { next_index }) = self.frames.last_mut() {
                self.path.push(format!("[{next_index}]"));
                *next_index += 1;
            }
        }

        self.frames.push(YamlFrame::Mapping {
            keys_seen: HashSet::new(),
            expect_key: true,
        });
        None
    }

    fn on_mapping_end(&mut self) -> Option<DuplicateKeyFinding> {
        self.frames.pop()?;

        if matches!(self.frames.last(), Some(YamlFrame::Mapping { .. })) {
            self.set_parent_mapping_expect_key(true);
        }

        if matches!(self.frames.last(), Some(YamlFrame::Sequence { .. })) {
            if self
                .path
                .last()
                .is_some_and(|segment| segment.starts_with('['))
            {
                self.path.pop();
            }
        } else if self.pending_key.is_none() {
            self.path.pop();
        }

        None
    }

    fn on_sequence_start(&mut self) -> Option<DuplicateKeyFinding> {
        if let Some(key) = self.pending_key.take() {
            self.path.push(key);
        }

        self.set_parent_mapping_expect_key(false);
        self.frames.push(YamlFrame::Sequence { next_index: 0 });
        None
    }

    fn on_sequence_end(&mut self) -> Option<DuplicateKeyFinding> {
        self.frames.pop()?;

        if self
            .path
            .last()
            .is_some_and(|segment| !segment.starts_with('['))
        {
            self.path.pop();
        }

        self.set_parent_mapping_expect_key(true);
        None
    }

    fn on_scalar(&mut self, event: *const sys::yaml_event_t) -> Option<DuplicateKeyFinding> {
        let value = unsafe { scalar_value(event) }?;

        let Some(YamlFrame::Mapping {
            keys_seen,
            expect_key,
        }) = self.frames.last_mut()
        else {
            return None;
        };

        if *expect_key {
            if !keys_seen.insert(value.clone()) {
                return Some(DuplicateKeyFinding {
                    key: value.clone(),
                    object_ref: object_ref(&self.path, &value),
                });
            }
            self.pending_key = Some(value);
            *expect_key = false;
            return None;
        }

        *expect_key = true;
        None
    }

    fn on_alias(&mut self) -> Option<DuplicateKeyFinding> {
        if matches!(self.frames.last(), Some(YamlFrame::Mapping { expect_key, .. }) if !*expect_key)
        {
            self.set_parent_mapping_expect_key(true);
        }
        None
    }

    fn set_parent_mapping_expect_key(&mut self, expect_key: bool) {
        if let Some(YamlFrame::Mapping {
            expect_key: parent_expect_key,
            ..
        }) = self.frames.last_mut()
        {
            *parent_expect_key = expect_key;
        }
    }
}

unsafe fn scalar_value(event: *const sys::yaml_event_t) -> Option<String> {
    let event = &*event;
    let ptr = event.data.scalar.value;
    if ptr.is_null() {
        return Some(String::new());
    }
    let len = event.data.scalar.length as usize;
    let bytes = slice::from_raw_parts(ptr, len);
    std::str::from_utf8(bytes).ok().map(str::to_string)
}

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

    const MINIMAL_YAML: &str = r#"
version: "1.0.0"
apiVersion: "v3.1.0"
kind: "DataContract"
id: "example"
status: "draft"
"#;

    #[test]
    fn yaml_valid_document_has_no_duplicate_keys() {
        assert_eq!(
            find_yaml_duplicate_key(MINIMAL_YAML),
            YamlDuplicateKeyScan::Ok
        );
    }

    #[test]
    fn yaml_root_duplicate_key() {
        let yaml = r#"
id: first
id: second
"#;
        let finding = match find_yaml_duplicate_key(yaml) {
            YamlDuplicateKeyScan::Duplicate(finding) => finding,
            other => panic!("expected duplicate, got {other:?}"),
        };
        assert_eq!(finding.key, "id");
        assert_eq!(finding.object_ref, "id");
    }

    #[test]
    fn yaml_nested_duplicate_key() {
        let yaml = r#"
schema:
  - name: customers
    name: duplicate
"#;
        let finding = match find_yaml_duplicate_key(yaml) {
            YamlDuplicateKeyScan::Duplicate(finding) => finding,
            other => panic!("expected duplicate, got {other:?}"),
        };
        assert_eq!(finding.key, "name");
        assert_eq!(finding.object_ref, "schema[0].name");
    }

    #[test]
    fn yaml_scan_fails_on_invalid_document() {
        let yaml = ":\n  bad: [\n";
        assert_eq!(
            find_yaml_duplicate_key(yaml),
            YamlDuplicateKeyScan::ScanFailed
        );
    }

    #[test]
    fn json_nested_duplicate_key() {
        let json = br#"{"schema":[{"name":"customers","name":"duplicate"}]}"#;
        let finding = find_json_duplicate_key(json).expect("duplicate");
        assert_eq!(finding.key, "name");
        assert_eq!(finding.object_ref, "schema[0].name");
    }
}