Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add specification property field's values restrictions based on validator setting #3329

Merged

Conversation

penja
Copy link
Contributor

@penja penja commented Dec 25, 2019

Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tickets fixes #3319
License MIT
Doc PR api-platform/docs#1015

Generate specification property restrictions based on Symfony’s built-in validator.

@penja penja changed the title Add specification property field's values restrictions based on validator setting Add specification property field's values restrictions based on validator setting (WIP) Dec 25, 2019
@penja penja force-pushed the property_shema_validation_restrictions branch from d912241 to 75d8123 Compare December 28, 2019 15:37
@penja
Copy link
Contributor Author

penja commented Dec 28, 2019

@dunglas @soyuka I've added implementation. Could you please review the PR? Thanks in advance

@penja penja changed the title Add specification property field's values restrictions based on validator setting (WIP) Add specification property field's values restrictions based on validator setting Dec 28, 2019
}
}
}

return $propertyMetadata->withIri($iri)->withRequired($required ?? false);
$propertyMetadata = $propertyMetadata->withIri($iri)->withRequired($required ?? false);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't break the above loop anymore, and it looks like we can have multiple constraints, hence different iris? I'm not sure that this case is handled / implemented correctly.

Copy link
Contributor Author

@penja penja Jan 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@soyuka Like in previous implementation we continue to set IRI and required only if it was not set before.

$schema = [];
}

$schema += array_merge(...$restrictions);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need this spread operator / array_merge?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes we must merge all restrictions into one schema

@penja penja requested a review from soyuka January 8, 2020 14:02
@soyuka soyuka requested a review from dunglas January 10, 2020 14:16
Copy link
Member

@dunglas dunglas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great addition! Thanks for working on this.

@@ -67,11 +68,19 @@ final class ValidatorPropertyMetadataFactory implements PropertyMetadataFactoryI

private $decorated;
private $validatorMetadataFactory;
/**
* @var iterable<PropertySchemaRestrictionMetadataInterface>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move it to the constructor please?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

public function __construct(ValidatorMetadataFactoryInterface $validatorMetadataFactory, PropertyMetadataFactoryInterface $decorated)
{
public function __construct(
ValidatorMetadataFactoryInterface $validatorMetadataFactory,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We put every parameters on the same line (same everywhere).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

public function __construct(Type $type = null, string $description = null, bool $readable = null, bool $writable = null, bool $readableLink = null, bool $writableLink = null, bool $required = null, bool $identifier = null, string $iri = null, $childInherited = null, array $attributes = null, SubresourceMetadata $subresource = null, bool $initializable = null, $default = null, $example = null)
{
/**
* @var array
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to remove (and should be array|null)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@penja
Copy link
Contributor Author

penja commented Jan 10, 2020

@dunglas @soyuka Thanks for the CR.

@penja penja requested a review from dunglas January 10, 2020 20:31
/**
* @param PropertySchemaRestrictionMetadataInterface[] $restrictionsMetadata
*/
public function __construct(ValidatorMetadataFactoryInterface $validatorMetadataFactory, PropertyMetadataFactoryInterface $decorated, iterable $restrictionsMetadata)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we default iterable $restrictionsMetadata to an array? I fear it may break some implementations if we leave this as-is.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@soyuka I've added default value

Copy link
Member

@soyuka soyuka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice thanks, may you rebase/sqash? I'd like to merge this.

@penja penja force-pushed the property_shema_validation_restrictions branch from 0a7461a to e9bdc58 Compare January 20, 2020 14:24
@penja
Copy link
Contributor Author

penja commented Jan 20, 2020

@soyuka Thanks, done.

@soyuka soyuka force-pushed the property_shema_validation_restrictions branch from e9bdc58 to 38d6955 Compare February 18, 2020 14:48
@soyuka soyuka merged commit dee4c09 into api-platform:master Feb 18, 2020
@soyuka
Copy link
Member

soyuka commented Feb 18, 2020

Thanks @penja!

@penja penja deleted the property_shema_validation_restrictions branch February 20, 2020 13:57
Comment on lines +47 to +55
case Type::BUILTIN_TYPE_INT:
case Type::BUILTIN_TYPE_FLOAT:
if (isset($constraint->min)) {
$restriction['minimum'] = (int) $constraint->min;
}

if (isset($constraint->max)) {
$restriction['maximum'] = (int) $constraint->max;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part looks wrong. Length is a string constraint. There seems to be confusion with Range, implemented in #4158

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's right. Could you provide a PR to fix it?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR: #4177

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PS: thanks @penja for implementing this feature!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@guilliamxavier thanks for fix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Result OpenAPI file doesn't contain field's values restrictions based on validator setting
5 participants