|
| 1 | +package com.APIprotector.apiprotector.library; |
| 2 | + |
| 3 | +import io.swagger.v3.oas.models.parameters.Parameter; |
| 4 | +import org.openapitools.openapidiff.core.exception.RendererException; |
| 5 | +import org.openapitools.openapidiff.core.model.*; |
| 6 | +import org.openapitools.openapidiff.core.output.Render; |
| 7 | + |
| 8 | +import java.io.IOException; |
| 9 | +import java.io.OutputStreamWriter; |
| 10 | +import java.util.List; |
| 11 | +import java.util.Map; |
| 12 | +import java.util.Optional; |
| 13 | + |
| 14 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 15 | + |
| 16 | +import java.util.*; |
| 17 | + |
| 18 | +public class JsonRenderer implements Render { |
| 19 | + protected ChangedOpenApi diff; |
| 20 | + |
| 21 | + public JsonRenderer() { |
| 22 | + } |
| 23 | + |
| 24 | + @Override |
| 25 | + public void render(ChangedOpenApi diff, OutputStreamWriter outputStreamWriter) { |
| 26 | + this.diff = diff; |
| 27 | + |
| 28 | + Map<String, Object> result = new LinkedHashMap<>(); |
| 29 | + |
| 30 | + if (diff.isUnchanged()) { |
| 31 | + result.put("message", "No differences. Specifications are equivalent."); |
| 32 | + } else { |
| 33 | + result.put("title", diff.getNewSpecOpenApi().getInfo().getTitle()); |
| 34 | + result.put("compatible", diff.isCompatible()); |
| 35 | + |
| 36 | + result.put("newEndpoints", endpointsToList(diff.getNewEndpoints())); |
| 37 | + result.put("missingEndpoints", endpointsToList(diff.getMissingEndpoints())); |
| 38 | + result.put("deprecatedEndpoints", endpointsToList(diff.getDeprecatedEndpoints())); |
| 39 | + result.put("changedOperations", changedOperationsToList(diff.getChangedOperations())); |
| 40 | + } |
| 41 | + |
| 42 | + ObjectMapper objectMapper = new ObjectMapper(); |
| 43 | + try { |
| 44 | + objectMapper.writerWithDefaultPrettyPrinter().writeValue(outputStreamWriter, result); |
| 45 | + outputStreamWriter.close(); |
| 46 | + } catch (IOException e) { |
| 47 | + throw new RendererException(e); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + private List<Map<String, Object>> endpointsToList(List<Endpoint> endpoints) { |
| 52 | + List<Map<String, Object>> list = new ArrayList<>(); |
| 53 | + if (endpoints != null) { |
| 54 | + for (Endpoint endpoint : endpoints) { |
| 55 | + Map<String, Object> item = new LinkedHashMap<>(); |
| 56 | + item.put("method", endpoint.getMethod().toString()); |
| 57 | + item.put("path", endpoint.getPathUrl()); |
| 58 | + item.put("summary", endpoint.getSummary()); |
| 59 | + list.add(item); |
| 60 | + } |
| 61 | + } |
| 62 | + return list; |
| 63 | + } |
| 64 | + |
| 65 | + private List<Map<String, Object>> changedOperationsToList(List<ChangedOperation> operations) { |
| 66 | + List<Map<String, Object>> list = new ArrayList<>(); |
| 67 | + if (operations != null) { |
| 68 | + for (ChangedOperation op : operations) { |
| 69 | + Map<String, Object> item = new LinkedHashMap<>(); |
| 70 | + item.put("method", op.getHttpMethod().toString()); |
| 71 | + item.put("path", op.getPathUrl()); |
| 72 | + item.put("summary", Optional.ofNullable(op.getSummary()).map(ChangedMetadata::getRight).orElse("")); |
| 73 | + |
| 74 | + if (Changed.result(op.getParameters()).isDifferent()) { |
| 75 | + item.put("parameters", paramChanges(op.getParameters())); |
| 76 | + } |
| 77 | + |
| 78 | + if (op.resultRequestBody().isDifferent()) { |
| 79 | + item.put("requestBody", contentChanges(op.getRequestBody().getContent())); |
| 80 | + } |
| 81 | + |
| 82 | + if (op.resultApiResponses().isDifferent()) { |
| 83 | + item.put("responses", responseChanges(op.getApiResponses())); |
| 84 | + } |
| 85 | + |
| 86 | + list.add(item); |
| 87 | + } |
| 88 | + } |
| 89 | + return list; |
| 90 | + } |
| 91 | + |
| 92 | + private List<Map<String, Object>> paramChanges(ChangedParameters changedParams) { |
| 93 | + List<Map<String, Object>> params = new ArrayList<>(); |
| 94 | + |
| 95 | + for (Parameter param : changedParams.getIncreased()) { |
| 96 | + params.add(Map.of("action", "add", "name", param.getName(), "in", param.getIn())); |
| 97 | + } |
| 98 | + |
| 99 | + for (ChangedParameter changed : changedParams.getChanged()) { |
| 100 | + String action = changed.isDeprecated() ? "deprecated" : "changed"; |
| 101 | + Parameter newParam = changed.getNewParameter(); |
| 102 | + params.add(Map.of("action", action, "name", newParam.getName(), "in", newParam.getIn())); |
| 103 | + } |
| 104 | + |
| 105 | + for (Parameter param : changedParams.getMissing()) { |
| 106 | + params.add(Map.of("action", "delete", "name", param.getName(), "in", param.getIn())); |
| 107 | + } |
| 108 | + |
| 109 | + return params; |
| 110 | + } |
| 111 | + |
| 112 | + private List<Map<String, Object>> contentChanges(ChangedContent changedContent) { |
| 113 | + List<Map<String, Object>> content = new ArrayList<>(); |
| 114 | + if (changedContent == null) return content; |
| 115 | + |
| 116 | + changedContent.getIncreased().forEach((type, value) -> |
| 117 | + content.add(Map.of("action", "add", "contentType", type))); |
| 118 | + |
| 119 | + changedContent.getMissing().forEach((type, value) -> |
| 120 | + content.add(Map.of("action", "delete", "contentType", type))); |
| 121 | + |
| 122 | + changedContent.getChanged().forEach((type, mediaType) -> |
| 123 | + content.add(Map.of( |
| 124 | + "action", "change", |
| 125 | + "contentType", type, |
| 126 | + "compatible", mediaType.isCompatible() |
| 127 | + ))); |
| 128 | + |
| 129 | + return content; |
| 130 | + } |
| 131 | + |
| 132 | + private List<Map<String, Object>> responseChanges(ChangedApiResponse responses) { |
| 133 | + List<Map<String, Object>> result = new ArrayList<>(); |
| 134 | + |
| 135 | + responses.getIncreased().forEach((code, value) -> |
| 136 | + result.add(Map.of("action", "add", "code", code))); |
| 137 | + |
| 138 | + responses.getMissing().forEach((code, value) -> |
| 139 | + result.add(Map.of("action", "delete", "code", code))); |
| 140 | + |
| 141 | + responses.getChanged().forEach((code, changed) -> { |
| 142 | + Map<String, Object> entry = new LinkedHashMap<>(); |
| 143 | + entry.put("action", "change"); |
| 144 | + entry.put("code", code); |
| 145 | + entry.put("mediaTypes", contentChanges(changed.getContent())); |
| 146 | + result.add(entry); |
| 147 | + }); |
| 148 | + |
| 149 | + return result; |
| 150 | + } |
| 151 | +} |
0 commit comments