Versions and output

Strict and lenient

Fail on what the target version cannot express, or drop it and read the diagnostics.

When a document uses a feature the target version cannot express, the library either throws (strict mode, the default) or drops the feature and reports it (lenient mode). Both list every problem at once, each with the JSON Pointer of its place in the document.

Strict mode

toJson(), toYaml() and toArray() are strict. One UnsupportedFeatureException lists every unsupported feature:

use Ark4ne\OpenApiWriter\Enums\Version;
use Ark4ne\OpenApiWriter\Exceptions\UnsupportedFeatureException;

try {
    $document->toJson(Version::V3_0_4);
} catch (UnsupportedFeatureException $exception) {
    echo $exception->getMessage();
}
3 features cannot be written for this version:
- /info/summary: summary needs 3.1.0
- /webhooks: webhooks needs 3.1.0
- /tags/1/parent: parent needs 3.2.0

$exception->diagnostics() returns the same list as diagnostics. Fix it by removing the features, targeting a newer version, or switching to the lenient mode.

Lenient mode

The Serializer with Strictness::Lenient writes what it can and reports the rest:

use Ark4ne\OpenApiWriter\Enums\Strictness;
use Ark4ne\OpenApiWriter\Serialization\Serializer;

$result = Serializer::for(Version::V3_0_4, Strictness::Lenient)
    ->serialize($document);

$json = $result->toJson(JSON_PRETTY_PRINT);

foreach ($result->diagnostics() as $diagnostic) {
    echo $diagnostic, "\n";
}
[dropped] /info/summary: summary needs 3.1.0
[dropped] /webhooks: webhooks needs 3.1.0
[converted] /components/schemas/Order/properties/note: nullable written as nullable: true
[dropped] /tags/1/parent: parent needs 3.2.0

Serializer::for() also takes the strings: Serializer::for('3.0.4', 'lenient').

Diagnostics

Diagnostics is iterable and countable:

MethodReturns
all()every Diagnostic
ofLevel(DiagnosticLevel $level)the diagnostics of one level
isEmpty()whether there is none

Each Diagnostic has three public properties, and casts to the one-line string shown above:

PropertyContent
levelDiagnosticLevel::Converted, Dropped or Unsupported
pointerwhere, as a JsonPointer: /components/schemas/Order/properties/note
messagewhat: nullable written as nullable: true

Converted is informative: the meaning is kept in another form. Dropped means the output lost something. Unsupported appears only in the exception of the strict mode.

use Ark4ne\OpenApiWriter\Serialization\DiagnosticLevel;

$lost = $result->diagnostics()->ofLevel(DiagnosticLevel::Dropped);

if ($lost !== []) {
    // log them, or fail the build
}

The validator is lenient

The validator writes the document in lenient mode: what the version cannot express is reported as an error instead of thrown. When there is any, the validator returns these errors alone, before checking the schema and the rules.