-
Notifications
You must be signed in to change notification settings - Fork 4
Add support for new features in http dispatcher #374
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |
|
|
||
| namespace OpenAPIExtractor; | ||
|
|
||
| use PhpParser\Node; | ||
| use PhpParser\Node\Identifier; | ||
| use PhpParser\Node\IntersectionType; | ||
| use PhpParser\Node\Name; | ||
|
|
@@ -155,7 +156,7 @@ public static function resolve(string $context, array $definitions, ParamTagValu | |
| return $type; | ||
| } | ||
| if ($node instanceof Name) { | ||
| return self::resolveIdentifier($context, $definitions, $node->getLast()); | ||
| return self::resolveNativeEnum($context, $node) ?? self::resolveIdentifier($context, $definitions, $node->getLast()); | ||
| } | ||
| if ($node instanceof IdentifierTypeNode || $node instanceof Identifier) { | ||
| return self::resolveIdentifier($context, $definitions, $node->name); | ||
|
|
@@ -411,6 +412,53 @@ enum: [(int)$node->constExpr->value], | |
| Logger::panic($context, "Unable to resolve OpenAPI type:\n" . var_export($node, true) . "\nPlease open an issue at https://github.com/nextcloud/openapi-extractor/issues/new with the error message and a link to your source code."); | ||
| } | ||
|
|
||
| public static function resolveNativeEnum(string $context, ?Node $node): ?OpenApiType { | ||
| $nullable = false; | ||
| if ($node instanceof NullableType) { | ||
| $nullable = true; | ||
| $node = $node->type; | ||
| } | ||
| if (!$node instanceof Name) { | ||
| return null; | ||
| } | ||
|
|
||
| global $enumsByFqcn; | ||
| $fqcn = ltrim($node->toString(), '\\'); | ||
| if (!array_key_exists($fqcn, $enumsByFqcn)) { | ||
| return null; | ||
| } | ||
|
|
||
| $enum = $enumsByFqcn[$fqcn]; | ||
| return new OpenApiType( | ||
| context: $context, | ||
| type: $enum->type, | ||
| format: $enum->format, | ||
| description: $enum->description, | ||
| enum: $enum->enum, | ||
| nullable: $nullable, | ||
| ); | ||
| } | ||
|
|
||
| public static function isInjectedParameter(?Node $node): bool { | ||
| if ($node instanceof NullableType) { | ||
| $node = $node->type; | ||
| } | ||
| if (!$node instanceof Name) { | ||
| return false; | ||
| } | ||
| if (self::resolveNativeEnum('', $node) !== null) { | ||
| return false; | ||
| } | ||
| // Anything else resolveIdentifier recognizes (e.g. the built-in | ||
| // `SortDirection`) is a real, documentable type, not a service. | ||
| try { | ||
| self::resolveIdentifier('', [], $node->getLast()); | ||
| return false; | ||
| } catch (LoggerException) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 馃檲 |
||
| return true; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @param OpenApiType[] $types | ||
| * @return OpenApiType[] | ||
|
|
@@ -467,13 +515,15 @@ private static function resolveIdentifier(string $context, array $definitions, s | |
| 'mixed', 'empty', 'array' => new OpenApiType(context: $context, type: 'object'), | ||
| 'object', 'stdClass' => new OpenApiType(context: $context, type: 'object', additionalProperties: true), | ||
| 'null' => new OpenApiType(context: $context, nullable: true), | ||
| 'SortDirection' => new OpenApiType(context: $context, type: 'string', enum: ['ASC', 'DESC']), | ||
| default => (function () use ($context, $definitions, $name) { | ||
| if (array_key_exists($name, $definitions)) { | ||
| return new OpenApiType( | ||
| context: $context, | ||
| ref: '#/components/schemas/' . Helpers::cleanSchemaName($name), | ||
| ); | ||
| } | ||
|
|
||
| Logger::panic($context, "Unable to resolve OpenAPI type for identifier '" . $name . "'"); | ||
| })(), | ||
| }; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCA\Notifications; | ||
|
|
||
| /** | ||
| * The severity level of a notification | ||
| */ | ||
| enum NotificationLevel: string { | ||
| case Info = 'info'; | ||
| case Warning = 'warning'; | ||
| case Error = 'error'; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCA\Notifications; | ||
|
|
||
| /** | ||
| * A pure enum, which is not backed by a scalar value and therefore can not be used as an OpenAPI type. | ||
| * This file only exists to make sure the extractor does not choke on non-backed enums. | ||
| */ | ||
| enum NotificationUnbackedEnum { | ||
| case A; | ||
| case B; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not so happy with this implementation, because it is limited to the core app.
I had looked into resolving classes at some point and php-parser has a way to do this, that would allow using any enum from any namespace, by resolving it properly.