[PHP] Supplying an invokerPackage without match api and model package names creates a non-working library
Description
If the generator is supplied with invokerPackage but not apiPackage and modelPackage, the API and Model classes are generated with a namespace that is incompatible with the autoloader declarations. The documentation on invokerPackage suggests that it will be prefixed to all class packages:
The main namespace to use for all classes. e.g. Yay\Pets
Swagger-codegen version
Latest, from Homebrew
Swagger declaration file content or url
Pet Store Swagger: http://petstore.swagger.io/v2/swagger.json
Command line used for generation
swagger-codegen generate -l php \
-i http://petstore.swagger.io/v2/swagger.json \
--invoker-package=SamplePetStore
Steps to reproduce
Run the above command, and then check out the file and namespace structure of the API and Model classes. The generated composer.json includes the following:
{
"autoload": {
"psr-4": { "SamplePetStore\\" : "lib/" }
},
"autoload-dev": {
"psr-4": { "SamplePetStore\\" : "test/" }
}
}
Which will cause autoloading of API and Model classes to fail.
Related issues
None I was able to find.
Suggest a Fix
I haven't yet tracked down the specific bug/fix, but ApiClient, which contains the correct namespace uses namespace {{invokerPackage}}; while API and Model use namespace {{apiPackage}}; and namespace {{modelPackage}};, respectively.
It seems like apiPackage and modelPackage need to be updated to include the correct invokerPackage OR, the autoload should include paths for API and Model classes. If the latter, the documentation should be updated.
Unsure if this impacts other libraries.
It seems like apiPackage and modelPackage need to be updated to include the correct invokerPackage
Sounds good to me. We'll need to update apiPackage, modelPackage in the following lines:
https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PhpClientCodegen.java#L234
https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PhpClientCodegen.java#L240
https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PhpClientCodegen.java#L244
Please let us know if you've time to submit the fix.
I'll take a swing at this and see what I can come up with.
Hi, @wing328 -- thanks for your patience on this issue. I have a question on expected behavior.
If a user specifies a custom invokerPackage (Foo for example) and a custom apiPackage (Bar), what should happen?
Current behavior is a broken library because API Classes get the namespace Bar but the autoloader doesn't get an entry for that. There seems to be a few options.
-
Always prefix
apiPackagewithinvokerPackage. This is the easiest and most durable. It does mean that if someone specifies a custominvokerPackageFooand a customapiPackageFoo\Bar, the code will end up with an API Class namespace ofFoo\Foo\Bar. This is not the most user-friendly, but the code will still work, it's easily fixed by the user, and it seems to align with the documentation for invokerPackage. -
Never prefix
apiPackagewithinvokerPackage. Instead, change the autoloader code to add entries for the new API Class namespaces. This has the upside of being a bit more flexible, but the downside of being more complicated to implement, at odds with one reading of the documentation forinvokerPackage, and making the config slightly more verbose in what I imagine is the typical use case. -
Try to guess if we should prefix
apiPackagewithinvokerPackage. Basically, we'd skip prefixing if the user already did it. So, with aninvokerPackageofFoo, the API class namespace becomesFoo\Barfor both theapiPackagevalues ofBarandFoo\Bar. This has the upside of doing what the user probably wants more frequently than (1) or (2), and being easier to implement than (2). It has the downside of being a little weird if you happen to want the namespace to beFoo\Foo\Barand it is also a bit more complicated to implement than (1). It also adds a tiny bit of "magic" to the build process for PHP. Whether or not that's an upside or downside depends on project philosophy.
My preference would be for the first option, but I think the design choice is better left to a maintainer. Do you have an opinion on this one?
@toomuchpete Please go with the first option.
One suggestion is to update the help text for these options with examples so as to elaborate on the usage.
cc @arnested