Skip to content

Commit

Permalink
fix: different context uses same model (#2183)
Browse files Browse the repository at this point in the history
* fix: different context uses same model

* style fix

* fix model registry test
  • Loading branch information
DjordyKoert authored Jan 6, 2024
1 parent 01b75ce commit 31da761
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Model/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function getSerializationContext(): array

public function getHash(): string
{
return md5(serialize([$this->type, $this->getGroups()]));
return md5(serialize([$this->type, $this->getSerializationContext()]));
}

/**
Expand Down
1 change: 1 addition & 0 deletions Model/ModelRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ private function modelToArray(Model $model): array
'type' => $getType($model->getType()),
'options' => $model->getOptions(),
'groups' => $model->getGroups(),
'serialization_context' => $model->getSerializationContext(),
];
}

Expand Down
7 changes: 7 additions & 0 deletions Tests/Functional/Controller/ApiController80.php
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,13 @@ public function serializedNameAction()
*
* @Model(type=EntityThroughNameConverter::class, serializationContext={"secret_name_converter_value"=true})
* )
*
* @OA\Response(
* response="201",
* description="Same class without context",
*
* @Model(type=EntityThroughNameConverter::class)
* )
*/
public function nameConverterContext()
{
Expand Down
11 changes: 10 additions & 1 deletion Tests/Functional/Controller/ApiController81.php
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,16 @@ public function serializedNameAction()
}

#[Route('/name_converter_context', methods: ['GET'])]
#[OA\Response(response: '200', description: '', content: new Model(type: EntityThroughNameConverter::class, serializationContext: ['secret_name_converter_value' => true]))]
#[OA\Response(
response: '200',
description: '',
content: new Model(type: EntityThroughNameConverter::class, serializationContext: ['secret_name_converter_value' => true])
)]
#[OA\Response(
response: '201',
description: 'Same class without context',
content: new Model(type: EntityThroughNameConverter::class)
)]
public function nameConverterContext()
{
}
Expand Down
31 changes: 30 additions & 1 deletion Tests/Functional/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -848,13 +848,42 @@ public function testEntityWithNullableSchemaSet()

public function testContextPassedToNameConverter()
{
$this->getOperation('/api/name_converter_context', 'get');
$operation = $this->getOperation('/api/name_converter_context', 'get');

$response = $this->getOperationResponse($operation, '200');
self::assertEquals([
'response' => '200',
'description' => '',
'content' => [
'application/json' => [
'schema' => ['$ref' => '#/components/schemas/EntityThroughNameConverter'],
],
],
], json_decode($response->toJson(), true));

$model = $this->getModel('EntityThroughNameConverter');
$this->assertCount(2, $model->properties);
$this->assertNotHasProperty('id', $model);
$this->assertHasProperty('name_converter_context_id', $model);
$this->assertNotHasProperty('name', $model);
$this->assertHasProperty('name_converter_context_name', $model);

$response = $this->getOperationResponse($operation, '201');
self::assertEquals([
'response' => '201',
'description' => 'Same class without context',
'content' => [
'application/json' => [
'schema' => ['$ref' => '#/components/schemas/EntityThroughNameConverter2'],
],
],
], json_decode($response->toJson(), true));

$model = $this->getModel('EntityThroughNameConverter2');
$this->assertCount(2, $model->properties);
$this->assertNotHasProperty('name_converter_context_id', $model);
$this->assertHasProperty('id', $model);
$this->assertNotHasProperty('name_converter_context_name', $model);
$this->assertHasProperty('name', $model);
}
}
11 changes: 10 additions & 1 deletion Tests/Model/ModelRegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,25 @@ public function testNameCollisionsAreLogged(Type $type, array $arrayType)
'type' => $arrayType,
'options' => null,
'groups' => ['group2'],
'serialization_context' => [
'groups' => ['group2'],
],
],
'taken_by' => [
'type' => $arrayType,
'options' => null,
'groups' => ['group1'],
'serialization_context' => [
'groups' => ['group1'],
'extra_context' => true,
],
],
]);

$registry = new ModelRegistry([], $this->createOpenApi(), []);
$registry->setLogger($logger);

$registry->register(new Model($type, ['group1']));
$registry->register(new Model($type, ['group1'], null, ['extra_context' => true]));
$registry->register(new Model($type, ['group2']));
}

Expand Down Expand Up @@ -128,6 +135,7 @@ public function testNameCollisionsAreLoggedWithAlternativeNames()
],
'options' => null,
'groups' => ['group2'],
'serialization_context' => ['groups' => ['group2']],
],
'taken_by' => [
'type' => [
Expand All @@ -140,6 +148,7 @@ public function testNameCollisionsAreLoggedWithAlternativeNames()
],
'options' => null,
'groups' => ['group1'],
'serialization_context' => ['groups' => ['group1']],
],
]);

Expand Down

0 comments on commit 31da761

Please sign in to comment.