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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ api_platform_extras:
enabled: false
#Mark schema properties as required by default when the type is not nullable.
default_required_properties: false
#Additionally mark nullable types as required - safe to use if api_platform.defaults.normalization_context.skip_null_values set to false (default true).
nullable_required: false
#Add @id as an optional property to all POST, PUT and PATCH schemas.
jsonld_update_schema: false
# NOT IMPLEMENTED YET
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ final class PropertyMetadataFactoryDecorator implements PropertyMetadataFactoryI
{
public function __construct(
private PropertyMetadataFactoryInterface $decorated,
private bool $nullableRequired,
) {}

public function create(string $resourceClass, string $property, array $options = []): ApiProperty
Expand All @@ -23,8 +24,10 @@ public function create(string $resourceClass, string $property, array $options =

if (
($options['schema_type'] ?? null) === Schema::TYPE_OUTPUT

&& $type !== null && $type::class !== NullableType::class
&& (
($type !== null && $this->nullableRequired)
|| ($type !== null && $type::class !== NullableType::class)
)
) {
return $propertyMetadata->withRequired(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public function process(ContainerBuilder $container): void
->setDefinition('netgen.api_platform_extras.metadata.property.metadata_factory', new Definition(PropertyMetadataFactoryDecorator::class))
->setArguments([
new Reference('netgen.api_platform_extras.metadata.property.metadata_factory.inner'),
$container->getParameter(sprintf('%s.nullable_required', self::BASE_FEATURE_PATH)),
])
->setDecoratedService('api_platform.metadata.property.metadata_factory', null, 19);
}
Expand Down
6 changes: 5 additions & 1 deletion src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ public function getConfigTreeBuilder(): TreeBuilder
->children()
->booleanNode('default_required_properties')
->defaultFalse()
->info('Mark schema properties as required by default when type is not nullable.')
->info('Mark schema properties as required by default when type is not nullable - Only output schemas.')
->end()
->booleanNode('nullable_required')
->defaultFalse()
->info('Additionally mark nullable types as required - safe to use if api_platform.defaults.normalization_context.skip_null_values set to false (default true).')
->end()
->booleanNode('jsonld_update_schema')
->defaultFalse()
Expand Down