jsoncompat 0.4.2

JSON Schema and OpenAPI Compatibility Checker
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
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
//! Object subset helpers.

use crate::SchemaNode;
use crate::subset::{SubschemaCheckContext, is_subschema_of_with_productive_context};
use json_schema_ast::{CountRange, PatternConstraint, PatternProperty, PatternSupport};
use serde_json::Value;
use std::collections::{HashMap, HashSet};

use super::{
    finite_property_name_strings_superset, scalar::check_enum_inclusion,
    schema_definitely_rejects_all_values, schema_may_under_accept_values,
};

pub(super) struct ObjectConstraints<'a> {
    pub(super) properties: &'a HashMap<String, SchemaNode>,
    pub(super) pattern_properties: &'a HashMap<String, PatternProperty<SchemaNode>>,
    pub(super) required: &'a HashSet<String>,
    pub(super) additional: &'a SchemaNode,
    pub(super) property_names: &'a SchemaNode,
    pub(super) property_count: CountRange<usize>,
    pub(super) dependent_required: &'a HashMap<String, Vec<String>>,
    pub(super) enumeration: Option<&'a [Value]>,
}

#[derive(Clone, Copy)]
struct SubPropertyConjuncts<'a> {
    property: Option<&'a SchemaNode>,
    pattern_properties: &'a HashMap<String, PatternProperty<SchemaNode>>,
    additional: &'a SchemaNode,
}

/// Return whether `property_name` can coexist with the subset's required names
/// under its maxProperties bound.
pub(super) fn property_name_can_fit_count(
    property_name: &str,
    required: &HashSet<String>,
    property_count: CountRange<usize>,
) -> bool {
    required.contains(property_name)
        || property_count
            .max()
            .is_none_or(|max_properties| required.len() < max_properties)
}

/// Like [`property_name_can_fit_count`], but also accounts for names that
/// would be forced by `dependentRequired` if `property_name` were present.
///
/// `required` is expected to already include the closure of unconditionally
/// required names. Adding an optional trigger can still force a fresh chain of
/// dependencies; if that chain overflows `maxProperties`, the trigger is
/// impossible and constraints guarded by it are vacuous.
pub(super) fn property_name_can_fit_with_dependencies(
    property_name: &str,
    required: &HashSet<String>,
    property_count: CountRange<usize>,
    dependent_required: &HashMap<String, Vec<String>>,
) -> bool {
    if !property_name_can_fit_count(property_name, required, property_count) {
        return false;
    }

    let Some(max_properties) = property_count.max() else {
        return true;
    };

    if required.len() > max_properties {
        return false;
    }
    if required.contains(property_name) {
        return true;
    }

    names_forced_by_required_and_property(required, property_name, dependent_required).len()
        <= max_properties
}

/// Check `propertyNames` implication, with a small cardinality shortcut.
///
/// When `maxProperties` is exactly filled by names guaranteed by the subset
/// (including names forced through `dependentRequired`), no other property name
/// can occur. In that case it is enough to check those literal names against
/// the superset's `propertyNames` schema instead
/// of requiring a global schema implication (which is often much stronger than
/// necessary). If the guaranteed set already exceeds `maxProperties`, the subset
/// object type is uninhabited, so this particular constraint is vacuous.
pub(super) fn property_names_subsumed_with_count(
    sub_property_names: &SchemaNode,
    sup_property_names: &SchemaNode,
    required: &HashSet<String>,
    property_count: CountRange<usize>,
    context: &mut SubschemaCheckContext,
) -> bool {
    if let Some(max_properties) = property_count.max()
        && required.len() >= max_properties
    {
        if required.len() > max_properties {
            return true;
        }

        for name in required {
            // A required name rejected by the subset's own propertyNames makes
            // the object branch uninhabited. `property_name_schema_may_accept`
            // only returns false when that rejection is definitive.
            if !property_name_schema_may_accept(sub_property_names, name) {
                return true;
            }
            if !context.superset_contains_value(sup_property_names, &Value::String(name.clone())) {
                return false;
            }
        }
        return true;
    }

    is_subschema_of_with_productive_context(sub_property_names, sup_property_names, context)
}

fn dependent_required_constraints_subsumed(
    sub: &ObjectConstraints<'_>,
    sup: &ObjectConstraints<'_>,
    guaranteed_names: &HashSet<String>,
    context: &mut SubschemaCheckContext,
) -> bool {
    sup.dependent_required
        .iter()
        .all(|(trigger, dependencies)| {
            !object_property_name_can_be_present(trigger, sub, guaranteed_names, context)
                || dependencies.iter().all(|dependency| {
                    dependent_requirement_is_guaranteed(
                        trigger,
                        dependency,
                        guaranteed_names,
                        sub.dependent_required,
                    )
                })
        })
}

pub(super) fn effective_property_count_with_forced_names(
    property_count: CountRange<usize>,
    forced_names: &HashSet<String>,
) -> Option<CountRange<usize>> {
    CountRange::new(
        property_count.min().max(forced_names.len()),
        property_count.max(),
    )
}

pub(super) fn object_constraints_subsumed(
    sub: ObjectConstraints<'_>,
    sup: ObjectConstraints<'_>,
    context: &mut SubschemaCheckContext,
) -> bool {
    // If the subset object branch has no inhabitants, it is trivially a
    // subset of every superset branch.  Keep this check before the required
    // and count implication checks below: those checks otherwise report
    // false negatives for schemas such as
    // `{type: object, required: ["x"], properties: {x: false}}`.
    if object_constraints_definitely_uninhabited(&sub) {
        return true;
    }

    let guaranteed_names_storage = (!sub.dependent_required.is_empty())
        .then(|| names_forced_by_required(sub.required, sub.dependent_required));
    let guaranteed_names = guaranteed_names_storage.as_ref().unwrap_or(sub.required);
    let Some(mut effective_sub_property_count) =
        effective_property_count_with_forced_names(sub.property_count, guaranteed_names)
    else {
        return true;
    };
    if let Some(name_capacity) = finite_object_name_capacity(&sub) {
        let capped_max = Some(
            effective_sub_property_count
                .max()
                .map_or(name_capacity, |max| max.min(name_capacity)),
        );
        let Some(capped_count) = CountRange::new(effective_sub_property_count.min(), capped_max)
        else {
            return true;
        };
        effective_sub_property_count = capped_count;
    }

    if !sup
        .property_count
        .contains_range(effective_sub_property_count)
        || !check_enum_inclusion(sub.enumeration, sup.enumeration)
        || !sup.required.is_subset(guaranteed_names)
    {
        return false;
    }

    // Once the subset is known to admit at most the empty object, all
    // per-property constraints in the superset are vacuous.  The range and
    // required checks above already ruled out supersets that reject `{}`.
    // Avoid walking property schemas/patterns here: doing so is both wasted
    // work and can be spuriously conservative for names that cannot exist.
    if effective_sub_property_count.max() == Some(0) {
        return true;
    }

    // When propertyNames has an exact finite language, there are no truly
    // arbitrary property names. Enumerate the possible names directly instead
    // of requiring global additionalProperties/patternProperties implication,
    // which is often much stronger than necessary.
    if let Some(finite_names) = finite_object_name_values(&sub) {
        for property_name in &finite_names {
            if !object_property_name_can_be_present(property_name, &sub, guaranteed_names, context)
            {
                continue;
            }
            if !context
                .superset_contains_value(sup.property_names, &Value::String(property_name.clone()))
            {
                return false;
            }
            if !object_property_schema_is_subsumed(
                property_name,
                SubPropertyConjuncts {
                    property: sub.properties.get(property_name),
                    pattern_properties: sub.pattern_properties,
                    additional: sub.additional,
                },
                sup.properties.get(property_name),
                sup.pattern_properties,
                sup.additional,
                context,
            ) {
                return false;
            }
        }
        return dependent_required_constraints_subsumed(&sub, &sup, guaranteed_names, context);
    }

    for (property_name, sub_schema) in sub.properties {
        if !property_name_can_fit_with_dependencies(
            property_name,
            guaranteed_names,
            effective_sub_property_count,
            sub.dependent_required,
        ) {
            continue;
        }
        if !object_property_schema_is_subsumed(
            property_name,
            SubPropertyConjuncts {
                property: Some(sub_schema),
                pattern_properties: sub.pattern_properties,
                additional: sub.additional,
            },
            sup.properties.get(property_name),
            sup.pattern_properties,
            sup.additional,
            context,
        ) {
            return false;
        }
    }

    for (property_name, sup_property_schema) in sup.properties {
        // Optional constraints on the superset are vacuous when the subset can
        // never contain that property name (because of propertyNames, an empty
        // explicit property schema, saturated counts, or dependent-required
        // contradictions).  The cheaper count/dependency check misses common
        // generated shapes where `propertyNames` excludes a declared optional
        // property.
        if !object_property_name_can_be_present(property_name, &sub, guaranteed_names, context) {
            continue;
        }
        if sub.properties.contains_key(property_name)
            || subset_property_conjuncts_subsume_schema(
                property_name,
                SubPropertyConjuncts {
                    property: None,
                    pattern_properties: sub.pattern_properties,
                    additional: sub.additional,
                },
                sup_property_schema,
                context,
            )
        {
            continue;
        }

        return false;
    }

    if let Some(max_properties) = effective_sub_property_count.max()
        && guaranteed_names.len() >= max_properties
    {
        // If guaranteed names exhaust the property budget, there are no
        // arbitrary additional names to reason about. Check the finite set of
        // guaranteed names directly against the superset's property/pattern/
        // additional constraints, then skip the global pattern/additional
        // implication checks that assume arbitrary names may occur.
        if guaranteed_names.len() > max_properties {
            return true;
        }
        for property_name in guaranteed_names {
            if !object_property_schema_is_subsumed(
                property_name,
                SubPropertyConjuncts {
                    property: sub.properties.get(property_name),
                    pattern_properties: sub.pattern_properties,
                    additional: sub.additional,
                },
                sup.properties.get(property_name),
                sup.pattern_properties,
                sup.additional,
                context,
            ) {
                return false;
            }
        }
        if !property_names_subsumed_with_count(
            sub.property_names,
            sup.property_names,
            guaranteed_names,
            effective_sub_property_count,
            context,
        ) {
            return false;
        }
        return dependent_required_constraints_subsumed(&sub, &sup, guaranteed_names, context);
    }

    for (pattern, sub_pattern_property) in sub.pattern_properties {
        let sup_schema = match sup.pattern_properties.get(pattern) {
            Some(sup_pattern_property) => &sup_pattern_property.schema,
            None if sup.pattern_properties.is_empty() => sup.additional,
            None => return false,
        };
        if !is_subschema_of_with_productive_context(
            &sub_pattern_property.schema,
            sup_schema,
            context,
        ) {
            return false;
        }

        // A property name matched by one pattern can also match any other
        // pattern. Without proving regex disjointness, preserve every
        // additional superset pattern constraint conservatively.
        for (sup_pattern, sup_pattern_property) in sup.pattern_properties {
            if sup_pattern == pattern {
                continue;
            }
            if !is_subschema_of_with_productive_context(
                &sub_pattern_property.schema,
                &sup_pattern_property.schema,
                context,
            ) {
                return false;
            }
        }
    }

    if !object_additional_schema_is_subsumed(
        sub.additional,
        sub.pattern_properties,
        sup.pattern_properties,
        sup.additional,
        context,
    ) || !property_names_subsumed_with_count(
        sub.property_names,
        sup.property_names,
        guaranteed_names,
        effective_sub_property_count,
        context,
    ) {
        return false;
    }

    dependent_required_constraints_subsumed(&sub, &sup, guaranteed_names, context)
}

/// A deliberately small, sound emptiness check for object constraints.
///
/// Full JSON Schema satisfiability is hard, but a few contradictions are
/// common in generated schemas and cheap to recognize: too many required
/// names for `maxProperties`, a required name rejected by `propertyNames`, or
/// a required property whose applicable schema is literally `false`.
pub(super) fn object_constraints_definitely_uninhabited(sub: &ObjectConstraints<'_>) -> bool {
    // `dependentRequired` edges rooted at an unconditionally required name are
    // themselves unconditional. Close over those edges before applying the
    // cheap cardinality/property-name contradiction checks below.
    let forced_names_storage = (!sub.dependent_required.is_empty())
        .then(|| names_forced_by_required(sub.required, sub.dependent_required));
    let forced_names = forced_names_storage.as_ref().unwrap_or(sub.required);

    if sub
        .property_count
        .max()
        .is_some_and(|max_properties| forced_names.len() > max_properties)
    {
        return true;
    }

    if forced_names_have_definite_contradiction(
        forced_names,
        sub.properties,
        sub.pattern_properties,
        sub.property_names,
        sub.additional,
    ) {
        return true;
    }

    // A finite propertyNames language also caps the number of distinct object
    // keys (JSON objects cannot contain the same key twice).  This catches
    // contradictions such as `propertyNames: { enum: ["a"] }` together with
    // `minProperties: 2`, even when additionalProperties is otherwise open.
    if let Some(name_capacity) = finite_property_name_capacity(sub.property_names)
        && name_capacity < sub.property_count.min()
    {
        return true;
    }

    // If there are no pattern properties and additionalProperties is exactly
    // false, only explicitly declared property names can ever appear.  This
    // gives a cheap finite capacity bound for minProperties.  Count only names
    // that are not definitely ruled out by their own schema or propertyNames;
    // over-counting is fine (it merely misses an emptiness shortcut), but
    // under-counting would be unsound.
    if sub.pattern_properties.is_empty()
        && schema_definitely_rejects_all_values(sub.additional)
        && sub.property_count.min() > 0
    {
        let possible_declared_names = sub
            .properties
            .iter()
            .filter(|(name, schema)| {
                if schema_definitely_rejects_all_values(schema)
                    || !property_name_schema_may_accept(sub.property_names, name)
                    || !property_name_can_fit_with_dependencies(
                        name,
                        forced_names,
                        sub.property_count,
                        sub.dependent_required,
                    )
                {
                    return false;
                }

                let forced_if_present = names_forced_by_required_and_property(
                    forced_names,
                    name,
                    sub.dependent_required,
                );
                !forced_names_have_definite_contradiction(
                    &forced_if_present,
                    sub.properties,
                    sub.pattern_properties,
                    sub.property_names,
                    sub.additional,
                )
            })
            .count();
        if possible_declared_names < sub.property_count.min() {
            return true;
        }
    }

    false
}

/// Best-effort finite upper bound for the set of property names accepted by a
/// propertyNames schema. Return `None` unless the bound is exact enough to be
/// sound for raw validation too.
pub(super) fn finite_property_name_capacity(schema: &SchemaNode) -> Option<usize> {
    finite_property_name_values(schema).map(|names| names.len())
}

fn finite_object_name_capacity(sub: &ObjectConstraints<'_>) -> Option<usize> {
    let mut capacity = finite_property_name_capacity(sub.property_names);
    if sub.pattern_properties.is_empty() && schema_definitely_rejects_all_values(sub.additional) {
        let declared = sub.properties.len();
        capacity = Some(capacity.map_or(declared, |cap| cap.min(declared)));
    }
    capacity
}

/// A finite superset of all names that can occur in this object branch.
fn finite_object_name_values(sub: &ObjectConstraints<'_>) -> Option<Vec<String>> {
    let property_names_values = finite_property_name_values(sub.property_names);
    let closed_declared_values = (sub.pattern_properties.is_empty()
        && schema_definitely_rejects_all_values(sub.additional))
    .then(|| sub.properties.keys().cloned().collect::<Vec<_>>());

    match (property_names_values, closed_declared_values) {
        (Some(names), Some(declared)) => {
            // Both constraints are exact finite supersets; their intersection
            // is still a sound (and tighter) finite superset of possible names.
            Some(
                names
                    .into_iter()
                    .filter(|name| declared.iter().any(|declared| declared == name))
                    .collect(),
            )
        }
        (Some(names), None) | (None, Some(names)) => Some(names),
        (None, None) => None,
    }
}

/// Exact finite set of strings accepted by a simple propertyNames schema.
/// Returns `None` whenever the internal evaluator may fail closed/open relative
/// to raw validation, or when the language is not obviously finite.
fn finite_property_name_values(schema: &SchemaNode) -> Option<Vec<String>> {
    finite_property_name_strings_superset(schema)
}

/// Return true only for contradictions that definitively make all objects with
/// every name in `forced_names` impossible. Unsupported regex/propertyNames
/// cases deliberately stay "maybe" so this remains a sound shortcut.
fn forced_names_have_definite_contradiction(
    forced_names: &HashSet<String>,
    properties: &HashMap<String, SchemaNode>,
    pattern_properties: &HashMap<String, PatternProperty<SchemaNode>>,
    property_names: &SchemaNode,
    additional: &SchemaNode,
) -> bool {
    for property_name in forced_names {
        if !property_name_schema_may_accept(property_names, property_name) {
            return true;
        }

        if properties
            .get(property_name)
            .is_some_and(schema_definitely_rejects_all_values)
        {
            return true;
        }

        let mut maybe_matched_by_pattern = false;
        for pattern_property in pattern_properties.values() {
            if !pattern_may_match_property_name(&pattern_property.pattern, property_name) {
                continue;
            }
            maybe_matched_by_pattern = true;
            if pattern_definitely_matches_property_name(&pattern_property.pattern, property_name)
                && schema_definitely_rejects_all_values(&pattern_property.schema)
            {
                return true;
            }
        }

        // An undeclared forced property that cannot match any pattern must be
        // accepted by additionalProperties. If that schema is literally false,
        // the object branch is empty.
        if !properties.contains_key(property_name)
            && !maybe_matched_by_pattern
            && schema_definitely_rejects_all_values(additional)
        {
            return true;
        }
    }

    false
}

fn names_forced_by_required_and_property(
    required: &HashSet<String>,
    property_name: &str,
    dependent_required: &HashMap<String, Vec<String>>,
) -> HashSet<String> {
    let mut seeds = required.clone();
    seeds.insert(property_name.to_owned());
    names_forced_by_required(&seeds, dependent_required)
}

pub(super) fn names_forced_by_required(
    required: &HashSet<String>,
    dependent_required: &HashMap<String, Vec<String>>,
) -> HashSet<String> {
    let mut forced = required.clone();
    let mut pending = required.iter().cloned().collect::<Vec<_>>();

    while let Some(name) = pending.pop() {
        let Some(dependencies) = dependent_required.get(&name) else {
            continue;
        };
        for dependency in dependencies {
            if forced.insert(dependency.clone()) {
                pending.push(dependency.clone());
            }
        }
    }

    forced
}

pub(super) fn dependent_requirement_is_guaranteed(
    trigger: &str,
    dependency: &str,
    required: &HashSet<String>,
    dependent_required: &HashMap<String, Vec<String>>,
) -> bool {
    if dependency == trigger || required.contains(dependency) {
        return true;
    }

    let mut pending = vec![trigger];
    let mut visited = HashSet::new();
    while let Some(current) = pending.pop() {
        if !visited.insert(current) {
            continue;
        }
        let Some(dependencies) = dependent_required.get(current) else {
            continue;
        };
        for guaranteed_dependency in dependencies {
            if guaranteed_dependency == dependency {
                return true;
            }
            pending.push(guaranteed_dependency);
        }
    }

    false
}

fn object_property_schema_is_subsumed(
    property_name: &str,
    sub: SubPropertyConjuncts<'_>,
    sup_property_schema: Option<&SchemaNode>,
    sup_pattern_properties: &HashMap<String, PatternProperty<SchemaNode>>,
    sup_additional: &SchemaNode,
    context: &mut SubschemaCheckContext,
) -> bool {
    let mut matched = false;

    if let Some(sup_property_schema) = sup_property_schema {
        matched = true;
        if !subset_property_conjuncts_subsume_schema(
            property_name,
            sub,
            sup_property_schema,
            context,
        ) {
            return false;
        }
    }

    for sup_pattern_property in sup_pattern_properties.values() {
        if !pattern_may_match_property_name(&sup_pattern_property.pattern, property_name) {
            continue;
        }
        matched = true;
        if !subset_property_conjuncts_subsume_schema(
            property_name,
            sub,
            &sup_pattern_property.schema,
            context,
        ) {
            return false;
        }
    }

    matched || subset_property_conjuncts_subsume_schema(property_name, sub, sup_additional, context)
}

fn subset_property_conjuncts_subsume_schema(
    property_name: &str,
    sub: SubPropertyConjuncts<'_>,
    sup_schema: &SchemaNode,
    context: &mut SubschemaCheckContext,
) -> bool {
    if let Some(sub_property_schema) = sub.property
        && is_subschema_of_with_productive_context(sub_property_schema, sup_schema, context)
    {
        return true;
    }

    let mut has_maybe_matching_pattern = false;
    for sub_pattern_property in sub.pattern_properties.values() {
        if !pattern_may_match_property_name(&sub_pattern_property.pattern, property_name) {
            continue;
        }

        has_maybe_matching_pattern = true;
        if pattern_definitely_matches_property_name(&sub_pattern_property.pattern, property_name)
            && is_subschema_of_with_productive_context(
                &sub_pattern_property.schema,
                sup_schema,
                context,
            )
        {
            return true;
        }
    }

    sub.property.is_none()
        && !has_maybe_matching_pattern
        && (context.assume_subset_omits_undeclared_properties
            || is_subschema_of_with_productive_context(sub.additional, sup_schema, context))
}

pub(super) fn implicit_property_conjuncts_subsume_schema(
    property_name: &str,
    pattern_properties: &HashMap<String, PatternProperty<SchemaNode>>,
    additional: &SchemaNode,
    sup_schema: &SchemaNode,
) -> bool {
    subset_property_conjuncts_subsume_schema(
        property_name,
        SubPropertyConjuncts {
            property: None,
            pattern_properties,
            additional,
        },
        sup_schema,
        &mut SubschemaCheckContext::default(),
    )
}

fn object_property_name_can_be_present(
    property_name: &str,
    sub: &ObjectConstraints<'_>,
    required: &HashSet<String>,
    context: &SubschemaCheckContext,
) -> bool {
    // If all available property slots are already consumed by required names,
    // a distinct optional trigger cannot be present. This is a cheap cardinality
    // check that avoids treating saturated objects as if arbitrary extra names
    // were still possible.
    if !property_name_can_fit_with_dependencies(
        property_name,
        required,
        sub.property_count,
        sub.dependent_required,
    ) {
        return false;
    }

    let forced_if_present =
        names_forced_by_required_and_property(required, property_name, sub.dependent_required);
    if forced_names_have_definite_contradiction(
        &forced_if_present,
        sub.properties,
        sub.pattern_properties,
        sub.property_names,
        sub.additional,
    ) {
        return false;
    }

    if !property_name_schema_may_accept(sub.property_names, property_name) {
        return false;
    }

    let explicit_property_can_admit = if let Some(schema) = sub.properties.get(property_name) {
        if schema_definitely_rejects_all_values(schema) {
            return false;
        }
        true
    } else {
        false
    };

    let mut definite_pattern_can_admit = false;
    for pattern_property in sub.pattern_properties.values() {
        if !pattern_definitely_matches_property_name(&pattern_property.pattern, property_name) {
            continue;
        }
        if schema_definitely_rejects_all_values(&pattern_property.schema) {
            return false;
        }
        definite_pattern_can_admit = true;
    }

    if explicit_property_can_admit || definite_pattern_can_admit {
        return true;
    }

    if !context.assume_subset_omits_undeclared_properties
        && !schema_definitely_rejects_all_values(sub.additional)
    {
        return true;
    }

    sub.pattern_properties.values().any(|pattern_property| {
        pattern_may_match_property_name(&pattern_property.pattern, property_name)
            && !schema_definitely_rejects_all_values(&pattern_property.schema)
    })
}

fn property_name_schema_may_accept(schema: &SchemaNode, property_name: &str) -> bool {
    let candidate = Value::String(property_name.to_owned());
    schema.accepts_value(&candidate) || schema_may_under_accept_values(schema)
}

fn object_additional_schema_is_subsumed(
    sub_additional: &SchemaNode,
    sub_pattern_properties: &HashMap<String, PatternProperty<SchemaNode>>,
    sup_pattern_properties: &HashMap<String, PatternProperty<SchemaNode>>,
    sup_additional: &SchemaNode,
    context: &mut SubschemaCheckContext,
) -> bool {
    if context.assume_subset_omits_undeclared_properties {
        return true;
    }

    if !is_subschema_of_with_productive_context(sub_additional, sup_additional, context) {
        return false;
    }

    sup_pattern_properties
        .iter()
        .filter(|(pattern, _)| !sub_pattern_properties.contains_key(*pattern))
        .all(|(_, sup_pattern_property)| {
            is_subschema_of_with_productive_context(
                sub_additional,
                &sup_pattern_property.schema,
                context,
            )
        })
}

fn pattern_definitely_matches_property_name(
    pattern: &PatternConstraint,
    property_name: &str,
) -> bool {
    match pattern.support() {
        PatternSupport::Supported => pattern.is_match(property_name),
        PatternSupport::Unsupported => false,
    }
}

fn pattern_may_match_property_name(pattern: &PatternConstraint, property_name: &str) -> bool {
    match pattern.support() {
        PatternSupport::Supported => pattern.is_match(property_name),
        PatternSupport::Unsupported => true,
    }
}