feat: add the schema export endpoint [IFC-3670]

Merged
OG
1d ago
+10100-1400

(the codspeed issue is not related to this PR)

Why

A schema owner who runs more than one Infrahub instance cannot copy the schema from one instance to another today. infrahubctl schema export writes values Infrahub generated and elements the schema owner did not define, so the file does not load without editing. The export rules are written by hand in the SDK and fall behind the schema model (spec, design document).

This pull request adds GET /api/schema/export, which builds the export on the server. The export is one document in the schema file format. It holds the nodes and generics the schema owner defined on a branch, and it loads into an empty instance with no edits. The SDK pull request, opsmill/infrahub-sdk-python#1554, makes infrahubctl schema export call this endpoint.

Not in this pull request:

  • An export that holds IDs, for a target that already has the same nodes.
  • A way to tell the client that a BRIEF export fell back to FULL. Only the server log shows it today.
  • Stopping an export when its client disconnects. An export that has started runs to the end.
  • Load shedding for exports. The admission middleware does not shed /api/schema paths.

Jira: IFC-3670, part of IFC-2964. Feature branch pull request: #11745.

Changes that need agreement

backend/AGENTS.md lists the first two under "Ask First". The third and the fifth change what API clients receive. The fourth differs from the design document. The sixth changes what a target gets from a BRIEF export.

  1. New REST endpoint GET /api/schema/export. Parameters: branch, namespaces (repeatable) and mode (BRIEF by default, or FULL). schema/openapi.json and the frontend REST types are regenerated.
  2. keep_in_export mark on seven field definitions in backend/infrahub/core/schema/definitions/internal.py: inherit_from on nodes; read_only, choices and unique on attributes; identifier, cardinality and optional on relationships. Without these fields a BRIEF candidate fails validation. The mark is not stored in the database. uv run invoke backend.generate writes the marked fields into KEEP_IN_EXPORT_FIELDS in the SDK's generated contract.py.
  3. New extensions.namespace field in REST error responses. Error.error_extensions adds fields to the extensions of each error. Only the new 404 for a namespace uses it. Clients need it because an unknown /api URL also returns a 404 in the same format.
  4. BRIEF can write a derived value next to the value it comes from. Example: an attribute loaded with default_value and no optional is stored with optional: true, and BRIEF writes both. The design document lists such derived values as left out; this is amendment A1 in its section 5, settled on 2026-12-01. The export still loads to the same schema but can be longer than the files the schema owner wrote.
  5. Renamed OpenAPI components. In schema/openapi.json, seven write components, such as NodeSchemaWrite, are each split into an -Input and an -Output component, because the export returns these models. The JSON the API sends does not change. A client generated from the OpenAPI document that uses the old names breaks. changelog/+schema-export-openapi-components.changed.md lists all seven.
  6. BRIEF does not write the deprecated default_filter. Only FULL writes it. A target loaded from a BRIEF export therefore has no default_filter, except on a node that takes it again from a generic Infrahub defines, such as IpamIPPrefix from BuiltinIPPrefix. Baptiste and Patrick decided this in the review on 2026-12-01: schema processing sets and changes default_filter, for example when a node takes it from its generic, so the stored value is not always one the schema owner wrote. The SDK get by a value that is not an ID, plain-string peers of a generic in object files, and the check of repository object files still read default_filter; the user documentation lists these cases.

What changed

Behavior:

  • GET /api/schema/export returns the user-defined nodes and generics of a branch as a schema file.
    • FULL writes every value a schema file can hold.
    • BRIEF leaves out every value Infrahub derives again when the file is loaded, such as a label taken from the name, and the deprecated default_filter (item 6).
  • Both modes leave out:
    • IDs;
    • state on every node, generic, attribute, relationship and dropdown choice, because a stored element is always present;
    • nodes and generics in restricted namespaces, and those the core schema defines, such as IpamNamespace;
    • inherited attributes and relationships on a node;
    • the six relationships schema processing adds (member_of_groups, subscriber_of_groups, parent, children, profiles, object_template), matched by kind, name and identifier, unless processing gives one of them other values than the stored ones, for example a parent the schema owner wrote with optional: true. Both modes then write it, so FULL also processes the schema.
  • Deprecated forms are converted: top-level regex, min_length and max_length move under parameters where the kind supports it, and NumberPool start_range and end_range become one ranges entry.
  • A namespace in namespaces that does not exist on the branch, or that is restricted, returns 404 with one error per namespace, before any work.
  • A schema load accepts and ignores a top-level infrahub_version field, which the export writes.

Implementation:

  • BRIEF does not use a list of derived fields. It loads a candidate into a copy of the core schema, runs schema processing as a load does, compares the result with the stored schema, and writes back each value that differs, until no value changes. When processing a candidate fails, the export returns the FULL document without default_filter and logs a warning.
  • The export runs in SchemaExportRunner (backend/infrahub/schema/export/runner.py), on a thread pool of its own with two threads per API worker (SCHEMA_EXPORT_MAX_CONCURRENT), so the worker answers other requests. Further requests wait for one of the two slots before the runner copies the branch schema, so a waiting request holds no copy; the admission middleware does not shed exports. A cancelled request keeps its thread until its export ends; a request cancelled while it waits never runs. The thread reads a copy of the branch schema with its own cache (SchemaBranch.duplicate_with_own_cache), so a schema refresh that purges the registry cache cannot remove a kind during an export.
  • The processed core schema is built once per process and copied for each pass.
  • The list of properties a node takes from its generic moved to a module constant in node_inheritance_handler.py, read by both the inheritance handler and the exporter. Behavior does not change.

What stayed the same: no database or migration change, no GraphQL change, and the existing schema endpoints are unchanged.

Suggested review order

  1. dev/knowledge/backend/schema-export.md: how the export works and why.
  2. backend/infrahub/schema/export/exporter.py, then backend/infrahub/api/schema.py.
  3. internal.py, tasks/backend.py, exceptions.py and api/exception_handlers.py.
  4. Tests: backend/tests/unit/schema/export/, backend/tests/functional/api/test_schema_export.py and the variation schemas in backend/tests/helpers/schema/export_variations.py. The functional tests export each variation schema and models/base/ in both modes, load the export into an empty instance, and compare the result with the source schema.

schema/openapi.json and frontend/app/src/shared/api/rest/types.generated.ts are generated.

How to review

Areas that need the most attention:

  • The reduction loop in SchemaExporter._reduce, and which errors send it to the FULL fallback.
  • GENERATED_RELATIONSHIPS is kept by hand. A unit test fails when processing adds a relationship of these kinds that the set does not hold.
  • Nodes and generics the core schema defines, such as IpamNamespace, are never exported. A change or an extension the schema owner made to them is left out with no warning. The user documentation lists this as a known limitation.

The implementation report, with the review findings, the decisions and the open items, is in opsmill-implement-report.md.

Impact & rollout

  • Backward compatibility: new endpoint, and the renamed OpenAPI components in item 5. A schema load now accepts infrahub_version and ignores it. An earlier release rejects that field, so an export loads only into the same release or a later one.
  • Performance: for models/base/, a BRIEF export took 1.6 s and a FULL export 0.55 s in the functional tests. While an export runs, it slows the other requests of the API worker by about 5 ms per await, because its thread holds the interpreter lock (measured by Aaron in the review). Each API worker runs at most two exports at the same time.
  • Config/env changes: none.
  • Deployment notes: the python_sdk pointer is on commit 4980fc50 of the SDK pull request branch, so CI runs against the SDK changes. Merge the SDK pull request first, then move the pointer to its merge commit before this pull request merges.

Checklist

🤖 Generated with Claude Code