Repository navigation
refactor: align SwaggerService/SwaggerSpecValidator with open api 3.0 - #203
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 3b46fc8bf8
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| $invalidValues = array_diff(Arr::wrap($data[$field]), $allowedValues); | ||
| $fields = Arr::wrap($data[$field]); | ||
|
|
||
| if (is_multidimensional($fields)) { |
There was a problem hiding this comment.
Avoid dev-only helper in runtime validator
When this package is installed without dev dependencies, ronasit/laravel-helpers is not installed because it is listed only under require-dev, so validating any document with a present optional value such as an operation schemes, parameter in, or security scheme flows will call an undefined global is_multidimensional() and fail with a fatal error instead of producing docs. Please replace this with local logic or move the helper package to runtime dependencies.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: a6f8419b68
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| 'security_definition_type' => ['basic', 'apiKey', 'oauth2'], | ||
| 'security_schemes_flows' => ['implicit', 'password', 'clientCredentials', 'authorizationCode'], | ||
| 'security_schemes_in' => ['query', 'header', 'cookie'], | ||
| 'security_schemes_type' => ['apiKey', 'http', 'mutualTLS', 'oauth2', 'openIdConnect'], |
There was a problem hiding this comment.
Only allow security types the generator can emit
Adding http, mutualTLS, oauth2, and openIdConnect here makes those OpenAPI 3 security schemes pass validation, but SwaggerService::generateSecuritySchemesObject() still emits only type, name, and in. When a project configures a newly allowed scheme such as HTTP bearer (type: http, scheme: bearer), the generated components.securitySchemes omits the required scheme field and tries to read nonexistent name/in config keys, so the package produces invalid docs or errors instead of usable authentication metadata.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
@DenTray the issue i am working on does not cover this case, but should we update generateSecuritySchemesObject method to support other security scheme types ?
'apiKey' => [
"type" => "apiKey", // required for all types
"in" => "header", // required for type apiKey
"name" => "X-API-Key", // required for type apiKey
"description" => "some description" // optional for any type
],
'httpBasic' => [
"type" => "http",
"scheme" => "basic", // required for type http
],
'bearerAuth' => [
'type' => 'http',
'scheme' => 'bearer',
'bearerFormat' => 'JWT', // optional for http bearer
],
'oauth' => [
'type' => 'oauth2',
'flows' => [ // required for type oauth2
'authorizationCode' => [
'authorizationUrl' => 'https://auth.example.com/authorize',
'tokenUrl' => 'https://auth.example.com/token',
'scopes' => [
'read' => 'read',
'write' => 'write',
],
],
]
],
'openId' => [
'type' => 'openIdConnect',
'openIdConnectUrl' => 'https://auth.example.com/openIdConnect', // required for type openIdConnect
],ref: https://swagger.io/specification/#security-scheme-object
There was a problem hiding this comment.
@neellii I believe it makes sense.
It looks like security_drivers in the auto-doc.php config hasn't been updated to the new format, and it's used in the generateSecuritySchemesObject method.
Now jwt is a proper OAS 3.0 HTTP bearer scheme, and laravel uses the actual Laravel session cookie name.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 8c180f0905
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| if (!array_is_list($fields)) { | ||
| $fields = array_keys($fields); |
There was a problem hiding this comment.
Restrict associative-key validation to flows
When a validated field is an associative array, this now validates the array keys for every field, not just OAuth flows. In any generated or additional document where a scalar/list field is accidentally an object, e.g. a schema type of { "object": true } or a parameter in of { "query": true }, the key matches an allowed value and validation passes even though the OpenAPI field value is invalid, so getDocFileContent() can serve malformed specs instead of reporting the bad field.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 7efe9670ad
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| if (!empty($securityDefinitions)) { | ||
| $data['securityDefinitions'] = $securityDefinitions; | ||
| if (!empty($securitySchemes)) { | ||
| $data['components']['securitySchemes'] = $securitySchemes; |
There was a problem hiding this comment.
Migrate non-empty docs to securitySchemes
When $this->data is non-empty (for example an existing temp/production doc from before this change, or the still-present fixtures used by add-data tests), this new OAS3 location is never applied and the old root securityDefinitions is left in place. addData() then re-saves and serves a 3.1 document whose auth schemes are ignored by OpenAPI 3 tooling, so existing users can lose documented auth after upgrading unless the process data is normalized/migrated, not only freshly generated empty docs.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
This is a separate architectural concern (temporary file reliability in crash scenarios) and is outside the scope of this security-related change.
| 'security_definition_type' => ['basic', 'apiKey', 'oauth2'], | ||
| 'security_schemes_flows' => ['implicit', 'password', 'clientCredentials', 'authorizationCode'], | ||
| 'security_schemes_in' => ['query', 'header', 'cookie'], | ||
| 'security_schemes_type' => ['apiKey', 'http', 'mutualTLS', 'oauth2', 'openIdConnect'], |
There was a problem hiding this comment.
@neellii I believe it makes sense.
It looks like security_drivers in the auto-doc.php config hasn't been updated to the new format, and it's used in the generateSecuritySchemesObject method.
Now jwt is a proper OAS 3.0 HTTP bearer scheme, and laravel uses the actual Laravel session cookie name.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: f02391de02
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| $this->validateFieldValue($securityDefinition, 'flow', self::ALLOWED_VALUES['security_definition_flow'], $parentId); | ||
| $this->validateFieldValue($securityScheme, 'type', self::ALLOWED_VALUES['security_schemes_type'], $schemeId); | ||
| $this->validateFieldValue($securityScheme, 'in', self::ALLOWED_VALUES['security_schemes_in'], $schemeId); | ||
| $this->validateFieldValue($securityScheme, 'flows', self::ALLOWED_VALUES['security_schemes_flows'], $schemeId, true); |
There was a problem hiding this comment.
Validate OAuth flow objects before accepting them
When an additional or persisted document defines an OAuth2 security scheme with an allowed flow key but omits that flow's required fields, e.g. flows.authorizationCode without authorizationUrl, tokenUrl, or scopes, this only checks the flow name and validation succeeds. getDocFileContent() can then serve a malformed OpenAPI 3 document instead of reporting the invalid security scheme, so incomplete OAuth configs are silently accepted.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
This probably isn't worth addressing in this MR, since these values are not used directly by SwaggerService.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 7f76954029
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| } | ||
|
|
||
| return !empty($securityToken); | ||
| return $this->request->hasHeader('authorization'); |
There was a problem hiding this comment.
Guard the authorization fallback when security is disabled
When auto-doc.security is left empty (the default), $securityDriver is null, so this new non-apiKey fallback treats any request with an Authorization header as secured and addSecurityToOperation() writes an empty scheme name ("" => []) into the operation. Projects that intentionally disable security but still send auth headers during tests will get malformed/misleading OpenAPI output referencing a non-existent security scheme; return false before this header check when no security driver is configured.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
It looks like already fixed issue :
return !empty($security) && $this->request->hasHeader('authorization');| $this->validateFieldValue($securityDefinition, 'in', self::ALLOWED_VALUES['security_definition_in'], $parentId); | ||
| $this->validateFieldValue($securityDefinition, 'flow', self::ALLOWED_VALUES['security_definition_flow'], $parentId); | ||
| $this->validateFieldValue($securityScheme, 'type', self::ALLOWED_VALUES['security_schemes_type'], $schemeId); | ||
| $this->validateFieldsPresent($securityScheme, self::REQUIRED_FIELDS['securitySchemesByType'][$securityScheme['type']], $schemeId); |
There was a problem hiding this comment.
Add required-field handling for mutualTLS
When a valid OAS 3 security scheme uses type: mutualTLS, the new allowed-types list accepts it, but this lookup has no corresponding securitySchemesByType['mutualTLS'] entry, so validation raises an undefined-key/type error and getDocFileContent() returns an error document instead of accepting the spec. Add an empty required-fields entry or skip the type-specific lookup for schemes with no additional required fields; the fresh evidence is that mutualTLS is allowed while this map still omits it.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 1e92a3cf94
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: ead36f086c
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
…tants in SwaggerSpecValidator refs: #201
refs: #201
| 'laravel_session' => [ | ||
| 'type' => 'apiKey', | ||
| 'name' => '__ym_uid', | ||
| 'name' => 'laravel_session', |
There was a problem hiding this comment.
@DenTray
It looks like previously, for some reason, a hardcoded Yandex Metrics value was used for the Laravel session here.
You can either use something more realistic, like:
'name' => env('SESSION_COOKIE', Str::slug((string) env('APP_NAME', 'laravel'), '_') . '_session'),or just keep laravel_session as it is now.
| $securityDriver = Arr::get($this->config, "security_drivers.{$security}"); | ||
|
|
||
| return match ($securityDriver['type']) { | ||
| 'apiKey' => !empty(match ($securityDriver['in']) { |
There was a problem hiding this comment.
| 'apiKey' => !empty(match ($securityDriver['in']) { | |
| 'apiKey' => (!empty(match ($securityDriver['in'])) { |
| return match ($securityDriver['type']) { | ||
| 'apiKey' => !empty(match ($securityDriver['in']) { | ||
| 'header' => $this->request->header($securityDriver['name']), | ||
| 'query' => $this->request->query($securityDriver['name']), | ||
| 'cookie' => $this->request->cookie($securityDriver['name']), | ||
| default => null, | ||
| }), |
There was a problem hiding this comment.
Missing keys in a driver config crash the generation with a raw PHP warning instead of a config exception. Verified locally: a driver without type throws ErrorException: Undefined array key "type", and type: apiKey without in throws Undefined array key "in". initConfig() only checks that the driver key exists, not its shape.
| return match ($securityDriver['type']) { | |
| 'apiKey' => !empty(match ($securityDriver['in']) { | |
| 'header' => $this->request->header($securityDriver['name']), | |
| 'query' => $this->request->query($securityDriver['name']), | |
| 'cookie' => $this->request->cookie($securityDriver['name']), | |
| default => null, | |
| }), | |
| return match (Arr::get($securityDriver, 'type')) { | |
| 'apiKey' => !empty(match (Arr::get($securityDriver, 'in')) { | |
| 'header' => $this->request->header($securityDriver['name']), | |
| 'query' => $this->request->query($securityDriver['name']), | |
| 'cookie' => $this->request->cookie($securityDriver['name']), | |
| default => null, | |
| }), |
| default => null, | ||
| }), | ||
| 'mutualTLS' => false, | ||
| default => $this->request->hasHeader('authorization'), |
There was a problem hiding this comment.
hasHeader() returns true for an empty Authorization: header, so an operation gets marked as secured without a token. Verified locally. The apiKey branch above uses !empty(), let's keep it consistent.
| default => $this->request->hasHeader('authorization'), | |
| default => !empty($this->request->header('authorization')), |
| @@ -770,26 +761,23 @@ protected function getSummary($request, array $annotations) | |||
| protected function requestSupportAuth(): bool | |||
There was a problem hiding this comment.
requestSupportAuth() is the core of this change, but it has no test coverage at all: no test passes a request carrying an auth token (generateGetRolesRequest() only sets Content-type), so security is [] in every fixture and both saveSecurity() and addSecurityToOperation() are never exercised. Please add a data-provider driven test covering apiKey+header, apiKey+query, apiKey+cookie, http (bearer) and mutualTLS, asserting the operation-level security block. getBaseRequest() in tests/TestCase.php:185 will need a cookies argument for the cookie case.
| 'requestFixture' => 'tmp_data_search_roles_request_query_security', | ||
| ], | ||
| [ | ||
| 'security' => 'header', |
There was a problem hiding this comment.
This new header case doesn't add coverage. After the SwaggerServiceMockTrait change the components.securitySchemes block is fed into the driver mock as input and simply echoed back in the assertion, so generateSecuritySchemes() isn't involved in this test at all - getAddEmptyData is what actually covers it. Behaviour-wise this case is identical to the query one (security: [] in both). Either drop it, or turn it into a real requestSupportAuth test (see the separate comment).
| } | ||
| }, | ||
| "securitySchemes": { | ||
| "query": { |
There was a problem hiding this comment.
The scheme key is query, but the test runs with security = 'header' - it should be header. It passes only because this block is echoed back from the mock rather than generated. Same mismatch in the sibling fixtures: tmp_data_search_roles_request_query_security.json has "name": "authorization" while the test config declares 'query' => ['name' => 'api_key'], and tmp_data_search_roles_request_laravel_security.json has {"name": "Cookie", "in": "header"} while the config declares ['name' => 'laravel', 'in' => 'cookie'].
| 'tag' => ['name'], | ||
| ]; | ||
|
|
||
| public const SECURITY_SCHEMES_REQUIRED_FIELDS_BY_TYPE = [ |
There was a problem hiding this comment.
Only the apiKey entry of SECURITY_SCHEMES_REQUIRED_FIELDS_BY_TYPE is covered by a test. Please add cases for the remaining three: http without scheme, oauth2 without flows, openIdConnect without openIdConnectUrl.
| 'security_definition_type' => ['basic', 'apiKey', 'oauth2'], | ||
| 'security_schemes_flows' => ['implicit', 'password', 'clientCredentials', 'authorizationCode'], | ||
| 'security_schemes_in' => ['query', 'header', 'cookie'], | ||
| 'security_schemes_type' => ['apiKey', 'http', 'mutualTLS', 'oauth2', 'openIdConnect'], |
There was a problem hiding this comment.
Double space after 'http',.
| 'security_schemes_type' => ['apiKey', 'http', 'mutualTLS', 'oauth2', 'openIdConnect'], | |
| 'security_schemes_type' => ['apiKey', 'http', 'mutualTLS', 'oauth2', 'openIdConnect'], |
- read the security driver `type` and `in` keys via `Arr::get` to avoid undefined array key errors on malformed driver configs - treat an empty `Authorization` header as unauthorized, consistently with the `apiKey` branch - remove the double space in the security scheme type list - cover `requestSupportAuth` with tests for the `apiKey` header, query and cookie locations, `http` and `mutualTLS` drivers, and malformed driver configs - support cookies and extra query data in the request test helpers - drop the redundant `header` case from `addDataWithSecurity`, which echoed the fixture back instead of exercising the generation - align the `laravel` and `query` security fixtures with the config declared by the test - cover the required fields of the `http`, `oauth2` and `openIdConnect` security scheme types Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 5d0145a4d2
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| $schemeId = "components.securitySchemes.{$index}"; | ||
|
|
||
| $this->validateFieldsPresent($securityDefinition, self::REQUIRED_FIELDS['security_definition'], $parentId); | ||
| $this->validateFieldsPresent($securityScheme, self::REQUIRED_FIELDS['securitySchemes'], $schemeId); |
There was a problem hiding this comment.
Allow referenced security schemes
When components.securitySchemes contains a valid OpenAPI Reference Object, such as auth: {"$ref": "#/components/securitySchemes/bearer"}, this unconditional required-field check rejects it for lacking type before validateRefs() can validate the reference. This prevents getDocFileContent() from serving otherwise valid specs that reuse a local or external security scheme; skip object-level security-scheme validation when the entry is a Reference Object.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: a462c71c2b
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| $securityDriver = Arr::get($this->config, "security_drivers.{$security}"); | ||
|
|
||
| return match (Arr::get($securityDriver, 'type')) { | ||
| 'apiKey' => (!empty(match (Arr::get($securityDriver, 'in'))) { |
There was a problem hiding this comment.
Fix the malformed nested match expression
Under every configuration, this extra closing parenthesis terminates the nested match before its arm block, so PHP reports syntax error, unexpected token ")", expecting "{" while parsing SwaggerService.php. Because the class cannot be loaded at all, documentation generation fails before requestSupportAuth() can run.
Useful? React with 👍 / 👎.
This reverts commit a462c71.
Replace the nested `match` with a `getApiKeyToken` method, so the remaining `match` only maps a security scheme type to the way its token is looked up. Return early when the driver declares no `name`: reading it via `Arr::get` yields `null`, and `Request::header(null)` returns every header, which would report any request as authorized. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
|
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: bf8d9b5af3
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| 'laravel_session' => [ | ||
| 'type' => 'apiKey', | ||
| 'name' => '__ym_uid', | ||
| 'name' => 'laravel_session', |
There was a problem hiding this comment.
Derive the Laravel session cookie name from configuration
When an application changes APP_NAME or explicitly configures session.cookie, Laravel's session cookie is not named laravel_session. Enabling this built-in driver then makes both the generated apiKey scheme and getApiKeyToken() look for the wrong cookie, so session-authenticated operations are left without a security requirement and Swagger clients send an unusable cookie. Use the application's configured session cookie name rather than this literal default.
Useful? React with 👍 / 👎.


Description
Update OpenAPI security generation in src/Services/SwaggerService.php to align with OAS 3.1:
Move security definitions from legacy securityDefinitions to components.securitySchemes.
Remove Swagger 2.0 fallback logic. Support explicit auth token lookup by configured in value:
header
query
cookie
Remove obsolete TODO comment related to migration to Swagger 3.0.
Update validation in src/Validators/SwaggerSpecValidator.php:
Validate security schemes in the OAS 3 location: components.securitySchemes.
Allow cookie as a valid value for security scheme location (in).
references: https://swagger.io/specification/#security-scheme-object
Fixes #201
Type of change
Checklist: