Skip to content

Capture extensions on the discriminator - #2

Merged
ChiragAgg5k merged 1 commit into
mainfrom
fix/discriminator-extensions
Aug 14, 2026
Merged

Capture extensions on the discriminator#2
ChiragAgg5k merged 1 commit into
mainfrom
fix/discriminator-extensions

Conversation

@ChiragAgg5k

Copy link
Copy Markdown
Member

CONTEXT.md states that an extension is "captured on every model that can carry one; never interpreted." Discriminator was the one model that did not follow it — the reader built it from propertyName and mapping and discarded every x- key sitting beside them, so an extension nested inside a discriminator was unreachable from the parsed Specification.

Why it matters

The standard mapping is single-valued: it maps one property value to one schema. A union whose members are distinguished by a combination of properties cannot be expressed with it at all.

Appwrite's spec hits this directly. Five of its attribute models share type: "string" and differ only by format, so mapping can name exactly one of them:

"discriminator": {
  "propertyName": "type",
  "mapping": {
    "string": "#/components/schemas/attributeString"   // only one schema can claim "string"
  },
  "x-mapping": {
    "#/components/schemas/attributeEmail":  { "type": "string", "format": "email" },
    "#/components/schemas/attributeEnum":   { "type": "string", "format": "enum"  },
    "#/components/schemas/attributeUrl":    { "type": "string", "format": "url"   },
    "#/components/schemas/attributeIp":     { "type": "string", "format": "ip"    },
    "#/components/schemas/attributeString": { "type": "string" }
  }
}

A consumer reading only mapping sees 6 of the 10 union members and silently collapses email, enum, url and ip into attributeString. With no way to reach x-mapping, there is no workaround on the consumer side.

The change

Discriminator gains an extensions array, defaulting to [], and the reader passes Value::extensions($value) — the helper it already uses for every other model. Nothing here interprets the extension; it is captured and handed to the consumer, which is the contract the rest of the library follows.

Compatibility

Additive. extensions is a new optional constructor argument in last position, so existing construction sites are unaffected, and the string form of the discriminator keeps defaulting to [].

Tests

Two added to tests/Schema/ReaderTest.php, both verified to fail without the src/ change and pass with it:

  • test_discriminator_captures_extensionsx-mapping and x-propertyNames survive alongside propertyName and mapping
  • test_discriminator_extensions_default_to_empty — the string form, and the object form with no extensions, both yield []

composer check (lint, pint, rector, phpunit) is green: 44 tests, 363 assertions.

CONTEXT.md states that an extension is "captured on every model that can
carry one". Discriminator was the exception: the reader built it from
propertyName and mapping and dropped every x- key alongside them, so an
extension nested inside a discriminator was unreachable from the parsed
Specification.

This matters because the standard mapping is single-valued — it maps one
property value to one schema. A union whose members are told apart by a
combination of properties cannot be expressed with it. Appwrite's spec
carries the full rule set in `x-mapping`:

    "discriminator": {
      "propertyName": "type",
      "mapping":   { "string": ".../attributeString" },
      "x-mapping": {
        ".../attributeEmail":  { "type": "string", "format": "email" },
        ".../attributeString": { "type": "string" }
      }
    }

Five of its attribute models share `type: "string"` and differ only by
`format`, so `mapping` can name just one of them and consumers reading
only `mapping` silently collapse the other four into it.

The reader already has Value::extensions(); this passes its result to the
constructor. Nothing here interprets the extension — it is captured and
handed to the consumer, which is the contract every other model follows.
@greptile-apps

greptile-apps Bot commented Aug 14, 2026

Copy link
Copy Markdown

Greptile Summary

The PR preserves x- extension fields found on discriminator objects without interpreting them.

  • Adds an optional extensions property to Discriminator.
  • Populates it through the shared Value::extensions() parser helper.
  • Tests extension preservation and empty defaults for string and object discriminator forms.

Confidence Score: 5/5

The PR appears safe to merge with no actionable defects identified.

The new constructor argument is optional and trailing, all discriminator construction paths remain compatible, and object-form extensions are captured consistently through the existing shared helper.

Important Files Changed

Filename Overview
src/Model/Schema/Discriminator.php Adds a backward-compatible trailing extensions constructor property with an empty-array default.
src/Parser/Schema/Reader.php Captures discriminator extension fields using the same helper used by other parsed models.
tests/Schema/ReaderTest.php Covers mixed extension values, preservation of standard discriminator fields, and empty defaults for both supported input forms.

Reviews (1): Last reviewed commit: "fix: capture extensions on the discrimin..." | Re-trigger Greptile

@ChiragAgg5k
ChiragAgg5k merged commit 9b1f038 into main Aug 14, 2026
5 checks passed
@ChiragAgg5k
ChiragAgg5k deleted the fix/discriminator-extensions branch August 14, 2026 04:27
ChiragAgg5k added a commit to appwrite/sdk-generator that referenced this pull request Aug 14, 2026
A discriminated union whose members are told apart by more than one
property cannot be expressed with the standard `mapping`, which maps a
single property value to a single schema. Five attribute models share
`type: "string"` and differ only by `format`, so `mapping` names just
one of them and the spec carries the real rule set in `x-mapping`.

Reading only `mapping` collapsed attributeEmail, attributeEnum,
attributeUrl and attributeIp into attributeString: getAttribute() and
getColumn() returned the wrong model, silently, with format dropped.
Ten cases became six. The dispatch is emitted into runtime request code
in 22 templates, so every SDK was affected, and nothing failed loudly.

Cases are now ordered most-specific-first, because the one-condition
`{type: string}` case would otherwise match an email before its own
two-condition case was ever tested.

`x-mapping` was unreachable until utopia-php/openapi#2, which captures
extensions on Discriminator the way every other model already does;
composer.lock moves to that merge.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant