Skip to content
Draft
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 @@ -432,6 +432,18 @@ protected String listDiff(int deepness, String name, ChangedList<?> listDiff) {
if (listDiff == null) {
return "";
}

// Check if this looks like a simple rename (1 added, 1 removed)
if (listDiff.getIncreased().size() == 1 && listDiff.getMissing().size() == 1) {
StringBuilder sb = new StringBuilder();
Object oldValue = listDiff.getMissing().get(0);
Object newValue = listDiff.getIncreased().get(0);
sb.append(format("%sRenamed %s value:\n\n", indent(deepness), name));
sb.append(format("%s* `%s` -> `%s`\n", indent(deepness), oldValue, newValue));
return sb.toString();
}

// Fall back to current behavior for other cases
return listItem(deepness, "Added " + name, listDiff.getIncreased())
+ listItem(deepness, "Removed " + name, listDiff.getMissing());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

import java.io.ByteArrayOutputStream;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.util.Collections;
import org.junit.jupiter.api.Test;
import org.openapitools.openapidiff.core.OpenApiCompare;
import org.openapitools.openapidiff.core.model.ChangedList;
import org.openapitools.openapidiff.core.model.ChangedOpenApi;

public class MarkdownRenderTest {
Expand Down Expand Up @@ -40,4 +43,66 @@ public void renderDoesNotFailWhenHTTPStatusCodeIsRange() {
render.render(diff, outputStreamWriter);
assertThat(outputStream.toString()).isNotBlank();
}

@Test
public void testEnumRenameDetection() {
MarkdownRender render = new MarkdownRender();

// Test 1:1 rename (should show as rename)
ChangedList<String> enumRename =
org.openapitools.openapidiff.core.compare.ListDiff.diff(
new ChangedList.SimpleChangedList<>(
Collections.singletonList("OldValue"), Collections.singletonList("NewValue")));
String renameResult = render.listDiff(2, "enum", enumRename);
assertThat(renameResult).contains("Renamed enum value:");
assertThat(renameResult).contains("`OldValue` -> `NewValue`");

// Test multiple additions and removals (should fall back to original behavior)
ChangedList<String> enumMultiple =
org.openapitools.openapidiff.core.compare.ListDiff.diff(
new ChangedList.SimpleChangedList<>(
Arrays.asList("OldValue1", "OldValue2"), Arrays.asList("NewValue1", "NewValue2")));
String multipleResult = render.listDiff(2, "enum", enumMultiple);
assertThat(multipleResult).contains("Added enum values:");
assertThat(multipleResult).contains("Removed enum values:");

// Test single addition (should use original behavior)
ChangedList<String> enumAdd =
org.openapitools.openapidiff.core.compare.ListDiff.diff(
new ChangedList.SimpleChangedList<>(
Collections.emptyList(), Collections.singletonList("NewValue")));
String addResult = render.listDiff(2, "enum", enumAdd);
assertThat(addResult).contains("Added enum value:");
assertThat(addResult).doesNotContain("Renamed");

// Test single removal (should use original behavior)
ChangedList<String> enumRemove =
org.openapitools.openapidiff.core.compare.ListDiff.diff(
new ChangedList.SimpleChangedList<>(
Collections.singletonList("OldValue"), Collections.emptyList()));
String removeResult = render.listDiff(2, "enum", enumRemove);
assertThat(removeResult).contains("Removed enum value:");
assertThat(removeResult).doesNotContain("Renamed");
}

@Test
public void testOriginalIssueScenario() {
// Test to reproduce the original issue scenario
MarkdownRender render = new MarkdownRender();
ChangedOpenApi diff =
OpenApiCompare.fromLocations("enum_issue_old.yaml", "enum_issue_new.yaml");

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
render.render(diff, outputStreamWriter);

String result = outputStream.toString();
System.out.println("=== ENHANCED OUTPUT ===");
System.out.println(result);
System.out.println("=== END OUTPUT ===");

// With the enhancement, this should show as a rename instead of separate add/remove
assertThat(result).contains("Renamed enum value");
assertThat(result).contains("`StatusThree` -> `StatusNew`");
}
}
33 changes: 33 additions & 0 deletions core/src/test/resources/enum_issue_new.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
openapi: 3.0.0
info:
title: Enum Issue Test
version: 1.0.0
paths:
/test:
get:
responses:
'200':
description: Success
content:
application/json:
schema:
$ref: '#/components/schemas/SomeStatus'
components:
schemas:
SomeStatus:
description: Some status description
enum:
- StatusOne
- StatusTwo
- StatusNew
type: string
x-ms-enum:
name: SomeStatus
modelAsString: false
values:
- name: StatusOne
value: StatusOne
- name: StatusTwo
value: StatusTwo
- name: StatusNew
value: StatusNew
33 changes: 33 additions & 0 deletions core/src/test/resources/enum_issue_old.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
openapi: 3.0.0
info:
title: Enum Issue Test
version: 1.0.0
paths:
/test:
get:
responses:
'200':
description: Success
content:
application/json:
schema:
$ref: '#/components/schemas/SomeStatus'
components:
schemas:
SomeStatus:
description: Some status description
enum:
- StatusOne
- StatusTwo
- StatusThree
type: string
x-ms-enum:
name: SomeStatus
modelAsString: false
values:
- name: StatusOne
value: StatusOne
- name: StatusTwo
value: StatusTwo
- name: StatusThree
value: StatusThree