|
| 1 | +"""Generate JSON Schema documents for Marshmallow schemas. |
| 2 | +
|
| 3 | +We keep this isolated from the core library logic: Commonmeta already ships |
| 4 | +hand-authored JSON Schemas under `commonmeta/resources/` for validation. |
| 5 | +
|
| 6 | +Note: `marshmallow-jsonschema` (0.13.0) imports `pkg_resources` at import time. |
| 7 | +`pkg_resources` (from setuptools) is deprecated and may be absent in modern |
| 8 | +environments. To avoid adding setuptools just for this, we install a minimal |
| 9 | +runtime shim for `pkg_resources.get_distribution` before importing the library. |
| 10 | +""" |
| 11 | + |
| 12 | +from __future__ import annotations |
| 13 | + |
| 14 | +import importlib.metadata |
| 15 | +import sys |
| 16 | +import types |
| 17 | +from typing import Any |
| 18 | + |
| 19 | +from marshmallow import Schema |
| 20 | + |
| 21 | + |
| 22 | +def _install_pkg_resources_shim() -> None: |
| 23 | + """Install a minimal `pkg_resources` shim into `sys.modules`. |
| 24 | +
|
| 25 | + `marshmallow-jsonschema` only uses `pkg_resources.get_distribution(...).version` |
| 26 | + to populate `__version__`. |
| 27 | + """ |
| 28 | + |
| 29 | + if "pkg_resources" in sys.modules: |
| 30 | + return |
| 31 | + |
| 32 | + shim = types.ModuleType("pkg_resources") |
| 33 | + |
| 34 | + def get_distribution(dist_name: str): |
| 35 | + try: |
| 36 | + version = importlib.metadata.version(dist_name) |
| 37 | + except importlib.metadata.PackageNotFoundError: |
| 38 | + version = "0.0.0" |
| 39 | + return types.SimpleNamespace(version=version) |
| 40 | + |
| 41 | + shim.get_distribution = get_distribution # type: ignore[attr-defined] |
| 42 | + sys.modules["pkg_resources"] = shim |
| 43 | + |
| 44 | + |
| 45 | +def marshmallow_to_jsonschema(schema: Schema) -> dict[str, Any]: |
| 46 | + """Convert a Marshmallow `Schema` instance to a JSON Schema dict.""" |
| 47 | + |
| 48 | + _install_pkg_resources_shim() |
| 49 | + |
| 50 | + from marshmallow_jsonschema import JSONSchema |
| 51 | + |
| 52 | + base: dict[str, Any] = JSONSchema().dump(schema) |
| 53 | + # marshmallow-jsonschema doesn't add the $schema keyword; add it for clarity. |
| 54 | + return {"$schema": "http://json-schema.org/draft-07/schema#", **base} |
| 55 | + |
| 56 | + |
| 57 | +def generate_jsonschema(name: str) -> dict[str, Any]: |
| 58 | + """Generate a JSON Schema by symbolic name. |
| 59 | +
|
| 60 | + Currently supported: |
| 61 | + - `crossref_xml`: the pre-serialization Crossref writer dict. |
| 62 | + """ |
| 63 | + |
| 64 | + if name == "crossref_xml": |
| 65 | + from .writers.crossref_xml_writer import CrossrefXMLSchema |
| 66 | + |
| 67 | + schema = marshmallow_to_jsonschema(CrossrefXMLSchema()) |
| 68 | + # Match existing resource naming and intent. |
| 69 | + schema["$id"] = "crossref-v5.4.0.json" |
| 70 | + schema["title"] = "Crossref XML Writer v5.4.0" |
| 71 | + schema["description"] = ( |
| 72 | + "JSON Schema for validating output before converting to Crossref XML." |
| 73 | + ) |
| 74 | + return schema |
| 75 | + |
| 76 | + raise ValueError(f"Unknown schema: {name}") |
0 commit comments