OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.81k stars 6.58k forks source link

[BUG] [PHP] generated zend-expressive stub contains syntax errors when pattern of property ends with `*` #2169

Open autopp opened 5 years ago

autopp commented 5 years ago

Bug Report Checklist

Description

Generated zend-expressice stub contains syntax errors when pattern of property ends with *. The pattern is embedded into annotation and breaks the DocComment.

This is also reproduced in the current sample. https://github.com/OpenAPITools/openapi-generator/blob/cefd29b6ffc5933ef66de994ecd60128f42f39b7/samples/server/openapi3/petstore/php-ze-ph/src/App/DTO/InlineObject3.php#L75

openapi-generator version
$ java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar version
4.0.0-SNAPSHOT

$ git log --oneline -n 1
cefd29b6ff (HEAD -> master, origin/master, origin/HEAD) [rust-server] Changed query parameters to be url encoded (#2136)
OpenAPI declaration file content or url

See: https://gist.github.com/autopp/472847da6c9de80450117a9cfecbc6d1#file-example-yml

Command line used for generation
$ java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate --skip-validate-spec -t modules/openapi-generator/src/main/resources/ze-ph -g php-ze-ph -o /tmp/php-ze-ph -i https://gist.githubusercontent.com/autopp/472847da6c9de80450117a9cfecbc6d1/raw/9ea6e9d6c0e3e9b724f92f2a52b76c7b42f8be29/example.yml
Steps to reproduce
  1. Run the above command to generate client code for Ruby.
  2. Run php -l /tmp/php-ze-ph/src/App/DTO/InlineObject.php to check syntax of the generated model code.
  3. Get error in src/App/DTO/InlineObject.php
    $ php -l /tmp/php-ze-ph/src/App/DTO/InlineObject.php
    Parse error: syntax error, unexpected '"', expecting function (T_FUNCTION) or const (T_CONST) in /tmp/php-ze-ph/src/App/DTO/InlineObject.php on line 15
    Errors parsing /tmp/php-ze-ph/src/App/DTO/InlineObject.php
Related issues/PRs

N/A

Suggest a fix

Sorry, I have no idea. I don't know how to escape */ in a DocComment.

auto-labeler[bot] commented 5 years ago

👍 Thanks for opening this issue! 🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

ackintosh commented 5 years ago

Thanks for the issue!

Quick idea for the PHP generator:

cc: Technical Committee @jebentier , @dkarlovi , @mandrean , @jfastnacht , @ybelenko , @renepardon

I think we can use # (or other symbols) as delimiter instead of / like below:

-    * @DTA\Validator(name="Regex", options={"pattern":"/^[A-Z].*/"})
+    * @DTA\Validator(name="Regex", options={"pattern":"#^[A-Z].*#"})

As I've never used zend-expressive can't say for sure but if the annotation (@DTA\Validator) is based on regex-functions like preg-match, the idea should work as expected. 💡


Note: For the similar reason, to avoid syntax error, the PHP generator replaces a media type */* to *_/_* . However, it can't be applicable in this issue as the approach breaks the regex strings.

https://github.com/OpenAPITools/openapi-generator/blob/add63cb981bd1cea3f7bdad5c575fa405e070130/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPhpCodegen.java#L718-L720

ackintosh commented 5 years ago

If no one has further feedback, I'll start working on this issue.