Skip to content

Additional properties: false to allow better type checking from JSON schemas #3388

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* GraphQL: Allow to format GraphQL errors based on exceptions (#3063)
* GraphQL: Add page-based pagination (#3175)
* OpenAPI: Add PHP default values to the documentation (#2386)
* OpenAPI: Set additional properties to false on all definitions (#3388)

## 2.5.4

Expand Down
1 change: 1 addition & 0 deletions src/JsonSchema/SchemaFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ public function buildSchema(string $className, string $format = 'json', string $
}

$definition = new \ArrayObject(['type' => 'object']);
$definition['additionalProperties'] = 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 should set this flag only if the allow_extra_attributes normalization context option is set to false: #1217

$definitions[$definitionName] = $definition;
if (null !== $resourceMetadata && null !== $description = $resourceMetadata->getDescription()) {
$definition['description'] = $description;
Expand Down
1 change: 1 addition & 0 deletions tests/JsonSchema/SchemaFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,6 @@ public function testBuildSchemaForNonResourceClass(): void
$this->assertArrayHasKey('bar', $definitions[$rootDefinitionKey]['properties']);
$this->assertArrayHasKey('type', $definitions[$rootDefinitionKey]['properties']['bar']);
$this->assertSame('integer', $definitions[$rootDefinitionKey]['properties']['bar']['type']);
$this->assertFalse($definitions[$rootDefinitionKey]['additionalProperties']);
}
}
24 changes: 24 additions & 0 deletions tests/Swagger/Serializer/DocumentationNormalizerV2Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ private function doTestNormalize(OperationMethodResolverInterface $operationMeth
'description' => 'This is a \DateTimeInterface object.',
'format' => 'date-time',
]), ],
'additionalProperties' => false,
]),
]),
];
Expand Down Expand Up @@ -488,6 +489,7 @@ interface_exists(AdvancedNameConverterInterface::class)
'description' => 'This is a converted name.',
]),
],
'additionalProperties' => false,
]),
]),
'securityDefinitions' => [
Expand Down Expand Up @@ -596,6 +598,7 @@ public function testNormalizeWithApiKeysEnabled(): void
'description' => 'This is a name.',
]),
],
'additionalProperties' => false,
]),
]),
'securityDefinitions' => [
Expand Down Expand Up @@ -786,6 +789,7 @@ public function testNormalizeWithOnlyNormalizationGroups(): void
'description' => 'This is a name.',
]),
],
'additionalProperties' => false,
]),
$ref => new \ArrayObject([
'type' => 'object',
Expand All @@ -797,6 +801,7 @@ public function testNormalizeWithOnlyNormalizationGroups(): void
'description' => 'This is a gerard.',
]),
],
'additionalProperties' => false,
]),
]),
];
Expand Down Expand Up @@ -981,6 +986,7 @@ public function testNormalizeNotAddExtraBodyParameters(): void
'description' => 'This is a name.',
]),
],
'additionalProperties' => false,
]),
$ref => new \ArrayObject([
'type' => 'object',
Expand All @@ -992,6 +998,7 @@ public function testNormalizeNotAddExtraBodyParameters(): void
'description' => 'This is a gerard.',
]),
],
'additionalProperties' => false,
]),
]),
];
Expand Down Expand Up @@ -1081,6 +1088,7 @@ public function testNormalizeWithSwaggerDefinitionName(): void
'readOnly' => true,
]),
],
'additionalProperties' => false,
]),
]),
];
Expand Down Expand Up @@ -1250,6 +1258,7 @@ public function testNormalizeWithOnlyDenormalizationGroups(): void
'description' => 'This is a name.',
]),
],
'additionalProperties' => false,
]),
'Dummy-dummy' => new \ArrayObject([
'type' => 'object',
Expand All @@ -1261,6 +1270,7 @@ public function testNormalizeWithOnlyDenormalizationGroups(): void
'description' => 'This is a gerard.',
]),
],
'additionalProperties' => false,
]),
]),
];
Expand Down Expand Up @@ -1433,6 +1443,7 @@ public function testNormalizeWithNormalizationAndDenormalizationGroups(): void
'description' => 'This is a name.',
]),
],
'additionalProperties' => false,
]),
'Dummy-dummy' => new \ArrayObject([
'type' => 'object',
Expand All @@ -1444,6 +1455,7 @@ public function testNormalizeWithNormalizationAndDenormalizationGroups(): void
'description' => 'This is a gerard.',
]),
],
'additionalProperties' => false,
]),
]),
];
Expand Down Expand Up @@ -1625,6 +1637,7 @@ public function testNormalizeSkipsNotReadableAndNotWritableProperties(): void
'description' => 'This is a name.',
]),
],
'additionalProperties' => false,
]),
]),
];
Expand Down Expand Up @@ -2007,6 +2020,7 @@ public function testNormalizeWithNestedNormalizationGroups(): void
'description' => 'This is a name.',
]),
],
'additionalProperties' => false,
]),
$ref => new \ArrayObject([
'type' => 'object',
Expand All @@ -2022,6 +2036,7 @@ public function testNormalizeWithNestedNormalizationGroups(): void
'$ref' => '#/definitions/'.$relatedDummyRef,
]),
],
'additionalProperties' => false,
]),
$relatedDummyRef => new \ArrayObject([
'type' => 'object',
Expand All @@ -2033,6 +2048,7 @@ public function testNormalizeWithNestedNormalizationGroups(): void
'description' => 'This is a name.',
]),
],
'additionalProperties' => false,
]),
]),
];
Expand Down Expand Up @@ -2141,6 +2157,7 @@ private function normalizeWithFilters($filterLocator): void
'type' => 'string',
]),
],
'additionalProperties' => false,
]),
]),
];
Expand Down Expand Up @@ -2303,6 +2320,7 @@ private function doTestNormalizeWithSubResource(OperationAwareFormatsProviderInt
'items' => ['$ref' => '#/definitions/Answer'],
]),
],
'additionalProperties' => false,
]),
'Answer' => new \ArrayObject([
'type' => 'object',
Expand All @@ -2315,6 +2333,7 @@ private function doTestNormalizeWithSubResource(OperationAwareFormatsProviderInt
'items' => ['$ref' => '#/definitions/Answer'],
]),
],
'additionalProperties' => false,
]),
]),
];
Expand Down Expand Up @@ -2404,6 +2423,7 @@ public function testNormalizeWithPropertySwaggerContext(): void
'example' => 'one',
]),
],
'additionalProperties' => false,
]),
]),
];
Expand Down Expand Up @@ -2504,6 +2524,7 @@ public function testNormalizeWithPaginationClientEnabled(): void
'example' => 'one',
]),
],
'additionalProperties' => false,
]),
]),
];
Expand Down Expand Up @@ -2697,6 +2718,7 @@ private function doTestNormalizeWithCustomFormatsDefinedAtOperationLevel(Operati
'description' => 'This is a name.',
]),
],
'additionalProperties' => false,
]),
]),
];
Expand Down Expand Up @@ -2911,6 +2933,7 @@ private function doTestNormalizeWithInputAndOutputClass(): void
'type' => 'integer',
]),
],
'additionalProperties' => false,
]),
'Dummy:InputDto' => new \ArrayObject([
'type' => 'object',
Expand All @@ -2929,6 +2952,7 @@ private function doTestNormalizeWithInputAndOutputClass(): void
'type' => 'integer',
]),
],
'additionalProperties' => false,
]),
]),
];
Expand Down
21 changes: 21 additions & 0 deletions tests/Swagger/Serializer/DocumentationNormalizerV3Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ private function doTestNormalize(OperationMethodResolverInterface $operationMeth
'format' => 'date-time',
]),
],
'additionalProperties' => false,
]),
]),
],
Expand Down Expand Up @@ -549,6 +550,7 @@ private function doTestNormalizeWithNameConverter(bool $legacy = false): void
'description' => 'This is a converted name.',
]),
],
'additionalProperties' => false,
]),
]),
'securitySchemes' => [
Expand Down Expand Up @@ -677,6 +679,7 @@ public function testNormalizeWithApiKeysEnabled(): void
'description' => 'This is a name.',
]),
],
'additionalProperties' => false,
]),
]),
'securitySchemes' => [
Expand Down Expand Up @@ -907,6 +910,7 @@ public function testNormalizeWithOnlyNormalizationGroups(): void
'description' => 'This is a name.',
]),
],
'additionalProperties' => false,
]),
$ref => new \ArrayObject([
'type' => 'object',
Expand All @@ -918,6 +922,7 @@ public function testNormalizeWithOnlyNormalizationGroups(): void
'description' => 'This is a gerard.',
]),
],
'additionalProperties' => false,
]),
]),
],
Expand Down Expand Up @@ -1031,6 +1036,7 @@ public function testNormalizeWithOpenApiDefinitionName(): void
'readOnly' => true,
]),
],
'additionalProperties' => false,
]),
]),
],
Expand Down Expand Up @@ -1239,6 +1245,7 @@ public function testNormalizeWithOnlyDenormalizationGroups(): void
'description' => 'This is a name.',
]),
],
'additionalProperties' => false,
]),
'Dummy-dummy' => new \ArrayObject([
'type' => 'object',
Expand All @@ -1250,6 +1257,7 @@ public function testNormalizeWithOnlyDenormalizationGroups(): void
'description' => 'This is a gerard.',
]),
],
'additionalProperties' => false,
]),
]),
],
Expand Down Expand Up @@ -1460,6 +1468,7 @@ public function testNormalizeWithNormalizationAndDenormalizationGroups(): void
'description' => 'This is a name.',
]),
],
'additionalProperties' => false,
]),
'Dummy-dummy' => new \ArrayObject([
'type' => 'object',
Expand All @@ -1471,6 +1480,7 @@ public function testNormalizeWithNormalizationAndDenormalizationGroups(): void
'description' => 'This is a gerard.',
]),
],
'additionalProperties' => false,
]),
]),
],
Expand Down Expand Up @@ -1985,6 +1995,7 @@ public function testNormalizeWithNestedNormalizationGroups(): void
'description' => 'This is a name.',
]),
],
'additionalProperties' => false,
]),
$ref => new \ArrayObject([
'type' => 'object',
Expand All @@ -2000,6 +2011,7 @@ public function testNormalizeWithNestedNormalizationGroups(): void
'$ref' => '#/components/schemas/'.$relatedDummyRef,
]),
],
'additionalProperties' => false,
]),
$relatedDummyRef => new \ArrayObject([
'type' => 'object',
Expand All @@ -2011,6 +2023,7 @@ public function testNormalizeWithNestedNormalizationGroups(): void
'description' => 'This is a name.',
]),
],
'additionalProperties' => false,
]),
]),
],
Expand Down Expand Up @@ -2155,6 +2168,7 @@ private function normalizeWithFilters($filterLocator): void
'type' => 'string',
]),
],
'additionalProperties' => false,
]),
]),
],
Expand Down Expand Up @@ -2330,6 +2344,7 @@ private function doTestNormalizeWithSubResource(OperationAwareFormatsProviderInt
'items' => ['$ref' => '#/components/schemas/Answer'],
]),
],
'additionalProperties' => false,
]),
'Answer' => new \ArrayObject([
'type' => 'object',
Expand All @@ -2342,6 +2357,7 @@ private function doTestNormalizeWithSubResource(OperationAwareFormatsProviderInt
'items' => ['$ref' => '#/components/schemas/Answer'],
]),
],
'additionalProperties' => false,
]),
]),
],
Expand Down Expand Up @@ -2456,6 +2472,7 @@ public function testNormalizeWithPropertyOpenApiContext(): void
'example' => 'one',
]),
],
'additionalProperties' => false,
]),
]),
],
Expand Down Expand Up @@ -2583,6 +2600,7 @@ public function testNormalizeWithPaginationClientEnabled(): void
'example' => 'one',
]),
],
'additionalProperties' => false,
]),
]),
],
Expand Down Expand Up @@ -2716,6 +2734,7 @@ public function testNormalizeWithPaginationCustomDefaultAndMaxItemsPerPage(): vo
'example' => 'one',
]),
],
'additionalProperties' => false,
]),
]),
],
Expand Down Expand Up @@ -2853,6 +2872,7 @@ public function testLegacyNormalizeWithPaginationCustomDefaultAndMaxItemsPerPage
'example' => 'one',
]),
],
'additionalProperties' => false,
]),
]),
],
Expand Down Expand Up @@ -3089,6 +3109,7 @@ private function doNormalizeWithCustomFormatsDefinedAtOperationLevel(OperationAw
'description' => 'This is a name.',
]),
],
'additionalProperties' => false,
]),
]),
],
Expand Down