Schemas

Composition and polymorphism

Combine schemas with allOf, oneOf and anyOf, add a discriminator, and use generic envelopes.

Composition builds a schema from others: allOf extends, oneOf and anyOf choose. Combined with references, it keeps each shape declared once.

Extending a schema

Schema
use Ark4ne\OpenApiWriter\Objects\Schema;

Schema::create('Admin')->allOf(
    Schema::ref('User'),
    Schema::object()->properties(
        Schema::array('permissions')
            ->items(Schema::string()),
    ),
);
components.schemas.Admin
{
  "allOf": [
    { "$ref": "#/components/schemas/User" },
    {
      "type": "object",
      "properties": {
        "permissions": {
          "type": "array",
          "items": { "type": "string" }
        }
      }
    }
  ]
}

Polymorphism with a discriminator

oneOf lists the variants; the Discriminator names the property that tells them apart, and mapping() maps its values to schema names or references.

use Ark4ne\OpenApiWriter\Objects\Discriminator;

$payment = Schema::create('Payment')
    ->oneOf(
        Schema::ref('CardPayment'),
        Schema::ref('TransferPayment'),
    )
    ->discriminator(
        Discriminator::create()
            ->propertyName('method')
            ->mapping([
                'card' => '#/components/schemas/CardPayment',
                'transfer' => '#/components/schemas/TransferPayment',
            ]),
    );

$card = Schema::object('CardPayment')
    ->properties(
        Schema::string('method')->const('card'),
        Schema::string('last4')->pattern('^[0-9]{4}$'),
    )
    ->required('method', 'last4');

$transfer = Schema::object('TransferPayment')
    ->properties(
        Schema::string('method')->const('transfer'),
        Schema::string('iban'),
    )
    ->required('method', 'iban');

Each variant declares the discriminator property. The validator checks that every mapping resolves (LocalReferencesRule). Discriminator::defaultMapping() exists from OpenAPI 3.2.

A nullable reference

nullable() on a reference writes an anyOf with the null type:

Schema
Schema::ref('User', 'owner')->nullable();
3.1.2
{
  "anyOf": [
    { "$ref": "#/components/schemas/User" },
    { "type": "null" }
  ]
}

For 3.0, the second branch is {"enum": [null], "type": "object", "nullable": true}.

A generic envelope

A closure returning a schema gives a reusable envelope, here a page of items:

use Ark4ne\OpenApiWriter\Objects\MediaType;
use Ark4ne\OpenApiWriter\Objects\Response;

$paginated = static fn(string $item): Schema => Schema::create()->allOf(
    Schema::ref('Page'),
    Schema::object()
        ->properties(
            Schema::array('data')->items(Schema::ref($item)),
        )
        ->required('data'),
);

$books = Response::ok()->content(
    MediaType::json()->schema($paginated('Book')),
);

Conditions and the JSON Schema core

MethodKeyword
allOf(), anyOf(), oneOf() and their add methodscomposition
not(SchemaContract|bool|null)not
if(), then(), else()conditional schemas
defs(SchemaContract ...)$defs, keyed by objectId
schema(?string), id(?string)$schema, $id
anchor(?string), dynamicAnchor(?string), dynamicRef(?string)$anchor, $dynamicAnchor, $dynamicRef
contentSchema(?SchemaContract)schema of an encoded string's content

allOf, anyOf, oneOf and not exist in every version. The others exist from OpenAPI 3.1: written for 3.0, they throw an UnsupportedFeatureException in strict mode, or are dropped in lenient mode. See Strict and lenient.