Skip to content
Merged
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
10 changes: 10 additions & 0 deletions bin/openapi
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ error_reporting(E_ALL);
// Possible options and their default values.
$options = [
'config' => [],
'defaults' => false,
'output' => false,
'format' => 'auto',
'exclude' => [],
Expand All @@ -35,6 +36,7 @@ $options = [
];
$aliases = [
'c' => 'config',
'D' => 'defaults',
'o' => 'output',
'e' => 'exclude',
'n' => 'pattern',
Expand Down Expand Up @@ -118,6 +120,13 @@ if (!$error && $options['bootstrap']) {
}
}
}

if ($options['defaults']) {
$logger->info('Default config');
$logger->info(json_encode((new Generator())->getDefaultConfig(), JSON_PRETTY_PRINT));
exit(1);
}

if (count($paths) === 0) {
$error = 'Specify at least one path.';
}
Expand All @@ -137,6 +146,7 @@ Usage: openapi [--option value] [/path/to/project ...]
Options:
--config (-c) Generator config.
ex: -c operationId.hash=false
--defaults (-D) Show default config.
--output (-o) Path to store the generated documentation.
ex: --output openapi.yaml
--exclude (-e) Exclude path(s).
Expand Down
32 changes: 17 additions & 15 deletions docs/guide/generating-openapi-documents.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,23 @@ For a list of all available options use the `-h` option
Usage: openapi [--option value] [/path/to/project ...]

Options:
--config (-c) Generator config
ex: -c operationId.hash=false
--output (-o) Path to store the generated documentation.
ex: --output openapi.yaml
--exclude (-e) Exclude path(s).
ex: --exclude vendor,library/Zend
--pattern (-n) Pattern of files to scan.
ex: --pattern "*.php" or --pattern "/\.(phps|php)$/"
--bootstrap (-b) Bootstrap a php file for defining constants, etc.
ex: --bootstrap config/constants.php
--processor (-p) Register an additional processor.
--format (-f) Force yaml or json.
--debug (-d) Show additional error information.
--version The OpenAPI version; defaults to 3.0.0.
--help (-h) Display this help message.
--config (-c) Generator config.
ex: -c operationId.hash=false
--defaults (-D) Show default config.
--output (-o) Path to store the generated documentation.
ex: --output openapi.yaml
--exclude (-e) Exclude path(s).
ex: --exclude vendor,library/Zend
--pattern (-n) Pattern of files to scan.
ex: --pattern "*.php" or --pattern "/\.(phps|php)$/"
--bootstrap (-b) Bootstrap php file(s) for defining constants, etc.
ex: --bootstrap config/constants.php
--add-processor (-a) Register an additional processor (allows multiple).
--remove-processor (-r) Remove an existing processor (allows multiple).
--format (-f) Force yaml or json.
--debug (-d) Show additional error information.
--version The OpenAPI version; defaults to 3.0.0.
--help (-h) Display this help message.
```

## Using PHP
Expand Down
25 changes: 23 additions & 2 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,27 @@ public function getDefaultConfig(): array
'generator' => [
'ignoreOtherAttributes' => false,
],
'mergeIntoOpenApi' => [
'mergeComponents' => false,
],
'expandEnums' => [
'enumNames' => null,
],
'augmentParameters' => [
'augmentOperationParameters' => true,
],
'pathFilter' => [
'tags' => [],
'paths' => [],
'recurseCleanup' => false,
],
'cleanUnusedComponents' => [
'enabled' => false,
],
'augmentTags' => [
'whitelist' => [],
'withDescription' => true,
],
'operationId' => [
'hash' => true,
],
Expand Down Expand Up @@ -309,8 +330,8 @@ public function setTypeResolver(?TypeResolverInterface $typeResolver): Generator
public function getTypeResolver(): TypeResolverInterface
{
$this->typeResolver ??= class_exists(StringTypeResolver::class)
? new TypeInfoTypeResolver()
: new LegacyTypeResolver();
? new TypeInfoTypeResolver()
: new LegacyTypeResolver();

return $this->typeResolver;
}
Expand Down
28 changes: 28 additions & 0 deletions tests/GeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,32 @@ public function testConfig(array $config, bool $expected): void
$generator->setConfig($config);
$this->assertOperationIdHash($generator, $expected);
}

public function testDefaultConfig(): void
{
$walker = function (callable $pipe) use (&$collectedConfig): void {
$rc = new \ReflectionClass($pipe);
$ctorparams = [];
foreach ($rc->getConstructor()?->getParameters() ?? [] as $rparam) {
$ctorparams[$rparam->getName()] = $rparam;
}

$processorKey = lcfirst($rc->getShortName());
foreach ($rc->getMethods() as $rm) {
if ($rm->isPublic() && str_starts_with($rm->getName(), 'set')) {
$name = lcfirst(substr($rm->getName(), 3));
if (array_key_exists($name, $ctorparams)) {
$collectedConfig[$processorKey][$name] = $ctorparams[$name]->getDefaultValue();
}
}
}
};

$collectedConfig = [];
$generator = new Generator();
$generator->getProcessorPipeline()->walk($walker);

// excludes generator config
$this->assertArrayIsEqualToArrayIgnoringListOfKeys($generator->getDefaultConfig(), $collectedConfig, ['generator']);
}
}
4 changes: 1 addition & 3 deletions tests/Processors/AugmentRefsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ public function testAugmentRefsForRequestBody(): void

$this->assertSame($analysis->openapi->paths[0]->post->requestBody->ref, 'OpenApi\Tests\Fixtures\Request');

$this->processorPipeline([
new AugmentRefs(),
])->process($analysis);
$this->processorPipeline([new AugmentRefs()])->process($analysis);

$this->assertSame($analysis->openapi->paths[0]->post->requestBody->ref, '#/components/requestBodies/Request');
}
Expand Down
4 changes: 1 addition & 3 deletions tests/Processors/DocBlockDescriptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ public function testDocBlockDescription(): void
{
$analysis = $this->analysisFromFixtures(
['UsingPhpDoc.php'],
$this->processorPipeline([
new DocBlockDescriptions(),
])
$this->processorPipeline([new DocBlockDescriptions()])
);

$operations = $analysis->getAnnotationsOfType(OA\Operation::class);
Expand Down
3 changes: 2 additions & 1 deletion tests/Processors/ExpandEnumsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ public function testEnumNamesInBackedStringEnum(): void
{
$analysis = $this->analysisFromFixtures(
['PHP/Enums/StatusEnumStringBacked.php'],
$this->processorPipeline([new ExpandEnums('enumNames')]),
$this->processorPipeline([new ExpandEnums()]),
config: ['expandEnums' => ['enumNames' => 'enumNames']],
);

$schema = $analysis->getAnnotationForSource(StatusEnumStringBacked::class);
Expand Down