swagger-codegen icon indicating copy to clipboard operation
swagger-codegen copied to clipboard

[PHP] Supplying an invokerPackage without match api and model package names creates a non-working library

Open toomuchpete opened this issue 9 years ago • 4 comments

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.

toomuchpete avatar Jan 10 '17 22:01 toomuchpete

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.

wing328 avatar Jan 12 '17 09:01 wing328

I'll take a swing at this and see what I can come up with.

toomuchpete avatar Jan 19 '17 22:01 toomuchpete

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.

  1. Always prefix apiPackage with invokerPackage. This is the easiest and most durable. It does mean that if someone specifies a custom invokerPackage Foo and a custom apiPackage Foo\Bar, the code will end up with an API Class namespace of Foo\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.

  2. Never prefix apiPackage with invokerPackage. 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 for invokerPackage, and making the config slightly more verbose in what I imagine is the typical use case.

  3. Try to guess if we should prefix apiPackage with invokerPackage. Basically, we'd skip prefixing if the user already did it. So, with an invokerPackage of Foo, the API class namespace becomes Foo\Bar for both the apiPackage values of Bar and Foo\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 be Foo\Foo\Bar and 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 avatar Jan 20 '17 20:01 toomuchpete

@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

wing328 avatar Jan 21 '17 08:01 wing328