feat: add the schema export endpoint [IFC-3670]
(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
BRIEFexport fell back toFULL. 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/schemapaths.
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.
- New REST endpoint
GET /api/schema/export. Parameters:branch,namespaces(repeatable) andmode(BRIEFby default, orFULL).schema/openapi.jsonand the frontend REST types are regenerated. keep_in_exportmark on seven field definitions inbackend/infrahub/core/schema/definitions/internal.py:inherit_fromon nodes;read_only,choicesanduniqueon attributes;identifier,cardinalityandoptionalon relationships. Without these fields aBRIEFcandidate fails validation. The mark is not stored in the database.uv run invoke backend.generatewrites the marked fields intoKEEP_IN_EXPORT_FIELDSin the SDK's generatedcontract.py.- New
extensions.namespacefield in REST error responses.Error.error_extensionsadds fields to theextensionsof each error. Only the new 404 for a namespace uses it. Clients need it because an unknown/apiURL also returns a 404 in the same format. BRIEFcan write a derived value next to the value it comes from. Example: an attribute loaded withdefault_valueand nooptionalis stored withoptional: true, andBRIEFwrites 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.- Renamed OpenAPI components. In
schema/openapi.json, seven write components, such asNodeSchemaWrite, are each split into an-Inputand an-Outputcomponent, 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.mdlists all seven. BRIEFdoes not write the deprecateddefault_filter. OnlyFULLwrites it. A target loaded from aBRIEFexport therefore has nodefault_filter, except on a node that takes it again from a generic Infrahub defines, such asIpamIPPrefixfromBuiltinIPPrefix. Baptiste and Patrick decided this in the review on 2026-12-01: schema processing sets and changesdefault_filter, for example when a node takes it from its generic, so the stored value is not always one the schema owner wrote. The SDKgetby a value that is not an ID, plain-string peers of a generic in object files, and the check of repository object files still readdefault_filter; the user documentation lists these cases.
What changed
Behavior:
GET /api/schema/exportreturns the user-defined nodes and generics of a branch as a schema file.FULLwrites every value a schema file can hold.BRIEFleaves out every value Infrahub derives again when the file is loaded, such as alabeltaken from the name, and the deprecateddefault_filter(item 6).
- Both modes leave out:
- IDs;
stateon every node, generic, attribute, relationship and dropdown choice, because a stored element is alwayspresent;- 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 aparentthe schema owner wrote withoptional: true. Both modes then write it, soFULLalso processes the schema.
- Deprecated forms are converted: top-level
regex,min_lengthandmax_lengthmove underparameterswhere the kind supports it, and NumberPoolstart_rangeandend_rangebecome onerangesentry. - A namespace in
namespacesthat 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_versionfield, which the export writes.
Implementation:
BRIEFdoes 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 theFULLdocument withoutdefault_filterand 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
dev/knowledge/backend/schema-export.md: how the export works and why.backend/infrahub/schema/export/exporter.py, thenbackend/infrahub/api/schema.py.internal.py,tasks/backend.py,exceptions.pyandapi/exception_handlers.py.- Tests:
backend/tests/unit/schema/export/,backend/tests/functional/api/test_schema_export.pyand the variation schemas inbackend/tests/helpers/schema/export_variations.py. The functional tests export each variation schema andmodels/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 theFULLfallback. GENERATED_RELATIONSHIPSis 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_versionand 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/, aBRIEFexport took 1.6 s and aFULLexport 0.55 s in the functional tests. While an export runs, it slows the other requests of the API worker by about 5 ms perawait, 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_sdkpointer is on commit4980fc50of 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
- Tests added/updated
- Changelog entry added (
uv run towncrier create ...) - External docs updated:
docs/docs/schema/export.mdx - Internal .md docs updated:
dev/knowledge/backend/schema-export.md - I have reviewed AI generated content
🤖 Generated with Claude Code