|
14 | 14 |
|
15 | 15 | """Post-generation fixes for constraints datamodel-code-generator ignores. |
16 | 16 |
|
17 | | -Six constraint families are handled: |
| 17 | +Seven constraint families are handled: |
18 | 18 |
|
19 | 19 | * ``minProperties`` on an object schema WITH declared properties is dropped by |
20 | 20 | the generator (issue #49): every field is optional, so an empty instance |
|
74 | 74 | then injects a ``model_validator(mode="after")``. More complex conditions are |
75 | 75 | skipped rather than approximated. |
76 | 76 |
|
| 77 | +* Simple conditional numeric bounds are also dropped: well-known ``Total`` |
| 78 | + categories constrain ``amount`` to negative or non-negative values. The script |
| 79 | + accepts only one discriminator, one target field, and one numeric bound in each |
| 80 | + ``if``/``then`` rule, and injects a ``model_validator(mode="after")``. Rules |
| 81 | + with ``else`` or multiple consequence fields are skipped. |
| 82 | +
|
77 | 83 | * ``additionalProperties: false`` on an object schema with named properties is |
78 | 84 | normally overridden by the generator's ``--extra-fields=allow`` flag. The |
79 | 85 | script detects schemas with ``additionalProperties: false`` and flips their |
@@ -148,6 +154,33 @@ def {marker}(self): |
148 | 154 | return self |
149 | 155 | ''' |
150 | 156 |
|
| 157 | +_CONDITIONAL_NUMERIC_MARKER = "_enforce_conditional_numeric_bounds" |
| 158 | + |
| 159 | +_CONDITIONAL_NUMERIC_TEMPLATE = ''' |
| 160 | + @model_validator(mode="after") |
| 161 | + def {marker}(self): |
| 162 | + """JSON Schema if/then: enforce conditional numeric bounds.""" |
| 163 | + rules = {rules!r} |
| 164 | + operators = {{ |
| 165 | + "minimum": lambda value, bound: value >= bound, |
| 166 | + "exclusiveMinimum": lambda value, bound: value > bound, |
| 167 | + "maximum": lambda value, bound: value <= bound, |
| 168 | + "exclusiveMaximum": lambda value, bound: value < bound, |
| 169 | + }} |
| 170 | + for rule in rules: |
| 171 | + if getattr(self, rule["discriminator"], None) not in rule["values"]: |
| 172 | + continue |
| 173 | + value = getattr(self, rule["field"], None) |
| 174 | + if value is not None and not operators[rule["bound"]]( |
| 175 | + value, rule["value"] |
| 176 | + ): |
| 177 | + raise ValueError( |
| 178 | + f"Field {{rule['field']!r}} violates conditional " |
| 179 | + f"{{rule['bound']}}={{rule['value']}}" |
| 180 | + ) |
| 181 | + return self |
| 182 | +''' |
| 183 | + |
151 | 184 | _UNIQUE_VALIDATOR_TEMPLATE = ''' |
152 | 185 | @field_validator("{field}", mode="after") |
153 | 186 | def {marker}_{field}(cls, value): # noqa: N805 |
@@ -690,6 +723,162 @@ def inject_conditional_required(source, class_name, rules): |
690 | 723 | return _ensure_pydantic_import(out, "model_validator") |
691 | 724 |
|
692 | 725 |
|
| 726 | +def find_conditional_numeric_bounds(schema_dir): |
| 727 | + """Map generated classes to simple if/then numeric-bound rules.""" |
| 728 | + rules_by_class = {} |
| 729 | + bound_names = { |
| 730 | + "minimum", |
| 731 | + "exclusiveMinimum", |
| 732 | + "maximum", |
| 733 | + "exclusiveMaximum", |
| 734 | + } |
| 735 | + |
| 736 | + def describe(node, properties): |
| 737 | + if not isinstance(node, dict) or set(node) != {"if", "then"}: |
| 738 | + return None |
| 739 | + condition = node["if"] |
| 740 | + consequence = node["then"] |
| 741 | + if ( |
| 742 | + not isinstance(condition, dict) |
| 743 | + or set(condition) != {"properties", "required"} |
| 744 | + or not isinstance(consequence, dict) |
| 745 | + or set(consequence) != {"properties"} |
| 746 | + ): |
| 747 | + return None |
| 748 | + condition_props = condition["properties"] |
| 749 | + condition_required = condition["required"] |
| 750 | + consequence_props = consequence["properties"] |
| 751 | + if ( |
| 752 | + not isinstance(condition_props, dict) |
| 753 | + or len(condition_props) != 1 |
| 754 | + or not isinstance(condition_required, list) |
| 755 | + or len(condition_required) != 1 |
| 756 | + or not isinstance(consequence_props, dict) |
| 757 | + or len(consequence_props) != 1 |
| 758 | + ): |
| 759 | + return None |
| 760 | + discriminator, predicate = next(iter(condition_props.items())) |
| 761 | + field, constraint = next(iter(consequence_props.items())) |
| 762 | + if condition_required != [discriminator] or not isinstance( |
| 763 | + predicate, dict |
| 764 | + ): |
| 765 | + return None |
| 766 | + if set(predicate) == {"const"}: |
| 767 | + values = [predicate["const"]] |
| 768 | + elif ( |
| 769 | + set(predicate) == {"enum"} |
| 770 | + and isinstance(predicate["enum"], list) |
| 771 | + and predicate["enum"] |
| 772 | + ): |
| 773 | + values = predicate["enum"] |
| 774 | + else: |
| 775 | + return None |
| 776 | + if ( |
| 777 | + not isinstance(constraint, dict) |
| 778 | + or len(constraint) != 1 |
| 779 | + or not set(constraint) <= bound_names |
| 780 | + ): |
| 781 | + return None |
| 782 | + bound, value = next(iter(constraint.items())) |
| 783 | + if ( |
| 784 | + discriminator not in properties |
| 785 | + or field not in properties |
| 786 | + or isinstance(value, bool) |
| 787 | + or not isinstance(value, (int, float)) |
| 788 | + or any( |
| 789 | + not isinstance(item, (str, int, float, bool)) for item in values |
| 790 | + ) |
| 791 | + ): |
| 792 | + return None |
| 793 | + return { |
| 794 | + "discriminator": discriminator, |
| 795 | + "values": values, |
| 796 | + "field": field, |
| 797 | + "bound": bound, |
| 798 | + "value": value, |
| 799 | + } |
| 800 | + |
| 801 | + def walk(node, current_class_name, class_properties, path_str): |
| 802 | + if not isinstance(node, dict): |
| 803 | + return |
| 804 | + if isinstance(node.get("title"), str): |
| 805 | + current_class_name = _alias_name(node["title"]) |
| 806 | + properties = node.get("properties") |
| 807 | + if isinstance(properties, dict): |
| 808 | + class_properties = properties |
| 809 | + if "if" in node or "then" in node: |
| 810 | + consequence = node.get("then") |
| 811 | + consequence_props = ( |
| 812 | + consequence.get("properties") |
| 813 | + if isinstance(consequence, dict) |
| 814 | + else None |
| 815 | + ) |
| 816 | + is_numeric_rule = isinstance(consequence_props, dict) and any( |
| 817 | + isinstance(constraint, dict) |
| 818 | + and bool(set(constraint) & bound_names) |
| 819 | + for constraint in consequence_props.values() |
| 820 | + ) |
| 821 | + if is_numeric_rule: |
| 822 | + rule = ( |
| 823 | + None if "else" in node else describe(node, class_properties) |
| 824 | + ) |
| 825 | + if rule is None: |
| 826 | + sys.stderr.write( |
| 827 | + f" ! {path_str}: unsupported conditional numeric " |
| 828 | + "rule; skipped\n" |
| 829 | + ) |
| 830 | + elif current_class_name is not None: |
| 831 | + rules_by_class.setdefault(current_class_name, []).append( |
| 832 | + rule |
| 833 | + ) |
| 834 | + if isinstance(properties, dict): |
| 835 | + for name, prop in properties.items(): |
| 836 | + walk(prop, _to_camel_case(name), properties, path_str) |
| 837 | + defs = node.get("$defs") |
| 838 | + if isinstance(defs, dict): |
| 839 | + for def_name, def_node in defs.items(): |
| 840 | + walk(def_node, _to_camel_case(def_name), {}, path_str) |
| 841 | + for key in ("allOf", "anyOf", "oneOf"): |
| 842 | + if isinstance(node.get(key), list): |
| 843 | + for item in node[key]: |
| 844 | + walk(item, current_class_name, class_properties, path_str) |
| 845 | + |
| 846 | + for path in sorted(Path(schema_dir).rglob("*.json")): |
| 847 | + try: |
| 848 | + schema = json.loads(path.read_text(encoding="utf-8")) |
| 849 | + except (OSError, json.JSONDecodeError): |
| 850 | + continue |
| 851 | + if not isinstance(schema, dict): |
| 852 | + continue |
| 853 | + root_title = schema.get("title") |
| 854 | + initial_class = ( |
| 855 | + _alias_name(root_title) if root_title else _to_camel_case(path.stem) |
| 856 | + ) |
| 857 | + walk(schema, initial_class, {}, str(path)) |
| 858 | + return rules_by_class |
| 859 | + |
| 860 | + |
| 861 | +def inject_conditional_numeric_bounds(source, class_name, rules): |
| 862 | + """Inject simple conditional numeric checks into one generated class.""" |
| 863 | + class_re = re.compile(rf"^class {re.escape(class_name)}\(", re.M) |
| 864 | + match = class_re.search(source) |
| 865 | + if not match: |
| 866 | + return source |
| 867 | + tail = re.compile(r"^\S", re.M) |
| 868 | + end_match = tail.search(source, match.end()) |
| 869 | + end = end_match.start() if end_match else len(source) |
| 870 | + if f"def {_CONDITIONAL_NUMERIC_MARKER}(" in source[match.start() : end]: |
| 871 | + return source |
| 872 | + method = _CONDITIONAL_NUMERIC_TEMPLATE.format( |
| 873 | + marker=_CONDITIONAL_NUMERIC_MARKER, |
| 874 | + rules=rules, |
| 875 | + ) |
| 876 | + body = source[:end].rstrip("\n") |
| 877 | + rest = source[end:] |
| 878 | + out = body + "\n" + method + ("\n" + rest if rest else "") |
| 879 | + return _ensure_pydantic_import(out, "model_validator") |
| 880 | + |
| 881 | + |
693 | 882 | def find_unique_items_fields(schema_dir): |
694 | 883 | """Map generated class names to fields carrying ``uniqueItems``. |
695 | 884 |
|
@@ -982,6 +1171,41 @@ def _patch_conditional_required(): |
982 | 1171 | return patched, 0 |
983 | 1172 |
|
984 | 1173 |
|
| 1174 | +def _patch_conditional_numeric_bounds(): |
| 1175 | + """Inject conditional numeric validators; return counts and status.""" |
| 1176 | + rules_by_class = find_conditional_numeric_bounds(RAW_SCHEMA_DIR) |
| 1177 | + if not rules_by_class: |
| 1178 | + sys.stdout.write( |
| 1179 | + "postprocess: no simple conditional numeric rules found\n" |
| 1180 | + ) |
| 1181 | + return 0, 0 |
| 1182 | + patched = 0 |
| 1183 | + for class_name, rules in sorted(rules_by_class.items()): |
| 1184 | + hits = [] |
| 1185 | + for path in sorted(OUTPUT_DIR.rglob("*.py")): |
| 1186 | + source = path.read_text(encoding="utf-8") |
| 1187 | + if not re.search( |
| 1188 | + rf"^class {re.escape(class_name)}\(", source, re.M |
| 1189 | + ): |
| 1190 | + continue |
| 1191 | + updated = inject_conditional_numeric_bounds( |
| 1192 | + source, class_name, rules |
| 1193 | + ) |
| 1194 | + if updated != source: |
| 1195 | + path.write_text(updated, encoding="utf-8") |
| 1196 | + patched += 1 |
| 1197 | + hits.append(path) |
| 1198 | + label = ( |
| 1199 | + ", ".join(str(path) for path in hits) or "NO GENERATED CLASS FOUND" |
| 1200 | + ) |
| 1201 | + sys.stdout.write( |
| 1202 | + f" conditional numeric bounds on '{class_name}' -> {label}\n" |
| 1203 | + ) |
| 1204 | + if not hits: |
| 1205 | + return patched, 1 |
| 1206 | + return patched, 0 |
| 1207 | + |
| 1208 | + |
985 | 1209 | def _patch_unique_items(): |
986 | 1210 | """Inject uniqueItems validators; return (patched_count, exit_code).""" |
987 | 1211 | unique_fields_by_class = find_unique_items_fields(SCHEMA_DIR) |
@@ -1121,18 +1345,20 @@ def main(): |
1121 | 1345 | patched_pn, rc_pn = _patch_property_names() |
1122 | 1346 | patched_ac, rc_ac = _patch_array_contains() |
1123 | 1347 | patched_cr, rc_cr = _patch_conditional_required() |
| 1348 | + patched_cn, rc_cn = _patch_conditional_numeric_bounds() |
1124 | 1349 | patched_ui, rc_ui = _patch_unique_items() |
1125 | 1350 | patched_ef, rc_ef = _patch_extra_forbid() |
1126 | 1351 | total = ( |
1127 | 1352 | patched_mp |
1128 | 1353 | + patched_pn |
1129 | 1354 | + patched_ac |
1130 | 1355 | + patched_cr |
| 1356 | + + patched_cn |
1131 | 1357 | + patched_ui |
1132 | 1358 | + patched_ef |
1133 | 1359 | ) |
1134 | 1360 | sys.stdout.write(f"postprocess: {total} module(s) patched\n") |
1135 | | - return rc_mp or rc_pn or rc_ac or rc_cr or rc_ui or rc_ef |
| 1361 | + return rc_mp or rc_pn or rc_ac or rc_cr or rc_cn or rc_ui or rc_ef |
1136 | 1362 |
|
1137 | 1363 |
|
1138 | 1364 | if __name__ == "__main__": |
|
0 commit comments