diff --git a/src/Types/StringSchema.php b/src/Types/StringSchema.php index fd77565..b37e146 100644 --- a/src/Types/StringSchema.php +++ b/src/Types/StringSchema.php @@ -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; } diff --git a/tests/Unit/Types/StringSchemaTest.php b/tests/Unit/Types/StringSchemaTest.php index ec380b1..50d3abd 100644 --- a/tests/Unit/Types/StringSchemaTest.php +++ b/tests/Unit/Types/StringSchemaTest.php @@ -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); @@ -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); +});