Search in sources :

Example 1 with Tag

use of io.swagger.v3.oas.annotations.tags.Tag in project cxf by apache.

the class OpenApiCustomizer method customize.

public void customize(final OpenAPI oas) {
    if (replaceTags || javadocProvider != null) {
        Map<String, ClassResourceInfo> operations = new HashMap<>();
        Map<Pair<String, String>, OperationResourceInfo> methods = new HashMap<>();
        cris.forEach(cri -> {
            cri.getMethodDispatcher().getOperationResourceInfos().forEach(ori -> {
                String normalizedPath = getNormalizedPath(cri.getURITemplate().getValue(), ori.getURITemplate().getValue());
                operations.put(normalizedPath, cri);
                methods.put(Pair.of(ori.getHttpMethod(), normalizedPath), ori);
            });
        });
        List<Tag> tags = new ArrayList<>();
        oas.getPaths().forEach((pathKey, pathItem) -> {
            Tag tag = null;
            if (replaceTags && operations.containsKey(pathKey)) {
                ClassResourceInfo cri = operations.get(pathKey);
                tag = new Tag();
                tag.setName(cri.getURITemplate().getValue().replaceAll("/", "_"));
                if (javadocProvider != null) {
                    tag.setDescription(javadocProvider.getClassDoc(cri));
                }
                if (!tags.contains(tag)) {
                    tags.add(tag);
                }
            }
            for (Map.Entry<HttpMethod, Operation> subentry : pathItem.readOperationsMap().entrySet()) {
                if (replaceTags && tag != null) {
                    subentry.getValue().setTags(Collections.singletonList(tag.getName()));
                }
                Pair<String, String> key = Pair.of(subentry.getKey().name(), pathKey);
                if (methods.containsKey(key) && javadocProvider != null) {
                    OperationResourceInfo ori = methods.get(key);
                    if (StringUtils.isBlank(subentry.getValue().getSummary())) {
                        subentry.getValue().setSummary(javadocProvider.getMethodDoc(ori));
                    }
                    if (subentry.getValue().getParameters() == null) {
                        List<Parameter> parameters = new ArrayList<>();
                        addParameters(parameters);
                        subentry.getValue().setParameters(parameters);
                    } else {
                        for (int i = 0; i < subentry.getValue().getParameters().size(); i++) {
                            if (StringUtils.isBlank(subentry.getValue().getParameters().get(i).getDescription())) {
                                subentry.getValue().getParameters().get(i).setDescription(javadocProvider.getMethodParameterDoc(ori, i));
                            }
                        }
                        addParameters(subentry.getValue().getParameters());
                    }
                    if (subentry.getValue().getResponses() != null && !subentry.getValue().getResponses().isEmpty()) {
                        ApiResponse response = subentry.getValue().getResponses().entrySet().iterator().next().getValue();
                        if (StringUtils.isBlank(response.getDescription())) {
                            response.setDescription(javadocProvider.getMethodResponseDoc(ori));
                        }
                    }
                }
            }
        });
        if (replaceTags && oas.getTags() != null) {
            oas.setTags(tags);
        }
    }
}
Also used : HashMap(java.util.HashMap) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) ArrayList(java.util.ArrayList) Operation(io.swagger.v3.oas.models.Operation) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) Parameter(io.swagger.v3.oas.models.parameters.Parameter) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) Tag(io.swagger.v3.oas.models.tags.Tag) HashMap(java.util.HashMap) Map(java.util.Map) HttpMethod(io.swagger.v3.oas.models.PathItem.HttpMethod) Pair(org.apache.commons.lang3.tuple.Pair)

Aggregations

Operation (io.swagger.v3.oas.models.Operation)1 HttpMethod (io.swagger.v3.oas.models.PathItem.HttpMethod)1 Parameter (io.swagger.v3.oas.models.parameters.Parameter)1 ApiResponse (io.swagger.v3.oas.models.responses.ApiResponse)1 Tag (io.swagger.v3.oas.models.tags.Tag)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Pair (org.apache.commons.lang3.tuple.Pair)1 ClassResourceInfo (org.apache.cxf.jaxrs.model.ClassResourceInfo)1 OperationResourceInfo (org.apache.cxf.jaxrs.model.OperationResourceInfo)1