Java Code Examples for io.github.swagger2markup.markup.builder.MarkupDocBuilder#toString()

The following examples show how to use io.github.swagger2markup.markup.builder.MarkupDocBuilder#toString() . 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: PathOperationComponent.java    From swagger2markup with Apache License 2.0 6 votes vote down vote up
/**
 * Adds a operation description to the document.
 *
 * @param operation the Swagger Operation
 */
private void buildDescriptionSection(MarkupDocBuilder markupDocBuilder, SwaggerPathOperation operation) {
    MarkupDocBuilder descriptionBuilder = copyMarkupDocBuilder(markupDocBuilder);
    applyPathsDocumentExtension(new Context(Position.OPERATION_DESCRIPTION_BEGIN, descriptionBuilder, operation));
    String description = operation.getOperation().getDescription();
    if (isNotBlank(description)) {
        descriptionBuilder.paragraph(markupDescription(MarkupLanguage.valueOf(config.getSchemaMarkupLanguage().name()), markupDocBuilder, description));
    }
    applyPathsDocumentExtension(new Context(Position.OPERATION_DESCRIPTION_END, descriptionBuilder, operation));
    String descriptionContent = descriptionBuilder.toString();

    applyPathsDocumentExtension(new Context(Position.OPERATION_DESCRIPTION_BEFORE, markupDocBuilder, operation));
    if (isNotBlank(descriptionContent)) {
        buildSectionTitle(markupDocBuilder, labels.getLabel(DESCRIPTION));
        markupDocBuilder.text(descriptionContent);
    }
    applyPathsDocumentExtension(new Context(Position.OPERATION_DESCRIPTION_AFTER, markupDocBuilder, operation));
}
 
Example 2
Source File: ParameterTableComponent.java    From swagger2markup with Apache License 2.0 5 votes vote down vote up
private String buildDescriptionForParameter(MarkupDocBuilder markupDocBuilder, ParameterAdapter parameterAdapter) {
    MarkupDocBuilder descriptionBuilder = copyMarkupDocBuilder(markupDocBuilder);
    String basicDescription = parameterAdapter.displayDescription(descriptionBuilder);
    descriptionBuilder.text(basicDescription);

    if (parameterAdapter.getMinItems().isPresent()) {
        descriptionBuilder.newLine(true).boldText(labels.getLabel(MIN_ITEMS)).text(COLON).text(String.valueOf(parameterAdapter.getMinItems().get()));
    }
    if (parameterAdapter.getMaxItems().isPresent()) {
        descriptionBuilder.newLine().boldText(labels.getLabel(MAX_ITEMS)).text(COLON).text(String.valueOf(parameterAdapter.getMaxItems().get()));
    }
    return descriptionBuilder.toString();
}
 
Example 3
Source File: ParameterTableComponent.java    From swagger2markup with Apache License 2.0 5 votes vote down vote up
private String getParameterNameColumnContent(MarkupDocBuilder markupDocBuilder, ParameterAdapter parameter) {
    MarkupDocBuilder parameterNameContent = copyMarkupDocBuilder(markupDocBuilder);

    parameterNameContent.boldTextLine(parameter.getName(), true);
    if (parameter.getRequired())
        parameterNameContent.italicText(labels.getLabel(FLAGS_REQUIRED).toLowerCase());
    else
        parameterNameContent.italicText(labels.getLabel(FLAGS_OPTIONAL).toLowerCase());
    return parameterNameContent.toString();
}
 
Example 4
Source File: ResponseComponent.java    From swagger2markup with Apache License 2.0 4 votes vote down vote up
@Override
public MarkupDocBuilder apply(MarkupDocBuilder markupDocBuilder, Parameters params) {
    SwaggerPathOperation operation = params.operation;
    Map<String, Response> responses = operation.getOperation().getResponses();

    MarkupDocBuilder responsesBuilder = copyMarkupDocBuilder(markupDocBuilder);
    applyPathsDocumentExtension(new PathsDocumentExtension.Context(PathsDocumentExtension.Position.OPERATION_RESPONSES_BEGIN, responsesBuilder, operation));
    if (MapUtils.isNotEmpty(responses)) {
        StringColumn.Builder httpCodeColumnBuilder = StringColumn.builder(StringColumnId.of(labels.getLabel(HTTP_CODE_COLUMN)))
                .putMetaData(TableComponent.WIDTH_RATIO, "2");
        StringColumn.Builder descriptionColumnBuilder = StringColumn.builder(StringColumnId.of(labels.getLabel(DESCRIPTION_COLUMN)))
                .putMetaData(TableComponent.WIDTH_RATIO, "14")
                .putMetaData(TableComponent.HEADER_COLUMN, "true");
        StringColumn.Builder schemaColumnBuilder = StringColumn.builder(StringColumnId.of(labels.getLabel(SCHEMA_COLUMN)))
                .putMetaData(TableComponent.WIDTH_RATIO, "4")
                .putMetaData(TableComponent.HEADER_COLUMN, "true");

        Map<String, Response> sortedResponses = toSortedMap(responses, config.getResponseOrdering());
        sortedResponses.forEach((String responseName, Response response) -> {
            String schemaContent = labels.getLabel(NO_CONTENT);
            if (response.getResponseSchema() != null) {
                Model model = response.getResponseSchema();
                Type type = null;

                if (model != null) {
                    type = ModelUtils.getType(model, definitions, definitionDocumentResolver);
                } else {
                    type = new BasicType("string", responseName);
                }

                if (config.isInlineSchemaEnabled()) {
                    type = createInlineType(type, labels.getLabel(RESPONSE) + " " + responseName, operation.getId() + " " + labels.getLabel(RESPONSE) + " " + responseName, params.inlineDefinitions);
                }

                schemaContent = type.displaySchema(markupDocBuilder);
            }

            MarkupDocBuilder descriptionBuilder = copyMarkupDocBuilder(markupDocBuilder);

            descriptionBuilder.text(markupDescription(MarkupLanguage.valueOf(config.getSchemaMarkupLanguage().name()),
                    markupDocBuilder, response.getDescription()));

            Map<String, Property> headers = response.getHeaders();
            if (MapUtils.isNotEmpty(headers)) {
                descriptionBuilder.newLine(true).boldText(labels.getLabel(HEADERS_COLUMN)).text(COLON);
                for (Map.Entry<String, Property> header : headers.entrySet()) {
                    descriptionBuilder.newLine(true);
                    Property headerProperty = header.getValue();
                    PropertyAdapter headerPropertyAdapter = new PropertyAdapter(headerProperty);
                    Type propertyType = headerPropertyAdapter.getType(definitionDocumentResolver);
                    String headerDescription = markupDescription(MarkupLanguage.valueOf(config.getSchemaMarkupLanguage().name()),
                            markupDocBuilder, headerProperty.getDescription());
                    Optional<Object> optionalDefaultValue = headerPropertyAdapter.getDefaultValue();

                    descriptionBuilder
                            .literalText(header.getKey())
                            .text(String.format(" (%s)", propertyType.displaySchema(markupDocBuilder)));

                    if (isNotBlank(headerDescription) || optionalDefaultValue.isPresent()) {
                        descriptionBuilder.text(COLON);

                        if (isNotBlank(headerDescription) && !headerDescription.endsWith("."))
                            headerDescription += ".";

                        descriptionBuilder.text(headerDescription);

                        optionalDefaultValue.ifPresent(o -> descriptionBuilder.text(" ")
                                .boldText(labels.getLabel(DEFAULT_COLUMN))
                                .text(COLON).literalText(Json.pretty(o)));
                    }
                }
            }

            httpCodeColumnBuilder.add(boldText(markupDocBuilder, responseName));
            descriptionColumnBuilder.add(descriptionBuilder.toString());
            schemaColumnBuilder.add(schemaContent);
        });

        responsesBuilder = tableComponent.apply(responsesBuilder, TableComponent.parameters(httpCodeColumnBuilder.build(),
                descriptionColumnBuilder.build(),
                schemaColumnBuilder.build()));
    }
    applyPathsDocumentExtension(new PathsDocumentExtension.Context(PathsDocumentExtension.Position.OPERATION_RESPONSES_END, responsesBuilder, operation));
    String responsesContent = responsesBuilder.toString();

    applyPathsDocumentExtension(new PathsDocumentExtension.Context(PathsDocumentExtension.Position.OPERATION_RESPONSES_BEFORE, markupDocBuilder, operation));
    if (isNotBlank(responsesContent)) {
        markupDocBuilder.sectionTitleLevel(params.titleLevel, labels.getLabel(RESPONSES));
        markupDocBuilder.text(responsesContent);
    }
    applyPathsDocumentExtension(new PathsDocumentExtension.Context(PathsDocumentExtension.Position.OPERATION_RESPONSES_AFTER, markupDocBuilder, operation));
    return markupDocBuilder;
}
 
Example 5
Source File: SecuritySchemeComponent.java    From swagger2markup with Apache License 2.0 4 votes vote down vote up
@Override
public MarkupDocBuilder apply(MarkupDocBuilder markupDocBuilder, Parameters params) {
    SwaggerPathOperation operation = params.operation;
    MarkupDocBuilder securityBuilder = copyMarkupDocBuilder(markupDocBuilder);
    List<Map<String, List<String>>> securitySchemes = operation.getOperation().getSecurity();
    applyPathsDocumentExtension(new PathsDocumentExtension.Context(PathsDocumentExtension.Position.OPERATION_SECURITY_BEGIN, securityBuilder, operation));
    if (CollectionUtils.isNotEmpty(securitySchemes)) {
        StringColumn.Builder typeColumnBuilder = StringColumn.builder(StringColumnId.of(labels.getLabel(TYPE_COLUMN)))
                .putMetaData(TableComponent.WIDTH_RATIO, "3");
        StringColumn.Builder nameColumnBuilder = StringColumn.builder(StringColumnId.of(labels.getLabel(NAME_COLUMN)))
                .putMetaData(TableComponent.WIDTH_RATIO, "4");
        StringColumn.Builder scopeColumnBuilder = StringColumn.builder(StringColumnId.of(labels.getLabel(SCOPES_COLUMN)))
                .putMetaData(TableComponent.WIDTH_RATIO, "13")
                .putMetaData(TableComponent.HEADER_COLUMN, "true");


        for (Map<String, List<String>> securityScheme : securitySchemes) {
            for (Map.Entry<String, List<String>> securityEntry : securityScheme.entrySet()) {
                String securityKey = securityEntry.getKey();
                String type = labels.getLabel(UNKNOWN);
                if (securityDefinitions != null && securityDefinitions.containsKey(securityKey)) {
                    type = securityDefinitions.get(securityKey).getType();
                }

                typeColumnBuilder.add(boldText(markupDocBuilder, type));
                nameColumnBuilder.add(boldText(markupDocBuilder, crossReference(markupDocBuilder, securityDocumentResolver.apply(securityKey), securityKey, securityKey)));
                scopeColumnBuilder.add(Joiner.on(",").join(securityEntry.getValue()));
            }
        }

        securityBuilder = tableComponent.apply(securityBuilder, TableComponent.parameters(typeColumnBuilder.build(),
                nameColumnBuilder.build(),
                scopeColumnBuilder.build()));
    }
    applyPathsDocumentExtension(new PathsDocumentExtension.Context(PathsDocumentExtension.Position.OPERATION_SECURITY_END, securityBuilder, operation));
    String securityContent = securityBuilder.toString();

    applyPathsDocumentExtension(new PathsDocumentExtension.Context(PathsDocumentExtension.Position.OPERATION_SECURITY_BEFORE, markupDocBuilder, operation));
    if (isNotBlank(securityContent)) {
        markupDocBuilder.sectionTitleLevel(params.titleLevel, labels.getLabel(SECURITY));
        markupDocBuilder.text(securityContent);
    }
    applyPathsDocumentExtension(new PathsDocumentExtension.Context(PathsDocumentExtension.Position.OPERATION_SECURITY_AFTER, markupDocBuilder, operation));
    return markupDocBuilder;
}
 
Example 6
Source File: ParameterTableComponent.java    From swagger2markup with Apache License 2.0 4 votes vote down vote up
@Override
public MarkupDocBuilder apply(MarkupDocBuilder markupDocBuilder, Parameters params) {
    SwaggerPathOperation operation = params.operation;
    List<ObjectType> inlineDefinitions = params.inlineDefinitions;
    List<Parameter> parameters = operation.getOperation().getParameters();
    if (config.getParameterOrdering() != null) {
        Comparator<io.github.swagger2markup.model.Parameter> parameterOrdering = config.getParameterOrdering();
        parameters.sort((o1, o2) -> {
            io.github.swagger2markup.model.Parameter p1 = new io.github.swagger2markup.model.Parameter(o1.getName(), o1.getIn());
            io.github.swagger2markup.model.Parameter p2 = new io.github.swagger2markup.model.Parameter(o2.getName(), o2.getIn());
            return parameterOrdering.compare(p1, p2);
        });
    }

    // Filter parameters to display in parameters section
    List<Parameter> filteredParameters = parameters.stream()
            .filter(this::filterParameter).collect(Collectors.toList());

    MarkupDocBuilder parametersBuilder = copyMarkupDocBuilder(markupDocBuilder);
    applyPathsDocumentExtension(new PathsDocumentExtension.Context(PathsDocumentExtension.Position.OPERATION_PARAMETERS_BEGIN, parametersBuilder, operation));
    if (CollectionUtils.isNotEmpty(filteredParameters)) {
        StringColumn.Builder typeColumnBuilder = StringColumn.builder(StringColumnId.of(labels.getLabel(TYPE_COLUMN)))
                .putMetaData(TableComponent.WIDTH_RATIO, "2");
        StringColumn.Builder nameColumnBuilder = StringColumn.builder(StringColumnId.of(labels.getLabel(NAME_COLUMN)))
                .putMetaData(TableComponent.WIDTH_RATIO, "3");
        StringColumn.Builder descriptionColumnBuilder = StringColumn.builder(StringColumnId.of(labels.getLabel(DESCRIPTION_COLUMN)))
                .putMetaData(TableComponent.WIDTH_RATIO, "9")
                .putMetaData(TableComponent.HEADER_COLUMN, "true");
        StringColumn.Builder schemaColumnBuilder = StringColumn.builder(StringColumnId.of(labels.getLabel(SCHEMA_COLUMN)))
                .putMetaData(TableComponent.WIDTH_RATIO, "4")
                .putMetaData(TableComponent.HEADER_COLUMN, "true");
        StringColumn.Builder defaultColumnBuilder = StringColumn.builder(StringColumnId.of(labels.getLabel(DEFAULT_COLUMN)))
                .putMetaData(TableComponent.WIDTH_RATIO, "2")
                .putMetaData(TableComponent.HEADER_COLUMN, "true");

        for (Parameter parameter : filteredParameters) {
            ParameterAdapter parameterAdapter = new ParameterAdapter(context,
                    operation, parameter, definitionDocumentResolver);

            inlineDefinitions.addAll(parameterAdapter.getInlineDefinitions());

            typeColumnBuilder.add(parameterAdapter.displayType(markupDocBuilder));
            nameColumnBuilder.add(getParameterNameColumnContent(markupDocBuilder, parameterAdapter));
            descriptionColumnBuilder.add(buildDescriptionForParameter(markupDocBuilder, parameterAdapter));
            schemaColumnBuilder.add(parameterAdapter.displaySchema(markupDocBuilder));
            defaultColumnBuilder.add(parameterAdapter.displayDefaultValue(markupDocBuilder));
        }

        parametersBuilder = tableComponent.apply(parametersBuilder, TableComponent.parameters(
                typeColumnBuilder.build(),
                nameColumnBuilder.build(),
                descriptionColumnBuilder.build(),
                schemaColumnBuilder.build(),
                defaultColumnBuilder.build()));
    }
    applyPathsDocumentExtension(new PathsDocumentExtension.Context(PathsDocumentExtension.Position.OPERATION_PARAMETERS_END, parametersBuilder, operation));
    String parametersContent = parametersBuilder.toString();

    applyPathsDocumentExtension(new PathsDocumentExtension.Context(PathsDocumentExtension.Position.OPERATION_PARAMETERS_BEFORE, markupDocBuilder, operation));
    if (isNotBlank(parametersContent)) {
        markupDocBuilder.sectionTitleLevel(params.titleLevel, labels.getLabel(PARAMETERS));
        markupDocBuilder.text(parametersContent);
    }
    applyPathsDocumentExtension(new PathsDocumentExtension.Context(PathsDocumentExtension.Position.OPERATION_PARAMETERS_AFTER, markupDocBuilder, operation));

    return markupDocBuilder;
}