Leonard Seymore
2020-03-28 a651c082fa7d49a77f225b26e042a95fdd818339
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package za.co.taulite.openapi.openapiparser;
 
import io.swagger.parser.OpenAPIParser;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.PathItem;
import io.swagger.v3.oas.models.media.Schema;
import org.springframework.stereotype.Component;
 
import java.net.URL;
import java.util.Map;
 
@Component
public class Converter {
 
    public boolean isOpenApiDescriptor(URL url) {
        return new OpenAPIParser().readLocation(url.toString(), null, null).getOpenAPI() != null;
    }
 
    public String domainToCsv(URL url) {
        var openApi = new OpenAPIParser().readLocation(url.toString(), null, null).getOpenAPI();
 
        var builder = new StringBuilder();
        builder.append("Object Name,Object Description,Property Name,Property Type,Property Description\n");
        openApi.getComponents().getSchemas().entrySet().forEach(p -> {
            var name = p.getKey();
            var value = p.getValue();
            var description = value.getDescription();
            value.getProperties().entrySet().forEach(pr -> {
                var prop = (Map.Entry<String, Schema>) pr;
                var propName = prop.getKey();
                var propValue = prop.getValue();
                builder.append(
                        String.format("%s,\"%s\",%s,%s,\"%s\"\n", name, description, propName, propValue.getType(),propValue.getDescription())
                );
            });
        });
 
        return builder.toString();
    }
 
    public String operationsToCsv(URL url) {
        var openApi = new OpenAPIParser().readLocation(url.toString(), null, null).getOpenAPI();
 
        var builder = new StringBuilder();
        builder.append("Operation ID,Operation Summary,Operation Description,Parameter Name,Parameter Description,Parameter Example,Parameter Required,Parameter Type,Parameter In\n");
        openApi.getPaths().values().forEach(p -> {
                var op = getOperation(p);
                if (op.getParameters() != null) {
                    op.getParameters().forEach(param -> {
                        builder.append(
                                String.format("%s,\"%s\",\"%s\",%s,\"%s\",%s,%b,%s,%s\n", op.getOperationId(), op.getSummary(), op.getDescription(),
                                        param.getName(), param.getDescription(), param.getExample(), param.getRequired(), param.getSchema().getType(), param.getIn())
                        );
                    });
                }
 
                if (op.getRequestBody() != null) {
                    var b = op.getRequestBody();
                    var c = b.getContent().get("application/json");
                    var ref = c.getSchema().get$ref().split("/");
                    var type = ref[ref.length - 1];
                    builder.append(
                            String.format("%s,\"%s\",\"%s\",%s,\"%s\",%s,%b,%s,%s\n", op.getOperationId(), op.getSummary(), op.getDescription(),
                                    null, b.getDescription(), c.getExample(), b.getRequired(), type, "body")
                    );
                }
 
            });
 
        return builder.toString();
    }
 
    private Operation getOperation(PathItem p) {
        if (p.getTrace() != null) return p.getTrace();
        if (p.getDelete() != null) return p.getDelete();
        if (p.getGet() != null) return p.getGet();
        if (p.getHead() != null) return p.getHead();
        if (p.getOptions() != null) return p.getOptions();
        if (p.getPatch() != null) return p.getPatch();
        if (p.getPost() != null) return p.getPost();
        if (p.getPut() != null) return p.getPut();
        return null;
    }
}