Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,13 @@ private String itemContent(
if (!changedMediaType.isCompatible() && changedMediaType.getSchema() != null) {
sb.append(incompatibilities(changedMediaType.getSchema()));
}
MediaTypeExampleChanges.render(changedMediaType)
.forEach(
(label, value) ->
sb.append(itemContent(label + ":", "", indent + 1))
.append("+\n[source,json]\n----\n")
.append(value)
.append("\n----\n"));
return sb.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,17 @@ private String itemContent(
if (!changedMediaType.isCompatible() && changedMediaType.getSchema() != null) {
sb.append(incompatibilities(changedMediaType.getSchema()));
}
MediaTypeExampleChanges.render(changedMediaType)
.forEach(
(label, value) ->
sb.append(StringUtils.repeat(' ', 10))
.append(label)
.append(":")
.append(System.lineSeparator())
.append(StringUtils.repeat(' ', 12))
.append(
value.replace("\n", System.lineSeparator() + StringUtils.repeat(' ', 12)))
.append(System.lineSeparator()));
return sb.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import static j2html.TagCreator.meta;
import static j2html.TagCreator.ol;
import static j2html.TagCreator.p;
import static j2html.TagCreator.pre;
import static j2html.TagCreator.span;
import static j2html.TagCreator.title;
import static j2html.TagCreator.ul;
Expand Down Expand Up @@ -357,6 +358,8 @@ private LiTag li_changedRequest(String name, ChangedMediaType request) {
allChanges(li, schema);
}
}
MediaTypeExampleChanges.render(request)
.forEach((label, value) -> li.with(div(h3(label), pre(value))));
return li;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,18 @@ protected String itemContent(int deepness, String mediaType, ChangedMediaType co
if (content.getSchema() != null) {
result += schema(deepness, content.getSchema());
}
String prefix = indent(deepness);
for (Map.Entry<String, String> detail : MediaTypeExampleChanges.render(content).entrySet()) {
result +=
format(
"%s%s:\n\n%s```json\n%s%s\n%s```\n\n",
prefix,
detail.getKey(),
prefix,
prefix,
detail.getValue().replace("\n", "\n" + prefix),
prefix);
}
return result;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package org.openapitools.openapidiff.core.output;

import static org.openapitools.openapidiff.core.model.Changed.result;

import com.fasterxml.jackson.core.JsonProcessingException;
import io.swagger.v3.core.util.Json;
import java.util.LinkedHashMap;
import java.util.Map;
import org.openapitools.openapidiff.core.model.ChangedExample;
import org.openapitools.openapidiff.core.model.ChangedExamples;
import org.openapitools.openapidiff.core.model.ChangedMediaType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/** Example details shared by the text and HTML reports; markup is handled by each renderer. */
final class MediaTypeExampleChanges {
private static final Logger LOGGER = LoggerFactory.getLogger(MediaTypeExampleChanges.class);

private MediaTypeExampleChanges() {}

static Map<String, String> render(ChangedMediaType mediaType) {
Map<String, String> details = new LinkedHashMap<>();
ChangedExample example = mediaType.getExample();
if (result(example).isDifferent()) {
add(details, "example", example.getLeftExample(), example.getRightExample());
}
ChangedExamples examples = mediaType.getExamples();
if (result(examples).isDifferent()) {
add(details, "examples", examples.getLeftExamples(), examples.getRightExamples());
}
return details;
}

private static void add(
Map<String, String> details, String name, Object oldValue, Object newValue) {
if (oldValue == null) {
details.put("Added " + name, json(newValue));
} else if (newValue == null) {
details.put("Removed " + name, json(oldValue));
} else {
details.put("Old " + name, json(oldValue));
details.put("New " + name, json(newValue));
}
}

private static String json(Object value) {
try {
return Json.mapper()
.writerWithDefaultPrettyPrinter()
.writeValueAsString(value)
.replace("\r\n", "\n");
} catch (JsonProcessingException e) {
LOGGER.warn(
"Could not serialize media type example as JSON ({}); rendering a placeholder",
e.getClass().getSimpleName());
// Do not call toString() on arbitrary example objects: it can fail as well.
return "\"[Example could not be serialized as JSON]\"";
}
}
}
Loading