io.github.swagger2markup.GroupBy Java Examples

The following examples show how to use io.github.swagger2markup.GroupBy. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Swagger2ToDocTest.java    From singleton with Eclipse Public License 2.0 6 votes vote down vote up
@Before
public void generateAsciiDoc() throws Exception {
	MvcResult mvcResult = this.mockMvc.perform(get(this.groupPath + "v1").accept(MediaType.APPLICATION_JSON))
			.andExpect(status().isOk()).andReturn();
	MockHttpServletResponse response = mvcResult.getResponse();
	Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder().withFlatBody().withListDelimiter()
			.withoutInlineSchema().withInterDocumentCrossReferences().withGeneratedExamples()
			.withPathsGroupedBy(GroupBy.TAGS).build();

	String swaggerJsonV1 = response.getContentAsString();
	Swagger2MarkupConverter.from(swaggerJsonV1).withConfig(config).build().toFolder(Paths.get(outputDirV1));

	mvcResult = this.mockMvc.perform(get(this.groupPath + "v2").accept(MediaType.APPLICATION_JSON))
			.andExpect(status().isOk()).andReturn();
	response = mvcResult.getResponse();
	String swaggerJsonV2 = response.getContentAsString();

	Swagger2MarkupConverter.from(swaggerJsonV2).withConfig(config).build().toFolder(Paths.get(outputDirV2));
}
 
Example #2
Source File: Swagger2MarkupTest.java    From SpringBootBucket with MIT License 6 votes vote down vote up
/**
     * 将swagger.yaml或swagger.json转换成漂亮的 AsciiDoc
     * 访问:http://localhost:9095/v2/api-docs
     * 将页面结果保存为src/main/resources/swagger.json
     */
    private void convertAsciidoc(String swaggerStr) {
//        Path localSwaggerFile = Paths.get(System.getProperty("swaggerOutputDir"), "swagger.json");
        Path outputFile = Paths.get(System.getProperty("asciiDocOutputDir"));
        Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder()
                .withMarkupLanguage(MarkupLanguage.ASCIIDOC)
                .withOutputLanguage(Language.ZH)
                .withPathsGroupedBy(GroupBy.TAGS)
                .withGeneratedExamples()
                .withoutInlineSchema()
                .build();
        Swagger2MarkupConverter converter = Swagger2MarkupConverter.from(swaggerStr)
                .withConfig(config)
                .build();
        converter.toFile(outputFile);
    }
 
Example #3
Source File: SwaggerToAsciiDocMarkdownConfluence.java    From springrestdoc with MIT License 6 votes vote down vote up
@Test
public void test() throws URISyntaxException {
    //Given
    Path file = Paths.get(SwaggerToAsciiDocMarkdownConfluence.class.getResource(resource).toURI());
    Path outputDirectory = Paths.get(BUILD_SWAGGER_2_MARKUP_PATH + outputPath);
    FileUtils.deleteQuietly(outputDirectory.toFile());

    //When
    Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder()
            .withPathsGroupedBy(GroupBy.TAGS)
            .withOutputLanguage(Language.EN)
            .withMarkupLanguage(markupLanguage)
            .build();

    Swagger2MarkupConverter.from(file)
            .withConfig(config)
            .build()
            .toFolder(outputDirectory);
}
 
Example #4
Source File: PathsDocumentExtension.java    From swagger2markup with Apache License 2.0 5 votes vote down vote up
private int increaseLevelOffset(int levelOffset) {
    //TODO: This method always receives levelOffset=2. Perhaps the parameter could be removed
    if (globalContext.getConfig().getPathsGroupedBy() == GroupBy.TAGS) {
        return ++levelOffset;
    } else {
        return levelOffset;
    }
}
 
Example #5
Source File: PathOperationComponent.java    From swagger2markup with Apache License 2.0 5 votes vote down vote up
/**
 * Adds a operation title to the document.
 *
 * @param title  the operation title
 * @param anchor optional anchor (null => auto-generate from title)
 */
private void buildOperationTitle(MarkupDocBuilder markupDocBuilder, String title, String anchor) {
    if (config.getPathsGroupedBy() == GroupBy.AS_IS) {
        markupDocBuilder.sectionTitleWithAnchorLevel2(title, anchor);
    } else {
        markupDocBuilder.sectionTitleWithAnchorLevel3(title, anchor);
    }
}
 
Example #6
Source File: PathOperationComponent.java    From swagger2markup with Apache License 2.0 5 votes vote down vote up
/**
 * Adds a operation section title to the document.
 *
 * @param title the section title
 */
private void buildSectionTitle(MarkupDocBuilder markupDocBuilder, String title) {
    if (config.getPathsGroupedBy() == GroupBy.AS_IS) {
        markupDocBuilder.sectionTitleLevel3(title);
    } else {
        markupDocBuilder.sectionTitleLevel4(title);
    }
}
 
Example #7
Source File: PathOperationComponent.java    From swagger2markup with Apache License 2.0 5 votes vote down vote up
private void buildTagsSection(MarkupDocBuilder markupDocBuilder, SwaggerPathOperation operation) {
    if (config.getPathsGroupedBy() == GroupBy.AS_IS) {
        List<String> tags = operation.getOperation().getTags();
        if (CollectionUtils.isNotEmpty(tags)) {
            buildSectionTitle(markupDocBuilder, labels.getLabel(TAGS));
            if (config.getTagOrdering() != null) {
                tags.sort(config.getTagOrdering());
            }
            markupDocBuilder.unorderedList(tags);
        }
    }
}
 
Example #8
Source File: PathOperationComponent.java    From swagger2markup with Apache License 2.0 5 votes vote down vote up
/**
 * Retrieves the title level for sections
 */
private int getSectionTitleLevel() {
    if (config.getPathsGroupedBy() == GroupBy.AS_IS) {
        return 3;
    } else {
        return 4;
    }
}
 
Example #9
Source File: PathOperationComponent.java    From swagger2markup with Apache License 2.0 5 votes vote down vote up
/**
 * Adds a example title to the document.
 *
 * @param title the section title
 */
private void buildExampleTitle(MarkupDocBuilder markupDocBuilder, String title) {
    if (config.getPathsGroupedBy() == GroupBy.AS_IS) {
        markupDocBuilder.sectionTitleLevel4(title);
    } else {
        markupDocBuilder.sectionTitleLevel5(title);
    }
}
 
Example #10
Source File: BodyParameterComponent.java    From swagger2markup with Apache License 2.0 5 votes vote down vote up
private void buildSectionTitle(MarkupDocBuilder markupDocBuilder, String title) {
    if (config.getPathsGroupedBy() == GroupBy.AS_IS) {
        markupDocBuilder.sectionTitleLevel3(title);
    } else {
        markupDocBuilder.sectionTitleLevel4(title);
    }
}
 
Example #11
Source File: PathsDocument.java    From swagger2markup with Apache License 2.0 5 votes vote down vote up
/**
 * Builds the paths section. Groups the paths either as-is, by tags or using regex.
 *
 * @param paths the Swagger paths
 */
private void buildsPathsSection(MarkupDocBuilder markupDocBuilder, Map<String, Path> paths) {
    List<SwaggerPathOperation> pathOperations = PathUtils.toPathOperationsList(paths, getHostname(), getBasePath(), config.getOperationOrdering());
    if (CollectionUtils.isNotEmpty(pathOperations)) {
        if (config.getPathsGroupedBy() == GroupBy.AS_IS) {
            pathOperations.forEach(operation -> buildOperation(markupDocBuilder, operation, config));
        } else if (config.getPathsGroupedBy() == GroupBy.TAGS) {
            Validate.notEmpty(context.getSchema().getTags(), "Tags must not be empty, when operations are grouped by tags");
            // Group operations by tag
            Multimap<String, SwaggerPathOperation> operationsGroupedByTag = TagUtils.groupOperationsByTag(pathOperations, config.getOperationOrdering());

            Map<String, Tag> tagsMap = TagUtils.toSortedMap(context.getSchema().getTags(), config.getTagOrdering());

            tagsMap.forEach((String tagName, Tag tag) -> {
                markupDocBuilder.sectionTitleWithAnchorLevel2(WordUtils.capitalize(tagName), tagName + "_resource");
                String description = tag.getDescription();
                if (StringUtils.isNotBlank(description)) {
                    markupDocBuilder.paragraph(description);
                }
                operationsGroupedByTag.get(tagName).forEach(operation -> buildOperation(markupDocBuilder, operation, config));

            });
        } else if (config.getPathsGroupedBy() == GroupBy.REGEX) {
            Validate.notNull(config.getHeaderPattern(), "Header regex pattern must not be empty when operations are grouped using regex");

            Pattern headerPattern = config.getHeaderPattern();
            Multimap<String, SwaggerPathOperation> operationsGroupedByRegex = RegexUtils.groupOperationsByRegex(pathOperations, headerPattern);
            Set<String> keys = operationsGroupedByRegex.keySet();
            String[] sortedHeaders = RegexUtils.toSortedArray(keys);

            for (String header : sortedHeaders) {
                markupDocBuilder.sectionTitleWithAnchorLevel2(WordUtils.capitalize(header), header + "_resource");
                operationsGroupedByRegex.get(header).forEach(operation -> buildOperation(markupDocBuilder, operation, config));
            }
        }
    }
}
 
Example #12
Source File: PathsDocument.java    From swagger2markup with Apache License 2.0 5 votes vote down vote up
/**
 * Builds the path title depending on the operationsGroupedBy configuration setting.
 */
private void buildPathsTitle(MarkupDocBuilder markupDocBuilder) {
    if (config.getPathsGroupedBy() == GroupBy.AS_IS) {
        buildPathsTitle(markupDocBuilder, labels.getLabel(SwaggerLabels.PATHS));
    } else if (config.getPathsGroupedBy() == GroupBy.REGEX) {
        buildPathsTitle(markupDocBuilder, labels.getLabel(SwaggerLabels.OPERATIONS));
    } else {
        buildPathsTitle(markupDocBuilder, labels.getLabel(SwaggerLabels.RESOURCES));
    }
}
 
Example #13
Source File: PathsDocument.java    From swagger2markup with Apache License 2.0 5 votes vote down vote up
/**
 * Adds a operation title to the document.
 *
 * @param title  the operation title
 * @param anchor optional anchor (null => auto-generate from title)
 */
private void buildOperationTitle(MarkupDocBuilder markupDocBuilder, String title, String anchor) {
    if (config.getPathsGroupedBy() == GroupBy.AS_IS) {
        markupDocBuilder.sectionTitleWithAnchorLevel2(title, anchor);
    } else {
        markupDocBuilder.sectionTitleWithAnchorLevel3(title, anchor);
    }
}
 
Example #14
Source File: Swagger2MarkupConfigBuilderTest.java    From swagger2markup with Apache License 2.0 4 votes vote down vote up
@Test
public void testConfigOfDefaults() {
    Map<String, String> configMap = new HashMap<>();
    configMap.put(Swagger2MarkupProperties.MARKUP_LANGUAGE, MarkupLanguage.MARKDOWN.toString());
    configMap.put("swagger2markup.extensions.uniqueId1.customProperty1", "123");
    configMap.put("swagger2markup.extensions.uniqueId1.customProperty2", "123");
    configMap.put("swagger2markup.extensions.uniqueId2.customPropertyList1", "123,456");
    configMap.put("swagger2markup.uniqueId1.customProperty1", "123");
    configMap.put("swagger2markup.uniqueId1.customProperty2", "123");

    Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder(configMap)
            .build();

    assertThat(config.getAnchorPrefix()).isNull();
    assertThat(config.getDefinitionOrderBy()).isEqualTo(OrderBy.NATURAL);
    assertThat(config.getDefinitionOrdering()).isEqualTo(Ordering.natural());
    assertThat(config.getDefinitionsDocument()).isEqualTo("definitions");
    assertThat(config.isGeneratedExamplesEnabled()).isFalse();
    assertThat(config.isInlineSchemaEnabled()).isEqualTo(true);
    assertThat(config.getInterDocumentCrossReferencesPrefix()).isNull();
    assertThat(config.getMarkupLanguage().name()).isEqualTo(MarkupLanguage.MARKDOWN.name());
    assertThat(config.getOperationOrderBy()).isEqualTo(OrderBy.NATURAL);
    assertThat(config.getOperationOrdering()).isNotNull();
    assertThat(config.getLanguage()).isEqualTo(Language.EN);
    assertThat(config.getOverviewDocument()).isEqualTo("overview");
    assertThat(config.getParameterOrderBy()).isEqualTo(OrderBy.NATURAL);
    assertThat(config.getParameterOrdering()).isEqualTo(Swagger2MarkupConfigBuilder.PARAMETER_IN_NATURAL_ORDERING.compound(Swagger2MarkupConfigBuilder.PARAMETER_NAME_NATURAL_ORDERING));
    assertThat(config.getPathsDocument()).isEqualTo("paths");
    assertThat(config.getPathsGroupedBy()).isEqualTo(GroupBy.AS_IS);
    assertThat(config.getPropertyOrderBy()).isEqualTo(OrderBy.NATURAL);
    assertThat(config.getPropertyOrdering()).isEqualTo(Ordering.natural());
    assertThat(config.getResponseOrderBy()).isEqualTo(OrderBy.NATURAL);
    assertThat(config.getResponseOrdering()).isEqualTo(Ordering.natural());
    assertThat(config.getSecurityDocument()).isEqualTo("security");
    assertThat(config.getSeparatedDefinitionsFolder()).isEqualTo("definitions");
    assertThat(config.getSeparatedOperationsFolder()).isEqualTo("operations");
    assertThat(config.getTagOrderBy()).isEqualTo(OrderBy.NATURAL);
    assertThat(config.getTagOrdering()).isEqualTo(Ordering.natural());
    assertThat(config.isFlatBodyEnabled()).isFalse();
    assertThat(config.isPathSecuritySectionEnabled()).isTrue();
    assertThat(config.isInterDocumentCrossReferencesEnabled()).isFalse();
    assertThat(config.isSeparatedDefinitionsEnabled()).isFalse();
    assertThat(config.isSeparatedOperationsEnabled()).isFalse();
    assertThat(config.getExtensionsProperties().getKeys()).hasSize(3)
    .containsOnly("uniqueId1.customProperty1",
            "uniqueId1.customProperty2",
            "uniqueId2.customPropertyList1"
            );
    assertThat(config.getExtensionsProperties().getString("uniqueId1.customProperty1").get()).isEqualTo("123");
    assertThat(config.getExtensionsProperties().getPathList("uniqueId2.customPropertyList1")).hasSize(1)
    .containsOnly(Paths.get("123,456"));
}
 
Example #15
Source File: Swagger2MarkupConfigBuilderTest.java    From swagger2markup with Apache License 2.0 4 votes vote down vote up
@Test
public void testConfigOfProperties() throws IOException {

    Properties properties = new Properties();
    properties.load(Swagger2MarkupConfigBuilderTest.class.getResourceAsStream("/config/config.properties"));

    Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder(properties).build();

    assertThat(config.getAnchorPrefix()).isEqualTo("anchorPrefix");
    assertThat(config.getDefinitionOrderBy()).isEqualTo(OrderBy.AS_IS);
    assertThat(config.getDefinitionOrdering()).isNull();
    assertThat(config.getDefinitionsDocument()).isEqualTo("definitionsTest");
    assertThat(config.isGeneratedExamplesEnabled()).isTrue();
    assertThat(config.isHostnameEnabled()).isEqualTo(false);
    assertThat(config.isInlineSchemaEnabled()).isEqualTo(false);
    assertThat(config.getInterDocumentCrossReferencesPrefix()).isEqualTo("xrefPrefix");
    assertThat(config.getMarkupLanguage().name()).isEqualTo(MarkupLanguage.MARKDOWN.name());
    assertThat(config.getOperationOrderBy()).isEqualTo(OrderBy.NATURAL);
    assertThat(config.getOperationOrdering()).isEqualTo(Swagger2MarkupConfigBuilder.OPERATION_PATH_NATURAL_ORDERING.compound(Swagger2MarkupConfigBuilder.OPERATION_METHOD_NATURAL_ORDERING));
    assertThat(config.getLanguage()).isEqualTo(Language.RU);
    assertThat(config.getOverviewDocument()).isEqualTo("overviewTest");
    assertThat(config.getParameterOrderBy()).isEqualTo(OrderBy.AS_IS);
    assertThat(config.getParameterOrdering()).isNull();
    assertThat(config.getPathsDocument()).isEqualTo("pathsTest");
    assertThat(config.getPathsGroupedBy()).isEqualTo(GroupBy.TAGS);
    assertThat(config.getPropertyOrderBy()).isEqualTo(OrderBy.AS_IS);
    assertThat(config.getPropertyOrdering()).isNull();
    assertThat(config.getResponseOrderBy()).isEqualTo(OrderBy.AS_IS);
    assertThat(config.getResponseOrdering()).isNull();
    assertThat(config.getSecurityDocument()).isEqualTo("securityTest");
    assertThat(config.getSeparatedDefinitionsFolder()).isEqualTo("definitionsTest");
    assertThat(config.getSeparatedOperationsFolder()).isEqualTo("operationsTest");
    assertThat(config.isListDelimiterEnabled()).isEqualTo(true);
    assertThat(config.getListDelimiter()).isEqualTo(Character.valueOf('|'));
    assertThat(config.getTagOrderBy()).isEqualTo(OrderBy.AS_IS);
    assertThat(config.getTagOrdering()).isNull();
    assertThat(config.isFlatBodyEnabled()).isTrue();
    assertThat(config.isPathSecuritySectionEnabled()).isFalse();
    assertThat(config.isInterDocumentCrossReferencesEnabled()).isTrue();
    assertThat(config.isSeparatedDefinitionsEnabled()).isTrue();
    assertThat(config.isSeparatedOperationsEnabled()).isTrue();
    assertThat(config.getExtensionsProperties().getKeys()).hasSize(5)
    .containsOnly("uniqueId1.customProperty1",
            "uniqueId1.customProperty2",
            "uniqueId2.customProperty1",
            "uniqueId2.customProperty2",
            "uniqueId2.customPropertyList1"
            );
    assertThat(config.getExtensionsProperties().getPathList("uniqueId2.customPropertyList1")).hasSize(2)
    .containsOnly(Paths.get("123"), Paths.get("456"));
}