No, unfortunately you can't set additionalProperties
globally. When writing the api schema manually you will have to add the option for every free-form object.
However, when creating the api schema via a framework you sometimes have the possibility to extend the swagger generator in a way where the additionalProperties
option is set for you. Here are examples for Laravel and asp.net:
config/l5-swagger
return [
// ...
'default' => [
// ...
'scanOptions' => [
// ...
'processors' => [
new \App\SwaggerProcessors\DisableAdditionalProperties(),
],
],
],
];
app/SwaggerProcessors/DisableAdditionalProperties.php
<?php
declare(strict_types=1);
namespace App\SwaggerProcessors;
use OpenApi\Analysis;
use OpenApi\Generator;
/**
* Adds "additionalProperties": false to each schema if it's not set
*/
class DisableAdditionalProperties
{
public function __invoke(Analysis $analysis)
{
if (is_object($analysis->openapi->components) && is_iterable($analysis->openapi->components->schemas)) {
foreach ($analysis->openapi->components->schemas as $key => &$schema) {
if ($schema->additionalProperties === Generator::UNDEFINED) {
$schema->additionalProperties = false;
}
}
}
}
}
schema.AdditionalPropertiesAllowed = true;