Kam*_*ski 1 php json annotations swagger-php swagger-2.0
我在 Php Laravel 中编写 API 并使用 swagger (2.0) 注释(lib:darkaonline/l5-swagger使用swagger-php)来生成 swagger.json,但是我有以下问题 - 当我输入时:
/**
*
* Space Schema
*
* @SWG\Get(
* path="/api/v1/client/space/schema",
* @SWG\Response(
* response=200,
* description="OK",
* @SWG\Property(property="result", type="json", example={ "aa": [ "bb", "cc" ] } )
* )
* )
Run Code Online (Sandbox Code Playgroud)
并尝试生成 swagger.json 我得到:
[Syntax Error] Expected PlainValue, got '[' in ...
Run Code Online (Sandbox Code Playgroud)
但是当我不使用方括号时,例如:
@SWG\Property(property="result", type="json", example={ "ee": "ff" })
Run Code Online (Sandbox Code Playgroud)
然后一切都很好。但是我需要使用方括号所以问题是:
[swagger注释中json字符串中(方括号)的转义字符是什么?
我还想补充一点,我的示例 json 非常大且复杂
我无意中找到了更好的解决方案:
将方括号 ([和])更改为:
@SWG\Property(property="result", type="json", example={ "aa": [ "bb", "cc" ] } )
Run Code Online (Sandbox Code Playgroud)
大括号({和}):
@SWG\Property(property="result", type="json", example={ "aa": { "bb", "cc" } } )
Run Code Online (Sandbox Code Playgroud)
如您所见,我们使用大括号,但我们不使用 key:value conwention(仅键),因此 swagger 能够检测数组。
我们在 swagger-ui 中有漂亮且易于格式化的 json :)