java - Spring Restdocs 使用 asciidoc 有条件地突出显示可选参数

标签 java maven rest-assured asciidoctor spring-restdocs

我正在使用 spring Restdocs 在我的 REST webapi 上生成文档;我已经集成了这个东西,并且生成了我的html(maven + asciidoc 插件,放心的api)。 我遇到的唯一问题是 ifeval 要么不像宣传的那样工作,要么是我弄错了。

我的自定义 request-fields.snippet 如下所示:

|===
|Path|Type|Description|Optional

{{#fields}}
 |{{#tableCellContent}}
ifeval::["{optional}"=="true"]
   `{{path}}`
endif::[]
ifeval::["{optional}"="false"]
   *`{{path}}`*
endif::[]
  {{/tableCellContent}}
  |{{#tableCellContent}}`{{type}}`{{/tableCellContent}}
  |{{#tableCellContent}}{{description}}{{/tableCellContent}}
  |{{#tableCellContent}}{{optional}}{{/tableCellContent}}

{{/fields}}
|===

tablecellcontent 中的“可选”值已正确呈现(“true”或“false”取决于具体情况),但 ifeval 未解析(因此在最终 html 上显示其自身未解析,没有错误)。

我尝试了不同的表达式语法,但似乎都不起作用;关于什么可能是正确的语法(如果有)的任何提示? 我正在考虑定义一个自定义属性并使用 ifndef 来获得相同的结果,但如果可能的话,我更愿意使用现有的 optional() 支持。

根据要求,我添加生成的 .adoc

|===
|Path|Type|Description|Optional

|
ifeval::["{optional}"=="true"]
   `name`
endif::[]
ifeval::["{optional}"=="false"]
   *`name`*
endif::[]
|`String`
|Name of the new Axis
|false

|
ifeval::["{optional}"=="true"]
   `description`
endif::[]
ifeval::["{optional}"=="false"]
   *`description`*
endif::[]
|`String`
|Description of the new Axis
|true

|
ifeval::["{optional}"=="true"]
   `tags[]`
endif::[]
ifeval::["{optional}"=="false"]
   *`tags[]`*
endif::[]
|`TagDto[]`
|Hierarchical view of axis' tags
|false

|
ifeval::["{optional}"=="true"]
   `tags[].id`
endif::[]
ifeval::["{optional}"=="false"]
   *`tags[].id`*
endif::[]
|`Number`
|Id of the tag
|false

|
ifeval::["{optional}"=="true"]
   `tags[].name`
endif::[]
ifeval::["{optional}"=="false"]
   *`tags[].name`*
endif::[]
|`String`
|Name of the tag
|false

|
ifeval::["{optional}"=="true"]
   `tags[].children`
endif::[]
ifeval::["{optional}"=="false"]
   *`tags[].children`*
endif::[]
|`TagDto[]`
|Child tags for this tag, if any
|true

|===

最佳答案

问题出在您的自定义模板中,您在 ifeval 宏中使用 {optional} 而不是 {{optional}} 。这意味着 {可选} 不会被字段的 可选 属性替换,因此,Asciidoctor 正在评估 "{可选}"== “true”“{可选}”==“false”

您需要更新模板才能使用{{可选}}:

|===
|Path|Type|Description|Optional

{{#fields}}
 |{{#tableCellContent}}
ifeval::["{{optional}}"=="true"]
   `{{path}}`
endif::[]
ifeval::["{{optional}}"=="false"]
   *`{{path}}`*
endif::[]
  {{/tableCellContent}}
  |{{#tableCellContent}}`{{type}}`{{/tableCellContent}}
  |{{#tableCellContent}}{{description}}{{/tableCellContent}}
  |{{#tableCellContent}}{{optional}}{{/tableCellContent}}

{{/fields}}
|===

关于java - Spring Restdocs 使用 asciidoc 有条件地突出显示可选参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37938283/

相关文章:

java - 通过 jdbc 运行 shell 命令

Java继承和方法重载

java - Maven 项目导入向导不显示子项目 pom

java - 放心地在对象数组中查找特定的键值对

java - 没有名为persistence.xml的EntityManager的持久性提供程序

java - 如何在 Java 中获取 mp4 电影的单个图像

java - 使用 Maven Shade 插件的父子 Pom 布局

java - Server Tomcat v8.5 Server at localhost 无法启动 Spring Project

RestAssured oAuth2 http 状态码 401

java - 空回复正文发帖放心