concept

variants & variantOverrides

Two related, top-level RenderNode fields that exist so one page's worth of authored content can serve more than one interface-language "flavor" (Swift and Objective-C, today) without DocC writing out two entirely separate files for every symbol. Fullest single-kind treatment prior to this page: the data sheet on node-symbol.html, which lists both fields but doesn't explain the mechanism behind them.

1. How swift-docc-render consumes this

The two fields do genuinely different jobs, and it's easy to conflate them because they sound alike.

variants is a routing table, not a content mechanism. In DocumentationTopic.vue's extractProps(json), the array is reduced into a languagePaths map (keyed by interfaceLanguage) purely to pull out swiftPath/objcPath:

const languagePaths = variants.reduce((memo, variant) => (
  variant.traits.reduce((_memo, trait) => (!trait.interfaceLanguage ? _memo : ({
    ..._memo,
    [trait.interfaceLanguage]: (_memo[trait.interfaceLanguage] || []).concat(variant.paths),
  })), memo)
), {});
const {
  [Language.objectiveC.key.api]: [objcPath] = [],
  [Language.swift.key.api]: [swiftPath] = [],
} = languagePaths;

(DocumentationTopic.vue#L658-L667.) Those two paths are what powers the Swift/Objective-C picker in the page's language toggle — clicking it navigates the Vue Router to a different URL (objcPath), which triggers an entirely new route fetch. No content is transformed in place for this half of the mechanism.

variantOverrides is the content-transforming half, and it exists because Objective-C variants are usually mostly identical to the Swift original (same abstract, same topic groups, same See Also) with only a few fields actually differing (declaration syntax, title, identifier.interfaceLanguage). Rather than duplicate the whole RenderNode, DocC emits one small RFC 6902 JSON Patch per non-Swift trait and lets the consumer apply it on demand. swift-docc-render's own patch applier is a from-scratch reimplementation of the RFC 6902 op set in src/utils/json-patch.js (add/copy/move/remove/replace/test, lines 300-306) — a separate codebase from swift-docc's own Swift-side applier (Section 3).

The trigger is a computed property, objcOverrides, in src/views/DocumentationTopic.vue:

objcOverrides: ({ topicData }) => {
  const { variantOverrides = [] } = topicData || {};
  const isObjcTrait = ({ interfaceLanguage }) => (
    interfaceLanguage === Language.objectiveC.key.api
  );
  const hasObjcTrait = ({ traits }) => traits.some(isObjcTrait);
  const objcVariant = variantOverrides.find(hasObjcTrait);
  return objcVariant ? objcVariant.patch : null;
},

(views/DocumentationTopic.vue#L126-L136.) When the route's ?language=occ query parameter is present — either on initial load (beforeRouteEnter) or on an in-place language switch that doesn't change the URL path (beforeRouteUpdate) — applyObjcOverrides() runs:

applyObjcOverrides() {
  this.topicDataObjc = apply(clone(this.topicData), this.objcOverrides);
},

(views/DocumentationTopic.vue#L267-L269.) This clones the Swift-variant RenderNode already in memory and applies the Objective-C patch to it, client-side, with no additional network request — one fetched JSON file serves both language presentations. A topicData computed getter then prefers topicDataObjc when it's set, falling back to the original topicDataDefault (views/DocumentationTopic.vue#L136-L143).

2. Worked example: one article, two languages

Real fixture: Tests/SwiftDocCTests/Test Resources/Variant-render-node.json in swift-docc's own test suite (not in DocCArchive's fixtures — neither ExampleDocs nor SampleLibrary emits an Objective-C variant, since both are Swift-only sample projects). Trimmed to the two fields this page is about:

{
  "identifier": { "interfaceLanguage": "swift", "url": "doc://org.swift.docc/documentation/mykit/my-article" },
  "metadata": { "title": "My Article", "role": "article", ... },
  "variants": [
    { "paths": ["documentation/mykit/my-article"], "traits": [{ "interfaceLanguage": "objc" }] },
    { "paths": ["documentation/mykit/my-article"], "traits": [{ "interfaceLanguage": "swift" }] }
  ],
  "variantOverrides": [
    {
      "traits": [{ "interfaceLanguage": "objc" }],
      "patch": [
        { "op": "replace", "path": "/metadata/title", "value": "My Article in Objective-C" },
        { "op": "replace", "path": "/identifier/interfaceLanguage", "value": "objc" }
      ]
    }
  ]
}

Loaded plain (Swift, default): page title reads "My Article", identifier.interfaceLanguage is "swift".

Same JSON, with ?language=occ in the URL: applyObjcOverrides() runs the two replace operations above against the in-memory clone — no second file fetched — and the page title becomes "My Article in Objective-C" with identifier.interfaceLanguage now "objc". Everything else in the RenderNode (abstract, topic groups, references) is untouched, since the patch only names two pointers.

Both paths entries here happen to be identical (documentation/mykit/my-article) because this particular article has no separate Objective-C route — it's addressed by query parameter, not by URL path, which is why the language switch in Section 1 is described as "in-place" for this shape of content. A symbol with a genuinely different Objective-C URL (e.g. different casing or a category-style path) would instead show two different paths values per trait, and the switch would be a real Vue Router navigation to objcPath.

3. Data sheet

Source: Sources/DocCArchive/Vendored/RenderNode.spec.json. RenderNode.variants (line 2173) is an array of RenderNodeVariant (line 2187: required traits, required paths); RenderNode.variantOverrides (line 2179) is an array of VariantOverride (line 2215: required traits, required patch). Both traits fields are arrays of RenderNodeVariantTrait (line 2208), currently a oneOf with a single member, TraitInterfaceLanguage (line 2294: { "interfaceLanguage": string }) — the schema is explicitly future-proofed for more trait kinds (e.g. platform, availability) that don't exist on the wire yet.

VariantOverride.patch resolves to the JSONPatch schema (line 2233): an array whose items are one of three shapes by required-field set — {op: "add"|"test", path, value}, {op: "remove", path}, or {op: "move"|"copy", path, from}. Notice what's missing: there is no fourth shape for op: "replace", even though replace is by far the most common operation in practice — the real fixture above uses it twice, and swift-docc's own Swift-side PatchOperation enum (Sources/SwiftDocC/Model/Rendering/Variants/PatchOperation.swift) defines and encodes replace alongside add/remove. replace happens to need the same required fields (op, path, value) as the add/test branch, so a real {"op": "replace", ...} patch entry still validates against that branch's properties even though "replace" isn't listed in its op enum (["add", "test"]) — most JSON Schema validators treat an unlisted enum value as a hard failure, though, so this is a real, latent validation bug in the vendored spec, not merely a documentation gap. This is the same category of wire-schema/production-code divergence as the aside style enum discrepancy documented on block-aside.html — treat the schema's op enum as incomplete rather than authoritative for this one field.

On the swift-docc production side, applying a VariantOverride's patch is not something swift-docc-render's browser-side code shares with anyone — swift-docc ships its own, independent JSON Patch implementation for the same RFC, JSONPatchApplier (Sources/SwiftDocC/Model/Rendering/Variants/JSONPatchApplier.swift), wrapped by RenderNodeVariantOverridesApplier.applyVariantOverrides(in:for:). This Swift-side applier is used for exactly one purpose today: the docc process-archive index command, which rebuilds the LMDB navigator index (index/navigator.index/index/data.mdb — the files flagged as not-yet-parsed-by-DocCArchive on Archive Structure §1) from an already-built .doccarchive. Because an archive on disk stores the Objective-C content as an override patch rather than a fully-expanded variant, the indexer has to run the same kind of patch-and-decode round trip the browser does, just in Swift instead of JS (Sources/SwiftDocC/Indexing/Navigator/NavigatorIndex.swift, lines 679-695 — the source comment there calls this code path "peculiar and very inefficient" and notes it exists only to keep docc process-archive index working).

4. Where else this shows up

variants and/or variantOverrides appear on every top-level page-producing RenderNode kind — symbol, article, tutorial (project), and overview — since the language-variant mechanism operates at the whole-page level, not per content block. They also surface inside the top-level references map's individual reference objects (e.g. a download or image reference can itself carry variant-specific data), and inside linkable-entities.json records (see Archive Structure §6), which nest their own variants array for the same reason — a cross-archive link needs to resolve to the right language variant without opening the target archive.

5. Modern CSS-only alternative

Not applicable

This is JSON-Patch application and client-side routing, not visual/interactive DOM state — there is no CSS mechanism that could apply a JSON Patch to a data structure or drive a router navigation. The one sliver of genuinely stateful UI behavior downstream of this mechanism, the Swift/Objective-C toggle control itself, is a standard navigation control (it changes the URL) rather than a show/hide-without-navigation widget, so it doesn't fit the "replace JS-driven visibility state with :checked/:target" framing this site's other Modern CSS sections use either.