Skip to content

Commit b3b618b

Browse files
authored
Merge pull request #8 from netgen/NGSTACK-1017-schema-decoration-nullable-required-properties
Ngstack 1017 schema decoration nullable required properties
2 parents 69ab8ff + 93c72b7 commit b3b618b

4 files changed

Lines changed: 13 additions & 3 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ api_platform_extras:
1212
enabled: false
1313
#Mark schema properties as required by default when the type is not nullable.
1414
default_required_properties: false
15+
#Additionally mark nullable types as required - safe to use if api_platform.defaults.normalization_context.skip_null_values set to false (default true).
16+
nullable_required: false
1517
#Add @id as an optional property to all POST, PUT and PATCH schemas.
1618
jsonld_update_schema: false
1719
# NOT IMPLEMENTED YET

src/ApiPlatform/JsonSchema/Metadata/Property/PropertyMetadataFactoryDecorator.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ final class PropertyMetadataFactoryDecorator implements PropertyMetadataFactoryI
1313
{
1414
public function __construct(
1515
private PropertyMetadataFactoryInterface $decorated,
16+
private bool $nullableRequired,
1617
) {}
1718

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

2425
if (
2526
($options['schema_type'] ?? null) === Schema::TYPE_OUTPUT
26-
27-
&& $type !== null && $type::class !== NullableType::class
27+
&& (
28+
($type !== null && $this->nullableRequired)
29+
|| ($type !== null && $type::class !== NullableType::class)
30+
)
2831
) {
2932
return $propertyMetadata->withRequired(true);
3033
}

src/DependencyInjection/CompilerPass/SchemaDecorationCompilerPass.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public function process(ContainerBuilder $container): void
4949
->setDefinition('netgen.api_platform_extras.metadata.property.metadata_factory', new Definition(PropertyMetadataFactoryDecorator::class))
5050
->setArguments([
5151
new Reference('netgen.api_platform_extras.metadata.property.metadata_factory.inner'),
52+
$container->getParameter(sprintf('%s.nullable_required', self::BASE_FEATURE_PATH)),
5253
])
5354
->setDecoratedService('api_platform.metadata.property.metadata_factory', null, 19);
5455
}

src/DependencyInjection/Configuration.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ public function getConfigTreeBuilder(): TreeBuilder
2929
->children()
3030
->booleanNode('default_required_properties')
3131
->defaultFalse()
32-
->info('Mark schema properties as required by default when type is not nullable.')
32+
->info('Mark schema properties as required by default when type is not nullable - Only output schemas.')
33+
->end()
34+
->booleanNode('nullable_required')
35+
->defaultFalse()
36+
->info('Additionally mark nullable types as required - safe to use if api_platform.defaults.normalization_context.skip_null_values set to false (default true).')
3337
->end()
3438
->booleanNode('jsonld_update_schema')
3539
->defaultFalse()

0 commit comments

Comments
 (0)