Skip to content
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

Warning Skipping unknown & Required @OA\Info() not found in zircote/swagger-php/src/Loggers/DefaultLogger.php #1695

Open
rabihmb opened this issue Jan 15, 2025 · 18 comments

Comments

@rabihmb
Copy link

rabihmb commented Jan 15, 2025

  1. My Own PHP Framework
  2. PHP Version : 8.2.4
  3. OS: Mac and Linux

I was previously using an older version of Swagger, which was functioning well. Recently, I upgraded to the latest version, for both:

composer require zircote/swagger-php;
composer require doctrine/annotations;

and

composer update zircote/swagger-php;
composer update doctrine/annotations;

The JSON file was successfully loaded and is displaying correctly in the Swagger UI, However, I’m encountering an issue that I’ve been unable to resolve despite trying multiple solutions. Before reaching out, I attempted various troubleshooting steps, but none were successful. Getting now warning displayed below

Warning: Skipping unknown \new_form in /Users/mike/Sites/localhost/vendor/zircote/swagger-php/src/Loggers/DefaultLogger.php on line 23
Warning: Required @OA\PathItem() not found in /Users/mike/Sites/localhost/vendor/zircote/swagger-php/src/Loggers/DefaultLogger.php on line 23
Warning: Required @OA\Info() not found in /Users/mike/Sites/localhost/vendor/zircote/swagger-php/src/Loggers/DefaultLogger.php on line 23
{
"openapi": "3.0.0"
}

Could you kindly let me know if there are any specific steps or configurations I should follow to ensure everything works seamlessly after the upgrade? taking into consideration that the json file was loaded and showing in the swagger ui well

@DerManoMann
Copy link
Collaborator

Depends to a degree on the versions - was this an upgrade from v3 -> v4 or v4 -> v5?

It would definitley help to get the current (non working) version and perhaps some reproducer code (ideally simplified to a single file)

@rabihmb
Copy link
Author

rabihmb commented Jan 16, 2025

I recently ran a composer upgrade to update Swagger, but I’m unsure which version I upgraded from. Currently, the production server is still running the old version, while my localhost has been upgraded to the latest version.

If needed, I can provide additional details, but I would appreciate your guidance on how to proceed.

@wonderwebapp
Copy link

I’m currently facing the same issue after upgrading, and I’ve had to leave it non-functional for now.

could you also advise on how to ensure a smooth transition for the production server when upgrading?

@DerManoMann
Copy link
Collaborator

I would love to help, but I seriously need more details or a reproducer.

  • For example, unknown \new_form - is new_form a function that got annotated?
  • I suppose you are using annotations, not attributes?

@rabihmb
Copy link
Author

rabihmb commented Jan 17, 2025

Thank you for your question. Yes, I am using annotations instead of attributes. Again, I was previously using an older version of Swagger, which was functioning well with the same structure.

For your reference, I’ve attached two files:

The first file generates the JSON content.
The second file contains the API class.

<?php
////////////////////////////////////////////////////////////////////////////////
require("framework/system/RESTful/vendor/autoload.php");
$openapi = \OpenApi\Generator::scan(["../"]);
header("Content-Type: application/json; charset=UTF-8");
echo $openapi->toJson();
//header("Content-Type: application/x-yaml");
//echo $openapi->toYaml();

<?php
require site_root . "vendor/autoload.php";
////////////////////////////////////////////////////////////////////////////////
use OpenApi\Annotations as OA;
//use OpenApi\Attributes as OA;
///////////////////////////////////////////////////////////////////////////////
/**
 * @OA\Tag(
 * name="user",
 * description="User related operations"
 * )
 * @OA\Info(
 *     title="Data API Document",
 *     version="1.0.7",
 *     description="All endpoints related to this website",
 *     @OA\License(name="Apache 2.0",url="http://www.apache.org/licenses/LICENSE-2.0.html")
 *     @OA\Contact(email="[email protected]")
 *     @OA\Attachable()
 * )
 * @OA\PathItem()
 * @OA\Server(
 *     url="https://mysite.com/api/",
 *     description="mysite Secured API"
 * )
 * @OA\SecurityScheme(
 *     type="http",
 *     description="Use the generated bearer code to get the token",
 *     scheme="bearer",
 *     bearerFormat="JWT",
 *     securityScheme="bearerAuth",
 * )
 */
class new_form extends api {

}

new new_form();

If you need the old version for comparison or to align it with the new one, please let me know the steps required to retrieve it. I’ll be happy to obtain the necessary details from the production server.

Note that I was running on version 3 as per the below:

{
"require": {
"zircote/swagger-php": "^3.2",
"doctrine/annotations": "^1.13"
}
}

and now upgraded to

{
"require": {
"zircote/swagger-php": "^5.0",
"doctrine/annotations": "^2.0"
}
}

Regards

@wonderwebapp
Copy link

I followed the guide at https://zircote.github.io/swagger-php/guide/faq.html#annotations-missing with the same issue too. Was working and stopped upon upgrade.

@ripon52
Copy link

ripon52 commented Jan 19, 2025

Same here, no longer working after upgrading from 3 to 5

@MH-Shakil38
Copy link

Experiencing the same issue after upgrading from version 3 to 5. It was working perfectly before the update.

@DerManoMann
Copy link
Collaborator

I think you are all facing this issue: https://zircote.github.io/swagger-php/guide/faq#warning-required-oa-info-not-found

As of v4 swagger-php uses reflection. In order for reflection to be able to find code, that code needs either to be loaded upfront (good old include/require) or autoload must be configured properly.

Taking @rabihmb's sample code I had to make the following changes:

  1. Add a `require '; line at the top of the scan script
  2. In the annotations add a couple missing commas to the @OA\License and @OA\Contact annotations

The key is that swagger-php is not using the API classes (which would trigger autoloading); it does a basic file scan and then uses reflection on the code it considers.

@rabihmb
Copy link
Author

rabihmb commented Jan 20, 2025

As mentioned previously, I followed the steps outlined and created a new class from scratch. The steps were basic and straightforward, but I’m still encountering the same issue. It appears the process is stuck on the same error.

could you kindly review the implementation and provide guidance on how to resolve this? I shared the code for additional details and further analysis.

<?php
////////////////////////////////////////////////////////////////////////////////
use OpenApi\Annotations as OA;
//////////////////////////////////////////////////////////////////////////////
/**
 * @OA\Tag(
 * name="user",
 * description="User related operations"
 * )
 * @OA\Info(
 *     title="Data API Document",
 *     version="1.0.7",
 *     description="All endpoints related to this website",
 *     @OA\License(name="Apache 2.0",url="http://www.apache.org/licenses/LICENSE-2.0.html");
 *     @OA\Contact(email="[email protected]");
 *     @OA\Attachable()
 * )
 * @OA\PathItem()
 * @OA\Server(
 *     url="https://mysite.com/api/",
 *     description="mysite Secured API"
 * )
 * @OA\SecurityScheme(
 *     type="http",
 *     description="Use the generated bearer code to get the token",
 *     scheme="bearer",
 *     bearerFormat="JWT",
 *     securityScheme="bearerAuth",
 * )
 */
class OpenApiSpec
{
}
Warning: Skipping unknown \OpenApiSpec in /Users/mike/Sites/localhost/vendor/library/vendor/zircote/swagger-php/src/Loggers/DefaultLogger.php on line 23

Warning: Required @OA\PathItem() not found in /Users/mike/Sites/localhost/vendor/library/vendor/zircote/swagger-php/src/Loggers/DefaultLogger.php on line 23

Warning: Required @OA\Info() not found in /Users/mike/Sites/localhost/vendor/library/vendor/zircote/swagger-php/src/Loggers/DefaultLogger.php on line 23
{ "openapi": "3.0.0" }

@DerManoMann
Copy link
Collaborator

Ok, first of all there are a couple minor issues with your annotations:

  • annotations are separated by ,, not ;
  • PathItem requires a path property

So, here is the corrected file (other.php) I used

<?php
////////////////////////////////////////////////////////////////////////////////
use OpenApi\Annotations as OA;
//////////////////////////////////////////////////////////////////////////////
/**
 * @OA\Tag(
 *     name="user",
 *     description="User related operations"
 * )
 * @OA\Info(
 *     title="Data API Document",
 *     version="1.0.7",
 *     description="All endpoints related to this website",
 *     @OA\License(name="Apache 2.0",url="http://www.apache.org/licenses/LICENSE-2.0.html"),
 *     @OA\Contact(email="[email protected]"),
 *     @OA\Attachable()
 * )
 * @OA\PathItem(path="/api")
 * @OA\Server(
 *     url="https://mysite.com/api/",
 *     description="mysite Secured API"
 * )
 * @OA\SecurityScheme(
 *     type="http",
 *     description="Use the generated bearer code to get the token",
 *     scheme="bearer",
 *     bearerFormat="JWT",
 *     securityScheme="bearerAuth"
 * )
 */
class OpenApiSpec
{
}

Secondly, you have two choices to generate the spec from the file.
For both, the key is that your code/annotations need to be either autoloaded or manually be loaded before processing.
Typically when using composer your code would be autoloaded automatically (if configured properly); for single file experiments or small apps you are responsible for that yourself.

  1. Use the openapi command line tool (assuming swagger-php is installed via composer) and other.php is inside a src folder.
    The -b option means that other.php is included before processing starts...
./vendor/bin/openapi -b src/other.php src/other.php
  1. Using code. Please note that here also other.php is require'd before processing.
<?php
////////////////////////////////////////////////////////////////////////////////
require("../../vendor/autoload.php");
require 'src/other.php';
$openapi = \OpenApi\Generator::scan(["./src/other.php"]);
header("Content-Type: application/json; charset=UTF-8");
echo $openapi->toJson();
//header("Content-Type: application/x-yaml");
//echo $openapi->toYaml();

@rabihmb
Copy link
Author

rabihmb commented Jan 20, 2025

Thank you very much for your advice. The below code were solved the problem

require 'src/other.php';
$openapi = \OpenApi\Generator::scan(["./src/other.php"]);

Is there any autoload to load the entire my src/ files recursively?

@DerManoMann
Copy link
Collaborator

Yep. It's called composer: https://getcomposer.org/doc/00-intro.md

@rabihmb
Copy link
Author

rabihmb commented Jan 21, 2025

No I mean to auto load the api files that We made. I have v1 and v2 and both are used so we need to load them dynamically instead of add require each file one by one.

@DerManoMann
Copy link
Collaborator

Yes, I understand and that is why I said composer :)
If you look at all swagger-php source code, the only require is to load ./vendor/autoload.php and composer takes care of the rest.

@rabihmb
Copy link
Author

rabihmb commented Jan 21, 2025

But currently isn't not :)

When I didn't require the v1 files it gave the same old issue, but when I add them one by one using require it works.

@DerManoMann
Copy link
Collaborator

Well, this is outside of the scope of this project. I do recommend reading the composer docs, in particular about autoloading: https://getcomposer.org/doc/01-basic-usage.md#autoloading

@OA
Copy link

OA commented Jan 21, 2025

my name on here is a little unfortunate.

code blocks prevent tagging me btw 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants