Spring restdocs有条件地突出显示可选参数与asciidoc

问题描述:

我使用spring restdocs在我的REST webapi上生成文档;我已经整合了这个东西和我的html生成(maven + asciidoc插件,restassured apis)。 我唯一的问题是,ifeval要么不作为广告或我错了。Spring restdocs有条件地突出显示可选参数与asciidoc

我的定制请求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的“可选”值正确呈现(“真”或“假”视情况而定),但ifeval没有被解析(因此在最终的html上没有解析,没有错误)。

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

按照要求我加入导致.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 

|=== 
+0

我刚刚得知,指令必须是在该行的开始;但即使纠正我仍然没有得到正确的结果(所有行都具有相同的格式,不考虑可选值) –

+1

在上面的一个错误是在第二种情况下是'='而不是'==',但可以只是在这个问题上的拼写错误。而不是看自定义模板,你可以分享生成的'.adoc'片段吗?另外,你如何将代码片段包含在你的主'.adoc'文件中? –

+0

是的,它是包含的,而=是一个错字;我添加了生成的adoc。 这特别导致没有值打印“路径” 谢谢。 –

的问题是在您的自定义模板,你正在使用{optional}而不是{{optional}}ifeval宏。这意味着{optional}不会被该字段的optional属性取代,因此,Asciidoctor正在评估"{optional}"=="true""{optional}"=="false"

你需要更新你的模板中使用{{optional}}

|=== 
|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}} 
|=== 
+0

谢谢,我确信我之前尝试过,并没有工作;今天早上它做到了。可能之前还有其他错误... –

+0

第二个中有错误,如果。应该是'ifeval :: [“{{可选}}”==“false”]' – Mateusz

+0

固定。谢谢。 –