|
| 1 | +#!/usr/bin/env python |
| 2 | +""" |
| 3 | +Utility functions for SSVC json schema handling. |
| 4 | +""" |
| 5 | + |
| 6 | +# Copyright (c) 2025 Carnegie Mellon University. |
| 7 | +# NO WARRANTY. THIS CARNEGIE MELLON UNIVERSITY AND SOFTWARE |
| 8 | +# ENGINEERING INSTITUTE MATERIAL IS FURNISHED ON AN "AS-IS" BASIS. |
| 9 | +# CARNEGIE MELLON UNIVERSITY MAKES NO WARRANTIES OF ANY KIND, |
| 10 | +# EITHER EXPRESSED OR IMPLIED, AS TO ANY MATTER INCLUDING, BUT |
| 11 | +# NOT LIMITED TO, WARRANTY OF FITNESS FOR PURPOSE OR |
| 12 | +# MERCHANTABILITY, EXCLUSIVITY, OR RESULTS OBTAINED FROM USE |
| 13 | +# OF THE MATERIAL. CARNEGIE MELLON UNIVERSITY DOES NOT MAKE |
| 14 | +# ANY WARRANTY OF ANY KIND WITH RESPECT TO FREEDOM FROM |
| 15 | +# PATENT, TRADEMARK, OR COPYRIGHT INFRINGEMENT. |
| 16 | +# Licensed under a MIT (SEI)-style license, please see LICENSE or contact |
| 17 | +# [email protected] for full terms. |
| 18 | +# [DISTRIBUTION STATEMENT A] This material has been approved for |
| 19 | +# public release and unlimited distribution. Please see Copyright notice |
| 20 | +# for non-US Government use and distribution. |
| 21 | +# This Software includes and/or makes use of Third-Party Software each |
| 22 | +# subject to its own license. |
| 23 | +# DM24-0278 |
| 24 | + |
| 25 | +from ssvc.utils.defaults import SCHEMA_ORDER |
| 26 | + |
| 27 | + |
| 28 | +# Copyright (c) 2025 Carnegie Mellon University. |
| 29 | +# NO WARRANTY. THIS CARNEGIE MELLON UNIVERSITY AND SOFTWARE |
| 30 | +# ENGINEERING INSTITUTE MATERIAL IS FURNISHED ON AN "AS-IS" BASIS. |
| 31 | +# CARNEGIE MELLON UNIVERSITY MAKES NO WARRANTIES OF ANY KIND, |
| 32 | +# EITHER EXPRESSED OR IMPLIED, AS TO ANY MATTER INCLUDING, BUT |
| 33 | +# NOT LIMITED TO, WARRANTY OF FITNESS FOR PURPOSE OR |
| 34 | +# MERCHANTABILITY, EXCLUSIVITY, OR RESULTS OBTAINED FROM USE |
| 35 | +# OF THE MATERIAL. CARNEGIE MELLON UNIVERSITY DOES NOT MAKE |
| 36 | +# ANY WARRANTY OF ANY KIND WITH RESPECT TO FREEDOM FROM |
| 37 | +# PATENT, TRADEMARK, OR COPYRIGHT INFRINGEMENT. |
| 38 | +# Licensed under a MIT (SEI)-style license, please see LICENSE or contact |
| 39 | +# [email protected] for full terms. |
| 40 | +# [DISTRIBUTION STATEMENT A] This material has been approved for |
| 41 | +# public release and unlimited distribution. Please see Copyright notice |
| 42 | +# for non-US Government use and distribution. |
| 43 | +# This Software includes and/or makes use of Third-Party Software each |
| 44 | +# subject to its own license. |
| 45 | +# DM24-0278 |
| 46 | +def reorder_title_first(obj): |
| 47 | + if isinstance(obj, dict): |
| 48 | + if "title" in obj: |
| 49 | + reordered = {"title": obj["title"]} |
| 50 | + for k, v in obj.items(): |
| 51 | + if k != "title": |
| 52 | + reordered[k] = reorder_title_first(v) |
| 53 | + return reordered |
| 54 | + else: |
| 55 | + return {k: reorder_title_first(v) for k, v in obj.items()} |
| 56 | + elif isinstance(obj, list): |
| 57 | + return [reorder_title_first(item) for item in obj] |
| 58 | + else: |
| 59 | + return obj |
| 60 | + |
| 61 | + |
| 62 | +def order_schema(schema: dict) -> dict: |
| 63 | + # create a new dict with the preferred order of fields first |
| 64 | + ordered_schema = {k: schema[k] for k in (SCHEMA_ORDER) if k in schema} |
| 65 | + |
| 66 | + # add the rest of the fields in their original order |
| 67 | + other_keys = [k for k in schema if k not in ordered_schema] |
| 68 | + for k in other_keys: |
| 69 | + ordered_schema[k] = schema[k] |
| 70 | + |
| 71 | + # recursively move "title" to the front of any nested objects |
| 72 | + ordered_schema = reorder_title_first(ordered_schema) |
| 73 | + |
| 74 | + return ordered_schema |
| 75 | + |
| 76 | + |
| 77 | +def strip_nullable_anyof(schema: dict) -> dict: |
| 78 | + """Recursively rewrite schema to drop `anyOf` [string, null] constructs.""" |
| 79 | + if isinstance(schema, dict): |
| 80 | + # If schema has "anyOf" |
| 81 | + if "anyOf" in schema: |
| 82 | + anyof: list[dict[str, Any]] = schema["anyOf"] |
| 83 | + string_schema = None |
| 84 | + has_null = False |
| 85 | + |
| 86 | + for option in anyof: |
| 87 | + if option.get("type") == "string": |
| 88 | + string_schema = option |
| 89 | + elif option.get("type") == "null": |
| 90 | + has_null = True |
| 91 | + |
| 92 | + # Replace with string schema if this was the pattern |
| 93 | + if string_schema and has_null and len(anyof) == 2: |
| 94 | + # Preserve the title if it was in the parent |
| 95 | + title = schema.get("title") |
| 96 | + schema = dict(string_schema) # copy |
| 97 | + if title: |
| 98 | + schema["title"] = title |
| 99 | + # Drop any default:null |
| 100 | + schema.pop("default", None) |
| 101 | + |
| 102 | + # Recurse into nested dicts/lists |
| 103 | + for key, value in list(schema.items()): |
| 104 | + schema[key] = strip_nullable_anyof(value) |
| 105 | + |
| 106 | + elif isinstance(schema, list): |
| 107 | + return [strip_nullable_anyof(item) for item in schema] |
| 108 | + |
| 109 | + return schema |
0 commit comments