首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring REST文档中的转义管道符号

问Spring REST文档中的转义管道符号
EN

Stack Overflow用户
提问于 2020-09-26 16:24:47
回答 1查看 183关注 0票数 1

我需要记录一个API,它接受由这个类表示的JSON:

代码语言:javascript
复制
public class Message {
    
    @NotNull(message = "Sender cannot be null")
    private String sender;
    
    @Pattern(regexp="HI|HELLO",message = "Message can be only 'HI' or 'HELLO'")
    private String content;
    
    // Constructor, Getters and Setters..
}

Spring Docs自动生成以下代码片段:

代码语言:javascript
复制
.Request fields:
|===
|Path|Type|Constraints|Description

|sender
|String
|Must not be null
|Sender of the message

|content
|String
|Must match the regular expression `HI|HELLO`
|Content of the message

|===

它由Asciidoctor用来在pdf中创建表。然而,在pdf中,表格被破坏了(因为管道):

​

​

如何转义正则表达式中的管道?

我找到了这个issue,但似乎与另一个项目有关。

这是生成文档的测试类(JUnit5):

代码语言:javascript
复制
@WebMvcTest(HomeController.class)
@AutoConfigureRestDocs(outputDir = "target/snippets")
public class HomeControllerTest {

    @Autowired
    private MockMvc mockMvc;
    
    @Test
    public void postMessageTest() throws Exception {
        ConstrainedFields constrainedFields = new ConstrainedFields(Message.class);
        
        this.mockMvc
                .perform(post("/message").content("{\"sender\":\"Marc\",\"content\":\"HI\"}")
                        .characterEncoding("utf-8")
                        .contentType(MediaType.APPLICATION_JSON))
                .andDo(print()).andExpect(status().isOk())
                .andDo(document("home-message", requestFields(
                        attributes(key("title").value("Request fields:")),
                        constrainedFields.withPath("sender").description("Sender of the message"),
                        constrainedFields.withPath("content").description("Content of the message")
                        )));
    }

    private static class ConstrainedFields {
        private final ConstraintDescriptions constraintDescriptions;
        
        ConstrainedFields(Class<?> input) {
            this.constraintDescriptions = new ConstraintDescriptions(input);
        }
        
        private FieldDescriptor withPath(String property) {
            return fieldWithPath(property).attributes(key("constraints").value(
                    // Let's assume there is only one constraint for each property
                    constraintDescriptions.descriptionsForProperty(property).get(0)));
        }
    }
}

重现该问题的完整项目是here。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-09-26 23:09:57

可能不是最好的解决方案,但是,在做了很少的研究之后,我发现在asciidoctor文档(source)中,一个简单的\字符足以转义竖线字符。然后,我们的目标是在.adoc代码片段中获得这一行:

代码语言:javascript
复制
|Must match the regular expression `HI\|HELLO`

这是在你的代码中提取“约束描述”的方法:

代码语言:javascript
复制
private FieldDescriptor withPath(String property) {
    return fieldWithPath(property).attributes(key("constraints").value(
            // Let's assume there is only one constraint for each property
            constraintDescriptions.descriptionsForProperty(property).get(0)));
}

在这里,我们可以“完全控制”将在.adoc中编写的文本。例如,我们可以从constraintDescriptions.descriptionsForProperty(property)获取约束描述,应用转义,然后将转义字符串传递给.value()

代码语言:javascript
复制
private FieldDescriptor withPath(String property) {
    // Let's assume there is only one constraint for each property
    String desc = constraintDescriptions.descriptionsForProperty(property).get(0);
    desc = desc.replace("|", "\\|");
    return fieldWithPath(property).attributes(key("constraints").value(desc));
}

这将在.adoc中生成转义行,并将在.pdf中正确呈现

​

​

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64075493

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档