1

我正在使用 Swagger 提供的出色的swagger2markup插件为我的 REST API 生成 Asciidoc 文档。我遵循了 swagger2markup 文档,并且正在使用 Spring MVC 集成测试从我的 Springfox Swagger 端点生成标记,如下所示(我正在使用 Maven):

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = { AppConfig.class, SwaggerConfig.class })
public class DocumentationIT {

    protected MockMvc mockMvc;

    @Autowired
    protected WebApplicationContext webApplicationContext;

    @Rule
    public JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation("src/docs/asciidoc/apidoc/generated-snippets");

    @Before
    public void setUp(){
        this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
                .apply(documentationConfiguration(this.restDocumentation))
                .build();
    }

    @Test
    public void convertSwaggerToAsciiDoc() throws Exception {
        this.mockMvc.perform(get("/v2/api-docs")
                .accept(MediaType.APPLICATION_JSON))
                .andDo(
                        Swagger2MarkupResultHandler
                                .outputDirectory("src/docs/asciidoc/apidoc")
                                .withExamples("src/docs/asciidoc/apidoc/generated-snippets").build())
                .andExpect(status().isOk());
    }
}

一切都很好,我的所有路径都在我的最终文档中,但是路径都直接出现在根目录中,并且没有按资源分组(即按控制器),因此Method 1from将出现在与fromController 1相同的级别。Method 2Controller 2

我的输出:

在此处输入图像描述

我想要什么:

在此处输入图像描述

据我所见,当使用从本地文件生成时,如在这个swagger2-markup Maven 项目模板中,您可以指定一个属性来告诉 swagger2markup 使用 config 属性按标签对路径进行分组<swagger2markup.pathsGroupedBy>TAGS</swagger2markup.pathsGroupedBy>,但是似乎没有这样的配置Swagger2MarkupResultHandler从测试中使用时。唯一的选择是withMarkupLanguage(),但没有withPathsGroupedBy()方法......

我在这里错过了什么吗?

4

1 回答 1

2

正如您所提到的,swagger2markup.pathsGroupedByswagger2Markup 提供了一个属性来指定路径的分组方式。但是,Swagger2MarkupResultHandler不要提供 API 来支持配置。

根据Swagger2Markup API

Swagger2Markup 的属性在类 io.github.swagger2markup.Swagger2MarkupProperties 中定义。这些属性按以下顺序考虑:

  1. Java 系统属性

  2. 自定义属性

  3. 默认属性(包含在 Swagger2Markup 中)

可以使用系统属性对其进行配置。因此,您可以在测试中设置swagger2markup.pathsGroupedByto的系统属性。TAGS

如果您更喜欢使用 Java API 配置它,您可以扩展Swagger2MarkupResultHandlerhandle并使用Swagger2MarkupConfigBuilder API覆盖该方法。

于 2016-09-26T14:43:23.227 回答