-
-
Notifications
You must be signed in to change notification settings - Fork 895
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 polymorphism support (based on discriminator) #6915
Labels
Comments
See also https://gist.github.com/vincentchalamon/456fb84af4ddf1281a63a0a06e633c60 +1 to reconsider, PR very welcome. |
made similar feature, but for <?php
namespace App\JsonSchema;
use ApiPlatform\JsonSchema\ResourceMetadataTrait;
use ApiPlatform\JsonSchema\Schema;
use ApiPlatform\JsonSchema\SchemaFactoryInterface;
use ApiPlatform\Metadata\Operation;
use Symfony\Component\DependencyInjection\Attribute\AsDecorator;
use Symfony\Component\DependencyInjection\Attribute\AutowireDecorated;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
#[AsDecorator(decorates: 'api_platform.json_schema.schema_factory')]
class DiscriminatorAwareSchemaFactory implements SchemaFactoryInterface
{
use ResourceMetadataTrait;
public function __construct(
#[AutowireDecorated]
private readonly SchemaFactoryInterface $decorated,
private readonly ClassMetadataFactoryInterface $serializerClassMetadataFactory,
) {
}
public function buildSchema(string $className, string $format = 'json', string $type = Schema::TYPE_OUTPUT, ?Operation $operation = null, ?Schema $schema = null, ?array $serializerContext = null, bool $forceCollection = false): Schema
{
if (
$type === Schema::TYPE_INPUT
&& $this->isResourceClass($className)
) {
$operation = $this->findOperation($className, $type, $operation, $serializerContext);
$inputOrOutputClass = $this->findOutputClass($className, $type, $operation, $serializerContext);
$serializerContext ??= $this->getSerializerContext($operation, $type);
if (null !== $inputOrOutputClass) {
$classMetadata = $this->serializerClassMetadataFactory->getMetadataFor($inputOrOutputClass);
$classDiscriminatorMapping = $classMetadata->getClassDiscriminatorMapping();
if (null !== $classDiscriminatorMapping) {
$definitions = $schema->getDefinitions();
$discriminatorSchemas = [];
foreach ($classDiscriminatorMapping->getTypesMapping() as $typePropertyValue => $typeClassName) {
$discriminatorSchema = $this->decorated->buildSchema($typeClassName, $format, $type, $operation, $schema, $serializerContext, $forceCollection);
$discriminatorPropertySchema = $definitions[$discriminatorSchema->getRootDefinitionKey()]['properties'][$classDiscriminatorMapping->getTypeProperty()];
unset($discriminatorPropertySchema['enum']);
$discriminatorPropertySchema['const'] = $typePropertyValue;
if (
is_array($discriminatorPropertySchema['type'])
&& false !== ($nullIndex = array_search('null', $discriminatorPropertySchema['type'], true))
) {
unset($discriminatorPropertySchema['type'][$nullIndex]);
if (1 === count($discriminatorPropertySchema['type'])) {
$discriminatorPropertySchema['type'] = reset($discriminatorPropertySchema['type']);
}
}
$discriminatorSchemas[$typePropertyValue] = $discriminatorSchema;
}
$originalSchema = $this->decorated->buildSchema($className, $format, $type, $operation, $schema, $serializerContext, $forceCollection);
$newDefinition = new \ArrayObject([
'oneOf' => array_values(array_map(static fn (Schema $discriminatorSchema) => ['$ref' => $discriminatorSchema['$ref']], $discriminatorSchemas)),
'discriminator' => [
'propertyName' => $classDiscriminatorMapping->getTypeProperty(),
'mapping' => array_map(static fn (Schema $discriminatorSchema) => $discriminatorSchema['$ref'], $discriminatorSchemas),
],
'required' => [
$classDiscriminatorMapping->getTypeProperty(),
],
'properties' => [
$classDiscriminatorMapping->getTypeProperty() => [
'type' => 'string',
'enum' => array_keys($discriminatorSchemas),
],
],
]);
$definitions[$originalSchema->getRootDefinitionKey()] = $newDefinition;
return $originalSchema;
}
}
}
return $this->decorated->buildSchema($className, $format, $type, $operation, $schema, $serializerContext, $forceCollection);
}
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Description
This was already closed in #2931, but I would like you to reconsider.
Since it's quite limiting not being able to use Doctrine's Inheritance mapping and OpenAPI has nowadays quite good polymorphism support thanks to
![image](https://private-user-images.githubusercontent.com/496233/402531578-6fe185cd-b55b-4aca-978a-f83e33e58229.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MzkzMzIwMjksIm5iZiI6MTczOTMzMTcyOSwicGF0aCI6Ii80OTYyMzMvNDAyNTMxNTc4LTZmZTE4NWNkLWI1NWItNGFjYS05NzhhLWY4M2UzM2U1ODIyOS5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjUwMjEyJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI1MDIxMlQwMzQyMDlaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT0zOTk3M2NlNmVhZTRkOWQwN2M0MWJlMTRjOWNhMWVmNGYwZTRlN2JmZDk5NWQyNDA4OTNjN2ZlMDBhODk0NTUxJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCJ9.SP1_zF4DGBRqUK8sHuToyRYNnUfKHQdxl6Ebu2M2lZQ)
discriminator
,oneOf
,anyOf
etc, I believe it makes sense to reconsider support of this. At beginning, I think most people would be fine if APIP at least allowed serialization/deserialization of properties in subclasses. Schema generator could be adjusted later to generate things like thisMain issue I would like to solve is that serializer does not see properties from subclasses. After further debugging I found that
PropertyNameCollectionFactory
andPropertyMetadataFactory
are responsible. However, these factories only accept$resourceClass
and not an actual class that Serializer is serializing/deserializing, hence it's not possible to simply add own implementation of these factories.Example
Here, I expect serialization will result in
or
Similarly, deserialization should accept (and write to) these properties
Further context
Currently, I have added support for this in application I'm maintaining by enabling
allow_extra_attributes
and implementing custom normalizer:Even OpenAPI schema is adjusted thanks to implementing own OpenApiFactory which generates definitions for subclasses, then linking to them from resource like so
The text was updated successfully, but these errors were encountered: