Skip to content

Commit

Permalink
feat: support configuring all params in alternative name models
Browse files Browse the repository at this point in the history
  • Loading branch information
herndlm committed Sep 27, 2024
1 parent 322c47b commit 72e6dc1
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,17 @@ public function getConfigTreeBuilder(): TreeBuilder
->thenInvalid('Model groups must be either `null` or an array.')
->end()
->end()
->variableNode('options')
->defaultValue(null)
->validate()
->ifTrue(function ($v) { return null !== $v && !is_array($v); })
->thenInvalid('Model options must be either `null` or an array.')
->end()
->end()
->arrayNode('serializationContext')
->defaultValue([])
->prototype('variable')->end()
->end()
->arrayNode('areas')
->defaultValue([])
->prototype('scalar')->end()
Expand Down
2 changes: 2 additions & 0 deletions src/DependencyInjection/NelmioApiDocExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,8 @@ private function findNameAliases(array $names, string $area): array
$aliases[$nameAlias['alias']] = [
'type' => $nameAlias['type'],
'groups' => $nameAlias['groups'],
'options' => $nameAlias['options'],
'serializationContext' => $nameAlias['serializationContext'],
];
}

Expand Down
2 changes: 1 addition & 1 deletion src/Model/ModelRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function __construct($modelDescribers, OA\OpenApi $api, array $alternativ
$this->api = $api;
$this->logger = new NullLogger();
foreach (array_reverse($alternativeNames) as $alternativeName => $criteria) {
$this->alternativeNames[] = $model = new Model(new Type('object', false, $criteria['type']), $criteria['groups']);
$this->alternativeNames[] = $model = new Model(new Type('object', false, $criteria['type']), $criteria['groups'], $criteria['options'], $criteria['serializationContext']);
$this->names[$model->getHash()] = $alternativeName;
$this->registeredModelNames[$alternativeName] = $model;
Util::getSchema($this->api, $alternativeName);
Expand Down
51 changes: 51 additions & 0 deletions tests/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,21 @@ public function testAlternativeNames(): void
'type' => 'App\Foo',
'groups' => ['group1', ['group2', 'parent' => 'child3']],
],
[
'alias' => 'Foo1',
'type' => 'App\Foo',
'options' => null,
],
[
'alias' => 'Foo1',
'type' => 'App\Foo',
'options' => ['foo' => 'bar'],
],
[
'alias' => 'Foo1',
'type' => 'App\Foo',
'serializationContext' => ['useJms' => false],
],
],
],
]]);
Expand All @@ -121,36 +136,72 @@ public function testAlternativeNames(): void
'alias' => 'Foo1',
'type' => 'App\Foo',
'groups' => ['group'],
'options' => null,
'serializationContext' => [],
'areas' => [],
],
[
'alias' => 'Foo2',
'type' => 'App\Foo',
'groups' => [],
'options' => null,
'serializationContext' => [],
'areas' => [],
],
[
'alias' => 'Foo3',
'type' => 'App\Foo',
'groups' => null,
'options' => null,
'serializationContext' => [],
'areas' => [],
],
[
'alias' => 'Foo4',
'type' => 'App\\Foo',
'groups' => ['group'],
'options' => null,
'serializationContext' => [],
'areas' => ['internal'],
],
[
'alias' => 'Foo1',
'type' => 'App\\Foo',
'groups' => null,
'options' => null,
'serializationContext' => [],
'areas' => ['internal'],
],
[
'alias' => 'Foo1',
'type' => 'App\Foo',
'groups' => ['group1', ['group2', 'parent' => 'child3']],
'options' => null,
'serializationContext' => [],
'areas' => [],
],
[
'alias' => 'Foo1',
'type' => 'App\Foo',
'groups' => null,
'options' => null,
'serializationContext' => [],
'areas' => [],
],
[
'alias' => 'Foo1',
'type' => 'App\Foo',
'groups' => null,
'options' => ['foo' => 'bar'],
'serializationContext' => [],
'areas' => [],
],
[
'alias' => 'Foo1',
'type' => 'App\Foo',
'groups' => null,
'options' => null,
'serializationContext' => ['useJms' => false],
'areas' => [],
],
], $config['models']['names']);
Expand Down
8 changes: 8 additions & 0 deletions tests/DependencyInjection/NelmioApiDocExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,14 @@ public function testNameAliasesArePassedToModelRegistry(): void
'Foo1' => [
'type' => 'App\\Foo',
'groups' => null,
'options' => null,
'serializationContext' => [],
],
'Test1' => [
'type' => 'App\\Test',
'groups' => null,
'options' => null,
'serializationContext' => [],
],
], $methodCall[1][0]);
$foundMethodCall = true;
Expand All @@ -75,10 +79,14 @@ public function testNameAliasesArePassedToModelRegistry(): void
'Foo1' => [
'type' => 'App\\Bar',
'groups' => null,
'options' => null,
'serializationContext' => [],
],
'Test1' => [
'type' => 'App\\Test',
'groups' => null,
'options' => null,
'serializationContext' => [],
],
], $methodCall[1][0]);
$foundMethodCall = true;
Expand Down
14 changes: 14 additions & 0 deletions tests/Model/ModelRegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public function testNameAliasingNotAppliedForCollections(): void
'Foo1' => [
'type' => self::class,
'groups' => ['group1'],
'options' => null,
'serializationContext' => [],
],
];
$registry = new ModelRegistry([], $this->createOpenApi(), $alternativeNames);
Expand Down Expand Up @@ -117,6 +119,8 @@ public function testNameCollisionsAreLoggedWithAlternativeNames(): void
$ref->getShortName() => [
'type' => $ref->getName(),
'groups' => ['group1'],
'options' => null,
'serializationContext' => [],
],
];
$logger = $this->createMock(LoggerInterface::class);
Expand Down Expand Up @@ -183,6 +187,8 @@ public static function getNameAlternatives(): \Generator
'Foo1' => [
'type' => self::class,
'groups' => ['group1'],
'options' => null,
'serializationContext' => [],
],
],
];
Expand All @@ -194,6 +200,8 @@ public static function getNameAlternatives(): \Generator
'Foo1' => [
'type' => self::class,
'groups' => ['group1'],
'options' => null,
'serializationContext' => [],
],
],
];
Expand All @@ -205,6 +213,8 @@ public static function getNameAlternatives(): \Generator
'Foo1' => [
'type' => self::class,
'groups' => ['group1', 'group2'],
'options' => null,
'serializationContext' => [],
],
],
];
Expand All @@ -216,6 +226,8 @@ public static function getNameAlternatives(): \Generator
'Foo1' => [
'type' => self::class,
'groups' => [],
'options' => null,
'serializationContext' => [],
],
],
];
Expand All @@ -227,6 +239,8 @@ public static function getNameAlternatives(): \Generator
'Foo1' => [
'type' => self::class,
'groups' => [],
'options' => null,
'serializationContext' => [],
],
],
];
Expand Down

0 comments on commit 72e6dc1

Please sign in to comment.