Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Types/StringSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,14 @@ protected function getContentFeatures(): array
{
$features = [];

if ($this->contentEncoding !== null) {
$features[] = SchemaFeature::ContentEncoding;
}

if ($this->contentMediaType !== null) {
$features[] = SchemaFeature::ContentMediaType;
}

if ($this->contentSchema !== null) {
$features[] = SchemaFeature::ContentSchema;
}
Expand Down
26 changes: 26 additions & 0 deletions tests/Unit/Types/StringSchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

namespace Cortex\JsonSchema\Tests\Unit\Types;

use ReflectionClass;
use Cortex\JsonSchema\Schema;
use Cortex\JsonSchema\Enums\SchemaFormat;
use Cortex\JsonSchema\Types\StringSchema;
use Cortex\JsonSchema\Enums\SchemaVersion;
use Cortex\JsonSchema\Enums\SchemaFeature;
use Cortex\JsonSchema\Exceptions\SchemaException;

covers(StringSchema::class);
Expand Down Expand Up @@ -275,3 +277,27 @@
'Feature "Schema for decoded content" is not supported in Draft 7. Minimum version required: Draft 2019-09.',
);
});

it('detects content features correctly', function (): void {
$stringSchema = Schema::string('payload', SchemaVersion::Draft_2019_09);
$stringSchema->contentEncoding('base64')
->contentMediaType('application/json')
->contentSchema(Schema::object());

$reflection = new ReflectionClass($stringSchema);
$contentFeaturesMethod = $reflection->getMethod('getContentFeatures');

$contentFeatures = $contentFeaturesMethod->invoke($stringSchema);

expect($contentFeatures)->toContain(SchemaFeature::ContentEncoding);
expect($contentFeatures)->toContain(SchemaFeature::ContentMediaType);
expect($contentFeatures)->toContain(SchemaFeature::ContentSchema);

// Test that features are included in overall feature detection
$getUsedMethod = $reflection->getMethod('getUsedFeatures');

$allFeatures = $getUsedMethod->invoke($stringSchema);
expect($allFeatures)->toContain(SchemaFeature::ContentEncoding);
expect($allFeatures)->toContain(SchemaFeature::ContentMediaType);
expect($allFeatures)->toContain(SchemaFeature::ContentSchema);
});
Loading