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 @@ -15,6 +15,7 @@
import org.apache.commons.lang3.StringUtils;
import org.openapitools.openapidiff.core.exception.RendererException;
import org.openapitools.openapidiff.core.model.*;
import org.openapitools.openapidiff.core.model.schema.ChangedEnum;
import org.openapitools.openapidiff.core.model.schema.ChangedOperationId;
import org.openapitools.openapidiff.core.utils.RefPointer;
import org.openapitools.openapidiff.core.utils.RefType;
Expand Down Expand Up @@ -197,6 +198,9 @@ private String incompatibilities(String propName, final ChangedSchema schema) {
String type = type(schema.getOldSchema()) + " -> " + type(schema.getNewSchema());
sb.append(property(propName, "Changed property type", type));
}
if (schema.getEnumeration() != null) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: This prints every enum change inside the 'Broken compatibility' block, including directions that are compatible for this context. For example, a response enum value removed next to a missing property would be reported as 'Removed enum value', though that removal does not break compatibility. Filter by direction, for example by checking changedEnum.isItemsChanged() per side and the request/response context.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At core/src/main/java/org/openapitools/openapidiff/core/output/ConsoleRender.java, line 201:

<comment>This prints every enum change inside the 'Broken compatibility' block, including directions that are compatible for this context. For example, a response enum value removed next to a missing property would be reported as 'Removed enum value', though that removal does not break compatibility. Filter by direction, for example by checking `changedEnum.isItemsChanged()` per side and the request/response context.</comment>

<file context>
@@ -197,6 +198,9 @@ private String incompatibilities(String propName, final ChangedSchema schema) {
       String type = type(schema.getOldSchema()) + " -> " + type(schema.getNewSchema());
       sb.append(property(propName, "Changed property type", type));
     }
+    if (schema.getEnumeration() != null) {
+      sb.append(enumValues(propName, schema.getEnumeration()));
+    }
</file context>

sb.append(enumValues(propName, schema.getEnumeration()));
}
String prefix = propName.isEmpty() ? "" : propName + ".";
sb.append(
properties(prefix, "Missing property", schema.getMissingProperties(), schema.getContext()));
Expand All @@ -210,6 +214,22 @@ private String items(String propName, ChangedSchema schema) {
return incompatibilities(propName + "[n]", schema);
}

private String enumValues(String propName, ChangedEnum<?> changedEnum) {
StringBuilder sb = new StringBuilder();
String prefix = propName.isEmpty() ? "" : propName + " ";
changedEnum
.getIncreased()
.forEach(value -> sb.append(enumValue("Added enum value", prefix + value)));
changedEnum
.getMissing()
.forEach(value -> sb.append(enumValue("Removed enum value", prefix + value)));
return sb.toString();
}

private String enumValue(String title, String value) {
return String.format("%s%s: %s%n", StringUtils.repeat(' ', 10), title, value);
}

private String properties(
String propPrefix, String title, Map<String, Schema<?>> properties, DiffContext context) {
StringBuilder sb = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,32 @@ public void renderShowsWhatsChangedSectionWithCorrectFormattingWhenEndpointIsCha
.containsSubsequence("- GET /widgets", "Parameter:", "- Changed query-param-1 in query");
}

@Test
public void renderShowsChangedEnumValuesWhenResponseEnumValueIsAdded() {
ConsoleRender render = new ConsoleRender();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
ChangedOpenApi diff = OpenApiCompare.fromLocations("enum_diff_1.yaml", "enum_diff_2.yaml");
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
render.render(diff, outputStreamWriter);
assertThat(outputStream.toString())
.containsSubsequence(
"Schema: Broken compatibility", "Added enum value: [n].someProperty ENUM_VALUE_3");
}

@Test
public void renderShowsRemovedEnumValuesWhenRequestEnumValueIsRemoved() {
ConsoleRender render = new ConsoleRender();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
ChangedOpenApi diff =
OpenApiCompare.fromLocations(
"enum_diff_request_body_1.yaml", "enum_diff_request_body_2.yaml");
render.render(diff, outputStreamWriter);
assertThat(outputStream.toString())
.containsSubsequence(
"Schema: Broken compatibility", "Removed enum value: someProperty ENUM_VALUE_2");
}

@Test
public void issue870_renderDoesNotFailWhenSchemaIsNullButExampleChanged() {
ConsoleRender render = new ConsoleRender();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ public void renderDoesNotFailWhenHTTPStatusCodeIsRange() {
assertThat(outputStream.toString()).isNotBlank();
}

@Test
public void renderShowsChangedEnumValuesWhenResponseEnumValueIsAdded() {
MarkdownRender render = new MarkdownRender();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
ChangedOpenApi diff = OpenApiCompare.fromLocations("enum_diff_1.yaml", "enum_diff_2.yaml");
render.render(diff, outputStreamWriter);
assertThat(outputStream.toString()).containsSubsequence("Added enum value", "`ENUM_VALUE_3`");
}

@Test
public void issue870_renderDoesNotFailWhenSchemaIsNullButExampleChanged() {
MarkdownRender render = new MarkdownRender();
Expand Down
30 changes: 30 additions & 0 deletions core/src/test/resources/enum_diff_1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
openapi: 3.0.0
info:
description: myDesc
title: myTitle
version: 1.0.0
paths:
/resources1/{id}/resources2:
get:
operationId: listResources2
parameters:
- name: id
in: path
required: true
schema:
type: string
responses:
'200':
description: successful operation
content:
application/json:
schema:
type: array
items:
type: object
properties:
someProperty:
type: string
enum:
- ENUM_VALUE_1
- ENUM_VALUE_2
31 changes: 31 additions & 0 deletions core/src/test/resources/enum_diff_2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
openapi: 3.0.0
info:
description: myDesc
title: myTitle
version: 1.0.0
paths:
/resources1/{id}/resources2:
get:
operationId: listResources2
parameters:
- name: id
in: path
required: true
schema:
type: string
responses:
'200':
description: successful operation
content:
application/json:
schema:
type: array
items:
type: object
properties:
someProperty:
type: string
enum:
- ENUM_VALUE_1
- ENUM_VALUE_2
- ENUM_VALUE_3
29 changes: 29 additions & 0 deletions core/src/test/resources/enum_diff_request_body_1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
openapi: 3.0.0
info:
description: myDesc
title: myTitle
version: 1.0.0
paths:
/resources1/{id}/resources2:
get:
operationId: listResources2
parameters:
- name: id
in: path
required: true
schema:
type: string
requestBody:
content:
application/json:
schema:
type: object
properties:
someProperty:
type: string
enum:
- ENUM_VALUE_1
- ENUM_VALUE_2
responses:
'200':
description: successful operation
28 changes: 28 additions & 0 deletions core/src/test/resources/enum_diff_request_body_2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
openapi: 3.0.0
info:
description: myDesc
title: myTitle
version: 1.0.0
paths:
/resources1/{id}/resources2:
get:
operationId: listResources2
parameters:
- name: id
in: path
required: true
schema:
type: string
requestBody:
content:
application/json:
schema:
type: object
properties:
someProperty:
type: string
enum:
- ENUM_VALUE_1
responses:
'200':
description: successful operation